Commit 582d3964 authored by Sylvain Defresne's avatar Sylvain Defresne Committed by Commit Bot

Add implementation for PrimaryAccountMutatorImpl [1/N]

Add implementation and test for PrimaryAccountMutatorImpl
methods {Set,Is}SettingPrimaryAccountAllowed.

Bug: 889902, 806778
Change-Id: Ibd75f2b3a988b4c403eab3b682a157e31e4b2e18
Reviewed-on: https://chromium-review.googlesource.com/c/1335937Reviewed-by: default avatarColin Blundell <blundell@chromium.org>
Reviewed-by: default avatarMihai Sardarescu <msarda@chromium.org>
Commit-Queue: Sylvain Defresne <sdefresne@chromium.org>
Cr-Commit-Position: refs/heads/master@{#608390}
parent 1435238d
...@@ -324,6 +324,10 @@ bool SigninManager::IsSigninAllowed() const { ...@@ -324,6 +324,10 @@ bool SigninManager::IsSigninAllowed() const {
return signin_allowed_.GetValue(); return signin_allowed_.GetValue();
} }
void SigninManager::SetSigninAllowed(bool allowed) {
signin_allowed_.SetValue(allowed);
}
void SigninManager::OnSigninAllowedPrefChanged() { void SigninManager::OnSigninAllowedPrefChanged() {
if (!IsSigninAllowed() && (IsAuthenticated() || AuthInProgress())) if (!IsSigninAllowed() && (IsAuthenticated() || AuthInProgress()))
SignOut(signin_metrics::SIGNOUT_PREF_CHANGED, SignOut(signin_metrics::SIGNOUT_PREF_CHANGED,
......
...@@ -163,8 +163,13 @@ class SigninManager : public SigninManagerBase, ...@@ -163,8 +163,13 @@ class SigninManager : public SigninManagerBase,
// Returns true if there's a signin in progress. // Returns true if there's a signin in progress.
bool AuthInProgress() const override; bool AuthInProgress() const override;
// Returns whether sign-in is allowed.
// TODO(crbug.com/806778): Remove method in super-class.
bool IsSigninAllowed() const override; bool IsSigninAllowed() const override;
// Sets whether sign-in is allowed or not.
void SetSigninAllowed(bool allowed);
// Returns true if the passed username is allowed by policy. Virtual for // Returns true if the passed username is allowed by policy. Virtual for
// mocking in tests. // mocking in tests.
virtual bool IsAllowedUsername(const std::string& username) const; virtual bool IsAllowedUsername(const std::string& username) const;
......
...@@ -129,11 +129,9 @@ class SigninManagerBase : public KeyedService { ...@@ -129,11 +129,9 @@ class SigninManagerBase : public KeyedService {
bool IsInitialized() const; bool IsInitialized() const;
// Returns true if a signin to Chrome is allowed (by policy or pref). // Returns true if a signin to Chrome is allowed (by policy or pref).
// TODO(tim): kSigninAllowed is defined for all platforms in pref_names.h. // TODO(crbug.com/806778): this method should not be used externally,
// If kSigninAllowed pref was non-Chrome OS-only, this method wouldn't be // instead the value of the kSigninAllowed preference should be checked.
// needed, but as is we provide this method to let all interested code // Once all external code has been modified, this method will be removed.
// code query the value in one way, versus half using PrefService directly
// and the other half using SigninManager.
virtual bool IsSigninAllowed() const; virtual bool IsSigninAllowed() const;
// If a user has previously signed in (and has not signed out), this returns // If a user has previously signed in (and has not signed out), this returns
......
...@@ -46,6 +46,7 @@ source_set("tests") { ...@@ -46,6 +46,7 @@ source_set("tests") {
"//mojo/public/cpp/bindings:bindings", "//mojo/public/cpp/bindings:bindings",
"//services/identity/public/cpp", "//services/identity/public/cpp",
"//services/identity/public/cpp:test_support", "//services/identity/public/cpp:test_support",
"//services/identity/public/cpp:tests",
"//services/identity/public/mojom", "//services/identity/public/mojom",
"//services/service_manager/public/cpp", "//services/service_manager/public/cpp",
"//services/service_manager/public/cpp:service_test_support", "//services/service_manager/public/cpp:service_test_support",
......
...@@ -64,3 +64,18 @@ source_set("test_support") { ...@@ -64,3 +64,18 @@ source_set("test_support") {
"//components/sync_preferences:test_support", "//components/sync_preferences:test_support",
] ]
} }
source_set("tests") {
testonly = true
sources = [
"primary_account_mutator_unittest.cc",
]
deps = [
":cpp",
":test_support",
"//base",
"//base/test:test_support",
"//testing/gtest",
]
}
...@@ -33,12 +33,11 @@ void PrimaryAccountMutatorImpl::ClearPrimaryAccount( ...@@ -33,12 +33,11 @@ void PrimaryAccountMutatorImpl::ClearPrimaryAccount(
} }
bool PrimaryAccountMutatorImpl::IsSettingPrimaryAccountAllowed() const { bool PrimaryAccountMutatorImpl::IsSettingPrimaryAccountAllowed() const {
NOTIMPLEMENTED(); return signin_manager_->IsSigninAllowed();
return false;
} }
void PrimaryAccountMutatorImpl::SetSettingPrimaryAccountAllowed(bool allowed) { void PrimaryAccountMutatorImpl::SetSettingPrimaryAccountAllowed(bool allowed) {
NOTIMPLEMENTED(); signin_manager_->SetSigninAllowed(allowed);
} }
bool PrimaryAccountMutatorImpl::IsClearingPrimaryAccountAllowed() const { bool PrimaryAccountMutatorImpl::IsClearingPrimaryAccountAllowed() const {
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "services/identity/public/cpp/primary_account_mutator.h"
#include "base/test/scoped_task_environment.h"
#include "services/identity/public/cpp/identity_test_environment.h"
#include "testing/platform_test.h"
using PrimaryAccountMutatorTest = PlatformTest;
// Checks that the method to control whether setting the primary account is
// working correctly and that the setting is respected by SetPrimaryAccount().
TEST_F(PrimaryAccountMutatorTest, SetSettingPrimaryAccountAllowed) {
base::test::ScopedTaskEnvironment task_environment;
identity::IdentityTestEnvironment environment;
identity::IdentityManager* identity_manager = environment.identity_manager();
identity::PrimaryAccountMutator* primary_account_mutator =
identity_manager->GetPrimaryAccountMutator();
// Abort the test if the current platform does not support mutation of the
// primary account (the returned PrimaryAccountMutator* will be null).
if (!primary_account_mutator)
return;
primary_account_mutator->SetSettingPrimaryAccountAllowed(false);
EXPECT_FALSE(primary_account_mutator->IsSettingPrimaryAccountAllowed());
primary_account_mutator->SetSettingPrimaryAccountAllowed(true);
EXPECT_TRUE(primary_account_mutator->IsSettingPrimaryAccountAllowed());
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment