From 2027fed32ff990a88ee539b7aec62c11dab24f81 Mon Sep 17 00:00:00 2001 From: Chun Zhang Date: Wed, 7 Jun 2023 14:46:54 -0700 Subject: [PATCH] Add Fast Pair Preferences. PiperOrigin-RevId: 538597935 --- fastpair/common/BUILD | 19 ++++++++++ fastpair/common/fast_pair_prefs.cc | 33 +++++++++++++++++ fastpair/common/fast_pair_prefs.h | 34 +++++++++++++++++ fastpair/common/fast_pair_prefs_test.cc | 49 +++++++++++++++++++++++++ 4 files changed, 135 insertions(+) create mode 100644 fastpair/common/fast_pair_prefs.cc create mode 100644 fastpair/common/fast_pair_prefs.h create mode 100644 fastpair/common/fast_pair_prefs_test.cc diff --git a/fastpair/common/BUILD b/fastpair/common/BUILD index 565daba5..27ed6b32 100644 --- a/fastpair/common/BUILD +++ b/fastpair/common/BUILD @@ -7,6 +7,7 @@ cc_library( "battery_notification.cc", "fast_pair_device.cc", "fast_pair_http_result.cc", + "fast_pair_prefs.cc", "pair_failure.cc", "protocol.cc", ], @@ -17,6 +18,7 @@ cc_library( "constant.h", "fast_pair_device.h", "fast_pair_http_result.h", + "fast_pair_prefs.h", "non_discoverable_advertisement.h", "pair_failure.h", "protocol.h", @@ -27,6 +29,8 @@ cc_library( deps = [ "//internal/crypto", "//internal/platform:logging", + "//internal/preferences", + "@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/status", "@com_google_absl//absl/strings", @@ -94,3 +98,18 @@ cc_test( "@com_google_googletest//:gtest_main", ], ) + +cc_test( + name = "fast_pair_prefs_test", + size = "small", + srcs = [ + "fast_pair_prefs_test.cc", + ], + shard_count = 16, + deps = [ + ":common", + "//internal/platform/implementation/g3", # build_cleaner: keep + "@com_github_protobuf_matchers//protobuf-matchers", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/fastpair/common/fast_pair_prefs.cc b/fastpair/common/fast_pair_prefs.cc new file mode 100644 index 00000000..7278822a --- /dev/null +++ b/fastpair/common/fast_pair_prefs.cc @@ -0,0 +1,33 @@ +// 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/common/fast_pair_prefs.h" + +namespace nearby { +namespace fastpair { +namespace prefs { +namespace { +using json = ::nlohmann::json; +} + +const char kNearbyFastPairUsersName[] = "nearby_fastpair.users"; + +void RegisterNearbyFastPairPrefs( + preferences::PreferencesManager* preferences_manager) { + preferences_manager->Set(kNearbyFastPairUsersName, json::object()); +} + +} // namespace prefs +} // namespace fastpair +} // namespace nearby diff --git a/fastpair/common/fast_pair_prefs.h b/fastpair/common/fast_pair_prefs.h new file mode 100644 index 00000000..a689b760 --- /dev/null +++ b/fastpair/common/fast_pair_prefs.h @@ -0,0 +1,34 @@ +// 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_COMMON_FASTPAIR_PREFS_H_ +#define THIRD_PARTY_NEARBY_FASTPAIR_COMMON_FASTPAIR_PREFS_H_ + +#include "absl/base/attributes.h" +#include "internal/preferences/preferences_manager.h" + +namespace nearby { +namespace fastpair { +namespace prefs { + +ABSL_CONST_INIT extern const char kNearbyFastPairUsersName[]; + +void RegisterNearbyFastPairPrefs( + preferences::PreferencesManager* preferences_manager); + +} // namespace prefs +} // namespace fastpair +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_FASTPAIR_COMMON_FASTPAIR_PREFS_H_ diff --git a/fastpair/common/fast_pair_prefs_test.cc b/fastpair/common/fast_pair_prefs_test.cc new file mode 100644 index 00000000..03f54a98 --- /dev/null +++ b/fastpair/common/fast_pair_prefs_test.cc @@ -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. + +#include "fastpair/common/fast_pair_prefs.h" + +#include +#include + +#include "gtest/gtest.h" + +namespace nearby { +namespace fastpair { +namespace prefs { +namespace { + +using json = ::nlohmann::json; + +constexpr char kPreferencesFilePath[] = "Google/Nearby/Preferences"; + +TEST(FastPairPreference, RegisterNearbyFastPairPrefs) { + auto preferences_manager = + std::make_unique(kPreferencesFilePath); + RegisterNearbyFastPairPrefs(preferences_manager.get()); + EXPECT_TRUE( + preferences_manager->Get(kNearbyFastPairUsersName, json()).empty()); + + json value = {{"current_user", "abc@gmail.com"}}; + preferences_manager->Set(kNearbyFastPairUsersName, value); + auto result = preferences_manager->Get(kNearbyFastPairUsersName, json()); + ASSERT_FALSE(result.empty()); + auto val = result["current_user"]; + EXPECT_EQ(val.get(), "abc@gmail.com"); +} + +} // namespace +} // namespace prefs +} // namespace fastpair +} // namespace nearby