Add FastPairMediator

PiperOrigin-RevId: 520437860
This commit is contained in:
Qin Wang
2023-03-29 14:09:43 -07:00
committed by Copybara-Service
parent c73ebe8778
commit 2c70aababf
6 changed files with 297 additions and 1 deletions
+54
View File
@@ -0,0 +1,54 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
licenses(["notice"])
cc_library(
name = "keyed_service",
srcs = [
"fast_pair_mediator.cc",
],
hdrs = [
"fast_pair_mediator.h",
],
compatible_with = ["//buildenv/target:non_prod"],
visibility = [
"//fastpair:__subpackages__",
],
deps = [
"//fastpair/common",
"//fastpair/scanning:scanner",
"//fastpair/server_access",
"//internal/platform:logging",
"@com_google_absl//absl/functional:any_invocable",
"@com_google_absl//absl/strings",
],
)
cc_test(
name = "fast_pair_mediator_test",
size = "small",
srcs = [
"fast_pair_mediator_test.cc",
],
shard_count = 16,
deps = [
":keyed_service",
"//fastpair/scanning:mocks",
"//fastpair/server_access:mocks",
"//internal/platform/implementation/g3", # build_cleaner: keep
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_googletest//:gtest_main",
],
)
@@ -0,0 +1,57 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastpair/keyed_service/fast_pair_mediator.h"
#include <memory>
#include <utility>
#include "fastpair/common/protocol.h"
#include "internal/platform/logging.h"
namespace nearby {
namespace fastpair {
Mediator::Mediator(std::unique_ptr<ScannerBroker> scanner_broker,
std::unique_ptr<FastPairRepository> fast_pair_repository)
: scanner_broker_(std::move(scanner_broker)),
fast_pair_repository_(std::move(fast_pair_repository)) {
scanner_broker_->AddObserver(this);
// TODO(b/275452353): Add feature_status_tracker IsFastPairEnabled()
// Currently default to true.
SetFastPairState(true);
}
void Mediator::OnDeviceFound(const FastPairDevice& device) {
NEARBY_LOGS(INFO) << __func__ << ": " << device;
// Show discovery notification
}
void Mediator::OnDeviceLost(const FastPairDevice& device) {
NEARBY_LOGS(INFO) << __func__ << ": " << device;
}
void Mediator::SetFastPairState(bool is_enabled) {
NEARBY_LOGS(VERBOSE) << __func__ << ": " << is_enabled;
if (is_enabled) {
scanner_broker_->StartScanning(Protocol::kFastPairInitialPairing);
return;
}
scanner_broker_->StopScanning(Protocol::kFastPairInitialPairing);
}
} // namespace fastpair
} // namespace nearby
@@ -0,0 +1,49 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_NEARBY_FASTPAIR_KEYED_SERVICE_FAST_PAIR_MEDIATOR_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_KEYED_SERVICE_FAST_PAIR_MEDIATOR_H_
#include <memory>
#include "fastpair/scanning/scanner_broker.h"
#include "fastpair/server_access/fast_pair_repository.h"
namespace nearby {
namespace fastpair {
// Implements the Mediator design pattern for the components in the Fast Pair
class Mediator final : public ScannerBroker::Observer {
public:
Mediator(std::unique_ptr<ScannerBroker> scanner_broker,
std::unique_ptr<FastPairRepository> fast_pair_repository);
Mediator(const Mediator&) = delete;
Mediator& operator=(const Mediator&) = delete;
~Mediator() override = default;
// ScannerBroker::Observer
void OnDeviceFound(const FastPairDevice& device) override;
void OnDeviceLost(const FastPairDevice& device) override;
private:
void SetFastPairState(bool is_enabled);
std::unique_ptr<ScannerBroker> scanner_broker_;
std::unique_ptr<FastPairRepository> fast_pair_repository_;
};
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_KEYED_SERVICE_FAST_PAIR_MEDIATOR_H_
@@ -0,0 +1,68 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastpair/keyed_service/fast_pair_mediator.h"
#include <memory>
#include <utility>
#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "fastpair/scanning/mock_scanner_broker.h"
#include "fastpair/server_access/mock_fast_pair_repository.h"
namespace nearby {
namespace fastpair {
namespace {
class MediatorTest : public testing::Test {
public:
void SetUp() override {
scanner_broker_ = std::make_unique<MockScannerBroker>();
mock_scanner_broker_ =
static_cast<MockScannerBroker*>(scanner_broker_.get());
fast_pair_repository_ = std::make_unique<MockFastPairRepository>();
mock_fast_pair_repository_ =
static_cast<MockFastPairRepository*>(fast_pair_repository_.get());
}
void TearDown() override {
scanner_broker_.reset();
fast_pair_repository_.reset();
mediator_.reset();
mock_scanner_broker_ = nullptr;
mock_fast_pair_repository_ = nullptr;
}
protected:
std::unique_ptr<ScannerBroker> scanner_broker_;
std::unique_ptr<FastPairRepository> fast_pair_repository_;
MockScannerBroker* mock_scanner_broker_;
MockFastPairRepository* mock_fast_pair_repository_;
std::unique_ptr<Mediator> mediator_;
};
TEST_F(MediatorTest, SannerBrokerCallStartScanning) {
EXPECT_CALL(*mock_scanner_broker_, StartScanning);
mediator_ = std::make_unique<Mediator>(
std::move(scanner_broker_),
std::move(fast_pair_repository_));
}
} // namespace
} // namespace fastpair
} // namespace nearby
+33 -1
View File
@@ -1,3 +1,17 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
licenses(["notice"])
cc_library(
@@ -32,7 +46,9 @@ cc_library(
cc_library(
name = "test_support",
srcs = ["fake_fast_pair_repository.cc"],
hdrs = ["fake_fast_pair_repository.h"],
hdrs = [
"fake_fast_pair_repository.h",
],
visibility = [
"//fastpair:__subpackages__",
],
@@ -46,6 +62,22 @@ cc_library(
],
)
cc_library(
name = "mocks",
testonly = 1,
hdrs = [
"mock_fast_pair_repository.h",
],
visibility = [
"//fastpair:__subpackages__",
],
deps = [
":server_access",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest_for_library_testonly",
],
)
cc_test(
name = "server_access_test",
srcs = [
@@ -0,0 +1,36 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_NEARBY_FASTPAIR_SERVER_ACCESS_MOCK_FAST_PAIR_REPOSITORY_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_SERVER_ACCESS_MOCK_FAST_PAIR_REPOSITORY_H_
#include "gmock/gmock.h"
#include "absl/strings/string_view.h"
#include "fastpair/server_access/fast_pair_repository.h"
namespace nearby {
namespace fastpair {
class MockFastPairRepository : public FastPairRepository {
public:
MOCK_METHOD(void, GetDeviceMetadata,
(absl::string_view hex_model_id, DeviceMetadataCallback callback),
(override));
};
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_SERVER_ACCESS_MOCK_FAST_PAIR_REPOSITORY_H_