1 // Copyright 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.
5 #include "chrome/browser/ui/views/sync/one_click_signin_bubble_view.h"
8 #include "base/bind_helpers.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "chrome/browser/ui/sync/one_click_signin_bubble_delegate.h"
11 #include "content/public/test/test_utils.h"
12 #include "ui/events/event_utils.h"
13 #include "ui/views/controls/button/label_button.h"
14 #include "ui/views/test/views_test_base.h"
15 #include "ui/views/widget/widget.h"
17 class OneClickSigninBubbleViewTest
: public views::ViewsTestBase
{
19 OneClickSigninBubbleViewTest()
20 : on_start_sync_called_(false),
21 mode_(OneClickSigninSyncStarter::CONFIGURE_SYNC_FIRST
),
22 bubble_learn_more_click_count_(0),
23 dialog_learn_more_click_count_(0),
24 advanced_click_count_(0),
25 anchor_widget_(NULL
) {
28 void SetUp() override
{
29 views::ViewsTestBase::SetUp();
31 // Create a widget to host the anchor view.
32 anchor_widget_
= new views::Widget
;
33 views::Widget::InitParams widget_params
= CreateParams(
34 views::Widget::InitParams::TYPE_WINDOW
);
35 anchor_widget_
->Init(widget_params
);
36 anchor_widget_
->Show();
39 void TearDown() override
{
40 OneClickSigninBubbleView::Hide();
41 anchor_widget_
->Close();
42 anchor_widget_
= NULL
;
43 views::ViewsTestBase::TearDown();
47 OneClickSigninBubbleView
* ShowOneClickSigninBubble(
48 BrowserWindow::OneClickSigninBubbleType bubble_type
) {
50 scoped_ptr
<OneClickSigninBubbleDelegate
> delegate
;
51 delegate
.reset(new OneClickSigninBubbleTestDelegate(this));
53 OneClickSigninBubbleView::ShowBubble(
58 anchor_widget_
->GetContentsView(),
59 base::Bind(&OneClickSigninBubbleViewTest::OnStartSync
,
60 base::Unretained(this)));
62 OneClickSigninBubbleView
* view
=
63 OneClickSigninBubbleView::view_for_testing();
64 EXPECT_TRUE(view
!= NULL
);
68 void OnStartSync(OneClickSigninSyncStarter::StartSyncMode mode
) {
69 on_start_sync_called_
= true;
73 bool on_start_sync_called_
;
74 OneClickSigninSyncStarter::StartSyncMode mode_
;
75 int bubble_learn_more_click_count_
;
76 int dialog_learn_more_click_count_
;
77 int advanced_click_count_
;
80 friend class OneClickSigninBubbleTestDelegate
;
82 class OneClickSigninBubbleTestDelegate
83 : public OneClickSigninBubbleDelegate
{
85 // |test| is not owned by this object.
86 explicit OneClickSigninBubbleTestDelegate(
87 OneClickSigninBubbleViewTest
* test
) : test_(test
) {}
89 // OneClickSigninBubbleDelegate:
90 void OnLearnMoreLinkClicked(bool is_dialog
) override
{
92 ++test_
->dialog_learn_more_click_count_
;
94 ++test_
->bubble_learn_more_click_count_
;
96 void OnAdvancedLinkClicked() override
{ ++test_
->advanced_click_count_
; }
99 OneClickSigninBubbleViewTest
* test_
;
101 DISALLOW_COPY_AND_ASSIGN(OneClickSigninBubbleTestDelegate
);
104 // Widget to host the anchor view of the bubble. Destroys itself when closed.
105 views::Widget
* anchor_widget_
;
107 DISALLOW_COPY_AND_ASSIGN(OneClickSigninBubbleViewTest
);
110 TEST_F(OneClickSigninBubbleViewTest
, ShowBubble
) {
111 ShowOneClickSigninBubble(BrowserWindow::ONE_CLICK_SIGNIN_BUBBLE_TYPE_BUBBLE
);
112 content::RunAllPendingInMessageLoop();
113 EXPECT_TRUE(OneClickSigninBubbleView::IsShowing());
116 TEST_F(OneClickSigninBubbleViewTest
, ShowDialog
) {
117 ShowOneClickSigninBubble(
118 BrowserWindow::ONE_CLICK_SIGNIN_BUBBLE_TYPE_MODAL_DIALOG
);
119 content::RunAllPendingInMessageLoop();
120 EXPECT_TRUE(OneClickSigninBubbleView::IsShowing());
123 TEST_F(OneClickSigninBubbleViewTest
, HideBubble
) {
124 ShowOneClickSigninBubble(BrowserWindow::ONE_CLICK_SIGNIN_BUBBLE_TYPE_BUBBLE
);
126 OneClickSigninBubbleView::Hide();
127 content::RunAllPendingInMessageLoop();
128 EXPECT_FALSE(OneClickSigninBubbleView::IsShowing());
131 TEST_F(OneClickSigninBubbleViewTest
, HideDialog
) {
132 ShowOneClickSigninBubble(
133 BrowserWindow::ONE_CLICK_SIGNIN_BUBBLE_TYPE_MODAL_DIALOG
);
135 OneClickSigninBubbleView::Hide();
136 content::RunAllPendingInMessageLoop();
137 EXPECT_FALSE(OneClickSigninBubbleView::IsShowing());
138 EXPECT_TRUE(on_start_sync_called_
);
139 EXPECT_EQ(OneClickSigninSyncStarter::UNDO_SYNC
, mode_
);
142 TEST_F(OneClickSigninBubbleViewTest
, BubbleOkButton
) {
143 OneClickSigninBubbleView
* view
=
144 ShowOneClickSigninBubble(
145 BrowserWindow::ONE_CLICK_SIGNIN_BUBBLE_TYPE_BUBBLE
);
147 // Simulate pressing the OK button. Set the message loop in the bubble
148 // view so that it can be quit once the bubble is hidden.
149 views::ButtonListener
* listener
= view
;
150 const ui::MouseEvent
event(ui::ET_MOUSE_PRESSED
, gfx::Point(), gfx::Point(),
151 ui::EventTimeForNow(), 0, 0);
152 listener
->ButtonPressed(view
->ok_button_
, event
);
154 // View should no longer be showing. The message loop will exit once the
155 // fade animation of the bubble is done.
156 content::RunAllPendingInMessageLoop();
157 EXPECT_FALSE(OneClickSigninBubbleView::IsShowing());
160 TEST_F(OneClickSigninBubbleViewTest
, DialogOkButton
) {
161 OneClickSigninBubbleView
* view
= ShowOneClickSigninBubble(
162 BrowserWindow::ONE_CLICK_SIGNIN_BUBBLE_TYPE_MODAL_DIALOG
);
164 // Simulate pressing the OK button. Set the message loop in the bubble
165 // view so that it can be quit once the bubble is hidden.
166 views::ButtonListener
* listener
= view
;
167 const ui::MouseEvent
event(ui::ET_MOUSE_PRESSED
, gfx::Point(), gfx::Point(),
168 ui::EventTimeForNow(), 0, 0);
169 listener
->ButtonPressed(view
->ok_button_
, event
);
171 // View should no longer be showing and sync should start
172 // The message loop will exit once the fade animation of the dialog is done.
173 content::RunAllPendingInMessageLoop();
174 EXPECT_FALSE(OneClickSigninBubbleView::IsShowing());
175 EXPECT_TRUE(on_start_sync_called_
);
176 EXPECT_EQ(OneClickSigninSyncStarter::SYNC_WITH_DEFAULT_SETTINGS
, mode_
);
179 TEST_F(OneClickSigninBubbleViewTest
, DialogUndoButton
) {
180 OneClickSigninBubbleView
* view
= ShowOneClickSigninBubble(
181 BrowserWindow::ONE_CLICK_SIGNIN_BUBBLE_TYPE_MODAL_DIALOG
);
183 // Simulate pressing the undo button. Set the message loop in the bubble
184 // view so that it can be quit once the bubble is hidden.
185 views::ButtonListener
* listener
= view
;
186 const ui::MouseEvent
event(ui::ET_MOUSE_PRESSED
, gfx::Point(), gfx::Point(),
187 ui::EventTimeForNow(), 0, 0);
188 listener
->ButtonPressed(view
->undo_button_
, event
);
190 // View should no longer be showing. The message loop will exit once the
191 // fade animation of the bubble is done.
192 content::RunAllPendingInMessageLoop();
193 EXPECT_FALSE(OneClickSigninBubbleView::IsShowing());
194 EXPECT_TRUE(on_start_sync_called_
);
195 EXPECT_EQ(OneClickSigninSyncStarter::UNDO_SYNC
, mode_
);
198 TEST_F(OneClickSigninBubbleViewTest
, BubbleAdvancedLink
) {
199 OneClickSigninBubbleView
* view
= ShowOneClickSigninBubble(
200 BrowserWindow::ONE_CLICK_SIGNIN_BUBBLE_TYPE_BUBBLE
);
202 // Simulate pressing a link in the bubble.
203 views::LinkListener
* listener
= view
;
204 listener
->LinkClicked(view
->advanced_link_
, 0);
206 // View should no longer be showing and the OnAdvancedLinkClicked method
207 // of the delegate should have been called.
208 content::RunAllPendingInMessageLoop();
209 EXPECT_FALSE(OneClickSigninBubbleView::IsShowing());
210 EXPECT_EQ(1, advanced_click_count_
);
213 TEST_F(OneClickSigninBubbleViewTest
, DialogAdvancedLink
) {
214 OneClickSigninBubbleView
* view
= ShowOneClickSigninBubble(
215 BrowserWindow::ONE_CLICK_SIGNIN_BUBBLE_TYPE_MODAL_DIALOG
);
217 // Simulate pressing a link in the bubble.
218 views::LinkListener
* listener
= view
;
219 listener
->LinkClicked(view
->advanced_link_
, 0);
221 // View should no longer be showing. No delegate method should have been
222 // called: the callback is responsible to open the settings page.
223 content::RunAllPendingInMessageLoop();
224 EXPECT_TRUE(on_start_sync_called_
);
225 EXPECT_EQ(OneClickSigninSyncStarter::CONFIGURE_SYNC_FIRST
, mode_
);
226 EXPECT_FALSE(OneClickSigninBubbleView::IsShowing());
227 EXPECT_EQ(0, advanced_click_count_
);
230 TEST_F(OneClickSigninBubbleViewTest
, BubbleLearnMoreLink
) {
231 OneClickSigninBubbleView
* view
= ShowOneClickSigninBubble(
232 BrowserWindow::ONE_CLICK_SIGNIN_BUBBLE_TYPE_BUBBLE
);
234 views::LinkListener
* listener
= view
;
235 listener
->LinkClicked(view
->learn_more_link_
, 0);
237 // View should no longer be showing and the OnLearnMoreLinkClicked method
238 // of the delegate should have been called with |is_dialog| == false.
239 content::RunAllPendingInMessageLoop();
240 EXPECT_FALSE(OneClickSigninBubbleView::IsShowing());
241 EXPECT_EQ(1, bubble_learn_more_click_count_
);
242 EXPECT_EQ(0, dialog_learn_more_click_count_
);
245 TEST_F(OneClickSigninBubbleViewTest
, DialogLearnMoreLink
) {
246 OneClickSigninBubbleView
* view
= ShowOneClickSigninBubble(
247 BrowserWindow::ONE_CLICK_SIGNIN_BUBBLE_TYPE_MODAL_DIALOG
);
249 views::LinkListener
* listener
= view
;
250 listener
->LinkClicked(view
->learn_more_link_
, 0);
252 // View should still be showing and the OnLearnMoreLinkClicked method
253 // of the delegate should have been called with |is_dialog| == true.
254 content::RunAllPendingInMessageLoop();
255 EXPECT_TRUE(OneClickSigninBubbleView::IsShowing());
256 EXPECT_EQ(0, bubble_learn_more_click_count_
);
257 EXPECT_EQ(1, dialog_learn_more_click_count_
);
260 TEST_F(OneClickSigninBubbleViewTest
, BubblePressEnterKey
) {
261 OneClickSigninBubbleView
* one_click_view
= ShowOneClickSigninBubble(
262 BrowserWindow::ONE_CLICK_SIGNIN_BUBBLE_TYPE_BUBBLE
);
264 // Simulate pressing the Enter key.
265 views::View
* view
= one_click_view
;
266 const ui::Accelerator
accelerator(ui::VKEY_RETURN
, 0);
267 view
->AcceleratorPressed(accelerator
);
269 // View should no longer be showing. The message loop will exit once the
270 // fade animation of the bubble is done.
271 content::RunAllPendingInMessageLoop();
272 EXPECT_FALSE(OneClickSigninBubbleView::IsShowing());
275 TEST_F(OneClickSigninBubbleViewTest
, DialogPressEnterKey
) {
276 OneClickSigninBubbleView
* one_click_view
= ShowOneClickSigninBubble(
277 BrowserWindow::ONE_CLICK_SIGNIN_BUBBLE_TYPE_MODAL_DIALOG
);
279 // Simulate pressing the Enter key.
280 views::View
* view
= one_click_view
;
281 const ui::Accelerator
accelerator(ui::VKEY_RETURN
, 0);
282 view
->AcceleratorPressed(accelerator
);
284 // View should no longer be showing. The message loop will exit once the
285 // fade animation of the bubble is done.
286 content::RunAllPendingInMessageLoop();
287 EXPECT_FALSE(OneClickSigninBubbleView::IsShowing());
288 EXPECT_TRUE(on_start_sync_called_
);
289 EXPECT_EQ(OneClickSigninSyncStarter::SYNC_WITH_DEFAULT_SETTINGS
, mode_
);
292 TEST_F(OneClickSigninBubbleViewTest
, BubblePressEscapeKey
) {
293 OneClickSigninBubbleView
* one_click_view
= ShowOneClickSigninBubble(
294 BrowserWindow::ONE_CLICK_SIGNIN_BUBBLE_TYPE_BUBBLE
);
296 // Simulate pressing the Escape key.
297 views::View
* view
= one_click_view
;
298 const ui::Accelerator
accelerator(ui::VKEY_ESCAPE
, 0);
299 view
->AcceleratorPressed(accelerator
);
301 // View should no longer be showing. The message loop will exit once the
302 // fade animation of the bubble is done.
303 content::RunAllPendingInMessageLoop();
304 EXPECT_FALSE(OneClickSigninBubbleView::IsShowing());
307 TEST_F(OneClickSigninBubbleViewTest
, DialogPressEscapeKey
) {
308 OneClickSigninBubbleView
* one_click_view
= ShowOneClickSigninBubble(
309 BrowserWindow::ONE_CLICK_SIGNIN_BUBBLE_TYPE_MODAL_DIALOG
);
311 // Simulate pressing the Escape key.
312 views::View
* view
= one_click_view
;
313 const ui::Accelerator
accelerator(ui::VKEY_ESCAPE
, 0);
314 view
->AcceleratorPressed(accelerator
);
316 // View should no longer be showing. The message loop will exit once the
317 // fade animation of the bubble is done.
318 content::RunAllPendingInMessageLoop();
319 EXPECT_FALSE(OneClickSigninBubbleView::IsShowing());
320 EXPECT_TRUE(on_start_sync_called_
);
321 EXPECT_EQ(OneClickSigninSyncStarter::UNDO_SYNC
, mode_
);