| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "ash/common/shelf/shelf_button_pressed_metric_tracker.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "ash/common/shelf/wm_shelf.h" | |
| 10 #include "ash/common/wm_shell.h" | |
| 11 #include "ash/test/ash_test_base.h" | |
| 12 #include "ash/test/shelf_button_pressed_metric_tracker_test_api.h" | |
| 13 #include "ash/test/shelf_view_test_api.h" | |
| 14 #include "base/macros.h" | |
| 15 #include "base/test/histogram_tester.h" | |
| 16 #include "base/test/simple_test_tick_clock.h" | |
| 17 #include "base/test/user_action_tester.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 #include "ui/events/event.h" | |
| 20 #include "ui/views/controls/button/button.h" | |
| 21 | |
| 22 namespace ash { | |
| 23 namespace test { | |
| 24 namespace { | |
| 25 | |
| 26 // A simple light weight test double dummy for a views::Button. | |
| 27 class DummyButton : public views::Button { | |
| 28 public: | |
| 29 DummyButton(); | |
| 30 | |
| 31 private: | |
| 32 DISALLOW_COPY_AND_ASSIGN(DummyButton); | |
| 33 }; | |
| 34 | |
| 35 DummyButton::DummyButton() : views::Button(nullptr) {} | |
| 36 | |
| 37 // A simple light weight test double dummy for a ui::Event. | |
| 38 class DummyEvent : public ui::Event { | |
| 39 public: | |
| 40 DummyEvent(); | |
| 41 ~DummyEvent() override; | |
| 42 int unique_id() const { return unique_id_; } | |
| 43 | |
| 44 private: | |
| 45 static int next_unique_id_; | |
| 46 int unique_id_; | |
| 47 | |
| 48 DISALLOW_COPY_AND_ASSIGN(DummyEvent); | |
| 49 }; | |
| 50 | |
| 51 int DummyEvent::next_unique_id_ = 0; | |
| 52 | |
| 53 DummyEvent::DummyEvent() | |
| 54 : Event(ui::ET_GESTURE_TAP, base::TimeTicks(), 0), | |
| 55 unique_id_(next_unique_id_++) {} | |
| 56 | |
| 57 DummyEvent::~DummyEvent() {} | |
| 58 | |
| 59 // Test fixture for the ShelfButtonPressedMetricTracker class. Relies on | |
| 60 // AshTestBase to initilize the UserMetricsRecorder and it's dependencies. | |
| 61 class ShelfButtonPressedMetricTrackerTest : public AshTestBase { | |
| 62 public: | |
| 63 static const char* | |
| 64 kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName; | |
| 65 | |
| 66 ShelfButtonPressedMetricTrackerTest(); | |
| 67 ~ShelfButtonPressedMetricTrackerTest() override; | |
| 68 | |
| 69 // AshTestBase: | |
| 70 void SetUp() override; | |
| 71 void TearDown() override; | |
| 72 | |
| 73 // Calls ButtonPressed on the test target with the given |event| | |
| 74 // and dummy values for the |sender| and |performed_action| parameters. | |
| 75 void ButtonPressed(const ui::Event& event); | |
| 76 | |
| 77 // Calls ButtonPressed on the test target with the given |performed_action| | |
| 78 // and dummy values for the |event| and |sender| parameters. | |
| 79 void ButtonPressed(ShelfAction performed_action); | |
| 80 | |
| 81 // Calls ButtonPressed on the test target with the given |sender| and | |
| 82 // |performed_action| and a dummy value for the |event| parameter. | |
| 83 void ButtonPressed(const views::Button* sender, ShelfAction performed_action); | |
| 84 | |
| 85 protected: | |
| 86 // The test target. Not owned. | |
| 87 ShelfButtonPressedMetricTracker* metric_tracker_; | |
| 88 | |
| 89 // The TickClock injected in to the test target. | |
| 90 base::SimpleTestTickClock* tick_clock_; | |
| 91 | |
| 92 private: | |
| 93 DISALLOW_COPY_AND_ASSIGN(ShelfButtonPressedMetricTrackerTest); | |
| 94 }; | |
| 95 | |
| 96 const char* ShelfButtonPressedMetricTrackerTest:: | |
| 97 kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName = | |
| 98 ShelfButtonPressedMetricTracker:: | |
| 99 kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName; | |
| 100 | |
| 101 ShelfButtonPressedMetricTrackerTest::ShelfButtonPressedMetricTrackerTest() {} | |
| 102 | |
| 103 ShelfButtonPressedMetricTrackerTest::~ShelfButtonPressedMetricTrackerTest() {} | |
| 104 | |
| 105 void ShelfButtonPressedMetricTrackerTest::SetUp() { | |
| 106 AshTestBase::SetUp(); | |
| 107 | |
| 108 WmShelf* wm_shelf = GetPrimaryShelf(); | |
| 109 ShelfViewTestAPI shelf_view_test_api(wm_shelf->GetShelfViewForTesting()); | |
| 110 | |
| 111 metric_tracker_ = shelf_view_test_api.shelf_button_pressed_metric_tracker(); | |
| 112 | |
| 113 ShelfButtonPressedMetricTrackerTestAPI test_api(metric_tracker_); | |
| 114 | |
| 115 std::unique_ptr<base::TickClock> test_tick_clock( | |
| 116 new base::SimpleTestTickClock()); | |
| 117 tick_clock_ = static_cast<base::SimpleTestTickClock*>(test_tick_clock.get()); | |
| 118 test_api.SetTickClock(std::move(test_tick_clock)); | |
| 119 | |
| 120 // Ensure the TickClock->NowTicks() doesn't return base::TimeTicks because | |
| 121 // ShelfButtonPressedMetricTracker interprets that value as unset. | |
| 122 tick_clock_->Advance(base::TimeDelta::FromMilliseconds(100)); | |
| 123 } | |
| 124 | |
| 125 void ShelfButtonPressedMetricTrackerTest::TearDown() { | |
| 126 tick_clock_ = nullptr; | |
| 127 | |
| 128 AshTestBase::TearDown(); | |
| 129 } | |
| 130 | |
| 131 void ShelfButtonPressedMetricTrackerTest::ButtonPressed( | |
| 132 const ui::Event& event) { | |
| 133 const DummyButton kDummyButton; | |
| 134 metric_tracker_->ButtonPressed(event, &kDummyButton, SHELF_ACTION_NONE); | |
| 135 } | |
| 136 | |
| 137 void ShelfButtonPressedMetricTrackerTest::ButtonPressed( | |
| 138 ShelfAction performed_action) { | |
| 139 const DummyEvent kDummyEvent; | |
| 140 const DummyButton kDummyButton; | |
| 141 metric_tracker_->ButtonPressed(kDummyEvent, &kDummyButton, performed_action); | |
| 142 } | |
| 143 | |
| 144 void ShelfButtonPressedMetricTrackerTest::ButtonPressed( | |
| 145 const views::Button* sender, | |
| 146 ShelfAction performed_action) { | |
| 147 const DummyEvent kDummyEvent; | |
| 148 metric_tracker_->ButtonPressed(kDummyEvent, sender, performed_action); | |
| 149 } | |
| 150 | |
| 151 } // namespace | |
| 152 | |
| 153 // Verifies that a Launcher_ButtonPressed_Mouse UMA user action is recorded when | |
| 154 // a button is pressed by a mouse event. | |
| 155 TEST_F(ShelfButtonPressedMetricTrackerTest, | |
| 156 Launcher_ButtonPressed_MouseIsRecordedWhenIconActivatedByMouse) { | |
| 157 // TODO: investigate failure in mash. http://crbug.com/695565. | |
| 158 if (WmShell::Get()->IsRunningInMash()) | |
| 159 return; | |
| 160 | |
| 161 const ui::MouseEvent mouse_event(ui::ET_MOUSE_PRESSED, gfx::Point(), | |
| 162 gfx::Point(), base::TimeTicks(), 0, 0); | |
| 163 | |
| 164 base::UserActionTester user_action_tester; | |
| 165 ButtonPressed(mouse_event); | |
| 166 EXPECT_EQ(1, | |
| 167 user_action_tester.GetActionCount("Launcher_ButtonPressed_Mouse")); | |
| 168 } | |
| 169 | |
| 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 // TODO: investigate failure in mash. http://crbug.com/695565. | |
| 175 if (WmShell::Get()->IsRunningInMash()) | |
| 176 return; | |
| 177 | |
| 178 const ui::TouchEvent touch_event(ui::ET_GESTURE_TAP, gfx::Point(), 0, | |
| 179 base::TimeTicks()); | |
| 180 | |
| 181 base::UserActionTester user_action_tester; | |
| 182 ButtonPressed(touch_event); | |
| 183 EXPECT_EQ(1, | |
| 184 user_action_tester.GetActionCount("Launcher_ButtonPressed_Touch")); | |
| 185 } | |
| 186 | |
| 187 // Verifies that a Launcher_LaunchTask UMA user action is recorded when | |
| 188 // pressing a button causes a new window to be created. | |
| 189 TEST_F(ShelfButtonPressedMetricTrackerTest, | |
| 190 Launcher_LaunchTaskIsRecordedWhenNewWindowIsCreated) { | |
| 191 // TODO: investigate failure in mash. http://crbug.com/695565. | |
| 192 if (WmShell::Get()->IsRunningInMash()) | |
| 193 return; | |
| 194 | |
| 195 base::UserActionTester user_action_tester; | |
| 196 ButtonPressed(SHELF_ACTION_NEW_WINDOW_CREATED); | |
| 197 EXPECT_EQ(1, user_action_tester.GetActionCount("Launcher_LaunchTask")); | |
| 198 } | |
| 199 | |
| 200 // Verifies that a Launcher_MinimizeTask UMA user action is recorded when | |
| 201 // pressing a button causes an existing window to be minimized. | |
| 202 TEST_F(ShelfButtonPressedMetricTrackerTest, | |
| 203 Launcher_MinimizeTaskIsRecordedWhenWindowIsMinimized) { | |
| 204 // TODO: investigate failure in mash. http://crbug.com/695565. | |
| 205 if (WmShell::Get()->IsRunningInMash()) | |
| 206 return; | |
| 207 | |
| 208 base::UserActionTester user_action_tester; | |
| 209 ButtonPressed(SHELF_ACTION_WINDOW_MINIMIZED); | |
| 210 EXPECT_EQ(1, user_action_tester.GetActionCount("Launcher_MinimizeTask")); | |
| 211 } | |
| 212 | |
| 213 // Verifies that a Launcher_SwitchTask UMA user action is recorded when | |
| 214 // pressing a button causes an existing window to be activated. | |
| 215 TEST_F(ShelfButtonPressedMetricTrackerTest, | |
| 216 Launcher_SwitchTaskIsRecordedWhenExistingWindowIsActivated) { | |
| 217 // TODO: investigate failure in mash. http://crbug.com/695565. | |
| 218 if (WmShell::Get()->IsRunningInMash()) | |
| 219 return; | |
| 220 | |
| 221 base::UserActionTester user_action_tester; | |
| 222 ButtonPressed(SHELF_ACTION_WINDOW_ACTIVATED); | |
| 223 EXPECT_EQ(1, user_action_tester.GetActionCount("Launcher_SwitchTask")); | |
| 224 } | |
| 225 | |
| 226 // Verify that a window activation action will record a data point if it was | |
| 227 // subsequent to a minimize action. | |
| 228 TEST_F(ShelfButtonPressedMetricTrackerTest, | |
| 229 VerifyDataRecordedAfterMinimizedAndSubsequentActivatedAction) { | |
| 230 const DummyButton kDummyButton; | |
| 231 | |
| 232 base::HistogramTester histogram_tester; | |
| 233 | |
| 234 ButtonPressed(&kDummyButton, SHELF_ACTION_WINDOW_MINIMIZED); | |
| 235 histogram_tester.ExpectTotalCount( | |
| 236 kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName, 0); | |
| 237 | |
| 238 ButtonPressed(&kDummyButton, SHELF_ACTION_WINDOW_ACTIVATED); | |
| 239 histogram_tester.ExpectTotalCount( | |
| 240 kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName, 1); | |
| 241 } | |
| 242 | |
| 243 // Verify that a multiple window activation actions will record a single data | |
| 244 // point if they are subsequent to a minimize action. | |
| 245 TEST_F(ShelfButtonPressedMetricTrackerTest, | |
| 246 VerifyDataRecordedAfterMinimizedAndMultipleSubsequentActivatedActions) { | |
| 247 const DummyButton kDummyButton; | |
| 248 | |
| 249 base::HistogramTester histogram_tester; | |
| 250 | |
| 251 ButtonPressed(&kDummyButton, SHELF_ACTION_WINDOW_MINIMIZED); | |
| 252 ButtonPressed(&kDummyButton, SHELF_ACTION_WINDOW_ACTIVATED); | |
| 253 ButtonPressed(&kDummyButton, SHELF_ACTION_WINDOW_ACTIVATED); | |
| 254 | |
| 255 histogram_tester.ExpectTotalCount( | |
| 256 kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName, 1); | |
| 257 } | |
| 258 | |
| 259 // Verify that a window activation action will not record a data point if it was | |
| 260 // not subsequent to a minimize action. | |
| 261 TEST_F(ShelfButtonPressedMetricTrackerTest, | |
| 262 VerifyDataRecordedAfterMinimizedAndNonSubsequentActivatedAction) { | |
| 263 const DummyButton kDummyButton; | |
| 264 | |
| 265 base::HistogramTester histogram_tester; | |
| 266 | |
| 267 ButtonPressed(&kDummyButton, SHELF_ACTION_WINDOW_MINIMIZED); | |
| 268 ButtonPressed(&kDummyButton, SHELF_ACTION_APP_LIST_SHOWN); | |
| 269 ButtonPressed(&kDummyButton, SHELF_ACTION_WINDOW_ACTIVATED); | |
| 270 | |
| 271 histogram_tester.ExpectTotalCount( | |
| 272 kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName, 0); | |
| 273 } | |
| 274 | |
| 275 // Verify no data is recorded if a second source button is pressed in between | |
| 276 // subsequent minimized and activated actions on the same source. | |
| 277 TEST_F(ShelfButtonPressedMetricTrackerTest, | |
| 278 VerifyDataRecordedAfterMinimizedButtonA) { | |
| 279 const DummyButton kDummyButton; | |
| 280 const DummyButton kSecondDummyButton; | |
| 281 | |
| 282 base::HistogramTester histogram_tester; | |
| 283 | |
| 284 ButtonPressed(&kDummyButton, SHELF_ACTION_WINDOW_MINIMIZED); | |
| 285 ButtonPressed(&kSecondDummyButton, SHELF_ACTION_WINDOW_MINIMIZED); | |
| 286 ButtonPressed(&kDummyButton, SHELF_ACTION_WINDOW_ACTIVATED); | |
| 287 | |
| 288 histogram_tester.ExpectTotalCount( | |
| 289 kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName, 0); | |
| 290 } | |
| 291 | |
| 292 // Verify the data value recorded when a window activation action is subsequent | |
| 293 // to a minimize action. | |
| 294 TEST_F(ShelfButtonPressedMetricTrackerTest, | |
| 295 VerifyTheValueRecordedBySubsequentMinimizedAndActivateActions) { | |
| 296 const int kTimeDeltaInMilliseconds = 17; | |
| 297 const DummyButton kDummyButton; | |
| 298 | |
| 299 base::HistogramTester histogram_tester; | |
| 300 | |
| 301 ButtonPressed(&kDummyButton, SHELF_ACTION_WINDOW_MINIMIZED); | |
| 302 tick_clock_->Advance( | |
| 303 base::TimeDelta::FromMilliseconds(kTimeDeltaInMilliseconds)); | |
| 304 ButtonPressed(&kDummyButton, SHELF_ACTION_WINDOW_ACTIVATED); | |
| 305 | |
| 306 histogram_tester.ExpectTotalCount( | |
| 307 kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName, 1); | |
| 308 histogram_tester.ExpectBucketCount( | |
| 309 kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName, | |
| 310 kTimeDeltaInMilliseconds, 1); | |
| 311 } | |
| 312 | |
| 313 } // namespace test | |
| 314 } // namespace ash | |
| OLD | NEW |