Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / signin / signin_error_notifier_ash_unittest.cc
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 "chrome/browser/signin/signin_error_notifier_ash.h"
6
7 #include "ash/test/ash_test_base.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/notifications/notification.h"
11 #include "chrome/browser/notifications/notification_ui_manager.h"
12 #include "chrome/browser/signin/fake_signin_manager.h"
13 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
14 #include "chrome/browser/signin/signin_error_notifier_factory_ash.h"
15 #include "chrome/browser/signin/signin_manager_factory.h"
16 #include "chrome/browser/ui/ash/test_views_delegate_with_parent.h"
17 #include "chrome/test/base/testing_browser_process.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "chrome/test/base/testing_profile_manager.h"
20 #include "components/signin/core/browser/fake_auth_status_provider.h"
21 #include "components/signin/core/browser/profile_oauth2_token_service.h"
22 #include "components/signin/core/browser/signin_error_controller.h"
23 #include "components/signin/core/browser/signin_manager.h"
24 #include "content/public/test/test_browser_thread_bundle.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26 #include "ui/message_center/notification.h"
27
28 #if !defined(OS_CHROMEOS)
29 #include "chrome/browser/ui/ash/ash_util.h"
30 #include "ui/aura/test/test_screen.h"
31 #include "ui/gfx/screen.h"
32 #include "ui/gfx/screen_type_delegate.h"
33 #endif
34
35 namespace ash {
36 namespace test {
37
38 namespace {
39
40 static const char kTestAccountId[] = "testuser@test.com";
41
42 // Notification ID corresponding to kProfileSigninNotificationId +
43 // kTestAccountId.
44 static const std::string kNotificationId =
45     "chrome://settings/signin/testuser@test.com";
46 }
47
48 #if !defined(OS_CHROMEOS)
49 class ScreenTypeDelegateDesktop : public gfx::ScreenTypeDelegate {
50  public:
51   ScreenTypeDelegateDesktop() {}
52   virtual gfx::ScreenType GetScreenTypeForNativeView(
53       gfx::NativeView view) OVERRIDE {
54     return chrome::IsNativeViewInAsh(view) ?
55         gfx::SCREEN_TYPE_ALTERNATE :
56         gfx::SCREEN_TYPE_NATIVE;
57   }
58  private:
59   DISALLOW_COPY_AND_ASSIGN(ScreenTypeDelegateDesktop);
60 };
61 #endif
62
63 class SigninErrorNotifierTest : public AshTestBase {
64  public:
65   virtual void SetUp() OVERRIDE {
66     views::ViewsDelegate::views_delegate = &views_delegate_;
67
68     // Create a signed-in profile.
69     TestingProfile::Builder builder;
70     builder.AddTestingFactory(SigninManagerFactory::GetInstance(),
71                               FakeSigninManagerBase::Build);
72     profile_ = builder.Build();
73     profile_->set_profile_name(kTestAccountId);
74
75     profile_manager_.reset(
76         new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
77     ASSERT_TRUE(profile_manager_->SetUp());
78
79     TestingBrowserProcess::GetGlobal();
80     AshTestBase::SetUp();
81
82     // Set up screen for Windows.
83 #if !defined(OS_CHROMEOS)
84     aura::TestScreen* test_screen = aura::TestScreen::Create();
85     gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen);
86     gfx::Screen::SetScreenTypeDelegate(new ScreenTypeDelegateDesktop);
87 #endif
88
89     error_controller_ =
90         ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get())->
91             signin_error_controller();
92     SigninErrorNotifierFactory::GetForProfile(profile_.get());
93     notification_ui_manager_ = g_browser_process->notification_ui_manager();
94   }
95
96   virtual void TearDown() OVERRIDE {
97     profile_manager_.reset();
98
99     AshTestBase::TearDown();
100   }
101
102  protected:
103   void GetMessage(base::string16* message) {
104     const Notification* notification =
105         g_browser_process->notification_ui_manager()->FindById(kNotificationId);
106     ASSERT_FALSE(notification == NULL);
107     *message = notification->message();
108   }
109
110   scoped_ptr<TestingProfileManager> profile_manager_;
111   scoped_ptr<TestingProfile> profile_;
112   SigninErrorController* error_controller_;
113   NotificationUIManager* notification_ui_manager_;
114   TestViewsDelegateWithParent views_delegate_;
115 };
116
117 TEST_F(SigninErrorNotifierTest, NoErrorAuthStatusProviders) {
118   ASSERT_FALSE(notification_ui_manager_->FindById(kNotificationId));
119   {
120     // Add a provider (removes itself on exiting this scope).
121     FakeAuthStatusProvider provider(error_controller_);
122     ASSERT_FALSE(notification_ui_manager_->FindById(kNotificationId));
123   }
124   ASSERT_FALSE(notification_ui_manager_->FindById(kNotificationId));
125 }
126
127 TEST_F(SigninErrorNotifierTest, ErrorAuthStatusProvider) {
128   {
129     FakeAuthStatusProvider provider(error_controller_);
130     ASSERT_FALSE(notification_ui_manager_->FindById(kNotificationId));
131     {
132       FakeAuthStatusProvider error_provider(error_controller_);
133       error_provider.SetAuthError(kTestAccountId, GoogleServiceAuthError(
134           GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS));
135       ASSERT_TRUE(notification_ui_manager_->FindById(kNotificationId));
136     }
137     // error_provider is removed now that we've left that scope.
138     ASSERT_FALSE(notification_ui_manager_->FindById(kNotificationId));
139   }
140   // All providers should be removed now.
141   ASSERT_FALSE(notification_ui_manager_->FindById(kNotificationId));
142 }
143
144 TEST_F(SigninErrorNotifierTest, AuthStatusProviderErrorTransition) {
145   {
146     FakeAuthStatusProvider provider0(error_controller_);
147     FakeAuthStatusProvider provider1(error_controller_);
148     provider0.SetAuthError(
149         kTestAccountId,
150         GoogleServiceAuthError(
151             GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS));
152     ASSERT_TRUE(notification_ui_manager_->FindById(kNotificationId));
153
154     base::string16 message;
155     GetMessage(&message);
156     ASSERT_FALSE(message.empty());
157
158     // Now set another auth error and clear the original.
159     provider1.SetAuthError(
160         kTestAccountId,
161         GoogleServiceAuthError(
162             GoogleServiceAuthError::UNEXPECTED_SERVICE_RESPONSE));
163     provider0.SetAuthError(
164         kTestAccountId, GoogleServiceAuthError::AuthErrorNone());
165
166     ASSERT_TRUE(notification_ui_manager_->FindById(kNotificationId));
167
168     base::string16 new_message;
169     GetMessage(&new_message);
170     ASSERT_FALSE(new_message.empty());
171
172     ASSERT_NE(new_message, message);
173
174     provider1.SetAuthError(
175         kTestAccountId, GoogleServiceAuthError::AuthErrorNone());
176     ASSERT_FALSE(notification_ui_manager_->FindById(kNotificationId));
177   }
178 }
179
180 // Verify that SigninErrorNotifier ignores certain errors.
181 TEST_F(SigninErrorNotifierTest, AuthStatusEnumerateAllErrors) {
182   typedef struct {
183     GoogleServiceAuthError::State error_state;
184     bool is_error;
185   } ErrorTableEntry;
186
187   ErrorTableEntry table[] = {
188     { GoogleServiceAuthError::NONE, false },
189     { GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS, true },
190     { GoogleServiceAuthError::USER_NOT_SIGNED_UP, true },
191     { GoogleServiceAuthError::CONNECTION_FAILED, false },
192     { GoogleServiceAuthError::CAPTCHA_REQUIRED, true },
193     { GoogleServiceAuthError::ACCOUNT_DELETED, true },
194     { GoogleServiceAuthError::ACCOUNT_DISABLED, true },
195     { GoogleServiceAuthError::SERVICE_UNAVAILABLE, true },
196     { GoogleServiceAuthError::TWO_FACTOR, true },
197     { GoogleServiceAuthError::REQUEST_CANCELED, true },
198     { GoogleServiceAuthError::HOSTED_NOT_ALLOWED, true },
199     { GoogleServiceAuthError::UNEXPECTED_SERVICE_RESPONSE, true },
200     { GoogleServiceAuthError::SERVICE_ERROR, true },
201   };
202   COMPILE_ASSERT(ARRAYSIZE_UNSAFE(table) == GoogleServiceAuthError::NUM_STATES,
203       kTable_size_does_not_match_number_of_auth_error_types);
204
205   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(table); ++i) {
206     FakeAuthStatusProvider provider(error_controller_);
207     provider.SetAuthError(kTestAccountId,
208                           GoogleServiceAuthError(table[i].error_state));
209     const Notification* notification = notification_ui_manager_->
210         FindById(kNotificationId);
211     ASSERT_EQ(table[i].is_error, notification != NULL);
212     if (table[i].is_error) {
213       EXPECT_FALSE(notification->title().empty());
214       EXPECT_FALSE(notification->message().empty());
215       EXPECT_EQ((size_t)1, notification->buttons().size());
216     }
217   }
218 }
219
220 }  // namespace test
221 }  // namespace ash