File: reload_button_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (244 lines) | stat: -rw-r--r-- 10,270 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/toolbar/reload_button.h"

#include "base/run_loop.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/views/chrome_test_views_delegate.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/events/event_utils.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/test/button_test_api.h"

class ReloadButtonTest : public ChromeRenderViewHostTestHarness {
 public:
  ReloadButtonTest();

  ReloadButtonTest(const ReloadButtonTest&) = delete;
  ReloadButtonTest& operator=(const ReloadButtonTest&) = delete;

  void CheckState(bool enabled,
                  ReloadButton::Mode intended_mode,
                  ReloadButton::Mode visible_mode,
                  bool double_click_timer_running,
                  bool mode_switch_timer_running);

  // These accessors eliminate the need to declare each testcase as a friend.
  void set_mouse_hovered(bool hovered) {
    reload_.testing_mouse_hovered_ = hovered;
  }
  int reload_count() { return reload_.testing_reload_count_; }

 protected:
  ReloadButton* reload() { return &reload_; }

 private:
  ChromeTestViewsDelegate<> views_delegate_;
  ReloadButton reload_;
};

ReloadButtonTest::ReloadButtonTest() : reload_(nullptr) {
  // Set the timer delays to 0 so that timers will fire as soon as we tell the
  // message loop to run pending tasks.
  reload_.double_click_timer_delay_ = base::TimeDelta();
  reload_.mode_switch_timer_delay_ = base::TimeDelta();
}

void ReloadButtonTest::CheckState(bool enabled,
                                  ReloadButton::Mode intended_mode,
                                  ReloadButton::Mode visible_mode,
                                  bool double_click_timer_running,
                                  bool mode_switch_timer_running) {
  EXPECT_EQ(enabled, reload_.GetEnabled());
  EXPECT_EQ(intended_mode, reload_.intended_mode_);
  EXPECT_EQ(visible_mode, reload_.visible_mode_);
  EXPECT_EQ(double_click_timer_running,
            reload_.double_click_timer_.IsRunning());
  EXPECT_EQ(mode_switch_timer_running, reload_.mode_switch_timer_.IsRunning());
}

TEST_F(ReloadButtonTest, Basic) {
  // The stop/reload button starts in the "enabled reload" state with no timers
  // running.
  CheckState(true, ReloadButton::Mode::kReload, ReloadButton::Mode::kReload,
             false, false);

  // Press the button.  This should start the double-click timer.
  ui::MouseEvent e(ui::EventType::kMousePressed, gfx::Point(), gfx::Point(),
                   ui::EventTimeForNow(), 0, 0);
  views::test::ButtonTestApi test_api(reload());
  test_api.NotifyClick(e);
  CheckState(true, ReloadButton::Mode::kReload, ReloadButton::Mode::kReload,
             true, false);

  // Now change the mode (as if the browser had started loading the page).  This
  // should cancel the double-click timer since the button is not hovered.
  reload()->ChangeMode(ReloadButton::Mode::kStop, false);
  CheckState(true, ReloadButton::Mode::kStop, ReloadButton::Mode::kStop, false,
             false);

  // Press the button again.  This should change back to reload.
  test_api.NotifyClick(e);
  CheckState(true, ReloadButton::Mode::kReload, ReloadButton::Mode::kReload,
             false, false);
}

TEST_F(ReloadButtonTest, DoubleClickTimer) {
  // Start by pressing the button.
  ui::MouseEvent e(ui::EventType::kMousePressed, gfx::Point(), gfx::Point(),
                   ui::EventTimeForNow(), 0, 0);
  views::test::ButtonTestApi test_api(reload());
  test_api.NotifyClick(e);

  // Try to press the button again.  This should do nothing because the timer is
  // running.
  int original_reload_count = reload_count();
  test_api.NotifyClick(e);
  CheckState(true, ReloadButton::Mode::kReload, ReloadButton::Mode::kReload,
             true, false);
  EXPECT_EQ(original_reload_count, reload_count());

  // Hover the button, and change mode.  The visible mode should not change,
  // again because the timer is running.
  set_mouse_hovered(true);
  reload()->ChangeMode(ReloadButton::Mode::kStop, false);
  CheckState(true, ReloadButton::Mode::kStop, ReloadButton::Mode::kReload, true,
             false);

  // Now fire the timer.  This should complete the mode change.
  base::RunLoop().RunUntilIdle();
  CheckState(true, ReloadButton::Mode::kStop, ReloadButton::Mode::kStop, false,
             false);
}

TEST_F(ReloadButtonTest, DisableOnHover) {
  // Change to stop and hover.
  ui::MouseEvent e(ui::EventType::kMousePressed, gfx::Point(), gfx::Point(),
                   ui::EventTimeForNow(), 0, 0);
  views::test::ButtonTestApi(reload()).NotifyClick(e);
  reload()->ChangeMode(ReloadButton::Mode::kStop, false);
  set_mouse_hovered(true);

  // Now change back to reload.  This should result in a disabled stop button
  // due to the hover.
  reload()->ChangeMode(ReloadButton::Mode::kReload, false);
  CheckState(false, ReloadButton::Mode::kReload, ReloadButton::Mode::kStop,
             false, true);

  // Un-hover the button, which should allow it to reset.
  set_mouse_hovered(false);
  ui::MouseEvent e2(ui::EventType::kMouseMoved, gfx::Point(), gfx::Point(),
                    ui::EventTimeForNow(), 0, 0);
  reload()->OnMouseExited(e2);
  CheckState(true, ReloadButton::Mode::kReload, ReloadButton::Mode::kReload,
             false, false);
}

TEST_F(ReloadButtonTest, ResetOnClick) {
  // Change to stop and hover.
  ui::MouseEvent e(ui::EventType::kMousePressed, gfx::Point(), gfx::Point(),
                   ui::EventTimeForNow(), 0, 0);
  views::test::ButtonTestApi test_api(reload());
  test_api.NotifyClick(e);
  reload()->ChangeMode(ReloadButton::Mode::kStop, false);
  set_mouse_hovered(true);

  // Press the button.  This should change back to reload despite the hover,
  // because it's a direct user action.
  test_api.NotifyClick(e);
  CheckState(true, ReloadButton::Mode::kReload, ReloadButton::Mode::kReload,
             false, false);
}

TEST_F(ReloadButtonTest, ResetOnTimer) {
  // Change to stop, hover, and change back to reload.
  ui::MouseEvent e(ui::EventType::kMousePressed, gfx::Point(), gfx::Point(),
                   ui::EventTimeForNow(), 0, 0);
  views::test::ButtonTestApi(reload()).NotifyClick(e);
  reload()->ChangeMode(ReloadButton::Mode::kStop, false);
  set_mouse_hovered(true);
  reload()->ChangeMode(ReloadButton::Mode::kReload, false);

  // Now fire the stop-to-reload timer.  This should reset the button.
  base::RunLoop().RunUntilIdle();
  CheckState(true, ReloadButton::Mode::kReload, ReloadButton::Mode::kReload,
             false, false);
}

TEST_F(ReloadButtonTest, AccessibleHasPopup) {
  ui::AXNodeData button_data;

  button_data = ui::AXNodeData();
  reload()->GetViewAccessibility().GetAccessibleNodeData(&button_data);
  EXPECT_FALSE(reload()->GetMenuEnabled());
  EXPECT_EQ(ax::mojom::HasPopup::kNone, button_data.GetHasPopup());

  button_data = ui::AXNodeData();
  reload()->SetMenuEnabled(true);
  reload()->GetViewAccessibility().GetAccessibleNodeData(&button_data);
  EXPECT_TRUE(reload()->GetMenuEnabled());
  EXPECT_EQ(ax::mojom::HasPopup::kMenu, button_data.GetHasPopup());
}

TEST_F(ReloadButtonTest, TooltipText) {
  reload()->SetVisibleMode(ReloadButton::Mode::kReload);
  EXPECT_FALSE(reload()->GetMenuEnabled());
  EXPECT_EQ(reload()->GetRenderedTooltipText(gfx::Point()),
            l10n_util::GetStringUTF16(IDS_TOOLTIP_RELOAD));
  reload()->SetVisibleMode(ReloadButton::Mode::kStop);
  EXPECT_EQ(reload()->GetRenderedTooltipText(gfx::Point()),
            l10n_util::GetStringUTF16(IDS_TOOLTIP_STOP));

  reload()->SetMenuEnabled(true);
  reload()->SetVisibleMode(ReloadButton::Mode::kReload);
  EXPECT_TRUE(reload()->GetMenuEnabled());
  EXPECT_EQ(reload()->GetRenderedTooltipText(gfx::Point()),
            l10n_util::GetStringUTF16(IDS_TOOLTIP_RELOAD_WITH_MENU));
  reload()->SetVisibleMode(ReloadButton::Mode::kStop);
  EXPECT_EQ(reload()->GetRenderedTooltipText(gfx::Point()),
            l10n_util::GetStringUTF16(IDS_TOOLTIP_STOP));
}

TEST_F(ReloadButtonTest, TooltipTextAccessibility) {
  ui::AXNodeData button_data;
  reload()->SetVisibleMode(ReloadButton::Mode::kReload);
  reload()->GetViewAccessibility().GetAccessibleNodeData(&button_data);
  EXPECT_FALSE(reload()->GetMenuEnabled());
  EXPECT_EQ(reload()->GetRenderedTooltipText(gfx::Point()),
            l10n_util::GetStringUTF16(IDS_TOOLTIP_RELOAD));
  EXPECT_EQ(button_data.GetString16Attribute(
                ax::mojom::StringAttribute::kDescription),
            reload()->GetRenderedTooltipText(gfx::Point()));
  button_data = ui::AXNodeData();
  reload()->SetVisibleMode(ReloadButton::Mode::kStop);
  reload()->GetViewAccessibility().GetAccessibleNodeData(&button_data);
  EXPECT_EQ(reload()->GetRenderedTooltipText(gfx::Point()),
            l10n_util::GetStringUTF16(IDS_TOOLTIP_STOP));
  EXPECT_EQ(button_data.GetString16Attribute(
                ax::mojom::StringAttribute::kDescription),
            reload()->GetRenderedTooltipText(gfx::Point()));
  button_data = ui::AXNodeData();

  reload()->SetMenuEnabled(true);
  reload()->SetVisibleMode(ReloadButton::Mode::kReload);
  reload()->GetViewAccessibility().GetAccessibleNodeData(&button_data);
  EXPECT_TRUE(reload()->GetMenuEnabled());
  EXPECT_EQ(reload()->GetRenderedTooltipText(gfx::Point()),
            l10n_util::GetStringUTF16(IDS_TOOLTIP_RELOAD_WITH_MENU));
  EXPECT_EQ(button_data.GetString16Attribute(
                ax::mojom::StringAttribute::kDescription),
            reload()->GetRenderedTooltipText(gfx::Point()));
  button_data = ui::AXNodeData();
  reload()->SetVisibleMode(ReloadButton::Mode::kStop);
  reload()->GetViewAccessibility().GetAccessibleNodeData(&button_data);
  EXPECT_EQ(reload()->GetRenderedTooltipText(gfx::Point()),
            l10n_util::GetStringUTF16(IDS_TOOLTIP_STOP));
  EXPECT_EQ(button_data.GetString16Attribute(
                ax::mojom::StringAttribute::kDescription),
            reload()->GetRenderedTooltipText(gfx::Point()));
}