summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/signin/signin_profile_attributes_updater_unittest.cc
blob: 01430a350b0fecf4651279c3e454aa15a3bfabd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// 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 "chrome/browser/signin/signin_profile_attributes_updater.h"

#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/profiles/profile_attributes_entry.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/signin/signin_util.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "components/signin/public/identity_manager/identity_test_utils.h"
#include "components/sync_preferences/pref_service_syncable.h"
#include "content/public/test/browser_task_environment.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {
#if !BUILDFLAG(IS_CHROMEOS_ASH)
const char kEmail[] = "example@email.com";

void CheckProfilePrefsReset(PrefService* pref_service,
                            bool expected_using_default_name) {
  EXPECT_TRUE(pref_service->GetBoolean(prefs::kProfileUsingDefaultAvatar));
  EXPECT_FALSE(pref_service->GetBoolean(prefs::kProfileUsingGAIAAvatar));
  EXPECT_EQ(expected_using_default_name,
            pref_service->GetBoolean(prefs::kProfileUsingDefaultName));
}

void CheckProfilePrefsSet(PrefService* pref_service,
                          bool expected_is_using_default_name) {
  EXPECT_FALSE(pref_service->GetBoolean(prefs::kProfileUsingDefaultAvatar));
  EXPECT_TRUE(pref_service->GetBoolean(prefs::kProfileUsingGAIAAvatar));
  EXPECT_EQ(expected_is_using_default_name,
            pref_service->GetBoolean(prefs::kProfileUsingDefaultName));
}

// Set the prefs to nondefault values.
void SetProfilePrefs(PrefService* pref_service) {
  pref_service->SetBoolean(prefs::kProfileUsingDefaultAvatar, false);
  pref_service->SetBoolean(prefs::kProfileUsingGAIAAvatar, true);
  pref_service->SetBoolean(prefs::kProfileUsingDefaultName, false);

  CheckProfilePrefsSet(pref_service, false);
}
#endif  // !BUILDFLAG(IS_CHROMEOS_ASH)
}  // namespace

class SigninProfileAttributesUpdaterTest : public testing::Test {
 public:
  SigninProfileAttributesUpdaterTest()
      : profile_manager_(TestingBrowserProcess::GetGlobal()) {}

  // Recreates |signin_profile_attributes_updater_|. Useful for tests that want
  // to set up the updater with specific preconditions.
  void RecreateSigninProfileAttributesUpdater() {
    signin_profile_attributes_updater_ =
        std::make_unique<SigninProfileAttributesUpdater>(
            identity_test_env_.identity_manager(),
            profile_manager_.profile_attributes_storage(), profile_->GetPath(),
            profile_->GetPrefs());
  }

  void SetUp() override {
    testing::Test::SetUp();

    ASSERT_TRUE(profile_manager_.SetUp());
    std::string name = "profile_name";
    profile_ = profile_manager_.CreateTestingProfile(
        name, /*prefs=*/nullptr, base::UTF8ToUTF16(name), 0, std::string(),
        TestingProfile::TestingFactories());

    RecreateSigninProfileAttributesUpdater();
  }

  content::BrowserTaskEnvironment task_environment_;
  TestingProfileManager profile_manager_;
  raw_ptr<TestingProfile> profile_;
  signin::IdentityTestEnvironment identity_test_env_;
  std::unique_ptr<SigninProfileAttributesUpdater>
      signin_profile_attributes_updater_;
};

#if !BUILDFLAG(IS_CHROMEOS_ASH)
// Tests that the browser state info is updated on signin and signout.
// ChromeOS does not support signout.
TEST_F(SigninProfileAttributesUpdaterTest, SigninSignout) {
  ProfileAttributesEntry* entry =
      profile_manager_.profile_attributes_storage()
          ->GetProfileAttributesWithPath(profile_->GetPath());
  ASSERT_NE(entry, nullptr);
  ASSERT_EQ(entry->GetSigninState(), SigninState::kNotSignedIn);
  EXPECT_FALSE(entry->IsSigninRequired());

  // Signin.
  identity_test_env_.MakePrimaryAccountAvailable(kEmail,
                                                 signin::ConsentLevel::kSync);
  EXPECT_TRUE(entry->IsAuthenticated());
  EXPECT_EQ(signin::GetTestGaiaIdForEmail(kEmail), entry->GetGAIAId());
  EXPECT_EQ(kEmail, base::UTF16ToUTF8(entry->GetUserName()));

  // Signout.
  identity_test_env_.ClearPrimaryAccount();
  EXPECT_EQ(entry->GetSigninState(), SigninState::kNotSignedIn);
  EXPECT_FALSE(entry->IsSigninRequired());
}
#endif  // !BUILDFLAG(IS_CHROMEOS_ASH)

#if !BUILDFLAG(IS_CHROMEOS_ASH)
TEST_F(SigninProfileAttributesUpdaterTest, SigninSignoutResetsProfilePrefs) {
  PrefService* pref_service = profile_->GetPrefs();
  ProfileAttributesEntry* entry =
      profile_manager_.profile_attributes_storage()
          ->GetProfileAttributesWithPath(profile_->GetPath());
  ASSERT_NE(entry, nullptr);

  // Set profile prefs.
  CheckProfilePrefsReset(pref_service, true);
#if !defined(OS_ANDROID)
  SetProfilePrefs(pref_service);

  // Set UPA should reset profile prefs.
  AccountInfo account_info = identity_test_env_.MakePrimaryAccountAvailable(
      "email1@example.com", signin::ConsentLevel::kSignin);
  EXPECT_FALSE(entry->IsAuthenticated());
  CheckProfilePrefsReset(pref_service, false);
  SetProfilePrefs(pref_service);
  // Signout should reset profile prefs.
  identity_test_env_.ClearPrimaryAccount();
  CheckProfilePrefsReset(pref_service, false);
#endif  // !defined(OS_ANDROID)

  SetProfilePrefs(pref_service);
  // Set primary account should reset profile prefs.
  AccountInfo primary_account = identity_test_env_.MakePrimaryAccountAvailable(
      "primary@example.com", signin::ConsentLevel::kSync);
  CheckProfilePrefsReset(pref_service, false);
  SetProfilePrefs(pref_service);
  // Disabling sync should reset profile prefs.
  identity_test_env_.ClearPrimaryAccount();
  CheckProfilePrefsReset(pref_service, false);
}

#if !defined(OS_ANDROID)
TEST_F(SigninProfileAttributesUpdaterTest,
       EnablingSyncWithUPAAccountShouldNotResetProfilePrefs) {
  PrefService* pref_service = profile_->GetPrefs();
  ProfileAttributesEntry* entry =
      profile_manager_.profile_attributes_storage()
          ->GetProfileAttributesWithPath(profile_->GetPath());
  ASSERT_NE(entry, nullptr);
  // Set UPA.
  AccountInfo account_info = identity_test_env_.MakePrimaryAccountAvailable(
      "email1@example.com", signin::ConsentLevel::kSignin);
  EXPECT_FALSE(entry->IsAuthenticated());
  SetProfilePrefs(pref_service);
  // Set primary account to be the same as the UPA.
  // Given it is the same account, profile prefs should keep the same state.
  identity_test_env_.SetPrimaryAccount(account_info.email,
                                       signin::ConsentLevel::kSync);
  EXPECT_TRUE(entry->IsAuthenticated());
  CheckProfilePrefsSet(pref_service, false);
  identity_test_env_.ClearPrimaryAccount();
  CheckProfilePrefsReset(pref_service, false);
}

TEST_F(SigninProfileAttributesUpdaterTest,
       EnablingSyncWithDifferentAccountThanUPAResetsProfilePrefs) {
  PrefService* pref_service = profile_->GetPrefs();
  ProfileAttributesEntry* entry =
      profile_manager_.profile_attributes_storage()
          ->GetProfileAttributesWithPath(profile_->GetPath());
  ASSERT_NE(entry, nullptr);
  AccountInfo account_info = identity_test_env_.MakePrimaryAccountAvailable(
      "email1@example.com", signin::ConsentLevel::kSignin);
  EXPECT_FALSE(entry->IsAuthenticated());
  SetProfilePrefs(pref_service);
  // Set primary account to a different account than the UPA.
  AccountInfo primary_account = identity_test_env_.MakePrimaryAccountAvailable(
      "primary@example.com", signin::ConsentLevel::kSync);
  EXPECT_TRUE(entry->IsAuthenticated());
  CheckProfilePrefsReset(pref_service, false);
}
#endif  // !defined(OS_ANDROID)

class SigninProfileAttributesUpdaterWithForceSigninTest
    : public SigninProfileAttributesUpdaterTest {
 public:
  SigninProfileAttributesUpdaterWithForceSigninTest()
      : forced_signin_setter_(true) {}

 private:
  signin_util::ScopedForceSigninSetterForTesting forced_signin_setter_;
};

TEST_F(SigninProfileAttributesUpdaterWithForceSigninTest, IsSigninRequired) {
  ProfileAttributesEntry* entry =
      profile_manager_.profile_attributes_storage()
          ->GetProfileAttributesWithPath(profile_->GetPath());
  ASSERT_NE(entry, nullptr);
  EXPECT_FALSE(entry->IsAuthenticated());
  EXPECT_TRUE(entry->IsSigninRequired());

  AccountInfo account_info = identity_test_env_.MakePrimaryAccountAvailable(
      kEmail, signin::ConsentLevel::kSync);

  EXPECT_TRUE(entry->IsAuthenticated());
  EXPECT_EQ(signin::GetTestGaiaIdForEmail(kEmail), entry->GetGAIAId());
  EXPECT_EQ(kEmail, base::UTF16ToUTF8(entry->GetUserName()));

  identity_test_env_.ClearPrimaryAccount();
  EXPECT_EQ(entry->GetSigninState(), SigninState::kNotSignedIn);
  EXPECT_TRUE(entry->IsSigninRequired());
}
#endif  // !BUILDFLAG(IS_CHROMEOS_ASH)