// 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_SHARING_INTERNAL_TEST_FAKE_PREFERENCE_MANAGER_H_ #define THIRD_PARTY_NEARBY_SHARING_INTERNAL_TEST_FAKE_PREFERENCE_MANAGER_H_ #include #include #include #include #include #include #include #include "absl/base/thread_annotations.h" #include "absl/container/flat_hash_map.h" #include "absl/strings/string_view.h" #include "absl/synchronization/mutex.h" #include "absl/time/time.h" #include "absl/types/span.h" #include "sharing/internal/api/preference_manager.h" #include "sharing/internal/api/private_certificate_data.h" namespace nearby { // An in memory fake PreferenceManager implementation for testing. class FakePreferenceManager : public nearby::sharing::api::PreferenceManager { public: void SetBoolean(absl::string_view key, bool value) override; void SetInteger(absl::string_view key, int value) override; void SetInt64(absl::string_view key, int64_t value) override; void SetString(absl::string_view key, absl::string_view value) override; void SetTime(absl::string_view key, absl::Time value) override; void SetBooleanArray(absl::string_view key, absl::Span value) override; void SetIntegerArray(absl::string_view key, absl::Span value) override; void SetInt64Array(absl::string_view key, absl::Span value) override; void SetStringArray(absl::string_view key, absl::Span value) override; void SetPrivateCertificateArray( absl::string_view key, absl::Span value) override; void SetCertificateExpirationArray( absl::string_view key, absl::Span> value) override; void SetDictionaryBooleanValue(absl::string_view key, absl::string_view dictionary_item, bool value) override; void SetDictionaryIntegerValue(absl::string_view key, absl::string_view dictionary_item, int value) override; void SetDictionaryInt64Value(absl::string_view key, absl::string_view dictionary_item, int64_t value) override; void SetDictionaryStringValue(absl::string_view key, absl::string_view dictionary_item, std::string value) override; void RemoveDictionaryItem(absl::string_view key, absl::string_view dictionary_item) override; bool GetBoolean(absl::string_view key, bool default_value) const override; int GetInteger(absl::string_view key, int default_value) const override; int64_t GetInt64(absl::string_view key, int64_t default_value) const override; std::string GetString(absl::string_view key, const std::string& default_value) const override; absl::Time GetTime(absl::string_view key, absl::Time default_value) const override; std::vector GetBooleanArray( absl::string_view key, absl::Span default_value) const override; std::vector GetIntegerArray( absl::string_view key, absl::Span default_value) const override; std::vector GetInt64Array( absl::string_view key, absl::Span default_value) const override; std::vector GetStringArray( absl::string_view key, absl::Span default_value) const override; std::vector GetPrivateCertificateArray(absl::string_view key) const override; std::vector> GetCertificateExpirationArray( absl::string_view key) const override; std::optional GetDictionaryBooleanValue( absl::string_view key, absl::string_view dictionary_item) const override; std::optional GetDictionaryIntegerValue( absl::string_view key, absl::string_view dictionary_item) const override; std::optional GetDictionaryInt64Value( absl::string_view key, absl::string_view dictionary_item) const override; std::optional GetDictionaryStringValue( absl::string_view key, absl::string_view dictionary_item) const override; void Remove(absl::string_view key) override; void AddObserver( absl::string_view name, std::function observer) override; void RemoveObserver(absl::string_view name) override; private: typedef std::variant Data; template void SetValue(absl::string_view key, T value) ABSL_LOCKS_EXCLUDED(mutex_); template T GetValue(absl::string_view key, const T& default_value) const ABSL_LOCKS_EXCLUDED(mutex_); template void SetArray(absl::string_view key, absl::Span values) ABSL_LOCKS_EXCLUDED(mutex_); template std::vector GetArray(absl::string_view key, absl::Span default_value) const ABSL_LOCKS_EXCLUDED(mutex_); template void SetDictionaryValue(absl::string_view key, absl::string_view dictionary_item, T value) ABSL_LOCKS_EXCLUDED(mutex_); template std::optional GetDictionaryValue(absl::string_view key, absl::string_view dictionary_item) const ABSL_LOCKS_EXCLUDED(mutex_); void NotifyPreferenceChanged(absl::string_view key) ABSL_LOCKS_EXCLUDED(mutex_); mutable absl::Mutex mutex_; absl::flat_hash_map values_ ABSL_GUARDED_BY(mutex_); absl::flat_hash_map> dictionaries_ ABSL_GUARDED_BY(mutex_); absl::flat_hash_map> arrays_ ABSL_GUARDED_BY(mutex_); absl::flat_hash_map> certs_ ABSL_GUARDED_BY(mutex_); absl::flat_hash_map>> cert_expirations_ ABSL_GUARDED_BY(mutex_); absl::flat_hash_map> observers_ ABSL_GUARDED_BY(mutex_); }; } // namespace nearby #endif // THIRD_PARTY_NEARBY_SHARING_INTERNAL_TEST_FAKE_PREFERENCE_MANAGER_H_