| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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. | |
| 4 | |
| 5 #include "components/signin/core/browser/signin_error_controller.h" | |
| 6 | |
| 7 #include <functional> | |
| 8 | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "chrome/browser/signin/fake_auth_status_provider.h" | |
| 11 #include "chrome/test/base/testing_profile.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 static const char kTestAccountId[] = "testuser@test.com"; | |
| 15 static const char kOtherTestAccountId[] = "otheruser@test.com"; | |
| 16 | |
| 17 class SigninErrorControllerTest : public testing::Test { | |
| 18 public: | |
| 19 virtual void SetUp() OVERRIDE { | |
| 20 error_controller_.reset(new SigninErrorController()); | |
| 21 } | |
| 22 | |
| 23 scoped_ptr<SigninErrorController> error_controller_; | |
| 24 }; | |
| 25 | |
| 26 TEST_F(SigninErrorControllerTest, NoErrorAuthStatusProviders) { | |
| 27 scoped_ptr<FakeAuthStatusProvider> provider; | |
| 28 | |
| 29 // No providers. | |
| 30 ASSERT_FALSE(error_controller_->HasError()); | |
| 31 | |
| 32 // Add a provider. | |
| 33 provider.reset(new FakeAuthStatusProvider(error_controller_.get())); | |
| 34 ASSERT_FALSE(error_controller_->HasError()); | |
| 35 | |
| 36 // Remove the provider. | |
| 37 provider.reset(); | |
| 38 ASSERT_FALSE(error_controller_->HasError()); | |
| 39 } | |
| 40 | |
| 41 TEST_F(SigninErrorControllerTest, ErrorAuthStatusProvider) { | |
| 42 scoped_ptr<FakeAuthStatusProvider> provider; | |
| 43 scoped_ptr<FakeAuthStatusProvider> error_provider; | |
| 44 | |
| 45 provider.reset(new FakeAuthStatusProvider(error_controller_.get())); | |
| 46 ASSERT_FALSE(error_controller_->HasError()); | |
| 47 | |
| 48 error_provider.reset(new FakeAuthStatusProvider(error_controller_.get())); | |
| 49 error_provider->SetAuthError(kTestAccountId, GoogleServiceAuthError( | |
| 50 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); | |
| 51 ASSERT_TRUE(error_controller_->HasError()); | |
| 52 | |
| 53 error_provider.reset(); | |
| 54 ASSERT_FALSE(error_controller_->HasError()); | |
| 55 | |
| 56 provider.reset(); | |
| 57 // All providers should be removed now. | |
| 58 ASSERT_FALSE(error_controller_->HasError()); | |
| 59 } | |
| 60 | |
| 61 TEST_F(SigninErrorControllerTest, AuthStatusProviderErrorTransition) { | |
| 62 scoped_ptr<FakeAuthStatusProvider> provider0( | |
| 63 new FakeAuthStatusProvider(error_controller_.get())); | |
| 64 scoped_ptr<FakeAuthStatusProvider> provider1( | |
| 65 new FakeAuthStatusProvider(error_controller_.get())); | |
| 66 | |
| 67 ASSERT_FALSE(error_controller_->HasError()); | |
| 68 provider0->SetAuthError( | |
| 69 kTestAccountId, | |
| 70 GoogleServiceAuthError( | |
| 71 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); | |
| 72 ASSERT_TRUE(error_controller_->HasError()); | |
| 73 provider1->SetAuthError( | |
| 74 kTestAccountId, | |
| 75 GoogleServiceAuthError(GoogleServiceAuthError::ACCOUNT_DISABLED)); | |
| 76 ASSERT_TRUE(error_controller_->HasError()); | |
| 77 | |
| 78 // Now resolve the auth errors - the menu item should go away. | |
| 79 provider0->SetAuthError(kTestAccountId, | |
| 80 GoogleServiceAuthError::AuthErrorNone()); | |
| 81 ASSERT_TRUE(error_controller_->HasError()); | |
| 82 provider1->SetAuthError(kTestAccountId, | |
| 83 GoogleServiceAuthError::AuthErrorNone()); | |
| 84 ASSERT_FALSE(error_controller_->HasError()); | |
| 85 | |
| 86 provider0.reset(); | |
| 87 provider1.reset(); | |
| 88 ASSERT_FALSE(error_controller_->HasError()); | |
| 89 } | |
| 90 | |
| 91 TEST_F(SigninErrorControllerTest, AuthStatusProviderAccountTransition) { | |
| 92 scoped_ptr<FakeAuthStatusProvider> provider0( | |
| 93 new FakeAuthStatusProvider(error_controller_.get())); | |
| 94 scoped_ptr<FakeAuthStatusProvider> provider1( | |
| 95 new FakeAuthStatusProvider(error_controller_.get())); | |
| 96 | |
| 97 ASSERT_FALSE(error_controller_->HasError()); | |
| 98 | |
| 99 provider0->SetAuthError( | |
| 100 kTestAccountId, | |
| 101 GoogleServiceAuthError( | |
| 102 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); | |
| 103 provider1->SetAuthError( | |
| 104 kOtherTestAccountId, | |
| 105 GoogleServiceAuthError(GoogleServiceAuthError::NONE)); | |
| 106 ASSERT_TRUE(error_controller_->HasError()); | |
| 107 ASSERT_STREQ(kTestAccountId, | |
| 108 error_controller_->error_account_id().c_str()); | |
| 109 | |
| 110 // Swap providers reporting errors. | |
| 111 provider1->set_error_without_status_change( | |
| 112 GoogleServiceAuthError( | |
| 113 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); | |
| 114 provider0->set_error_without_status_change( | |
| 115 GoogleServiceAuthError(GoogleServiceAuthError::NONE)); | |
| 116 error_controller_->AuthStatusChanged(); | |
| 117 ASSERT_TRUE(error_controller_->HasError()); | |
| 118 ASSERT_STREQ(kOtherTestAccountId, | |
| 119 error_controller_->error_account_id().c_str()); | |
| 120 | |
| 121 // Now resolve the auth errors - the menu item should go away. | |
| 122 provider0->set_error_without_status_change( | |
| 123 GoogleServiceAuthError::AuthErrorNone()); | |
| 124 provider1->set_error_without_status_change( | |
| 125 GoogleServiceAuthError::AuthErrorNone()); | |
| 126 error_controller_->AuthStatusChanged(); | |
| 127 ASSERT_FALSE(error_controller_->HasError()); | |
| 128 | |
| 129 provider0.reset(); | |
| 130 provider1.reset(); | |
| 131 ASSERT_FALSE(error_controller_->HasError()); | |
| 132 } | |
| 133 | |
| 134 // Verify that SigninErrorController handles errors properly. | |
| 135 TEST_F(SigninErrorControllerTest, AuthStatusEnumerateAllErrors) { | |
| 136 typedef struct { | |
| 137 GoogleServiceAuthError::State error_state; | |
| 138 bool is_error; | |
| 139 } ErrorTableEntry; | |
| 140 | |
| 141 ErrorTableEntry table[] = { | |
| 142 { GoogleServiceAuthError::NONE, false }, | |
| 143 { GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS, true }, | |
| 144 { GoogleServiceAuthError::USER_NOT_SIGNED_UP, true }, | |
| 145 { GoogleServiceAuthError::CONNECTION_FAILED, false }, | |
| 146 { GoogleServiceAuthError::CAPTCHA_REQUIRED, true }, | |
| 147 { GoogleServiceAuthError::ACCOUNT_DELETED, true }, | |
| 148 { GoogleServiceAuthError::ACCOUNT_DISABLED, true }, | |
| 149 { GoogleServiceAuthError::SERVICE_UNAVAILABLE, true }, | |
| 150 { GoogleServiceAuthError::TWO_FACTOR, true }, | |
| 151 { GoogleServiceAuthError::REQUEST_CANCELED, true }, | |
| 152 { GoogleServiceAuthError::HOSTED_NOT_ALLOWED, true }, | |
| 153 { GoogleServiceAuthError::UNEXPECTED_SERVICE_RESPONSE, true }, | |
| 154 { GoogleServiceAuthError::SERVICE_ERROR, true }, | |
| 155 }; | |
| 156 COMPILE_ASSERT(ARRAYSIZE_UNSAFE(table) == GoogleServiceAuthError::NUM_STATES, | |
| 157 kTable_size_does_not_match_number_of_auth_error_types); | |
| 158 | |
| 159 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(table); ++i) { | |
| 160 FakeAuthStatusProvider provider(error_controller_.get()); | |
| 161 provider.SetAuthError(kTestAccountId, | |
| 162 GoogleServiceAuthError(table[i].error_state)); | |
| 163 | |
| 164 EXPECT_EQ(error_controller_->HasError(), table[i].is_error); | |
| 165 | |
| 166 if (table[i].is_error) { | |
| 167 EXPECT_EQ(table[i].error_state, | |
| 168 error_controller_->auth_error().state()); | |
| 169 EXPECT_STREQ(kTestAccountId, | |
| 170 error_controller_->error_account_id().c_str()); | |
| 171 } else { | |
| 172 EXPECT_EQ(GoogleServiceAuthError::NONE, | |
| 173 error_controller_->auth_error().state()); | |
| 174 EXPECT_STREQ("", | |
| 175 error_controller_->error_account_id().c_str()); | |
| 176 } | |
| 177 } | |
| 178 } | |
| 179 | |
| 180 // Verify that existing error is not replaced by new error. | |
| 181 TEST_F(SigninErrorControllerTest, AuthStatusChange) { | |
| 182 scoped_ptr<FakeAuthStatusProvider> fake_provider0( | |
| 183 new FakeAuthStatusProvider(error_controller_.get())); | |
| 184 scoped_ptr<FakeAuthStatusProvider> fake_provider1( | |
| 185 new FakeAuthStatusProvider(error_controller_.get())); | |
| 186 | |
| 187 // If there are multiple providers in the provider set... | |
| 188 // | |
| 189 // | provider0 | provider1 | ... | |
| 190 // | NONE | INVALID_GAIA_CREDENTIALS | ... | |
| 191 // | |
| 192 // SigninErrorController picks the first error found when iterating through | |
| 193 // the set. But if another error crops up... | |
| 194 // | |
| 195 // | provider0 | provider1 | ... | |
| 196 // | SERVICE_UNAVAILABLE | INVALID_GAIA_CREDENTIALS | ... | |
| 197 // | |
| 198 // we want the controller to still use the original error. | |
| 199 | |
| 200 // The provider pointers are stored in a set, which is sorted by std::less. | |
| 201 std::less<SigninErrorController::AuthStatusProvider*> compare; | |
| 202 FakeAuthStatusProvider* provider0 = | |
| 203 compare(fake_provider0.get(), fake_provider1.get()) ? | |
| 204 fake_provider0.get() : fake_provider1.get(); | |
| 205 FakeAuthStatusProvider* provider1 = | |
| 206 provider0 == fake_provider0.get() ? | |
| 207 fake_provider1.get() : fake_provider0.get(); | |
| 208 | |
| 209 provider0->SetAuthError( | |
| 210 kTestAccountId, | |
| 211 GoogleServiceAuthError( | |
| 212 GoogleServiceAuthError::NONE)); | |
| 213 provider1->SetAuthError( | |
| 214 kOtherTestAccountId, | |
| 215 GoogleServiceAuthError( | |
| 216 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); | |
| 217 ASSERT_EQ(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS, | |
| 218 error_controller_->auth_error().state()); | |
| 219 ASSERT_STREQ(kOtherTestAccountId, | |
| 220 error_controller_->error_account_id().c_str()); | |
| 221 | |
| 222 // Change the 1st provider's error. | |
| 223 provider1->SetAuthError( | |
| 224 kOtherTestAccountId, | |
| 225 GoogleServiceAuthError( | |
| 226 GoogleServiceAuthError::SERVICE_UNAVAILABLE)); | |
| 227 ASSERT_EQ(GoogleServiceAuthError::SERVICE_UNAVAILABLE, | |
| 228 error_controller_->auth_error().state()); | |
| 229 ASSERT_STREQ(kOtherTestAccountId, | |
| 230 error_controller_->error_account_id().c_str()); | |
| 231 | |
| 232 // Set the 0th provider's error -- nothing should change. | |
| 233 provider0->SetAuthError( | |
| 234 kTestAccountId, | |
| 235 GoogleServiceAuthError( | |
| 236 GoogleServiceAuthError::UNEXPECTED_SERVICE_RESPONSE)); | |
| 237 ASSERT_EQ(GoogleServiceAuthError::SERVICE_UNAVAILABLE, | |
| 238 error_controller_->auth_error().state()); | |
| 239 ASSERT_STREQ(kOtherTestAccountId, | |
| 240 error_controller_->error_account_id().c_str()); | |
| 241 | |
| 242 // Clear the 1st provider's error, so the 0th provider's error is used. | |
| 243 provider1->SetAuthError( | |
| 244 kOtherTestAccountId, | |
| 245 GoogleServiceAuthError( | |
| 246 GoogleServiceAuthError::NONE)); | |
| 247 ASSERT_EQ(GoogleServiceAuthError::UNEXPECTED_SERVICE_RESPONSE, | |
| 248 error_controller_->auth_error().state()); | |
| 249 ASSERT_STREQ(kTestAccountId, | |
| 250 error_controller_->error_account_id().c_str()); | |
| 251 | |
| 252 fake_provider0.reset(); | |
| 253 fake_provider1.reset(); | |
| 254 ASSERT_FALSE(error_controller_->HasError()); | |
| 255 } | |
| OLD | NEW |