Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / ash / shelf / shelf_button_pressed_metric_tracker_unittest.cc
blobba2f3b13a7621c2ae60add769438f51986a8180e
1 // Copyright 2015 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 "ash/shelf/shelf_button_pressed_metric_tracker.h"
7 #include "ash/shelf/shelf.h"
8 #include "ash/test/ash_test_base.h"
9 #include "ash/test/shelf_button_pressed_metric_tracker_test_api.h"
10 #include "ash/test/shelf_test_api.h"
11 #include "ash/test/shelf_view_test_api.h"
12 #include "base/macros.h"
13 #include "base/test/histogram_tester.h"
14 #include "base/test/simple_test_tick_clock.h"
15 #include "base/test/user_action_tester.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "ui/events/event.h"
18 #include "ui/views/controls/button/button.h"
20 namespace ash {
21 namespace test {
22 namespace {
24 // A simple light weight test double dummy for a views::Button.
25 class DummyButton : public views::Button {
26 public:
27 DummyButton();
29 private:
30 DISALLOW_COPY_AND_ASSIGN(DummyButton);
33 DummyButton::DummyButton() : views::Button(nullptr) {
36 // A simple light weight test double dummy for a ui::Event.
37 class DummyEvent : public ui::Event {
38 public:
39 DummyEvent();
40 ~DummyEvent() override;
41 int unique_id() const { return unique_id_; }
43 private:
44 static int next_unique_id_;
45 int unique_id_;
47 DISALLOW_COPY_AND_ASSIGN(DummyEvent);
50 int DummyEvent::next_unique_id_ = 0;
52 DummyEvent::DummyEvent()
53 : Event(ui::ET_GESTURE_TAP, base::TimeDelta(), 0),
54 unique_id_(next_unique_id_++) {
57 DummyEvent::~DummyEvent() {
60 // Test fixture for the ShelfButtonPressedMetricTracker class. Relies on
61 // AshTestBase to initilize the UserMetricsRecorder and it's dependencies.
62 class ShelfButtonPressedMetricTrackerTest : public AshTestBase {
63 public:
64 static const char*
65 kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName;
67 ShelfButtonPressedMetricTrackerTest();
68 ~ShelfButtonPressedMetricTrackerTest() override;
70 // AshTestBase:
71 void SetUp() override;
72 void TearDown() override;
74 // Calls ButtonPressed on the test target with the given |event|
75 // and dummy values for the |sender| and |performed_action| parameters.
76 void ButtonPressed(const ui::Event& event);
78 // Calls ButtonPressed on the test target with the given |performed_action|
79 // and dummy values for the |event| and |sender| parameters.
80 void ButtonPressed(ShelfItemDelegate::PerformedAction performed_action);
82 // Calls ButtonPressed on the test target with the given |sender| and
83 // |performed_action| and a dummy value for the |event| parameter.
84 void ButtonPressed(const views::Button* sender,
85 ShelfItemDelegate::PerformedAction performed_action);
87 protected:
88 // The test target. Not owned.
89 ShelfButtonPressedMetricTracker* metric_tracker_;
91 // The TickClock injected in to the test target.
92 base::SimpleTestTickClock* tick_clock_;
94 private:
95 DISALLOW_COPY_AND_ASSIGN(ShelfButtonPressedMetricTrackerTest);
98 const char* ShelfButtonPressedMetricTrackerTest::
99 kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName =
100 ShelfButtonPressedMetricTracker::
101 kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName;
103 ShelfButtonPressedMetricTrackerTest::ShelfButtonPressedMetricTrackerTest() {
106 ShelfButtonPressedMetricTrackerTest::~ShelfButtonPressedMetricTrackerTest() {
109 void ShelfButtonPressedMetricTrackerTest::SetUp() {
110 AshTestBase::SetUp();
112 Shelf* shelf = Shelf::ForPrimaryDisplay();
113 ShelfViewTestAPI shelf_view_test_api(ShelfTestAPI(shelf).shelf_view());
115 metric_tracker_ = shelf_view_test_api.shelf_button_pressed_metric_tracker();
117 ShelfButtonPressedMetricTrackerTestAPI test_api(metric_tracker_);
119 scoped_ptr<base::TickClock> test_tick_clock(new base::SimpleTestTickClock());
120 tick_clock_ = static_cast<base::SimpleTestTickClock*>(test_tick_clock.get());
121 test_api.SetTickClock(test_tick_clock.Pass());
123 // Ensure the TickClock->NowTicks() doesn't return base::TimeTicks because
124 // ShelfButtonPressedMetricTracker interprets that value as unset.
125 tick_clock_->Advance(base::TimeDelta::FromMilliseconds(100));
128 void ShelfButtonPressedMetricTrackerTest::TearDown() {
129 tick_clock_ = nullptr;
131 AshTestBase::TearDown();
134 void ShelfButtonPressedMetricTrackerTest::ButtonPressed(
135 const ui::Event& event) {
136 const DummyButton kDummyButton;
137 metric_tracker_->ButtonPressed(event, &kDummyButton,
138 ShelfItemDelegate::kNoAction);
141 void ShelfButtonPressedMetricTrackerTest::ButtonPressed(
142 ShelfItemDelegate::PerformedAction performed_action) {
143 const DummyEvent kDummyEvent;
144 const DummyButton kDummyButton;
145 metric_tracker_->ButtonPressed(kDummyEvent, &kDummyButton, performed_action);
148 void ShelfButtonPressedMetricTrackerTest::ButtonPressed(
149 const views::Button* sender,
150 ShelfItemDelegate::PerformedAction performed_action) {
151 const DummyEvent kDummyEvent;
152 metric_tracker_->ButtonPressed(kDummyEvent, sender, performed_action);
155 } // namespace
157 // Verifies that a Launcher_ButtonPressed_Mouse UMA user action is recorded when
158 // a button is pressed by a mouse event.
159 TEST_F(ShelfButtonPressedMetricTrackerTest,
160 Launcher_ButtonPressed_MouseIsRecordedWhenIconActivatedByMouse) {
161 const ui::MouseEvent mouse_event(ui::ET_MOUSE_PRESSED, gfx::Point(),
162 gfx::Point(), base::TimeDelta(), 0, 0);
164 base::UserActionTester user_action_tester;
165 ButtonPressed(mouse_event);
166 EXPECT_EQ(1,
167 user_action_tester.GetActionCount("Launcher_ButtonPressed_Mouse"));
170 // Verifies that a Launcher_ButtonPressed_Touch UMA user action is recorded when
171 // a button is pressed by a touch event.
172 TEST_F(ShelfButtonPressedMetricTrackerTest,
173 Launcher_ButtonPressed_MouseIsRecordedWhenIconActivatedByTouch) {
174 const ui::TouchEvent touch_event(ui::ET_GESTURE_TAP, gfx::Point(), 0,
175 base::TimeDelta());
177 base::UserActionTester user_action_tester;
178 ButtonPressed(touch_event);
179 EXPECT_EQ(1,
180 user_action_tester.GetActionCount("Launcher_ButtonPressed_Touch"));
183 // Verifies that a Launcher_LaunchTask UMA user action is recorded when
184 // pressing a button causes a new window to be created.
185 TEST_F(ShelfButtonPressedMetricTrackerTest,
186 Launcher_LaunchTaskIsRecordedWhenNewWindowIsCreated) {
187 base::UserActionTester user_action_tester;
188 ButtonPressed(ShelfItemDelegate::kNewWindowCreated);
189 EXPECT_EQ(1, user_action_tester.GetActionCount("Launcher_LaunchTask"));
192 // Verifies that a Launcher_MinimizeTask UMA user action is recorded when
193 // pressing a button causes an existing window to be minimized.
194 TEST_F(ShelfButtonPressedMetricTrackerTest,
195 Launcher_MinimizeTaskIsRecordedWhenWindowIsMinimized) {
196 base::UserActionTester user_action_tester;
197 ButtonPressed(ShelfItemDelegate::kExistingWindowMinimized);
198 EXPECT_EQ(1, user_action_tester.GetActionCount("Launcher_MinimizeTask"));
201 // Verifies that a Launcher_SwitchTask UMA user action is recorded when
202 // pressing a button causes an existing window to be activated.
203 TEST_F(ShelfButtonPressedMetricTrackerTest,
204 Launcher_SwitchTaskIsRecordedWhenExistingWindowIsActivated) {
205 base::UserActionTester user_action_tester;
206 ButtonPressed(ShelfItemDelegate::kExistingWindowActivated);
207 EXPECT_EQ(1, user_action_tester.GetActionCount("Launcher_SwitchTask"));
210 // Verify that a window activation action will record a data point if it was
211 // subsequent to a minimize action.
212 TEST_F(ShelfButtonPressedMetricTrackerTest,
213 VerifyDataRecordedAfterMinimizedAndSubsequentActivatedAction) {
214 const DummyButton kDummyButton;
216 base::HistogramTester histogram_tester;
218 ButtonPressed(&kDummyButton, ShelfItemDelegate::kExistingWindowMinimized);
219 histogram_tester.ExpectTotalCount(
220 kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName, 0);
222 ButtonPressed(&kDummyButton, ShelfItemDelegate::kExistingWindowActivated);
223 histogram_tester.ExpectTotalCount(
224 kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName, 1);
227 // Verify that a multiple window activation actions will record a single data
228 // point if they are subsequent to a minimize action.
229 TEST_F(ShelfButtonPressedMetricTrackerTest,
230 VerifyDataRecordedAfterMinimizedAndMultipleSubsequentActivatedActions) {
231 const DummyButton kDummyButton;
233 base::HistogramTester histogram_tester;
235 ButtonPressed(&kDummyButton, ShelfItemDelegate::kExistingWindowMinimized);
236 ButtonPressed(&kDummyButton, ShelfItemDelegate::kExistingWindowActivated);
237 ButtonPressed(&kDummyButton, ShelfItemDelegate::kExistingWindowActivated);
239 histogram_tester.ExpectTotalCount(
240 kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName, 1);
243 // Verify that a window activation action will not record a data point if it was
244 // not subsequent to a minimize action.
245 TEST_F(ShelfButtonPressedMetricTrackerTest,
246 VerifyDataRecordedAfterMinimizedAndNonSubsequentActivatedAction) {
247 const DummyButton kDummyButton;
249 base::HistogramTester histogram_tester;
251 ButtonPressed(&kDummyButton, ShelfItemDelegate::kExistingWindowMinimized);
252 ButtonPressed(&kDummyButton, ShelfItemDelegate::kAppListMenuShown);
253 ButtonPressed(&kDummyButton, ShelfItemDelegate::kExistingWindowActivated);
255 histogram_tester.ExpectTotalCount(
256 kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName, 0);
259 // Verify no data is recorded if a second source button is pressed in between
260 // subsequent minimized and activated actions on the same source.
261 TEST_F(ShelfButtonPressedMetricTrackerTest,
262 VerifyDataRecordedAfterMinimizedButtonA) {
263 const DummyButton kDummyButton;
264 const DummyButton kSecondDummyButton;
266 base::HistogramTester histogram_tester;
268 ButtonPressed(&kDummyButton, ShelfItemDelegate::kExistingWindowMinimized);
269 ButtonPressed(&kSecondDummyButton,
270 ShelfItemDelegate::kExistingWindowMinimized);
271 ButtonPressed(&kDummyButton, ShelfItemDelegate::kExistingWindowActivated);
273 histogram_tester.ExpectTotalCount(
274 kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName, 0);
277 // Verify the data value recorded when a window activation action is subsequent
278 // to a minimize action.
279 TEST_F(ShelfButtonPressedMetricTrackerTest,
280 VerifyTheValueRecordedBySubsequentMinimizedAndActivateActions) {
281 const int kTimeDeltaInMilliseconds = 17;
282 const DummyButton kDummyButton;
284 base::HistogramTester histogram_tester;
286 ButtonPressed(&kDummyButton, ShelfItemDelegate::kExistingWindowMinimized);
287 tick_clock_->Advance(
288 base::TimeDelta::FromMilliseconds(kTimeDeltaInMilliseconds));
289 ButtonPressed(&kDummyButton, ShelfItemDelegate::kExistingWindowActivated);
291 histogram_tester.ExpectTotalCount(
292 kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName, 1);
293 histogram_tester.ExpectBucketCount(
294 kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName,
295 kTimeDeltaInMilliseconds, 1);
298 } // namespace test
299 } // namespace ash