| 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 "chrome/browser/signin/fake_auth_status_provider.h" |
| 8 #include "chrome/browser/signin/signin_manager.h" | 9 #include "chrome/browser/signin/signin_manager.h" |
| 9 #include "chrome/browser/signin/signin_manager_factory.h" | 10 #include "chrome/browser/signin/signin_manager_factory.h" |
| 10 #include "chrome/browser/signin/signin_manager_fake.h" | 11 #include "chrome/browser/signin/signin_manager_fake.h" |
| 11 #include "chrome/browser/signin/token_service_factory.h" | 12 #include "chrome/browser/signin/token_service_factory.h" |
| 12 #include "chrome/browser/ui/global_error/global_error_service.h" | 13 #include "chrome/browser/ui/global_error/global_error_service.h" |
| 13 #include "chrome/browser/ui/global_error/global_error_service_factory.h" | 14 #include "chrome/browser/ui/global_error/global_error_service_factory.h" |
| 14 #include "chrome/test/base/testing_profile.h" | 15 #include "chrome/test/base/testing_profile.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 17 |
| 17 class FakeAuthStatusProvider : public SigninGlobalError::AuthStatusProvider { | |
| 18 public: | |
| 19 FakeAuthStatusProvider() : auth_error_(GoogleServiceAuthError::None()) {} | |
| 20 | |
| 21 // AuthStatusProvider implementation. | |
| 22 GoogleServiceAuthError GetAuthStatus() const OVERRIDE { return auth_error_; } | |
| 23 | |
| 24 void set_auth_error(const GoogleServiceAuthError& error) { | |
| 25 auth_error_ = error; | |
| 26 } | |
| 27 | |
| 28 private: | |
| 29 GoogleServiceAuthError auth_error_; | |
| 30 }; | |
| 31 | |
| 32 class SigninGlobalErrorTest : public testing::Test { | 18 class SigninGlobalErrorTest : public testing::Test { |
| 33 public: | 19 public: |
| 34 void SetUp() OVERRIDE { | 20 void SetUp() OVERRIDE { |
| 35 // Create a signed-in profile. | 21 // Create a signed-in profile. |
| 36 profile_.reset(new TestingProfile()); | 22 profile_.reset(new TestingProfile()); |
| 37 | 23 |
| 38 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( | 24 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( |
| 39 profile_.get(), FakeSigninManager::Build); | 25 profile_.get(), FakeSigninManager::Build); |
| 40 SigninManager* manager = | 26 SigninManager* manager = |
| 41 SigninManagerFactory::GetForProfile(profile_.get()); | 27 SigninManagerFactory::GetForProfile(profile_.get()); |
| 42 manager->SetAuthenticatedUsername("testuser@test.com"); | 28 manager->SetAuthenticatedUsername("testuser@test.com"); |
| 43 global_error_ = manager->signin_global_error(); | 29 global_error_ = manager->signin_global_error(); |
| 44 } | 30 } |
| 45 | 31 |
| 46 scoped_ptr<TestingProfile> profile_; | 32 scoped_ptr<TestingProfile> profile_; |
| 47 SigninGlobalError* global_error_; | 33 SigninGlobalError* global_error_; |
| 48 }; | 34 }; |
| 49 | 35 |
| 50 TEST_F(SigninGlobalErrorTest, NoAuthStatusProviders) { | 36 TEST_F(SigninGlobalErrorTest, NoAuthStatusProviders) { |
| 51 ASSERT_FALSE(global_error_->HasBadge()); | 37 ASSERT_FALSE(global_error_->HasBadge()); |
| 52 } | 38 } |
| 53 | 39 |
| 54 TEST_F(SigninGlobalErrorTest, NoErrorAuthStatusProviders) { | 40 TEST_F(SigninGlobalErrorTest, NoErrorAuthStatusProviders) { |
| 55 FakeAuthStatusProvider provider; | 41 { |
| 56 global_error_->AddProvider(&provider); | 42 // Add a provider (removes itself on exiting this scope). |
| 57 ASSERT_FALSE(global_error_->HasBadge()); | 43 FakeAuthStatusProvider provider(global_error_); |
| 58 global_error_->RemoveProvider(&provider); | 44 ASSERT_FALSE(global_error_->HasBadge()); |
| 45 } |
| 59 ASSERT_FALSE(global_error_->HasBadge()); | 46 ASSERT_FALSE(global_error_->HasBadge()); |
| 60 } | 47 } |
| 61 | 48 |
| 62 TEST_F(SigninGlobalErrorTest, ErrorAuthStatusProvider) { | 49 TEST_F(SigninGlobalErrorTest, ErrorAuthStatusProvider) { |
| 63 FakeAuthStatusProvider provider; | 50 { |
| 64 FakeAuthStatusProvider error_provider; | 51 FakeAuthStatusProvider provider(global_error_); |
| 65 error_provider.set_auth_error(GoogleServiceAuthError( | 52 ASSERT_FALSE(global_error_->HasBadge()); |
| 66 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); | 53 { |
| 67 global_error_->AddProvider(&provider); | 54 FakeAuthStatusProvider error_provider(global_error_); |
| 68 ASSERT_FALSE(global_error_->HasBadge()); | 55 error_provider.set_auth_error(GoogleServiceAuthError( |
| 69 global_error_->AddProvider(&error_provider); | 56 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); |
| 70 ASSERT_TRUE(global_error_->HasBadge()); | 57 ASSERT_TRUE(global_error_->HasBadge()); |
| 71 global_error_->RemoveProvider(&error_provider); | 58 } |
| 72 ASSERT_FALSE(global_error_->HasBadge()); | 59 // error_provider is removed now that we've left that scope. |
| 73 global_error_->RemoveProvider(&provider); | 60 ASSERT_FALSE(global_error_->HasBadge()); |
| 61 } |
| 62 // All providers should be removed now. |
| 74 ASSERT_FALSE(global_error_->HasBadge()); | 63 ASSERT_FALSE(global_error_->HasBadge()); |
| 75 } | 64 } |
| 76 | 65 |
| 77 TEST_F(SigninGlobalErrorTest, AuthStatusProviderErrorTransition) { | 66 TEST_F(SigninGlobalErrorTest, AuthStatusProviderErrorTransition) { |
| 78 FakeAuthStatusProvider provider0; | 67 { |
| 79 FakeAuthStatusProvider provider1; | 68 FakeAuthStatusProvider provider0(global_error_); |
| 80 global_error_->AddProvider(&provider0); | 69 FakeAuthStatusProvider provider1(global_error_); |
| 81 global_error_->AddProvider(&provider1); | 70 ASSERT_FALSE(global_error_->HasBadge()); |
| 82 ASSERT_FALSE(global_error_->HasBadge()); | 71 provider0.set_auth_error( |
| 83 provider0.set_auth_error( | 72 GoogleServiceAuthError( |
| 84 GoogleServiceAuthError(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); | 73 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); |
| 85 ASSERT_FALSE(global_error_->HasBadge()); | 74 ASSERT_TRUE(global_error_->HasBadge()); |
| 86 global_error_->AuthStatusChanged(); | 75 provider1.set_auth_error( |
| 87 ASSERT_TRUE(global_error_->HasBadge()); | 76 GoogleServiceAuthError(GoogleServiceAuthError::ACCOUNT_DISABLED)); |
| 88 provider1.set_auth_error( | 77 ASSERT_TRUE(global_error_->HasBadge()); |
| 89 GoogleServiceAuthError(GoogleServiceAuthError::ACCOUNT_DISABLED)); | |
| 90 global_error_->AuthStatusChanged(); | |
| 91 ASSERT_TRUE(global_error_->HasBadge()); | |
| 92 | 78 |
| 93 // Now resolve the auth errors - badge should go away. | 79 // Now resolve the auth errors - badge should go away. |
| 94 provider0.set_auth_error(GoogleServiceAuthError::None()); | 80 provider0.set_auth_error(GoogleServiceAuthError::None()); |
| 95 global_error_->AuthStatusChanged(); | 81 ASSERT_TRUE(global_error_->HasBadge()); |
| 96 ASSERT_TRUE(global_error_->HasBadge()); | 82 provider1.set_auth_error(GoogleServiceAuthError::None()); |
| 97 provider1.set_auth_error(GoogleServiceAuthError::None()); | 83 ASSERT_FALSE(global_error_->HasBadge()); |
| 98 global_error_->AuthStatusChanged(); | 84 } |
| 99 ASSERT_FALSE(global_error_->HasBadge()); | |
| 100 | |
| 101 global_error_->RemoveProvider(&provider0); | |
| 102 ASSERT_FALSE(global_error_->HasBadge()); | |
| 103 global_error_->RemoveProvider(&provider1); | |
| 104 ASSERT_FALSE(global_error_->HasBadge()); | 85 ASSERT_FALSE(global_error_->HasBadge()); |
| 105 } | 86 } |
| 106 | 87 |
| 107 // Verify that SigninGlobalError ignores certain errors. | 88 // Verify that SigninGlobalError ignores certain errors. |
| 108 TEST_F(SigninGlobalErrorTest, AuthStatusEnumerateAllErrors) { | 89 TEST_F(SigninGlobalErrorTest, AuthStatusEnumerateAllErrors) { |
| 109 typedef struct { | 90 typedef struct { |
| 110 GoogleServiceAuthError::State error_state; | 91 GoogleServiceAuthError::State error_state; |
| 111 bool is_error; | 92 bool is_error; |
| 112 } ErrorTableEntry; | 93 } ErrorTableEntry; |
| 113 | 94 |
| 114 ErrorTableEntry table[] = { | 95 ErrorTableEntry table[] = { |
| 115 { GoogleServiceAuthError::NONE, false }, | 96 { GoogleServiceAuthError::NONE, false }, |
| 116 { GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS, true }, | 97 { GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS, true }, |
| 117 { GoogleServiceAuthError::USER_NOT_SIGNED_UP, true }, | 98 { GoogleServiceAuthError::USER_NOT_SIGNED_UP, true }, |
| 118 { GoogleServiceAuthError::CONNECTION_FAILED, false }, | 99 { GoogleServiceAuthError::CONNECTION_FAILED, false }, |
| 119 { GoogleServiceAuthError::CAPTCHA_REQUIRED, true }, | 100 { GoogleServiceAuthError::CAPTCHA_REQUIRED, true }, |
| 120 { GoogleServiceAuthError::ACCOUNT_DELETED, true }, | 101 { GoogleServiceAuthError::ACCOUNT_DELETED, true }, |
| 121 { GoogleServiceAuthError::ACCOUNT_DISABLED, true }, | 102 { GoogleServiceAuthError::ACCOUNT_DISABLED, true }, |
| 122 { GoogleServiceAuthError::SERVICE_UNAVAILABLE, true }, | 103 { GoogleServiceAuthError::SERVICE_UNAVAILABLE, true }, |
| 123 { GoogleServiceAuthError::TWO_FACTOR, true }, | 104 { GoogleServiceAuthError::TWO_FACTOR, true }, |
| 124 { GoogleServiceAuthError::REQUEST_CANCELED, true }, | 105 { GoogleServiceAuthError::REQUEST_CANCELED, true }, |
| 125 { GoogleServiceAuthError::HOSTED_NOT_ALLOWED, true }, | 106 { GoogleServiceAuthError::HOSTED_NOT_ALLOWED, true }, |
| 126 }; | 107 }; |
| 127 COMPILE_ASSERT(ARRAYSIZE_UNSAFE(table) == GoogleServiceAuthError::NUM_STATES, | 108 COMPILE_ASSERT(ARRAYSIZE_UNSAFE(table) == GoogleServiceAuthError::NUM_STATES, |
| 128 kTable_size_does_not_match_number_of_auth_error_types); | 109 kTable_size_does_not_match_number_of_auth_error_types); |
| 129 | 110 |
| 130 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(table); ++i) { | 111 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(table); ++i) { |
| 131 FakeAuthStatusProvider provider; | 112 FakeAuthStatusProvider provider(global_error_); |
| 132 provider.set_auth_error(GoogleServiceAuthError(table[i].error_state)); | 113 provider.set_auth_error(GoogleServiceAuthError(table[i].error_state)); |
| 133 GlobalErrorService* service = | 114 GlobalErrorService* service = |
| 134 GlobalErrorServiceFactory::GetForProfile(profile_.get()); | 115 GlobalErrorServiceFactory::GetForProfile(profile_.get()); |
| 135 global_error_->AddProvider(&provider); | |
| 136 EXPECT_EQ(global_error_->HasBadge(), table[i].is_error); | 116 EXPECT_EQ(global_error_->HasBadge(), table[i].is_error); |
| 137 // Should badge the wrench menu if there's an error. | 117 // Should badge the wrench menu if there's an error. |
| 138 EXPECT_EQ(service->GetFirstBadgeResourceID() != 0, table[i].is_error); | 118 EXPECT_EQ(service->GetFirstBadgeResourceID() != 0, table[i].is_error); |
| 139 #if defined(OS_CHROMEOS) | 119 #if defined(OS_CHROMEOS) |
| 140 // Only on chromeos do we have a separate menu item - on other platforms | 120 // Only on chromeos do we have a separate menu item - on other platforms |
| 141 // there's code in WrenchMenuModel to re-use the "sign in to chrome" | 121 // there's code in WrenchMenuModel to re-use the "sign in to chrome" |
| 142 // menu item to display auth status/errors. | 122 // menu item to display auth status/errors. |
| 143 EXPECT_EQ(global_error_->HasMenuItem(), table[i].is_error); | 123 EXPECT_EQ(global_error_->HasMenuItem(), table[i].is_error); |
| 144 #else | 124 #else |
| 145 EXPECT_FALSE(global_error_->HasMenuItem()); | 125 EXPECT_FALSE(global_error_->HasMenuItem()); |
| 146 #endif | 126 #endif |
| 147 EXPECT_EQ(global_error_->MenuItemLabel().empty(), !table[i].is_error); | 127 EXPECT_EQ(global_error_->MenuItemLabel().empty(), !table[i].is_error); |
| 148 EXPECT_EQ(global_error_->GetBubbleViewMessage().empty(), | 128 EXPECT_EQ(global_error_->GetBubbleViewMessage().empty(), |
| 149 !table[i].is_error); | 129 !table[i].is_error); |
| 150 EXPECT_FALSE(global_error_->GetBubbleViewTitle().empty()); | 130 EXPECT_FALSE(global_error_->GetBubbleViewTitle().empty()); |
| 151 EXPECT_FALSE(global_error_->GetBubbleViewAcceptButtonLabel().empty()); | 131 EXPECT_FALSE(global_error_->GetBubbleViewAcceptButtonLabel().empty()); |
| 152 EXPECT_TRUE(global_error_->GetBubbleViewCancelButtonLabel().empty()); | 132 EXPECT_TRUE(global_error_->GetBubbleViewCancelButtonLabel().empty()); |
| 153 global_error_->RemoveProvider(&provider); | |
| 154 } | 133 } |
| 155 } | 134 } |
| OLD | NEW |