chromium/ash/session/test_pref_service_provider.cc

// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/session/test_pref_service_provider.h"

#include <algorithm>

#include "ash/public/cpp/ash_prefs.h"
#include "base/check.h"
#include "components/account_id/account_id.h"
#include "components/prefs/testing_pref_service.h"

namespace ash {

TestPrefServiceProvider::TestPrefServiceProvider() = default;
TestPrefServiceProvider::~TestPrefServiceProvider() = default;

void TestPrefServiceProvider::CreateSigninPrefsIfNeeded() {
  if (signin_prefs_)
    return;

  auto pref_service = std::make_unique<TestingPrefServiceSimple>();
  RegisterSigninProfilePrefs(pref_service->registry(), /*country=*/"",
                             /**for_test=*/true);
  signin_prefs_ = std::move(pref_service);
}

void TestPrefServiceProvider::SetSigninPrefs(
    std::unique_ptr<PrefService> signin_prefs) {
  DCHECK(!signin_prefs_);
  signin_prefs_ = std::move(signin_prefs);
}

PrefService* TestPrefServiceProvider::GetSigninPrefs() {
  return signin_prefs_.get();
}

void TestPrefServiceProvider::CreateUserPrefs(const AccountId& account_id) {
  auto pref_service = std::make_unique<TestingPrefServiceSimple>();
  RegisterUserProfilePrefs(pref_service->registry(), /*country=*/"",
                           /*for_test=*/true);
  SetUserPrefs(account_id, std::move(pref_service));
}

void TestPrefServiceProvider::SetUserPrefs(
    const AccountId& account_id,
    std::unique_ptr<PrefService> pref_service) {
  const auto pair =
      user_prefs_map_.emplace(account_id, std::move(pref_service));
  DCHECK(pair.second);
}

PrefService* TestPrefServiceProvider::GetUserPrefs(
    const AccountId& account_id) {
  auto it = user_prefs_map_.find(account_id);
  if (it == user_prefs_map_.end())
    return nullptr;

  return it->second.get();
}

}  // namespace ash