diff --git a/fastpair/BUILD b/fastpair/BUILD index fd159f14..1f0bca00 100644 --- a/fastpair/BUILD +++ b/fastpair/BUILD @@ -113,9 +113,9 @@ cc_test( deps = [ ":fast_pair_events", ":fast_pair_service", - ":test_support", "//fastpair/internal", "//fastpair/message_stream:fake_provider", + "//fastpair/plugins:fake_fast_pair_plugin", "//fastpair/server_access:test_support", "//internal/platform:logging", "//internal/platform:test_util", @@ -128,17 +128,15 @@ cc_test( cc_library( name = "test_support", testonly = True, - srcs = ["fake_fast_pair_plugin.cc"], hdrs = [ - "fake_fast_pair_plugin.h", + "mock_fast_pair_seeker.h", ], visibility = [ "//fastpair:__subpackages__", ], deps = [ - ":fast_pair_events", - ":fast_pair_plugin", ":fast_pair_seeker", - "//fastpair/common", + "@com_google_absl//absl/status", + "@com_google_googletest//:gtest_for_library_testonly", ], ) diff --git a/fastpair/fast_pair_seeker.h b/fastpair/fast_pair_seeker.h index d945c923..bc9677d9 100644 --- a/fastpair/fast_pair_seeker.h +++ b/fastpair/fast_pair_seeker.h @@ -48,7 +48,7 @@ class FastPairSeeker { // // Returns an error if pairing flow could not be started. Otherwise, the // pairing result will be returned via the `callback`. - virtual absl::Status StartInitialPairing(FastPairDevice& device, + virtual absl::Status StartInitialPairing(const FastPairDevice& device, const InitialPairingParam& params, PairingCallback callback) = 0; @@ -60,7 +60,7 @@ class FastPairSeeker { // Returns an error if pairing flow could not be started. Otherwise, the // pairing result will be returned via the `callback`. virtual absl::Status StartSubsequentPairing( - FastPairDevice& device, const SubsequentPairingParam& params, + const FastPairDevice& device, const SubsequentPairingParam& params, PairingCallback callback) = 0; // Starts asynchronous retroactive pairing flow. This pairing flow is used @@ -71,7 +71,7 @@ class FastPairSeeker { // Returns an error if pairing flow could not be started. Otherwise, the // pairing result will be returned via the `callback`. virtual absl::Status StartRetroactivePairing( - FastPairDevice& device, const RetroactivePairingParam& param, + const FastPairDevice& device, const RetroactivePairingParam& param, PairingCallback callback) = 0; }; } // namespace fastpair diff --git a/fastpair/fast_pair_service_test.cc b/fastpair/fast_pair_service_test.cc index 96cdff17..b0c05110 100644 --- a/fastpair/fast_pair_service_test.cc +++ b/fastpair/fast_pair_service_test.cc @@ -20,10 +20,10 @@ #include "gmock/gmock.h" #include "protobuf-matchers/protocol-buffer-matchers.h" #include "gtest/gtest.h" -#include "fastpair/fake_fast_pair_plugin.h" #include "fastpair/fast_pair_events.h" #include "fastpair/internal/fast_pair_seeker_impl.h" #include "fastpair/message_stream/fake_provider.h" +#include "fastpair/plugins/fake_fast_pair_plugin.h" #include "fastpair/server_access/fake_fast_pair_repository.h" #include "internal/platform/logging.h" #include "internal/platform/medium_environment.h" diff --git a/fastpair/internal/fast_pair_seeker_impl.cc b/fastpair/internal/fast_pair_seeker_impl.cc index ca84cd04..34806e33 100644 --- a/fastpair/internal/fast_pair_seeker_impl.cc +++ b/fastpair/internal/fast_pair_seeker_impl.cc @@ -26,19 +26,19 @@ namespace nearby { namespace fastpair { absl::Status FastPairSeekerImpl::StartInitialPairing( - FastPairDevice& device, const InitialPairingParam& params, + const FastPairDevice& device, const InitialPairingParam& params, PairingCallback callback) { return absl::UnimplementedError("StartInitialPairing"); } absl::Status FastPairSeekerImpl::StartSubsequentPairing( - FastPairDevice& device, const SubsequentPairingParam& params, + const FastPairDevice& device, const SubsequentPairingParam& params, PairingCallback callback) { return absl::UnimplementedError("StartSubsequentPairing"); } absl::Status FastPairSeekerImpl::StartRetroactivePairing( - FastPairDevice& device, const RetroactivePairingParam& param, + const FastPairDevice& device, const RetroactivePairingParam& param, PairingCallback callback) { return absl::UnimplementedError("StartRetroactivePairing"); } diff --git a/fastpair/internal/fast_pair_seeker_impl.h b/fastpair/internal/fast_pair_seeker_impl.h index 08c9dc9b..e0a3bdfb 100644 --- a/fastpair/internal/fast_pair_seeker_impl.h +++ b/fastpair/internal/fast_pair_seeker_impl.h @@ -59,15 +59,15 @@ class FastPairSeekerImpl : public FastPairSeekerExt, devices_(devices) {} // From FastPairSeeker. - absl::Status StartInitialPairing(FastPairDevice& device, + absl::Status StartInitialPairing(const FastPairDevice& device, const InitialPairingParam& params, PairingCallback callback) override; - absl::Status StartSubsequentPairing(FastPairDevice& device, + absl::Status StartSubsequentPairing(const FastPairDevice& device, const SubsequentPairingParam& params, PairingCallback callback) override; - absl::Status StartRetroactivePairing(FastPairDevice& device, + absl::Status StartRetroactivePairing(const FastPairDevice& device, const RetroactivePairingParam& param, PairingCallback callback) override; diff --git a/fastpair/mock_fast_pair_seeker.h b/fastpair/mock_fast_pair_seeker.h new file mode 100644 index 00000000..cd9b19f7 --- /dev/null +++ b/fastpair/mock_fast_pair_seeker.h @@ -0,0 +1,46 @@ +// 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_MOCK_FAST_PAIR_SEEKER_H_ +#define THIRD_PARTY_NEARBY_FASTPAIR_MOCK_FAST_PAIR_SEEKER_H_ + +#include "gmock/gmock.h" +#include "absl/status/status.h" +#include "fastpair/fast_pair_seeker.h" + +namespace nearby { +namespace fastpair { + +class MockFastPairSeeker : public FastPairSeeker { + public: + MOCK_METHOD(absl::Status, StartInitialPairing, + (const FastPairDevice& device, const InitialPairingParam& params, + PairingCallback callback), + (override)); + + MOCK_METHOD(absl::Status, StartSubsequentPairing, + (const FastPairDevice& device, + const SubsequentPairingParam& params, PairingCallback callback), + (override)); + + MOCK_METHOD(absl::Status, StartRetroactivePairing, + (const FastPairDevice& device, + const RetroactivePairingParam& param, PairingCallback callback), + (override)); +}; + +} // namespace fastpair +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_FASTPAIR_MOCK_FAST_PAIR_SEEKER_H_ diff --git a/fastpair/plugins/BUILD b/fastpair/plugins/BUILD new file mode 100644 index 00000000..707b7eaa --- /dev/null +++ b/fastpair/plugins/BUILD @@ -0,0 +1,69 @@ +# 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. + +package_group( + name = "nearby_fastpair", + packages = [ + "//fastpair/...", + ], +) + +cc_library( + name = "fake_initial_pair_plugin", + hdrs = ["fake_initial_pair_plugin.h"], + compatible_with = ["//buildenv/target:non_prod"], + visibility = ["//:__subpackages__"], + deps = [ + "//fastpair:fast_pair_events", + "//fastpair:fast_pair_plugin", + "//fastpair:fast_pair_seeker", + "//fastpair/common", + "//internal/platform:types", + ], +) + +cc_test( + name = "fake_initial_pair_plugin_test", + size = "small", + srcs = [ + "fake_initial_pair_plugin_test.cc", + ], + deps = [ + ":fake_initial_pair_plugin", + "//fastpair:test_support", + "//fastpair/common", + "//internal/platform/implementation/g3", # build_cleaner: keep + "@com_github_protobuf_matchers//protobuf-matchers", + "@com_google_absl//absl/status", + "@com_google_googletest//:gtest_main", + ], +) + +cc_library( + name = "fake_fast_pair_plugin", + testonly = True, + srcs = ["fake_fast_pair_plugin.cc"], + hdrs = [ + "fake_fast_pair_plugin.h", + ], + visibility = [ + "//fastpair:__subpackages__", + ], + deps = [ + "//fastpair:fast_pair_events", + "//fastpair:fast_pair_plugin", + "//fastpair:fast_pair_seeker", + "//fastpair/common", + ], +) diff --git a/fastpair/fake_fast_pair_plugin.cc b/fastpair/plugins/fake_fast_pair_plugin.cc similarity index 94% rename from fastpair/fake_fast_pair_plugin.cc rename to fastpair/plugins/fake_fast_pair_plugin.cc index 55e82b4d..d04f3cbd 100644 --- a/fastpair/fake_fast_pair_plugin.cc +++ b/fastpair/plugins/fake_fast_pair_plugin.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "fastpair/fake_fast_pair_plugin.h" +#include "fastpair/plugins/fake_fast_pair_plugin.h" namespace nearby { namespace fastpair { diff --git a/fastpair/fake_fast_pair_plugin.h b/fastpair/plugins/fake_fast_pair_plugin.h similarity index 100% rename from fastpair/fake_fast_pair_plugin.h rename to fastpair/plugins/fake_fast_pair_plugin.h diff --git a/fastpair/plugins/fake_initial_pair_plugin.h b/fastpair/plugins/fake_initial_pair_plugin.h new file mode 100644 index 00000000..6f4da38c --- /dev/null +++ b/fastpair/plugins/fake_initial_pair_plugin.h @@ -0,0 +1,61 @@ +// 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_PLUGINS_INITIAL_PAIR_PLUGIN_H_ +#define THIRD_PARTY_NEARBY_FASTPAIR_PLUGINS_INITIAL_PAIR_PLUGIN_H_ + +#include + +#include "fastpair/common/fast_pair_device.h" +#include "fastpair/fast_pair_events.h" +#include "fastpair/fast_pair_plugin.h" +#include "fastpair/fast_pair_seeker.h" +#include "internal/platform/logging.h" + +namespace nearby { +namespace fastpair { + +// Simple plugin that starts pairing when it sees a discoverable advertisement. +class FakeInitialPairPlugin : public FastPairPlugin { + public: + class Provider : public FastPairPluginProvider { + public: + std::unique_ptr GetPlugin( + FastPairSeeker* seeker, const FastPairDevice* device) override { + return std::make_unique(seeker, device); + } + }; + + FakeInitialPairPlugin(FastPairSeeker* seeker, const FastPairDevice* device) + : seeker_(seeker), device_(device) {} + + void OnInitialDiscoveryEvent(const InitialDiscoveryEvent& event) override { + absl::Status status = seeker_->StartInitialPairing( + *device_, InitialPairingParam{}, + {.on_pairing_result = [](const FastPairDevice& device, + absl::Status status) { + NEARBY_LOGS(INFO) << "Pairing result: " << status; + }}); + NEARBY_LOGS(INFO) << "StartInitialPairing: " << status; + } + + private: + FastPairSeeker* seeker_; + const FastPairDevice* device_; +}; + +} // namespace fastpair +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_FASTPAIR_PLUGINS_INITIAL_PAIR_PLUGIN_H_ diff --git a/fastpair/plugins/fake_initial_pair_plugin_test.cc b/fastpair/plugins/fake_initial_pair_plugin_test.cc new file mode 100644 index 00000000..0a885627 --- /dev/null +++ b/fastpair/plugins/fake_initial_pair_plugin_test.cc @@ -0,0 +1,51 @@ +// 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/plugins/fake_initial_pair_plugin.h" + +#include + +#include "gmock/gmock.h" +#include "protobuf-matchers/protocol-buffer-matchers.h" +#include "gtest/gtest.h" +#include "absl/status/status.h" +#include "fastpair/common/protocol.h" +#include "fastpair/mock_fast_pair_seeker.h" + +namespace nearby { +namespace fastpair { +namespace { + +using ::testing::Return; + +} // namespace + +TEST(FakeInitialPairPluginTest, InitialPair) { + constexpr absl::string_view kModelId = "123456"; + constexpr absl::string_view kBleAddress = "F1:F2:F3:F4:F5:F6"; + MockFastPairSeeker seeker; + FastPairDevice device(kModelId, kBleAddress, + Protocol::kFastPairInitialPairing); + EXPECT_CALL(seeker, StartInitialPairing) + .Times(1) + .WillOnce(Return(absl::OkStatus())); + + FakeInitialPairPlugin::Provider provider; + std::unique_ptr plugin = provider.GetPlugin(&seeker, &device); + plugin->OnInitialDiscoveryEvent(InitialDiscoveryEvent{}); + + plugin.reset(); +} +} // namespace fastpair +} // namespace nearby