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 "chrome/browser/signin/fake_auth_status_provider.h"
10 #include "chrome/browser/signin/fake_signin_manager.h"
11 #include "chrome/browser/signin/signin_manager.h"
12 #include "chrome/browser/signin/signin_manager_factory.h"
13 #include "chrome/browser/ui/global_error/global_error_service.h"
14 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
15 #include "chrome/common/pref_names.h"
16 #include "chrome/test/base/testing_profile.h"
17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 static const char kTestAccountId
[] = "testuser@test.com";
21 static const char kOtherTestAccountId
[] = "otheruser@test.com";
23 class SigninGlobalErrorTest
: public testing::Test
{
25 virtual void SetUp() OVERRIDE
{
26 // Create a signed-in profile.
27 profile_
.reset(new TestingProfile());
29 SigninManagerBase
* manager
= static_cast<SigninManagerBase
*>(
30 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse(
31 profile_
.get(), FakeSigninManagerBase::Build
));
32 profile_
->GetPrefs()->SetString(
33 prefs::kGoogleServicesUsername
, kTestAccountId
);
34 manager
->Initialize(profile_
.get(), NULL
);
35 global_error_
= SigninGlobalError::GetForProfile(profile_
.get());
38 content::TestBrowserThreadBundle thread_bundle_
;
39 scoped_ptr
<TestingProfile
> profile_
;
40 SigninGlobalError
* global_error_
;
43 TEST_F(SigninGlobalErrorTest
, NoAuthStatusProviders
) {
44 ASSERT_FALSE(global_error_
->HasMenuItem());
47 TEST_F(SigninGlobalErrorTest
, NoErrorAuthStatusProviders
) {
49 // Add a provider (removes itself on exiting this scope).
50 FakeAuthStatusProvider
provider(global_error_
);
51 ASSERT_FALSE(global_error_
->HasMenuItem());
53 ASSERT_FALSE(global_error_
->HasMenuItem());
56 TEST_F(SigninGlobalErrorTest
, ErrorAuthStatusProvider
) {
58 FakeAuthStatusProvider
provider(global_error_
);
59 ASSERT_FALSE(global_error_
->HasMenuItem());
61 FakeAuthStatusProvider
error_provider(global_error_
);
62 error_provider
.SetAuthError(kTestAccountId
, GoogleServiceAuthError(
63 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
));
64 ASSERT_TRUE(global_error_
->HasMenuItem());
66 // error_provider is removed now that we've left that scope.
67 ASSERT_FALSE(global_error_
->HasMenuItem());
69 // All providers should be removed now.
70 ASSERT_FALSE(global_error_
->HasMenuItem());
73 TEST_F(SigninGlobalErrorTest
, AuthStatusProviderErrorTransition
) {
75 FakeAuthStatusProvider
provider0(global_error_
);
76 FakeAuthStatusProvider
provider1(global_error_
);
77 ASSERT_FALSE(global_error_
->HasMenuItem());
78 provider0
.SetAuthError(
80 GoogleServiceAuthError(
81 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
));
82 ASSERT_TRUE(global_error_
->HasMenuItem());
83 provider1
.SetAuthError(
85 GoogleServiceAuthError(GoogleServiceAuthError::ACCOUNT_DISABLED
));
86 ASSERT_TRUE(global_error_
->HasMenuItem());
88 // Now resolve the auth errors - the menu item should go away.
89 provider0
.SetAuthError(kTestAccountId
,
90 GoogleServiceAuthError::AuthErrorNone());
91 ASSERT_TRUE(global_error_
->HasMenuItem());
92 provider1
.SetAuthError(kTestAccountId
,
93 GoogleServiceAuthError::AuthErrorNone());
94 ASSERT_FALSE(global_error_
->HasMenuItem());
96 ASSERT_FALSE(global_error_
->HasMenuItem());
99 TEST_F(SigninGlobalErrorTest
, AuthStatusProviderAccountTransition
) {
101 FakeAuthStatusProvider
provider0(global_error_
);
102 FakeAuthStatusProvider
provider1(global_error_
);
103 ASSERT_FALSE(global_error_
->HasMenuItem());
105 provider0
.SetAuthError(
107 GoogleServiceAuthError(
108 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
));
109 provider1
.SetAuthError(
111 GoogleServiceAuthError(GoogleServiceAuthError::NONE
));
112 ASSERT_TRUE(global_error_
->HasMenuItem());
113 ASSERT_STREQ(kTestAccountId
,
114 global_error_
->GetAccountIdOfLastAuthError().c_str());
116 // Swap providers reporting errors.
117 provider1
.set_error_without_status_change(
118 GoogleServiceAuthError(
119 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
));
120 provider0
.set_error_without_status_change(
121 GoogleServiceAuthError(GoogleServiceAuthError::NONE
));
122 global_error_
->AuthStatusChanged();
123 ASSERT_TRUE(global_error_
->HasMenuItem());
124 ASSERT_STREQ(kOtherTestAccountId
,
125 global_error_
->GetAccountIdOfLastAuthError().c_str());
127 // Now resolve the auth errors - the menu item should go away.
128 provider0
.set_error_without_status_change(
129 GoogleServiceAuthError::AuthErrorNone());
130 provider1
.set_error_without_status_change(
131 GoogleServiceAuthError::AuthErrorNone());
132 global_error_
->AuthStatusChanged();
133 ASSERT_FALSE(global_error_
->HasMenuItem());
135 ASSERT_FALSE(global_error_
->HasMenuItem());
138 // Verify that SigninGlobalError ignores certain errors.
139 TEST_F(SigninGlobalErrorTest
, AuthStatusEnumerateAllErrors
) {
141 GoogleServiceAuthError::State error_state
;
145 ErrorTableEntry table
[] = {
146 { GoogleServiceAuthError::NONE
, false },
147 { GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
, true },
148 { GoogleServiceAuthError::USER_NOT_SIGNED_UP
, true },
149 { GoogleServiceAuthError::CONNECTION_FAILED
, false },
150 { GoogleServiceAuthError::CAPTCHA_REQUIRED
, true },
151 { GoogleServiceAuthError::ACCOUNT_DELETED
, true },
152 { GoogleServiceAuthError::ACCOUNT_DISABLED
, true },
153 { GoogleServiceAuthError::SERVICE_UNAVAILABLE
, true },
154 { GoogleServiceAuthError::TWO_FACTOR
, true },
155 { GoogleServiceAuthError::REQUEST_CANCELED
, true },
156 { GoogleServiceAuthError::HOSTED_NOT_ALLOWED
, true },
157 { GoogleServiceAuthError::UNEXPECTED_SERVICE_RESPONSE
, true },
158 { GoogleServiceAuthError::SERVICE_ERROR
, true },
160 COMPILE_ASSERT(ARRAYSIZE_UNSAFE(table
) == GoogleServiceAuthError::NUM_STATES
,
161 kTable_size_does_not_match_number_of_auth_error_types
);
163 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(table
); ++i
) {
164 FakeAuthStatusProvider
provider(global_error_
);
165 provider
.SetAuthError(kTestAccountId
,
166 GoogleServiceAuthError(table
[i
].error_state
));
167 EXPECT_EQ(global_error_
->HasMenuItem(), table
[i
].is_error
);
168 // Only on chromeos do we have a separate menu item - on other platforms
169 // there's code in WrenchMenuModel to re-use the "sign in to chrome"
170 // menu item to display auth status/errors.
171 EXPECT_EQ(global_error_
->HasMenuItem(), table
[i
].is_error
);
172 EXPECT_EQ(global_error_
->MenuItemLabel().empty(), !table
[i
].is_error
);
173 EXPECT_EQ(global_error_
->GetBubbleViewMessages().empty(),
175 EXPECT_FALSE(global_error_
->GetBubbleViewTitle().empty());
176 EXPECT_FALSE(global_error_
->GetBubbleViewAcceptButtonLabel().empty());
177 EXPECT_TRUE(global_error_
->GetBubbleViewCancelButtonLabel().empty());