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 "base/prefs/pref_service.h"
9#include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
10#include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
11#include "chrome/browser/signin/fake_signin_manager.h"
12#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
13#include "chrome/browser/signin/signin_global_error_factory.h"
14#include "chrome/browser/signin/signin_manager_factory.h"
15#include "chrome/browser/ui/global_error/global_error_service.h"
16#include "chrome/browser/ui/global_error/global_error_service_factory.h"
17#include "chrome/common/pref_names.h"
18#include "chrome/test/base/testing_profile.h"
19#include "components/signin/core/browser/fake_auth_status_provider.h"
20#include "components/signin/core/browser/signin_error_controller.h"
21#include "components/signin/core/browser/signin_manager.h"
22#include "content/public/test/test_browser_thread_bundle.h"
23#include "testing/gtest/include/gtest/gtest.h"
24
25static const char kTestAccountId[] = "testuser@test.com";
26static const char kTestUsername[] = "testuser@test.com";
27
28class SigninGlobalErrorTest : public testing::Test {
29 public:
30  virtual void SetUp() OVERRIDE {
31    // Create a signed-in profile.
32    TestingProfile::Builder builder;
33    builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
34                              BuildFakeProfileOAuth2TokenService);
35    builder.AddTestingFactory(SigninManagerFactory::GetInstance(),
36                              FakeSigninManagerBase::Build);
37    profile_ = builder.Build();
38
39    profile_->GetPrefs()->SetString(
40        prefs::kGoogleServicesUsername, kTestAccountId);
41    SigninManagerFactory::GetForProfile(profile_.get())
42        ->SetAuthenticatedUsername(kTestAccountId);
43
44    global_error_ = SigninGlobalErrorFactory::GetForProfile(profile_.get());
45    error_controller_ = ProfileOAuth2TokenServiceFactory::GetForProfile(
46        profile_.get())->signin_error_controller();
47  }
48
49  content::TestBrowserThreadBundle thread_bundle_;
50  scoped_ptr<TestingProfile> profile_;
51  SigninGlobalError* global_error_;
52  SigninErrorController* error_controller_;
53};
54
55TEST_F(SigninGlobalErrorTest, NoErrorAuthStatusProviders) {
56  scoped_ptr<FakeAuthStatusProvider> provider;
57
58  ASSERT_FALSE(global_error_->HasMenuItem());
59
60  // Add a provider.
61  provider.reset(new FakeAuthStatusProvider(error_controller_));
62  ASSERT_FALSE(global_error_->HasMenuItem());
63
64  // Remove the provider.
65  provider.reset();
66  ASSERT_FALSE(global_error_->HasMenuItem());
67}
68
69TEST_F(SigninGlobalErrorTest, ErrorAuthStatusProvider) {
70  scoped_ptr<FakeAuthStatusProvider> provider;
71  scoped_ptr<FakeAuthStatusProvider> error_provider;
72
73  provider.reset(new FakeAuthStatusProvider(error_controller_));
74  ASSERT_FALSE(global_error_->HasMenuItem());
75
76  error_provider.reset(new FakeAuthStatusProvider(error_controller_));
77  error_provider->SetAuthError(
78      kTestAccountId,
79      kTestUsername,
80      GoogleServiceAuthError(
81          GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS));
82  ASSERT_TRUE(global_error_->HasMenuItem());
83
84  error_provider.reset();
85  ASSERT_FALSE(global_error_->HasMenuItem());
86
87  provider.reset();
88  error_provider.reset();
89  ASSERT_FALSE(global_error_->HasMenuItem());
90}
91
92// Verify that SigninGlobalError ignores certain errors.
93TEST_F(SigninGlobalErrorTest, AuthStatusEnumerateAllErrors) {
94  typedef struct {
95    GoogleServiceAuthError::State error_state;
96    bool is_error;
97  } ErrorTableEntry;
98
99  ErrorTableEntry table[] = {
100    { GoogleServiceAuthError::NONE, false },
101    { GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS, true },
102    { GoogleServiceAuthError::USER_NOT_SIGNED_UP, true },
103    { GoogleServiceAuthError::CONNECTION_FAILED, false },
104    { GoogleServiceAuthError::CAPTCHA_REQUIRED, true },
105    { GoogleServiceAuthError::ACCOUNT_DELETED, true },
106    { GoogleServiceAuthError::ACCOUNT_DISABLED, true },
107    { GoogleServiceAuthError::SERVICE_UNAVAILABLE, true },
108    { GoogleServiceAuthError::TWO_FACTOR, true },
109    { GoogleServiceAuthError::REQUEST_CANCELED, true },
110    { GoogleServiceAuthError::HOSTED_NOT_ALLOWED, true },
111    { GoogleServiceAuthError::UNEXPECTED_SERVICE_RESPONSE, true },
112    { GoogleServiceAuthError::SERVICE_ERROR, true },
113  };
114  COMPILE_ASSERT(ARRAYSIZE_UNSAFE(table) == GoogleServiceAuthError::NUM_STATES,
115      kTable_size_does_not_match_number_of_auth_error_types);
116
117  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(table); ++i) {
118    FakeAuthStatusProvider provider(error_controller_);
119    provider.SetAuthError(kTestAccountId,
120                          kTestUsername,
121                          GoogleServiceAuthError(table[i].error_state));
122
123    EXPECT_EQ(global_error_->HasMenuItem(), table[i].is_error);
124    EXPECT_EQ(global_error_->MenuItemLabel().empty(), !table[i].is_error);
125    EXPECT_EQ(global_error_->GetBubbleViewMessages().empty(),
126              !table[i].is_error);
127    EXPECT_FALSE(global_error_->GetBubbleViewTitle().empty());
128    EXPECT_FALSE(global_error_->GetBubbleViewAcceptButtonLabel().empty());
129    EXPECT_TRUE(global_error_->GetBubbleViewCancelButtonLabel().empty());
130  }
131}
132