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.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(),
52 FakeSigninManagerBase::Build
));
53 profile_
= profile_manager_
.CreateTestingProfile(
54 "Person 1", scoped_ptr
<PrefServiceSyncable
>(),
55 base::UTF8ToUTF16("Person 1"), 0, std::string(), testing_factories
);
57 SigninManagerFactory::GetForProfile(profile())
58 ->SetAuthenticatedAccountInfo(kTestAccountId
, kTestUsername
);
59 ProfileInfoCache
& cache
=
60 profile_manager_
.profile_manager()->GetProfileInfoCache();
61 cache
.SetAuthInfoOfProfileAtIndex(
62 cache
.GetIndexOfProfileWithPath(profile()->GetPath()),
63 kTestGaiaId
, base::UTF8ToUTF16(kTestUsername
));
65 global_error_
= SigninGlobalErrorFactory::GetForProfile(profile());
66 error_controller_
= SigninErrorControllerFactory::GetForProfile(profile());
69 TestingProfile
* profile() { return profile_
; }
70 TestingProfileManager
* testing_profile_manager() {
71 return &profile_manager_
;
73 SigninGlobalError
* global_error() { return global_error_
; }
74 SigninErrorController
* error_controller() { return error_controller_
; }
77 content::TestBrowserThreadBundle thread_bundle_
;
78 TestingProfileManager profile_manager_
;
79 TestingProfile
* profile_
;
80 SigninGlobalError
* global_error_
;
81 SigninErrorController
* error_controller_
;
84 TEST_F(SigninGlobalErrorTest
, NoErrorAuthStatusProviders
) {
85 scoped_ptr
<FakeAuthStatusProvider
> provider
;
87 ASSERT_FALSE(global_error()->HasMenuItem());
90 provider
.reset(new FakeAuthStatusProvider(error_controller()));
91 ASSERT_FALSE(global_error()->HasMenuItem());
93 // Remove the provider.
95 ASSERT_FALSE(global_error()->HasMenuItem());
98 TEST_F(SigninGlobalErrorTest
, ErrorAuthStatusProvider
) {
99 scoped_ptr
<FakeAuthStatusProvider
> provider
;
100 scoped_ptr
<FakeAuthStatusProvider
> error_provider
;
102 provider
.reset(new FakeAuthStatusProvider(error_controller()));
103 ASSERT_FALSE(global_error()->HasMenuItem());
105 error_provider
.reset(new FakeAuthStatusProvider(error_controller()));
106 error_provider
->SetAuthError(
108 GoogleServiceAuthError(
109 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
));
110 ASSERT_TRUE(global_error()->HasMenuItem());
112 error_provider
.reset();
113 ASSERT_FALSE(global_error()->HasMenuItem());
116 error_provider
.reset();
117 ASSERT_FALSE(global_error()->HasMenuItem());
120 // Verify that SigninGlobalError ignores certain errors.
121 TEST_F(SigninGlobalErrorTest
, AuthStatusEnumerateAllErrors
) {
123 GoogleServiceAuthError::State error_state
;
127 ErrorTableEntry table
[] = {
128 { GoogleServiceAuthError::NONE
, false },
129 { GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
, true },
130 { GoogleServiceAuthError::USER_NOT_SIGNED_UP
, true },
131 { GoogleServiceAuthError::CONNECTION_FAILED
, false },
132 { GoogleServiceAuthError::CAPTCHA_REQUIRED
, true },
133 { GoogleServiceAuthError::ACCOUNT_DELETED
, true },
134 { GoogleServiceAuthError::ACCOUNT_DISABLED
, true },
135 { GoogleServiceAuthError::SERVICE_UNAVAILABLE
, true },
136 { GoogleServiceAuthError::TWO_FACTOR
, true },
137 { GoogleServiceAuthError::REQUEST_CANCELED
, true },
138 { GoogleServiceAuthError::HOSTED_NOT_ALLOWED
, true },
139 { GoogleServiceAuthError::UNEXPECTED_SERVICE_RESPONSE
, true },
140 { GoogleServiceAuthError::SERVICE_ERROR
, true },
141 { GoogleServiceAuthError::WEB_LOGIN_REQUIRED
, true },
143 static_assert(arraysize(table
) == GoogleServiceAuthError::NUM_STATES
,
144 "table size should match number of auth error types");
146 // Mark the profile with an active timestamp so profile_metrics logs it.
147 testing_profile_manager()->UpdateLastUser(profile());
149 for (size_t i
= 0; i
< arraysize(table
); ++i
) {
150 base::HistogramTester histogram_tester
;
151 FakeAuthStatusProvider
provider(error_controller());
152 provider
.SetAuthError(kTestAccountId
,
153 GoogleServiceAuthError(table
[i
].error_state
));
155 EXPECT_EQ(global_error()->HasMenuItem(), table
[i
].is_error
);
156 EXPECT_EQ(global_error()->MenuItemLabel().empty(), !table
[i
].is_error
);
157 EXPECT_EQ(global_error()->GetBubbleViewMessages().empty(),
159 EXPECT_FALSE(global_error()->GetBubbleViewTitle().empty());
160 EXPECT_FALSE(global_error()->GetBubbleViewAcceptButtonLabel().empty());
161 EXPECT_TRUE(global_error()->GetBubbleViewCancelButtonLabel().empty());
163 ProfileMetrics::LogNumberOfProfiles(
164 testing_profile_manager()->profile_manager());
166 if (table
[i
].is_error
)
167 histogram_tester
.ExpectBucketCount("Signin.AuthError", i
, 1);
168 histogram_tester
.ExpectBucketCount(
169 "Profile.NumberOfProfilesWithAuthErrors", table
[i
].is_error
, 1);