OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/signin/signin_global_error.h" | 5 #include "chrome/browser/signin/signin_global_error.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/prefs/pref_service.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" |
9 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" | 15 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" |
10 #include "chrome/browser/signin/fake_signin_manager.h" | 16 #include "chrome/browser/signin/fake_signin_manager.h" |
11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 17 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
12 #include "chrome/browser/signin/signin_error_controller_factory.h" | 18 #include "chrome/browser/signin/signin_error_controller_factory.h" |
13 #include "chrome/browser/signin/signin_global_error_factory.h" | 19 #include "chrome/browser/signin/signin_global_error_factory.h" |
14 #include "chrome/browser/signin/signin_manager_factory.h" | 20 #include "chrome/browser/signin/signin_manager_factory.h" |
15 #include "chrome/browser/ui/global_error/global_error_service.h" | 21 #include "chrome/browser/ui/global_error/global_error_service.h" |
16 #include "chrome/browser/ui/global_error/global_error_service_factory.h" | 22 #include "chrome/browser/ui/global_error/global_error_service_factory.h" |
17 #include "chrome/common/pref_names.h" | 23 #include "chrome/common/pref_names.h" |
| 24 #include "chrome/test/base/testing_browser_process.h" |
18 #include "chrome/test/base/testing_profile.h" | 25 #include "chrome/test/base/testing_profile.h" |
| 26 #include "chrome/test/base/testing_profile_manager.h" |
19 #include "components/signin/core/browser/fake_auth_status_provider.h" | 27 #include "components/signin/core/browser/fake_auth_status_provider.h" |
20 #include "components/signin/core/browser/signin_error_controller.h" | 28 #include "components/signin/core/browser/signin_error_controller.h" |
21 #include "components/signin/core/browser/signin_manager.h" | 29 #include "components/signin/core/browser/signin_manager.h" |
22 #include "content/public/test/test_browser_thread_bundle.h" | 30 #include "content/public/test/test_browser_thread_bundle.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
24 | 32 |
25 static const char kTestAccountId[] = "testuser@test.com"; | 33 static const char kTestAccountId[] = "testuser@test.com"; |
26 static const char kTestUsername[] = "testuser@test.com"; | 34 static const char kTestUsername[] = "testuser@test.com"; |
27 | 35 |
28 class SigninGlobalErrorTest : public testing::Test { | 36 class SigninGlobalErrorTest : public testing::Test { |
29 public: | 37 public: |
| 38 SigninGlobalErrorTest() : |
| 39 profile_manager_(TestingBrowserProcess::GetGlobal()) {} |
| 40 |
30 void SetUp() override { | 41 void SetUp() override { |
| 42 ASSERT_TRUE(profile_manager_.SetUp()); |
| 43 |
31 // Create a signed-in profile. | 44 // Create a signed-in profile. |
32 TestingProfile::Builder builder; | 45 TestingProfile::TestingFactories testing_factories; |
33 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), | 46 testing_factories.push_back(std::make_pair( |
34 BuildFakeProfileOAuth2TokenService); | 47 ProfileOAuth2TokenServiceFactory::GetInstance(), |
35 builder.AddTestingFactory(SigninManagerFactory::GetInstance(), | 48 BuildFakeProfileOAuth2TokenService)); |
36 FakeSigninManagerBase::Build); | 49 testing_factories.push_back(std::make_pair( |
37 profile_ = builder.Build(); | 50 SigninManagerFactory::GetInstance(), |
| 51 FakeSigninManagerBase::Build)); |
| 52 profile_ = profile_manager_.CreateTestingProfile( |
| 53 "Person 1", scoped_ptr<PrefServiceSyncable>(), |
| 54 base::UTF8ToUTF16("Person 1"), 0, std::string(), testing_factories); |
38 | 55 |
39 SigninManagerFactory::GetForProfile(profile_.get()) | 56 SigninManagerFactory::GetForProfile(profile()) |
40 ->SetAuthenticatedUsername(kTestAccountId); | 57 ->SetAuthenticatedUsername(kTestAccountId); |
| 58 ProfileInfoCache& cache = |
| 59 profile_manager_.profile_manager()->GetProfileInfoCache(); |
| 60 cache.SetUserNameOfProfileAtIndex( |
| 61 cache.GetIndexOfProfileWithPath(profile()->GetPath()), |
| 62 base::UTF8ToUTF16(kTestAccountId)); |
41 | 63 |
42 global_error_ = SigninGlobalErrorFactory::GetForProfile(profile_.get()); | 64 global_error_ = SigninGlobalErrorFactory::GetForProfile(profile()); |
43 error_controller_ = SigninErrorControllerFactory::GetForProfile( | 65 error_controller_ = SigninErrorControllerFactory::GetForProfile(profile()); |
44 profile_.get()); | |
45 } | 66 } |
46 | 67 |
| 68 TestingProfile* profile() { return profile_; } |
| 69 TestingProfileManager* testing_profile_manager() { |
| 70 return &profile_manager_; |
| 71 } |
| 72 SigninGlobalError* global_error() { return global_error_; } |
| 73 SigninErrorController* error_controller() { return error_controller_; } |
| 74 |
| 75 private: |
47 content::TestBrowserThreadBundle thread_bundle_; | 76 content::TestBrowserThreadBundle thread_bundle_; |
48 scoped_ptr<TestingProfile> profile_; | 77 TestingProfileManager profile_manager_; |
| 78 TestingProfile* profile_; |
49 SigninGlobalError* global_error_; | 79 SigninGlobalError* global_error_; |
50 SigninErrorController* error_controller_; | 80 SigninErrorController* error_controller_; |
51 }; | 81 }; |
52 | 82 |
53 TEST_F(SigninGlobalErrorTest, NoErrorAuthStatusProviders) { | 83 TEST_F(SigninGlobalErrorTest, NoErrorAuthStatusProviders) { |
54 scoped_ptr<FakeAuthStatusProvider> provider; | 84 scoped_ptr<FakeAuthStatusProvider> provider; |
55 | 85 |
56 ASSERT_FALSE(global_error_->HasMenuItem()); | 86 ASSERT_FALSE(global_error()->HasMenuItem()); |
57 | 87 |
58 // Add a provider. | 88 // Add a provider. |
59 provider.reset(new FakeAuthStatusProvider(error_controller_)); | 89 provider.reset(new FakeAuthStatusProvider(error_controller())); |
60 ASSERT_FALSE(global_error_->HasMenuItem()); | 90 ASSERT_FALSE(global_error()->HasMenuItem()); |
61 | 91 |
62 // Remove the provider. | 92 // Remove the provider. |
63 provider.reset(); | 93 provider.reset(); |
64 ASSERT_FALSE(global_error_->HasMenuItem()); | 94 ASSERT_FALSE(global_error()->HasMenuItem()); |
65 } | 95 } |
66 | 96 |
67 TEST_F(SigninGlobalErrorTest, ErrorAuthStatusProvider) { | 97 TEST_F(SigninGlobalErrorTest, ErrorAuthStatusProvider) { |
68 scoped_ptr<FakeAuthStatusProvider> provider; | 98 scoped_ptr<FakeAuthStatusProvider> provider; |
69 scoped_ptr<FakeAuthStatusProvider> error_provider; | 99 scoped_ptr<FakeAuthStatusProvider> error_provider; |
70 | 100 |
71 provider.reset(new FakeAuthStatusProvider(error_controller_)); | 101 provider.reset(new FakeAuthStatusProvider(error_controller())); |
72 ASSERT_FALSE(global_error_->HasMenuItem()); | 102 ASSERT_FALSE(global_error()->HasMenuItem()); |
73 | 103 |
74 error_provider.reset(new FakeAuthStatusProvider(error_controller_)); | 104 error_provider.reset(new FakeAuthStatusProvider(error_controller())); |
75 error_provider->SetAuthError( | 105 error_provider->SetAuthError( |
76 kTestAccountId, | 106 kTestAccountId, |
77 kTestUsername, | 107 kTestUsername, |
78 GoogleServiceAuthError( | 108 GoogleServiceAuthError( |
79 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); | 109 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); |
80 ASSERT_TRUE(global_error_->HasMenuItem()); | 110 ASSERT_TRUE(global_error()->HasMenuItem()); |
81 | 111 |
82 error_provider.reset(); | 112 error_provider.reset(); |
83 ASSERT_FALSE(global_error_->HasMenuItem()); | 113 ASSERT_FALSE(global_error()->HasMenuItem()); |
84 | 114 |
85 provider.reset(); | 115 provider.reset(); |
86 error_provider.reset(); | 116 error_provider.reset(); |
87 ASSERT_FALSE(global_error_->HasMenuItem()); | 117 ASSERT_FALSE(global_error()->HasMenuItem()); |
88 } | 118 } |
89 | 119 |
90 // Verify that SigninGlobalError ignores certain errors. | 120 // Verify that SigninGlobalError ignores certain errors. |
91 TEST_F(SigninGlobalErrorTest, AuthStatusEnumerateAllErrors) { | 121 TEST_F(SigninGlobalErrorTest, AuthStatusEnumerateAllErrors) { |
92 typedef struct { | 122 typedef struct { |
93 GoogleServiceAuthError::State error_state; | 123 GoogleServiceAuthError::State error_state; |
94 bool is_error; | 124 bool is_error; |
95 } ErrorTableEntry; | 125 } ErrorTableEntry; |
96 | 126 |
97 ErrorTableEntry table[] = { | 127 ErrorTableEntry table[] = { |
98 { GoogleServiceAuthError::NONE, false }, | 128 { GoogleServiceAuthError::NONE, false }, |
99 { GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS, true }, | 129 { GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS, true }, |
100 { GoogleServiceAuthError::USER_NOT_SIGNED_UP, true }, | 130 { GoogleServiceAuthError::USER_NOT_SIGNED_UP, true }, |
101 { GoogleServiceAuthError::CONNECTION_FAILED, false }, | 131 { GoogleServiceAuthError::CONNECTION_FAILED, false }, |
102 { GoogleServiceAuthError::CAPTCHA_REQUIRED, true }, | 132 { GoogleServiceAuthError::CAPTCHA_REQUIRED, true }, |
103 { GoogleServiceAuthError::ACCOUNT_DELETED, true }, | 133 { GoogleServiceAuthError::ACCOUNT_DELETED, true }, |
104 { GoogleServiceAuthError::ACCOUNT_DISABLED, true }, | 134 { GoogleServiceAuthError::ACCOUNT_DISABLED, true }, |
105 { GoogleServiceAuthError::SERVICE_UNAVAILABLE, true }, | 135 { GoogleServiceAuthError::SERVICE_UNAVAILABLE, true }, |
106 { GoogleServiceAuthError::TWO_FACTOR, true }, | 136 { GoogleServiceAuthError::TWO_FACTOR, true }, |
107 { GoogleServiceAuthError::REQUEST_CANCELED, true }, | 137 { GoogleServiceAuthError::REQUEST_CANCELED, true }, |
108 { GoogleServiceAuthError::HOSTED_NOT_ALLOWED, true }, | 138 { GoogleServiceAuthError::HOSTED_NOT_ALLOWED, true }, |
109 { GoogleServiceAuthError::UNEXPECTED_SERVICE_RESPONSE, true }, | 139 { GoogleServiceAuthError::UNEXPECTED_SERVICE_RESPONSE, true }, |
110 { GoogleServiceAuthError::SERVICE_ERROR, true }, | 140 { GoogleServiceAuthError::SERVICE_ERROR, true }, |
111 { GoogleServiceAuthError::WEB_LOGIN_REQUIRED, true }, | 141 { GoogleServiceAuthError::WEB_LOGIN_REQUIRED, true }, |
112 }; | 142 }; |
113 static_assert(arraysize(table) == GoogleServiceAuthError::NUM_STATES, | 143 static_assert(arraysize(table) == GoogleServiceAuthError::NUM_STATES, |
114 "table size should match number of auth error types"); | 144 "table size should match number of auth error types"); |
115 | 145 |
| 146 // Mark the profile with an active timestamp so profile_metrics logs it. |
| 147 testing_profile_manager()->UpdateLastUser(profile()); |
| 148 |
116 for (size_t i = 0; i < arraysize(table); ++i) { | 149 for (size_t i = 0; i < arraysize(table); ++i) { |
117 FakeAuthStatusProvider provider(error_controller_); | 150 base::HistogramTester histogram_tester; |
| 151 FakeAuthStatusProvider provider(error_controller()); |
118 provider.SetAuthError(kTestAccountId, | 152 provider.SetAuthError(kTestAccountId, |
119 kTestUsername, | 153 kTestUsername, |
120 GoogleServiceAuthError(table[i].error_state)); | 154 GoogleServiceAuthError(table[i].error_state)); |
121 | 155 |
122 EXPECT_EQ(global_error_->HasMenuItem(), table[i].is_error); | 156 EXPECT_EQ(global_error()->HasMenuItem(), table[i].is_error); |
123 EXPECT_EQ(global_error_->MenuItemLabel().empty(), !table[i].is_error); | 157 EXPECT_EQ(global_error()->MenuItemLabel().empty(), !table[i].is_error); |
124 EXPECT_EQ(global_error_->GetBubbleViewMessages().empty(), | 158 EXPECT_EQ(global_error()->GetBubbleViewMessages().empty(), |
125 !table[i].is_error); | 159 !table[i].is_error); |
126 EXPECT_FALSE(global_error_->GetBubbleViewTitle().empty()); | 160 EXPECT_FALSE(global_error()->GetBubbleViewTitle().empty()); |
127 EXPECT_FALSE(global_error_->GetBubbleViewAcceptButtonLabel().empty()); | 161 EXPECT_FALSE(global_error()->GetBubbleViewAcceptButtonLabel().empty()); |
128 EXPECT_TRUE(global_error_->GetBubbleViewCancelButtonLabel().empty()); | 162 EXPECT_TRUE(global_error()->GetBubbleViewCancelButtonLabel().empty()); |
| 163 |
| 164 ProfileMetrics::LogNumberOfProfiles( |
| 165 testing_profile_manager()->profile_manager()); |
| 166 |
| 167 if (table[i].is_error) |
| 168 histogram_tester.ExpectBucketCount("Signin.AuthError", i, 1); |
| 169 histogram_tester.ExpectBucketCount( |
| 170 "Profile.NumberOfProfilesWithAuthErrors", table[i].is_error, 1); |
129 } | 171 } |
130 } | 172 } |
OLD | NEW |