test_pref_service_provider.cc 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/session/test_pref_service_provider.h"
  5. #include <algorithm>
  6. #include "ash/public/cpp/ash_prefs.h"
  7. #include "base/check.h"
  8. #include "components/account_id/account_id.h"
  9. #include "components/prefs/testing_pref_service.h"
  10. namespace ash {
  11. TestPrefServiceProvider::TestPrefServiceProvider() = default;
  12. TestPrefServiceProvider::~TestPrefServiceProvider() = default;
  13. void TestPrefServiceProvider::CreateSigninPrefsIfNeeded() {
  14. if (signin_prefs_)
  15. return;
  16. auto pref_service = std::make_unique<TestingPrefServiceSimple>();
  17. RegisterSigninProfilePrefs(pref_service->registry(), true /* for_test */);
  18. signin_prefs_ = std::move(pref_service);
  19. }
  20. void TestPrefServiceProvider::SetSigninPrefs(
  21. std::unique_ptr<PrefService> signin_prefs) {
  22. DCHECK(!signin_prefs_);
  23. signin_prefs_ = std::move(signin_prefs);
  24. }
  25. PrefService* TestPrefServiceProvider::GetSigninPrefs() {
  26. return signin_prefs_.get();
  27. }
  28. void TestPrefServiceProvider::CreateUserPrefs(const AccountId& account_id) {
  29. auto pref_service = std::make_unique<TestingPrefServiceSimple>();
  30. RegisterUserProfilePrefs(pref_service->registry(), true /* for_test */);
  31. SetUserPrefs(account_id, std::move(pref_service));
  32. }
  33. void TestPrefServiceProvider::SetUserPrefs(
  34. const AccountId& account_id,
  35. std::unique_ptr<PrefService> pref_service) {
  36. DCHECK(user_prefs_map_.find(account_id) == user_prefs_map_.end());
  37. user_prefs_map_[account_id] = std::move(pref_service);
  38. }
  39. PrefService* TestPrefServiceProvider::GetUserPrefs(
  40. const AccountId& account_id) {
  41. auto it = user_prefs_map_.find(account_id);
  42. if (it == user_prefs_map_.end())
  43. return nullptr;
  44. return it->second.get();
  45. }
  46. } // namespace ash