From c1ffccfacf0833949b5d8153beccc06dcde2fbab Mon Sep 17 00:00:00 2001 From: hai007 Date: Thu, 2 Feb 2023 14:16:05 -0800 Subject: [PATCH] Create FakeFastPairRepository for test support PiperOrigin-RevId: 506720516 --- fastpair/server_access/BUILD | 14 ++++++ .../fake_fast_pair_repository.cc | 46 ++++++++++++++++++ .../server_access/fake_fast_pair_repository.h | 48 +++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 fastpair/server_access/fake_fast_pair_repository.cc create mode 100644 fastpair/server_access/fake_fast_pair_repository.h diff --git a/fastpair/server_access/BUILD b/fastpair/server_access/BUILD index 7e6a722a..72fa3fb0 100644 --- a/fastpair/server_access/BUILD +++ b/fastpair/server_access/BUILD @@ -29,6 +29,20 @@ cc_library( ], ) +cc_library( + name = "test_support", + srcs = ["fake_fast_pair_repository.cc"], + hdrs = ["fake_fast_pair_repository.h"], + deps = [ + ":server_access", + "//fastpair/common", + "//fastpair/proto:fastpair_cc_proto", + "//fastpair/repository", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/strings", + ], +) + cc_test( name = "server_access_test", srcs = [ diff --git a/fastpair/server_access/fake_fast_pair_repository.cc b/fastpair/server_access/fake_fast_pair_repository.cc new file mode 100644 index 00000000..b5e3b9d1 --- /dev/null +++ b/fastpair/server_access/fake_fast_pair_repository.cc @@ -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. + +#include "fastpair/server_access/fake_fast_pair_repository.h" + +#include +#include + +#include "absl/strings/string_view.h" +#include "fastpair/proto/fastpair_rpcs.proto.h" + +namespace nearby { +namespace fastpair { +void FakeFastPairRepository::SetFakeMetadata(absl::string_view hex_model_id, + proto::Device metadata) { + proto::GetObservedDeviceResponse response; + *response.mutable_device() = metadata; + data_[hex_model_id] = std::make_unique(response); +} + +void FakeFastPairRepository::ClearFakeMetadata(absl::string_view hex_model_id) { + data_.erase(hex_model_id); +} + +void FakeFastPairRepository::GetDeviceMetadata( + absl::string_view hex_model_id, DeviceMetadataCallback callback) { + callback_ = std::move(callback); + if (data_.contains(hex_model_id)) { + std::move(callback_)(*data_[hex_model_id]); + return; + } +} + +} // namespace fastpair +} // namespace nearby diff --git a/fastpair/server_access/fake_fast_pair_repository.h b/fastpair/server_access/fake_fast_pair_repository.h new file mode 100644 index 00000000..071a6a85 --- /dev/null +++ b/fastpair/server_access/fake_fast_pair_repository.h @@ -0,0 +1,48 @@ +// 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_FAKE_FAST_PAIR_REPOSITORY_H_ +#define THIRD_PARTY_NEARBY_FASTPAIR_SERVER_ACCESS_FAKE_FAST_PAIR_REPOSITORY_H_ + +#include +#include + +#include "absl/container/flat_hash_map.h" +#include "absl/strings/string_view.h" +#include "fastpair/repository/device_metadata.h" +#include "fastpair/server_access/fast_pair_repository.h" + +namespace nearby { +namespace fastpair { +class FakeFastPairRepository : public FastPairRepository { + public: + FakeFastPairRepository(); + FakeFastPairRepository(const FakeFastPairRepository&) = delete; + FakeFastPairRepository& operator=(const FakeFastPairRepository&) = delete; + ~FakeFastPairRepository() override; + + void SetFakeMetadata(absl::string_view hex_model_id, proto::Device metadata); + void ClearFakeMetadata(absl::string_view hex_model_id); + // FastPairRepository:: + void GetDeviceMetadata(absl::string_view hex_model_id, + DeviceMetadataCallback callback) override; + + private: + absl::flat_hash_map> data_; + DeviceMetadataCallback callback_; +}; +} // namespace fastpair +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_FASTPAIR_SERVER_ACCESS_FAKE_FAST_PAIR_REPOSITORY_H_