1 // Copyright (c) 2013 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.
5 #include "chrome/browser/signin/signin_global_error.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/test/histogram_tester.h"
11 #include "chrome/browser/prefs/pref_service_syncable.h"
12 #include "chrome/browser/profiles/profile_info_cache.h"
13 #include "chrome/browser/profiles/profile_manager.h"
14 #include "chrome/browser/profiles/profile_metrics.h"
15 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
16 #include "chrome/browser/signin/fake_signin_manager_builder.h"
17 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
18 #include "chrome/browser/signin/signin_error_controller_factory.h"
19 #include "chrome/browser/signin/signin_global_error_factory.h"
20 #include "chrome/browser/signin/signin_manager_factory.h"
21 #include "chrome/browser/ui/global_error/global_error_service.h"
22 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
23 #include "chrome/common/pref_names.h"
24 #include "chrome/test/base/testing_browser_process.h"
25 #include "chrome/test/base/testing_profile.h"
26 #include "chrome/test/base/testing_profile_manager.h"
27 #include "components/signin/core/browser/fake_auth_status_provider.h"
28 #include "components/signin/core/browser/signin_error_controller.h"
29 #include "components/signin/core/browser/signin_manager.h"
30 #include "content/public/test/test_browser_thread_bundle.h"
31 #include "testing/gtest/include/gtest/gtest.h"
33 static const char kTestAccountId
[] = "id-testuser@test.com";
34 static const char kTestGaiaId
[] = "gaiaid-testuser@test.com";
35 static const char kTestUsername
[] = "testuser@test.com";
37 class SigninGlobalErrorTest
: public testing::Test
{
39 SigninGlobalErrorTest() :
40 profile_manager_(TestingBrowserProcess::GetGlobal()) {}
42 void SetUp() override
{
43 ASSERT_TRUE(profile_manager_
.SetUp());
45 // Create a signed-in profile.
46 TestingProfile::TestingFactories testing_factories
;
47 testing_factories
.push_back(std::make_pair(
48 ProfileOAuth2TokenServiceFactory::GetInstance(),
49 BuildFakeProfileOAuth2TokenService
));
50 testing_factories
.push_back(std::make_pair(
51 SigninManagerFactory::GetInstance(), BuildFakeSigninManagerBase
));
52 profile_
= profile_manager_
.CreateTestingProfile(
53 "Person 1", scoped_ptr
<PrefServiceSyncable
>(),
54 base::UTF8ToUTF16("Person 1"), 0, std::string(), testing_factories
);
56 SigninManagerFactory::GetForProfile(profile())
57 ->SetAuthenticatedAccountInfo(kTestAccountId
, kTestUsername
);
58 ProfileInfoCache
& cache
=
59 profile_manager_
.profile_manager()->GetProfileInfoCache();
60 cache
.SetAuthInfoOfProfileAtIndex(
61 cache
.GetIndexOfProfileWithPath(profile()->GetPath()),
62 kTestGaiaId
, base::UTF8ToUTF16(kTestUsername
));
64 global_error_
= SigninGlobalErrorFactory::GetForProfile(profile());
65 error_controller_
= SigninErrorControllerFactory::GetForProfile(profile());
68 TestingProfile
* profile() { return profile_
; }
69 TestingProfileManager
* testing_profile_manager() {
70 return &profile_manager_
;
72 SigninGlobalError
* global_error() { return global_error_
; }
73 SigninErrorController
* error_controller() { return error_controller_
; }
76 content::TestBrowserThreadBundle thread_bundle_
;
77 TestingProfileManager profile_manager_
;
78 TestingProfile
* profile_
;
79 SigninGlobalError
* global_error_
;
80 SigninErrorController
* error_controller_
;
83 TEST_F(SigninGlobalErrorTest
, NoErrorAuthStatusProviders
) {
84 scoped_ptr
<FakeAuthStatusProvider
> provider
;
86 ASSERT_FALSE(global_error()->HasMenuItem());
89 provider
.reset(new FakeAuthStatusProvider(error_controller()));
90 ASSERT_FALSE(global_error()->HasMenuItem());
92 // Remove the provider.
94 ASSERT_FALSE(global_error()->HasMenuItem());
97 TEST_F(SigninGlobalErrorTest
, ErrorAuthStatusProvider
) {
98 scoped_ptr
<FakeAuthStatusProvider
> provider
;
99 scoped_ptr
<FakeAuthStatusProvider
> error_provider
;
101 provider
.reset(new FakeAuthStatusProvider(error_controller()));
102 ASSERT_FALSE(global_error()->HasMenuItem());
104 error_provider
.reset(new FakeAuthStatusProvider(error_controller()));
105 error_provider
->SetAuthError(
107 GoogleServiceAuthError(
108 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
));
109 ASSERT_TRUE(global_error()->HasMenuItem());
111 error_provider
.reset();
112 ASSERT_FALSE(global_error()->HasMenuItem());
115 error_provider
.reset();
116 ASSERT_FALSE(global_error()->HasMenuItem());
119 // Verify that SigninGlobalError ignores certain errors.
120 TEST_F(SigninGlobalErrorTest
, AuthStatusEnumerateAllErrors
) {
122 GoogleServiceAuthError::State error_state
;
126 ErrorTableEntry table
[] = {
127 { GoogleServiceAuthError::NONE
, false },
128 { GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
, true },
129 { GoogleServiceAuthError::USER_NOT_SIGNED_UP
, true },
130 { GoogleServiceAuthError::CONNECTION_FAILED
, false },
131 { GoogleServiceAuthError::CAPTCHA_REQUIRED
, true },
132 { GoogleServiceAuthError::ACCOUNT_DELETED
, true },
133 { GoogleServiceAuthError::ACCOUNT_DISABLED
, true },
134 { GoogleServiceAuthError::SERVICE_UNAVAILABLE
, true },
135 { GoogleServiceAuthError::TWO_FACTOR
, true },
136 { GoogleServiceAuthError::REQUEST_CANCELED
, true },
137 { GoogleServiceAuthError::HOSTED_NOT_ALLOWED
, true },
138 { GoogleServiceAuthError::UNEXPECTED_SERVICE_RESPONSE
, true },
139 { GoogleServiceAuthError::SERVICE_ERROR
, true },
140 { GoogleServiceAuthError::WEB_LOGIN_REQUIRED
, true },
142 static_assert(arraysize(table
) == GoogleServiceAuthError::NUM_STATES
,
143 "table size should match number of auth error types");
145 // Mark the profile with an active timestamp so profile_metrics logs it.
146 testing_profile_manager()->UpdateLastUser(profile());
148 for (size_t i
= 0; i
< arraysize(table
); ++i
) {
149 base::HistogramTester histogram_tester
;
150 FakeAuthStatusProvider
provider(error_controller());
151 provider
.SetAuthError(kTestAccountId
,
152 GoogleServiceAuthError(table
[i
].error_state
));
154 EXPECT_EQ(global_error()->HasMenuItem(), table
[i
].is_error
);
155 EXPECT_EQ(global_error()->MenuItemLabel().empty(), !table
[i
].is_error
);
156 EXPECT_EQ(global_error()->GetBubbleViewMessages().empty(),
158 EXPECT_FALSE(global_error()->GetBubbleViewTitle().empty());
159 EXPECT_FALSE(global_error()->GetBubbleViewAcceptButtonLabel().empty());
160 EXPECT_TRUE(global_error()->GetBubbleViewCancelButtonLabel().empty());
162 ProfileMetrics::LogNumberOfProfiles(
163 testing_profile_manager()->profile_manager());
165 if (table
[i
].is_error
)
166 histogram_tester
.ExpectBucketCount("Signin.AuthError", i
, 1);
167 histogram_tester
.ExpectBucketCount(
168 "Profile.NumberOfProfilesWithAuthErrors", table
[i
].is_error
, 1);