Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / views / toolbar / reload_button_unittest.cc
blob87579af18f913baae1ea9849a8f6a0b8910f9a94
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 "base/message_loop/message_loop.h"
6 #include "chrome/browser/ui/views/toolbar/reload_button.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/events/event_utils.h"
10 class ReloadButtonTest : public testing::Test {
11 public:
12 ReloadButtonTest();
14 void CheckState(bool enabled,
15 ReloadButton::Mode intended_mode,
16 ReloadButton::Mode visible_mode,
17 bool double_click_timer_running,
18 bool stop_to_reload_timer_running);
20 // These accessors eliminate the need to declare each testcase as a friend.
21 void set_mouse_hovered(bool hovered) {
22 reload_.testing_mouse_hovered_ = hovered;
24 int reload_count() { return reload_.testing_reload_count_; }
26 protected:
27 // We need a message loop for the timers to post events.
28 base::MessageLoop loop_;
30 ReloadButton reload_;
33 ReloadButtonTest::ReloadButtonTest() : reload_(NULL) {
34 // Set the timer delays to 0 so that timers will fire as soon as we tell the
35 // message loop to run pending tasks.
36 reload_.double_click_timer_delay_ = base::TimeDelta();
37 reload_.stop_to_reload_timer_delay_ = base::TimeDelta();
40 void ReloadButtonTest::CheckState(bool enabled,
41 ReloadButton::Mode intended_mode,
42 ReloadButton::Mode visible_mode,
43 bool double_click_timer_running,
44 bool stop_to_reload_timer_running) {
45 EXPECT_EQ(enabled, reload_.enabled());
46 EXPECT_EQ(intended_mode, reload_.intended_mode_);
47 EXPECT_EQ(visible_mode, reload_.visible_mode_);
48 EXPECT_EQ(double_click_timer_running,
49 reload_.double_click_timer_.IsRunning());
50 EXPECT_EQ(stop_to_reload_timer_running,
51 reload_.stop_to_reload_timer_.IsRunning());
54 TEST_F(ReloadButtonTest, Basic) {
55 // The stop/reload button starts in the "enabled reload" state with no timers
56 // running.
57 CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, false,
58 false);
60 // Press the button. This should start the double-click timer.
61 ui::MouseEvent e(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(),
62 ui::EventTimeForNow(), 0, 0);
63 reload_.ButtonPressed(&reload_, e);
64 CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, true,
65 false);
67 // Now change the mode (as if the browser had started loading the page). This
68 // should cancel the double-click timer since the button is not hovered.
69 reload_.ChangeMode(ReloadButton::MODE_STOP, false);
70 CheckState(true, ReloadButton::MODE_STOP, ReloadButton::MODE_STOP, false,
71 false);
73 // Press the button again. This should change back to reload.
74 reload_.ButtonPressed(&reload_, e);
75 CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, false,
76 false);
79 TEST_F(ReloadButtonTest, DoubleClickTimer) {
80 // Start by pressing the button.
81 ui::MouseEvent e(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(),
82 ui::EventTimeForNow(), 0, 0);
83 reload_.ButtonPressed(&reload_, e);
85 // Try to press the button again. This should do nothing because the timer is
86 // running.
87 int original_reload_count = reload_count();
88 reload_.ButtonPressed(&reload_, e);
89 CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, true,
90 false);
91 EXPECT_EQ(original_reload_count, reload_count());
93 // Hover the button, and change mode. The visible mode should not change,
94 // again because the timer is running.
95 set_mouse_hovered(true);
96 reload_.ChangeMode(ReloadButton::MODE_STOP, false);
97 CheckState(true, ReloadButton::MODE_STOP, ReloadButton::MODE_RELOAD, true,
98 false);
100 // Now fire the timer. This should complete the mode change.
101 loop_.RunUntilIdle();
102 CheckState(true, ReloadButton::MODE_STOP, ReloadButton::MODE_STOP, false,
103 false);
106 TEST_F(ReloadButtonTest, DisableOnHover) {
107 // Change to stop and hover.
108 ui::MouseEvent e(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(),
109 ui::EventTimeForNow(), 0, 0);
110 reload_.ButtonPressed(&reload_, e);
111 reload_.ChangeMode(ReloadButton::MODE_STOP, false);
112 set_mouse_hovered(true);
114 // Now change back to reload. This should result in a disabled stop button
115 // due to the hover.
116 reload_.ChangeMode(ReloadButton::MODE_RELOAD, false);
117 CheckState(false, ReloadButton::MODE_RELOAD, ReloadButton::MODE_STOP, false,
118 true);
120 // Un-hover the button, which should allow it to reset.
121 set_mouse_hovered(false);
122 ui::MouseEvent e2(ui::ET_MOUSE_MOVED, gfx::Point(), gfx::Point(),
123 ui::EventTimeForNow(), 0, 0);
124 reload_.OnMouseExited(e2);
125 CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, false,
126 false);
129 TEST_F(ReloadButtonTest, ResetOnClick) {
130 // Change to stop and hover.
131 ui::MouseEvent e(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(),
132 ui::EventTimeForNow(), 0, 0);
133 reload_.ButtonPressed(&reload_, e);
134 reload_.ChangeMode(ReloadButton::MODE_STOP, false);
135 set_mouse_hovered(true);
137 // Press the button. This should change back to reload despite the hover,
138 // because it's a direct user action.
139 reload_.ButtonPressed(&reload_, e);
140 CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, false,
141 false);
144 TEST_F(ReloadButtonTest, ResetOnTimer) {
145 // Change to stop, hover, and change back to reload.
146 ui::MouseEvent e(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(),
147 ui::EventTimeForNow(), 0, 0);
148 reload_.ButtonPressed(&reload_, e);
149 reload_.ChangeMode(ReloadButton::MODE_STOP, false);
150 set_mouse_hovered(true);
151 reload_.ChangeMode(ReloadButton::MODE_RELOAD, false);
153 // Now fire the stop-to-reload timer. This should reset the button.
154 loop_.RunUntilIdle();
155 CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, false,
156 false);