[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / views / toolbar / reload_button_unittest.cc
blob3355ea659f978dcafbf6963ac667638e834700f2
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"
9 class ReloadButtonTest : public testing::Test {
10 public:
11 ReloadButtonTest();
13 void CheckState(bool enabled,
14 ReloadButton::Mode intended_mode,
15 ReloadButton::Mode visible_mode,
16 bool double_click_timer_running,
17 bool stop_to_reload_timer_running);
19 // These accessors eliminate the need to declare each testcase as a friend.
20 void set_mouse_hovered(bool hovered) {
21 reload_.testing_mouse_hovered_ = hovered;
23 int reload_count() { return reload_.testing_reload_count_; }
25 protected:
26 // We need a message loop for the timers to post events.
27 base::MessageLoop loop_;
29 ReloadButton reload_;
32 ReloadButtonTest::ReloadButtonTest() : reload_(NULL) {
33 // Set the timer delays to 0 so that timers will fire as soon as we tell the
34 // message loop to run pending tasks.
35 reload_.double_click_timer_delay_ = base::TimeDelta();
36 reload_.stop_to_reload_timer_delay_ = base::TimeDelta();
39 void ReloadButtonTest::CheckState(bool enabled,
40 ReloadButton::Mode intended_mode,
41 ReloadButton::Mode visible_mode,
42 bool double_click_timer_running,
43 bool stop_to_reload_timer_running) {
44 EXPECT_EQ(enabled, reload_.enabled());
45 EXPECT_EQ(intended_mode, reload_.intended_mode_);
46 EXPECT_EQ(visible_mode, reload_.visible_mode_);
47 EXPECT_EQ(double_click_timer_running,
48 reload_.double_click_timer_.IsRunning());
49 EXPECT_EQ(stop_to_reload_timer_running,
50 reload_.stop_to_reload_timer_.IsRunning());
53 TEST_F(ReloadButtonTest, Basic) {
54 // The stop/reload button starts in the "enabled reload" state with no timers
55 // running.
56 CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, false,
57 false);
59 // Press the button. This should start the double-click timer.
60 ui::MouseEvent e(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), 0, 0);
61 reload_.ButtonPressed(&reload_, e);
62 CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, true,
63 false);
65 // Now change the mode (as if the browser had started loading the page). This
66 // should cancel the double-click timer since the button is not hovered.
67 reload_.ChangeMode(ReloadButton::MODE_STOP, false);
68 CheckState(true, ReloadButton::MODE_STOP, ReloadButton::MODE_STOP, false,
69 false);
71 // Press the button again. This should change back to reload.
72 reload_.ButtonPressed(&reload_, e);
73 CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, false,
74 false);
77 TEST_F(ReloadButtonTest, DoubleClickTimer) {
78 // Start by pressing the button.
79 ui::MouseEvent e(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), 0, 0);
80 reload_.ButtonPressed(&reload_, e);
82 // Try to press the button again. This should do nothing because the timer is
83 // running.
84 int original_reload_count = reload_count();
85 reload_.ButtonPressed(&reload_, e);
86 CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, true,
87 false);
88 EXPECT_EQ(original_reload_count, reload_count());
90 // Hover the button, and change mode. The visible mode should not change,
91 // again because the timer is running.
92 set_mouse_hovered(true);
93 reload_.ChangeMode(ReloadButton::MODE_STOP, false);
94 CheckState(true, ReloadButton::MODE_STOP, ReloadButton::MODE_RELOAD, true,
95 false);
97 // Now fire the timer. This should complete the mode change.
98 loop_.RunUntilIdle();
99 CheckState(true, ReloadButton::MODE_STOP, ReloadButton::MODE_STOP, false,
100 false);
103 TEST_F(ReloadButtonTest, DisableOnHover) {
104 // Change to stop and hover.
105 ui::MouseEvent e(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), 0, 0);
106 reload_.ButtonPressed(&reload_, e);
107 reload_.ChangeMode(ReloadButton::MODE_STOP, false);
108 set_mouse_hovered(true);
110 // Now change back to reload. This should result in a disabled stop button
111 // due to the hover.
112 reload_.ChangeMode(ReloadButton::MODE_RELOAD, false);
113 CheckState(false, ReloadButton::MODE_RELOAD, ReloadButton::MODE_STOP, false,
114 true);
116 // Un-hover the button, which should allow it to reset.
117 set_mouse_hovered(false);
118 ui::MouseEvent e2(ui::ET_MOUSE_MOVED, gfx::Point(), gfx::Point(), 0, 0);
119 reload_.OnMouseExited(e2);
120 CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, false,
121 false);
124 TEST_F(ReloadButtonTest, ResetOnClick) {
125 // Change to stop and hover.
126 ui::MouseEvent e(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), 0, 0);
127 reload_.ButtonPressed(&reload_, e);
128 reload_.ChangeMode(ReloadButton::MODE_STOP, false);
129 set_mouse_hovered(true);
131 // Press the button. This should change back to reload despite the hover,
132 // because it's a direct user action.
133 reload_.ButtonPressed(&reload_, e);
134 CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, false,
135 false);
138 TEST_F(ReloadButtonTest, ResetOnTimer) {
139 // Change to stop, hover, and change back to reload.
140 ui::MouseEvent e(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), 0, 0);
141 reload_.ButtonPressed(&reload_, e);
142 reload_.ChangeMode(ReloadButton::MODE_STOP, false);
143 set_mouse_hovered(true);
144 reload_.ChangeMode(ReloadButton::MODE_RELOAD, false);
146 // Now fire the stop-to-reload timer. This should reset the button.
147 loop_.RunUntilIdle();
148 CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, false,
149 false);