OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/signin/signin_global_error.h" |
| 6 |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "chrome/browser/signin/signin_manager.h" |
| 9 #include "chrome/browser/signin/signin_manager_factory.h" |
| 10 #include "chrome/browser/signin/signin_manager_fake.h" |
| 11 #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_factory.h" |
| 14 #include "chrome/test/base/testing_profile.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 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 { |
| 33 public: |
| 34 void SetUp() OVERRIDE { |
| 35 // Create a signed-in profile. |
| 36 profile_.reset(new TestingProfile()); |
| 37 |
| 38 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( |
| 39 profile_.get(), FakeSigninManager::Build); |
| 40 SigninManager* manager = |
| 41 SigninManagerFactory::GetForProfile(profile_.get()); |
| 42 manager->SetAuthenticatedUsername("testuser@test.com"); |
| 43 global_error_ = manager->signin_global_error(); |
| 44 } |
| 45 |
| 46 scoped_ptr<TestingProfile> profile_; |
| 47 SigninGlobalError* global_error_; |
| 48 }; |
| 49 |
| 50 TEST_F(SigninGlobalErrorTest, NoAuthStatusProviders) { |
| 51 ASSERT_FALSE(global_error_->HasBadge()); |
| 52 } |
| 53 |
| 54 TEST_F(SigninGlobalErrorTest, NoErrorAuthStatusProviders) { |
| 55 FakeAuthStatusProvider provider; |
| 56 global_error_->AddProvider(&provider); |
| 57 ASSERT_FALSE(global_error_->HasBadge()); |
| 58 global_error_->RemoveProvider(&provider); |
| 59 ASSERT_FALSE(global_error_->HasBadge()); |
| 60 } |
| 61 |
| 62 TEST_F(SigninGlobalErrorTest, ErrorAuthStatusProvider) { |
| 63 FakeAuthStatusProvider provider; |
| 64 FakeAuthStatusProvider error_provider; |
| 65 error_provider.set_auth_error(GoogleServiceAuthError( |
| 66 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); |
| 67 global_error_->AddProvider(&provider); |
| 68 ASSERT_FALSE(global_error_->HasBadge()); |
| 69 global_error_->AddProvider(&error_provider); |
| 70 ASSERT_TRUE(global_error_->HasBadge()); |
| 71 global_error_->RemoveProvider(&error_provider); |
| 72 ASSERT_FALSE(global_error_->HasBadge()); |
| 73 global_error_->RemoveProvider(&provider); |
| 74 ASSERT_FALSE(global_error_->HasBadge()); |
| 75 } |
| 76 |
| 77 TEST_F(SigninGlobalErrorTest, AuthStatusProviderErrorTransition) { |
| 78 FakeAuthStatusProvider provider0; |
| 79 FakeAuthStatusProvider provider1; |
| 80 global_error_->AddProvider(&provider0); |
| 81 global_error_->AddProvider(&provider1); |
| 82 ASSERT_FALSE(global_error_->HasBadge()); |
| 83 provider0.set_auth_error( |
| 84 GoogleServiceAuthError(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); |
| 85 ASSERT_FALSE(global_error_->HasBadge()); |
| 86 global_error_->AuthStatusChanged(); |
| 87 ASSERT_TRUE(global_error_->HasBadge()); |
| 88 provider1.set_auth_error( |
| 89 GoogleServiceAuthError(GoogleServiceAuthError::ACCOUNT_DISABLED)); |
| 90 global_error_->AuthStatusChanged(); |
| 91 ASSERT_TRUE(global_error_->HasBadge()); |
| 92 |
| 93 // Now resolve the auth errors - badge should go away. |
| 94 provider0.set_auth_error(GoogleServiceAuthError::None()); |
| 95 global_error_->AuthStatusChanged(); |
| 96 ASSERT_TRUE(global_error_->HasBadge()); |
| 97 provider1.set_auth_error(GoogleServiceAuthError::None()); |
| 98 global_error_->AuthStatusChanged(); |
| 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()); |
| 105 } |
| 106 |
| 107 // Verify that SigninGlobalError ignores certain errors. |
| 108 TEST_F(SigninGlobalErrorTest, AuthStatusEnumerateAllErrors) { |
| 109 typedef struct { |
| 110 GoogleServiceAuthError::State error_state; |
| 111 bool is_error; |
| 112 } ErrorTableEntry; |
| 113 |
| 114 ErrorTableEntry table[] = { |
| 115 { GoogleServiceAuthError::NONE, false }, |
| 116 { GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS, true }, |
| 117 { GoogleServiceAuthError::USER_NOT_SIGNED_UP, true }, |
| 118 { GoogleServiceAuthError::CONNECTION_FAILED, false }, |
| 119 { GoogleServiceAuthError::CAPTCHA_REQUIRED, true }, |
| 120 { GoogleServiceAuthError::ACCOUNT_DELETED, true }, |
| 121 { GoogleServiceAuthError::ACCOUNT_DISABLED, true }, |
| 122 { GoogleServiceAuthError::SERVICE_UNAVAILABLE, true }, |
| 123 { GoogleServiceAuthError::TWO_FACTOR, true }, |
| 124 { GoogleServiceAuthError::REQUEST_CANCELED, true }, |
| 125 { GoogleServiceAuthError::HOSTED_NOT_ALLOWED, true }, |
| 126 }; |
| 127 COMPILE_ASSERT(ARRAYSIZE_UNSAFE(table) == GoogleServiceAuthError::NUM_STATES, |
| 128 kTable_size_does_not_match_number_of_auth_error_types); |
| 129 |
| 130 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(table); ++i) { |
| 131 FakeAuthStatusProvider provider; |
| 132 provider.set_auth_error(GoogleServiceAuthError(table[i].error_state)); |
| 133 GlobalErrorService* service = |
| 134 GlobalErrorServiceFactory::GetForProfile(profile_.get()); |
| 135 global_error_->AddProvider(&provider); |
| 136 EXPECT_EQ(global_error_->HasBadge(), table[i].is_error); |
| 137 // Should badge the wrench menu if there's an error. |
| 138 EXPECT_EQ(service->GetFirstBadgeResourceID() != 0, table[i].is_error); |
| 139 #if defined(OS_CHROMEOS) |
| 140 // 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" |
| 142 // menu item to display auth status/errors. |
| 143 EXPECT_EQ(global_error_->HasMenuItem(), table[i].is_error); |
| 144 #else |
| 145 EXPECT_FALSE(global_error_->HasMenuItem()); |
| 146 #endif |
| 147 EXPECT_EQ(global_error_->MenuItemLabel().empty(), !table[i].is_error); |
| 148 EXPECT_EQ(global_error_->GetBubbleViewMessage().empty(), |
| 149 !table[i].is_error); |
| 150 EXPECT_FALSE(global_error_->GetBubbleViewTitle().empty()); |
| 151 EXPECT_FALSE(global_error_->GetBubbleViewAcceptButtonLabel().empty()); |
| 152 EXPECT_TRUE(global_error_->GetBubbleViewCancelButtonLabel().empty()); |
| 153 global_error_->RemoveProvider(&provider); |
| 154 } |
| 155 } |
OLD | NEW |