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_profile_oauth2_token_service.h"
10 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
11 #include "chrome/browser/signin/fake_signin_manager.h"
12 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
13 #include "chrome/browser/signin/signin_global_error_factory.h"
14 #include "chrome/browser/signin/signin_manager_factory.h"
15 #include "chrome/browser/ui/global_error/global_error_service.h"
16 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
17 #include "chrome/common/pref_names.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "components/signin/core/browser/fake_auth_status_provider.h"
20 #include "components/signin/core/browser/signin_error_controller.h"
21 #include "components/signin/core/browser/signin_manager.h"
22 #include "content/public/test/test_browser_thread_bundle.h"
23 #include "testing/gtest/include/gtest/gtest.h"
25 static const char kTestAccountId
[] = "testuser@test.com";
26 static const char kTestUsername
[] = "testuser@test.com";
28 class SigninGlobalErrorTest
: public testing::Test
{
30 virtual void SetUp() OVERRIDE
{
31 // Create a signed-in profile.
32 TestingProfile::Builder builder
;
33 builder
.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
34 BuildFakeProfileOAuth2TokenService
);
35 builder
.AddTestingFactory(SigninManagerFactory::GetInstance(),
36 FakeSigninManagerBase::Build
);
37 profile_
= builder
.Build();
39 profile_
->GetPrefs()->SetString(
40 prefs::kGoogleServicesUsername
, kTestAccountId
);
41 SigninManagerFactory::GetForProfile(profile_
.get())
42 ->SetAuthenticatedUsername(kTestAccountId
);
44 global_error_
= SigninGlobalErrorFactory::GetForProfile(profile_
.get());
45 error_controller_
= ProfileOAuth2TokenServiceFactory::GetForProfile(
46 profile_
.get())->signin_error_controller();
49 content::TestBrowserThreadBundle thread_bundle_
;
50 scoped_ptr
<TestingProfile
> profile_
;
51 SigninGlobalError
* global_error_
;
52 SigninErrorController
* error_controller_
;
55 TEST_F(SigninGlobalErrorTest
, NoErrorAuthStatusProviders
) {
56 scoped_ptr
<FakeAuthStatusProvider
> provider
;
58 ASSERT_FALSE(global_error_
->HasMenuItem());
61 provider
.reset(new FakeAuthStatusProvider(error_controller_
));
62 ASSERT_FALSE(global_error_
->HasMenuItem());
64 // Remove the provider.
66 ASSERT_FALSE(global_error_
->HasMenuItem());
69 TEST_F(SigninGlobalErrorTest
, ErrorAuthStatusProvider
) {
70 scoped_ptr
<FakeAuthStatusProvider
> provider
;
71 scoped_ptr
<FakeAuthStatusProvider
> error_provider
;
73 provider
.reset(new FakeAuthStatusProvider(error_controller_
));
74 ASSERT_FALSE(global_error_
->HasMenuItem());
76 error_provider
.reset(new FakeAuthStatusProvider(error_controller_
));
77 error_provider
->SetAuthError(
80 GoogleServiceAuthError(
81 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
));
82 ASSERT_TRUE(global_error_
->HasMenuItem());
84 error_provider
.reset();
85 ASSERT_FALSE(global_error_
->HasMenuItem());
88 error_provider
.reset();
89 ASSERT_FALSE(global_error_
->HasMenuItem());
92 // Verify that SigninGlobalError ignores certain errors.
93 TEST_F(SigninGlobalErrorTest
, AuthStatusEnumerateAllErrors
) {
95 GoogleServiceAuthError::State error_state
;
99 ErrorTableEntry table
[] = {
100 { GoogleServiceAuthError::NONE
, false },
101 { GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
, true },
102 { GoogleServiceAuthError::USER_NOT_SIGNED_UP
, true },
103 { GoogleServiceAuthError::CONNECTION_FAILED
, false },
104 { GoogleServiceAuthError::CAPTCHA_REQUIRED
, true },
105 { GoogleServiceAuthError::ACCOUNT_DELETED
, true },
106 { GoogleServiceAuthError::ACCOUNT_DISABLED
, true },
107 { GoogleServiceAuthError::SERVICE_UNAVAILABLE
, true },
108 { GoogleServiceAuthError::TWO_FACTOR
, true },
109 { GoogleServiceAuthError::REQUEST_CANCELED
, true },
110 { GoogleServiceAuthError::HOSTED_NOT_ALLOWED
, true },
111 { GoogleServiceAuthError::UNEXPECTED_SERVICE_RESPONSE
, true },
112 { GoogleServiceAuthError::SERVICE_ERROR
, true },
114 COMPILE_ASSERT(ARRAYSIZE_UNSAFE(table
) == GoogleServiceAuthError::NUM_STATES
,
115 kTable_size_does_not_match_number_of_auth_error_types
);
117 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(table
); ++i
) {
118 FakeAuthStatusProvider
provider(error_controller_
);
119 provider
.SetAuthError(kTestAccountId
,
121 GoogleServiceAuthError(table
[i
].error_state
));
123 EXPECT_EQ(global_error_
->HasMenuItem(), table
[i
].is_error
);
124 EXPECT_EQ(global_error_
->MenuItemLabel().empty(), !table
[i
].is_error
);
125 EXPECT_EQ(global_error_
->GetBubbleViewMessages().empty(),
127 EXPECT_FALSE(global_error_
->GetBubbleViewTitle().empty());
128 EXPECT_FALSE(global_error_
->GetBubbleViewAcceptButtonLabel().empty());
129 EXPECT_TRUE(global_error_
->GetBubbleViewCancelButtonLabel().empty());