blob: 1d919727f52292652d7bb1979c68121b477ba072 [file] [log] [blame]
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/signin/signin_global_error.h"
#include "base/memory/scoped_ptr.h"
#include "base/prefs/pref_service.h"
#include "chrome/browser/signin/fake_auth_status_provider.h"
#include "chrome/browser/signin/fake_signin_manager.h"
#include "chrome/browser/signin/signin_manager.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/ui/global_error/global_error_service.h"
#include "chrome/browser/ui/global_error/global_error_service_factory.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "testing/gtest/include/gtest/gtest.h"
static const char kTestAccountId[] = "testuser@test.com";
static const char kOtherTestAccountId[] = "otheruser@test.com";
class SigninGlobalErrorTest : public testing::Test {
public:
virtual void SetUp() OVERRIDE {
// Create a signed-in profile.
profile_.reset(new TestingProfile());
SigninManagerBase* manager = static_cast<SigninManagerBase*>(
SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse(
profile_.get(), FakeSigninManagerBase::Build));
profile_->GetPrefs()->SetString(
prefs::kGoogleServicesUsername, kTestAccountId);
manager->Initialize(profile_.get(), NULL);
global_error_ = SigninGlobalError::GetForProfile(profile_.get());
}
content::TestBrowserThreadBundle thread_bundle_;
scoped_ptr<TestingProfile> profile_;
SigninGlobalError* global_error_;
};
TEST_F(SigninGlobalErrorTest, NoAuthStatusProviders) {
ASSERT_FALSE(global_error_->HasMenuItem());
}
TEST_F(SigninGlobalErrorTest, NoErrorAuthStatusProviders) {
{
// Add a provider (removes itself on exiting this scope).
FakeAuthStatusProvider provider(global_error_);
ASSERT_FALSE(global_error_->HasMenuItem());
}
ASSERT_FALSE(global_error_->HasMenuItem());
}
TEST_F(SigninGlobalErrorTest, ErrorAuthStatusProvider) {
{
FakeAuthStatusProvider provider(global_error_);
ASSERT_FALSE(global_error_->HasMenuItem());
{
FakeAuthStatusProvider error_provider(global_error_);
error_provider.SetAuthError(kTestAccountId, GoogleServiceAuthError(
GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS));
ASSERT_TRUE(global_error_->HasMenuItem());
}
// error_provider is removed now that we've left that scope.
ASSERT_FALSE(global_error_->HasMenuItem());
}
// All providers should be removed now.
ASSERT_FALSE(global_error_->HasMenuItem());
}
TEST_F(SigninGlobalErrorTest, AuthStatusProviderErrorTransition) {
{
FakeAuthStatusProvider provider0(global_error_);
FakeAuthStatusProvider provider1(global_error_);
ASSERT_FALSE(global_error_->HasMenuItem());
provider0.SetAuthError(
kTestAccountId,
GoogleServiceAuthError(
GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS));
ASSERT_TRUE(global_error_->HasMenuItem());
provider1.SetAuthError(
kTestAccountId,
GoogleServiceAuthError(GoogleServiceAuthError::ACCOUNT_DISABLED));
ASSERT_TRUE(global_error_->HasMenuItem());
// Now resolve the auth errors - the menu item should go away.
provider0.SetAuthError(kTestAccountId,
GoogleServiceAuthError::AuthErrorNone());
ASSERT_TRUE(global_error_->HasMenuItem());
provider1.SetAuthError(kTestAccountId,
GoogleServiceAuthError::AuthErrorNone());
ASSERT_FALSE(global_error_->HasMenuItem());
}
ASSERT_FALSE(global_error_->HasMenuItem());
}
TEST_F(SigninGlobalErrorTest, AuthStatusProviderAccountTransition) {
{
FakeAuthStatusProvider provider0(global_error_);
FakeAuthStatusProvider provider1(global_error_);
ASSERT_FALSE(global_error_->HasMenuItem());
provider0.SetAuthError(
kTestAccountId,
GoogleServiceAuthError(
GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS));
provider1.SetAuthError(
kOtherTestAccountId,
GoogleServiceAuthError(GoogleServiceAuthError::NONE));
ASSERT_TRUE(global_error_->HasMenuItem());
ASSERT_STREQ(kTestAccountId,
global_error_->GetAccountIdOfLastAuthError().c_str());
// Swap providers reporting errors.
provider1.set_error_without_status_change(
GoogleServiceAuthError(
GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS));
provider0.set_error_without_status_change(
GoogleServiceAuthError(GoogleServiceAuthError::NONE));
global_error_->AuthStatusChanged();
ASSERT_TRUE(global_error_->HasMenuItem());
ASSERT_STREQ(kOtherTestAccountId,
global_error_->GetAccountIdOfLastAuthError().c_str());
// Now resolve the auth errors - the menu item should go away.
provider0.set_error_without_status_change(
GoogleServiceAuthError::AuthErrorNone());
provider1.set_error_without_status_change(
GoogleServiceAuthError::AuthErrorNone());
global_error_->AuthStatusChanged();
ASSERT_FALSE(global_error_->HasMenuItem());
}
ASSERT_FALSE(global_error_->HasMenuItem());
}
// Verify that SigninGlobalError ignores certain errors.
TEST_F(SigninGlobalErrorTest, AuthStatusEnumerateAllErrors) {
typedef struct {
GoogleServiceAuthError::State error_state;
bool is_error;
} ErrorTableEntry;
ErrorTableEntry table[] = {
{ GoogleServiceAuthError::NONE, false },
{ GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS, true },
{ GoogleServiceAuthError::USER_NOT_SIGNED_UP, true },
{ GoogleServiceAuthError::CONNECTION_FAILED, false },
{ GoogleServiceAuthError::CAPTCHA_REQUIRED, true },
{ GoogleServiceAuthError::ACCOUNT_DELETED, true },
{ GoogleServiceAuthError::ACCOUNT_DISABLED, true },
{ GoogleServiceAuthError::SERVICE_UNAVAILABLE, true },
{ GoogleServiceAuthError::TWO_FACTOR, true },
{ GoogleServiceAuthError::REQUEST_CANCELED, true },
{ GoogleServiceAuthError::HOSTED_NOT_ALLOWED, true },
{ GoogleServiceAuthError::UNEXPECTED_SERVICE_RESPONSE, true },
{ GoogleServiceAuthError::SERVICE_ERROR, true },
};
COMPILE_ASSERT(ARRAYSIZE_UNSAFE(table) == GoogleServiceAuthError::NUM_STATES,
kTable_size_does_not_match_number_of_auth_error_types);
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(table); ++i) {
FakeAuthStatusProvider provider(global_error_);
provider.SetAuthError(kTestAccountId,
GoogleServiceAuthError(table[i].error_state));
EXPECT_EQ(global_error_->HasMenuItem(), table[i].is_error);
// Only on chromeos do we have a separate menu item - on other platforms
// there's code in WrenchMenuModel to re-use the "sign in to chrome"
// menu item to display auth status/errors.
EXPECT_EQ(global_error_->HasMenuItem(), table[i].is_error);
EXPECT_EQ(global_error_->MenuItemLabel().empty(), !table[i].is_error);
EXPECT_EQ(global_error_->GetBubbleViewMessages().empty(),
!table[i].is_error);
EXPECT_FALSE(global_error_->GetBubbleViewTitle().empty());
EXPECT_FALSE(global_error_->GetBubbleViewAcceptButtonLabel().empty());
EXPECT_TRUE(global_error_->GetBubbleViewCancelButtonLabel().empty());
}
}