diff --git a/fastpair/keyed_service/BUILD b/fastpair/keyed_service/BUILD new file mode 100644 index 00000000..0eaa6bb7 --- /dev/null +++ b/fastpair/keyed_service/BUILD @@ -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", + ], +) diff --git a/fastpair/keyed_service/fast_pair_mediator.cc b/fastpair/keyed_service/fast_pair_mediator.cc new file mode 100644 index 00000000..97111344 --- /dev/null +++ b/fastpair/keyed_service/fast_pair_mediator.cc @@ -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 +#include + +#include "fastpair/common/protocol.h" +#include "internal/platform/logging.h" + +namespace nearby { +namespace fastpair { +Mediator::Mediator(std::unique_ptr scanner_broker, + std::unique_ptr 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 diff --git a/fastpair/keyed_service/fast_pair_mediator.h b/fastpair/keyed_service/fast_pair_mediator.h new file mode 100644 index 00000000..42c03c77 --- /dev/null +++ b/fastpair/keyed_service/fast_pair_mediator.h @@ -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 + +#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 scanner_broker, + std::unique_ptr 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 scanner_broker_; + std::unique_ptr fast_pair_repository_; +}; + +} // namespace fastpair +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_FASTPAIR_KEYED_SERVICE_FAST_PAIR_MEDIATOR_H_ diff --git a/fastpair/keyed_service/fast_pair_mediator_test.cc b/fastpair/keyed_service/fast_pair_mediator_test.cc new file mode 100644 index 00000000..be454a9c --- /dev/null +++ b/fastpair/keyed_service/fast_pair_mediator_test.cc @@ -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 +#include + +#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(); + mock_scanner_broker_ = + static_cast(scanner_broker_.get()); + + fast_pair_repository_ = std::make_unique(); + mock_fast_pair_repository_ = + static_cast(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 scanner_broker_; + std::unique_ptr fast_pair_repository_; + MockScannerBroker* mock_scanner_broker_; + MockFastPairRepository* mock_fast_pair_repository_; + std::unique_ptr mediator_; +}; + +TEST_F(MediatorTest, SannerBrokerCallStartScanning) { + EXPECT_CALL(*mock_scanner_broker_, StartScanning); + mediator_ = std::make_unique( + std::move(scanner_broker_), + std::move(fast_pair_repository_)); +} + +} // namespace + +} // namespace fastpair +} // namespace nearby diff --git a/fastpair/server_access/BUILD b/fastpair/server_access/BUILD index ae76a51c..120b732b 100644 --- a/fastpair/server_access/BUILD +++ b/fastpair/server_access/BUILD @@ -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 = [ diff --git a/fastpair/server_access/mock_fast_pair_repository.h b/fastpair/server_access/mock_fast_pair_repository.h new file mode 100644 index 00000000..d18ca94c --- /dev/null +++ b/fastpair/server_access/mock_fast_pair_repository.h @@ -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_