| 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/metrics/desktop_task_switch_metric_recorder.h" | |
| 6 | |
| 7 #include "ash/shell.h" | |
| 8 #include "ash/test/ash_test_base.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/test/user_action_tester.h" | |
| 11 #include "ui/aura/test/test_window_delegate.h" | |
| 12 #include "ui/aura/window.h" | |
| 13 #include "ui/compositor/layer_type.h" | |
| 14 #include "ui/events/test/event_generator.h" | |
| 15 #include "ui/wm/public/activation_client.h" | |
| 16 #include "ui/wm/public/window_types.h" | |
| 17 | |
| 18 using aura::client::ActivationChangeObserver; | |
| 19 | |
| 20 namespace ash { | |
| 21 namespace { | |
| 22 | |
| 23 const char kDesktopTaskSwitchUserAction[] = "Desktop_SwitchTask"; | |
| 24 | |
| 25 // Test fixture for the DesktopTaskSwitchMetricsRecorder class. NOTE: This | |
| 26 // fixture extends AshTestBase so that the UserMetricsRecorder instance required | |
| 27 // by the test target can be obtained through Shell::GetInstance()->metrics() | |
| 28 // and the test target is not the same instance as the one owned by the | |
| 29 // UserMetricsRecorder instance. | |
| 30 class DesktopTaskSwitchMetricRecorderTest : public test::AshTestBase { | |
| 31 public: | |
| 32 DesktopTaskSwitchMetricRecorderTest(); | |
| 33 ~DesktopTaskSwitchMetricRecorderTest() override; | |
| 34 | |
| 35 // test::AshTestBase: | |
| 36 void SetUp() override; | |
| 37 void TearDown() override; | |
| 38 | |
| 39 // Resets the recorded user action counts. | |
| 40 void ResetActionCounts(); | |
| 41 | |
| 42 // Returns the number of times the "Desktop_SwitchTask" user action was | |
| 43 // recorded. | |
| 44 int GetActionCount() const; | |
| 45 | |
| 46 // Creates a positionable window such that wm::IsWindowUserPositionable(...) | |
| 47 // would retun true. | |
| 48 scoped_ptr<aura::Window> CreatePositionableWindow() const; | |
| 49 | |
| 50 // Creates a non-positionable window such that | |
| 51 // wm::IsWindowUserPositionable(...) would retun false. | |
| 52 scoped_ptr<aura::Window> CreateNonPositionableWindow() const; | |
| 53 | |
| 54 // Wrapper to notify the test target's OnWindowActivated(...) method that | |
| 55 // |window| was activated due to an INPUT_EVENT. | |
| 56 void ActiveTaskWindowWithUserInput(aura::Window* window); | |
| 57 | |
| 58 protected: | |
| 59 // Records UMA user action counts. | |
| 60 scoped_ptr<base::UserActionTester> user_action_tester_; | |
| 61 | |
| 62 // The test target. | |
| 63 scoped_ptr<DesktopTaskSwitchMetricRecorder> metrics_recorder_; | |
| 64 | |
| 65 private: | |
| 66 DISALLOW_COPY_AND_ASSIGN(DesktopTaskSwitchMetricRecorderTest); | |
| 67 }; | |
| 68 | |
| 69 DesktopTaskSwitchMetricRecorderTest::DesktopTaskSwitchMetricRecorderTest() { | |
| 70 } | |
| 71 | |
| 72 DesktopTaskSwitchMetricRecorderTest::~DesktopTaskSwitchMetricRecorderTest() { | |
| 73 } | |
| 74 | |
| 75 void DesktopTaskSwitchMetricRecorderTest::SetUp() { | |
| 76 test::AshTestBase::SetUp(); | |
| 77 metrics_recorder_.reset(new DesktopTaskSwitchMetricRecorder); | |
| 78 user_action_tester_.reset(new base::UserActionTester); | |
| 79 } | |
| 80 | |
| 81 void DesktopTaskSwitchMetricRecorderTest::TearDown() { | |
| 82 user_action_tester_.reset(); | |
| 83 metrics_recorder_.reset(); | |
| 84 test::AshTestBase::TearDown(); | |
| 85 } | |
| 86 | |
| 87 void DesktopTaskSwitchMetricRecorderTest::ActiveTaskWindowWithUserInput( | |
| 88 aura::Window* window) { | |
| 89 metrics_recorder_->OnWindowActivated( | |
| 90 ActivationChangeObserver::ActivationReason::INPUT_EVENT, window, nullptr); | |
| 91 } | |
| 92 | |
| 93 void DesktopTaskSwitchMetricRecorderTest::ResetActionCounts() { | |
| 94 user_action_tester_->ResetCounts(); | |
| 95 } | |
| 96 | |
| 97 int DesktopTaskSwitchMetricRecorderTest::GetActionCount() const { | |
| 98 return user_action_tester_->GetActionCount(kDesktopTaskSwitchUserAction); | |
| 99 } | |
| 100 | |
| 101 scoped_ptr<aura::Window> | |
| 102 DesktopTaskSwitchMetricRecorderTest::CreatePositionableWindow() const { | |
| 103 scoped_ptr<aura::Window> window( | |
| 104 new aura::Window(new aura::test::TestWindowDelegate)); | |
| 105 window->SetType(ui::wm::WINDOW_TYPE_NORMAL); | |
| 106 window->Init(ui::LAYER_NOT_DRAWN); | |
| 107 return window.Pass(); | |
| 108 } | |
| 109 | |
| 110 scoped_ptr<aura::Window> | |
| 111 DesktopTaskSwitchMetricRecorderTest::CreateNonPositionableWindow() const { | |
| 112 scoped_ptr<aura::Window> window( | |
| 113 new aura::Window(new aura::test::TestWindowDelegate)); | |
| 114 window->SetType(ui::wm::WINDOW_TYPE_UNKNOWN); | |
| 115 window->Init(ui::LAYER_NOT_DRAWN); | |
| 116 return window.Pass(); | |
| 117 } | |
| 118 | |
| 119 // Verify user action is recorded when a positionable window is activated given | |
| 120 // that a null window was activated last. | |
| 121 TEST_F(DesktopTaskSwitchMetricRecorderTest, | |
| 122 ActivatePositionableWindowWhenNullWindowWasActivatedLast) { | |
| 123 scoped_ptr<aura::Window> null_window; | |
| 124 scoped_ptr<aura::Window> positionable_window = | |
| 125 CreatePositionableWindow().Pass(); | |
| 126 | |
| 127 ActiveTaskWindowWithUserInput(null_window.get()); | |
| 128 ResetActionCounts(); | |
| 129 | |
| 130 ActiveTaskWindowWithUserInput(positionable_window.get()); | |
| 131 EXPECT_EQ(1, GetActionCount()); | |
| 132 } | |
| 133 | |
| 134 // Verify user action is recorded whena positionable window is activated given | |
| 135 // a different positionable window was activated last. | |
| 136 TEST_F( | |
| 137 DesktopTaskSwitchMetricRecorderTest, | |
| 138 ActivatePositionableWindowWhenADifferentPositionableWindowWasActivatedLast)
{ | |
| 139 scoped_ptr<aura::Window> positionable_window_1 = | |
| 140 CreatePositionableWindow().Pass(); | |
| 141 scoped_ptr<aura::Window> positionable_window_2 = | |
| 142 CreatePositionableWindow().Pass(); | |
| 143 | |
| 144 ActiveTaskWindowWithUserInput(positionable_window_1.get()); | |
| 145 ResetActionCounts(); | |
| 146 | |
| 147 ActiveTaskWindowWithUserInput(positionable_window_2.get()); | |
| 148 EXPECT_EQ(1, GetActionCount()); | |
| 149 } | |
| 150 | |
| 151 // Verify user action is not recorded when a positionable window is activated | |
| 152 // given the same positionable window was activated last. | |
| 153 TEST_F( | |
| 154 DesktopTaskSwitchMetricRecorderTest, | |
| 155 ActivatePositionableWindowWhenTheSamePositionableWindowWasActivatedLast) { | |
| 156 scoped_ptr<aura::Window> positionable_window = | |
| 157 CreatePositionableWindow().Pass(); | |
| 158 | |
| 159 ActiveTaskWindowWithUserInput(positionable_window.get()); | |
| 160 ResetActionCounts(); | |
| 161 | |
| 162 ActiveTaskWindowWithUserInput(positionable_window.get()); | |
| 163 EXPECT_EQ(0, GetActionCount()); | |
| 164 } | |
| 165 | |
| 166 // Verify user action is recorded when a positionable window is activated given | |
| 167 // a non-positionable window was activated last. | |
| 168 TEST_F(DesktopTaskSwitchMetricRecorderTest, | |
| 169 ActivatePositionableWindowWhenANonPositionableWindowWasActivatedLast) { | |
| 170 scoped_ptr<aura::Window> non_positionable_window = | |
| 171 CreateNonPositionableWindow().Pass(); | |
| 172 scoped_ptr<aura::Window> positionable_window = | |
| 173 CreatePositionableWindow().Pass(); | |
| 174 | |
| 175 ActiveTaskWindowWithUserInput(non_positionable_window.get()); | |
| 176 ResetActionCounts(); | |
| 177 | |
| 178 ActiveTaskWindowWithUserInput(positionable_window.get()); | |
| 179 EXPECT_EQ(1, GetActionCount()); | |
| 180 } | |
| 181 | |
| 182 // Verify user action is not recorded when a non-positionable window is | |
| 183 // activated between two activations of the same positionable window. | |
| 184 TEST_F(DesktopTaskSwitchMetricRecorderTest, | |
| 185 ActivateNonPositionableWindowBetweenTwoPositionableWindowActivations) { | |
| 186 scoped_ptr<aura::Window> positionable_window = | |
| 187 CreatePositionableWindow().Pass(); | |
| 188 scoped_ptr<aura::Window> non_positionable_window = | |
| 189 CreateNonPositionableWindow().Pass(); | |
| 190 | |
| 191 ActiveTaskWindowWithUserInput(positionable_window.get()); | |
| 192 ResetActionCounts(); | |
| 193 | |
| 194 ActiveTaskWindowWithUserInput(non_positionable_window.get()); | |
| 195 EXPECT_EQ(0, GetActionCount()); | |
| 196 | |
| 197 ActiveTaskWindowWithUserInput(positionable_window.get()); | |
| 198 EXPECT_EQ(0, GetActionCount()); | |
| 199 } | |
| 200 | |
| 201 // Verify user action is not recorded when a null window is activated. | |
| 202 TEST_F(DesktopTaskSwitchMetricRecorderTest, ActivateNullWindow) { | |
| 203 scoped_ptr<aura::Window> positionable_window = | |
| 204 CreatePositionableWindow().Pass(); | |
| 205 scoped_ptr<aura::Window> null_window = nullptr; | |
| 206 | |
| 207 ActiveTaskWindowWithUserInput(positionable_window.get()); | |
| 208 ResetActionCounts(); | |
| 209 | |
| 210 ActiveTaskWindowWithUserInput(null_window.get()); | |
| 211 EXPECT_EQ(0, GetActionCount()); | |
| 212 } | |
| 213 | |
| 214 // Verify user action is not recorded when a non-positionable window is | |
| 215 // activated. | |
| 216 TEST_F(DesktopTaskSwitchMetricRecorderTest, ActivateNonPositionableWindow) { | |
| 217 scoped_ptr<aura::Window> positionable_window = | |
| 218 CreatePositionableWindow().Pass(); | |
| 219 scoped_ptr<aura::Window> non_positionable_window = | |
| 220 CreateNonPositionableWindow().Pass(); | |
| 221 | |
| 222 ActiveTaskWindowWithUserInput(positionable_window.get()); | |
| 223 ResetActionCounts(); | |
| 224 | |
| 225 ActiveTaskWindowWithUserInput(non_positionable_window.get()); | |
| 226 EXPECT_EQ(0, GetActionCount()); | |
| 227 } | |
| 228 | |
| 229 // Verify user action is not recorded when the ActivationReason is not an | |
| 230 // INPUT_EVENT. | |
| 231 TEST_F(DesktopTaskSwitchMetricRecorderTest, | |
| 232 ActivatePositionableWindowWithNonInputEventReason) { | |
| 233 scoped_ptr<aura::Window> positionable_window_1 = | |
| 234 CreatePositionableWindow().Pass(); | |
| 235 scoped_ptr<aura::Window> positionable_window_2 = | |
| 236 CreatePositionableWindow().Pass(); | |
| 237 | |
| 238 ActiveTaskWindowWithUserInput(positionable_window_1.get()); | |
| 239 ResetActionCounts(); | |
| 240 | |
| 241 metrics_recorder_->OnWindowActivated( | |
| 242 ActivationChangeObserver::ActivationReason::ACTIVATION_CLIENT, | |
| 243 positionable_window_2.get(), nullptr); | |
| 244 EXPECT_EQ(0, GetActionCount()); | |
| 245 } | |
| 246 | |
| 247 // Test fixture to test the integration of the DesktopTaskSwitchMetricsRecorder | |
| 248 // class with ash::Shell environment. | |
| 249 class DesktopTaskSwitchMetricRecorderWithShellIntegrationTest | |
| 250 : public test::AshTestBase { | |
| 251 public: | |
| 252 DesktopTaskSwitchMetricRecorderWithShellIntegrationTest(); | |
| 253 ~DesktopTaskSwitchMetricRecorderWithShellIntegrationTest() override; | |
| 254 | |
| 255 // test::AshTestBase: | |
| 256 void SetUp() override; | |
| 257 void TearDown() override; | |
| 258 | |
| 259 // Returns the number of times the "Desktop_SwitchTask" user action was | |
| 260 // recorded. | |
| 261 int GetActionCount() const; | |
| 262 | |
| 263 // Creates a positionable window with the given |bounds| such that | |
| 264 // wm::IsWindowUserPositionable(...) would retun true. | |
| 265 aura::Window* CreatePositionableWindowInShellWithBounds( | |
| 266 const gfx::Rect& bounds); | |
| 267 | |
| 268 protected: | |
| 269 // Records UMA user action counts. | |
| 270 scoped_ptr<base::UserActionTester> user_action_tester_; | |
| 271 | |
| 272 // Delegate used when creating new windows using the | |
| 273 // CreatePositionableWindowInShellWithBounds(...) method. | |
| 274 aura::test::TestWindowDelegate test_window_delegate_; | |
| 275 | |
| 276 private: | |
| 277 DISALLOW_COPY_AND_ASSIGN( | |
| 278 DesktopTaskSwitchMetricRecorderWithShellIntegrationTest); | |
| 279 }; | |
| 280 | |
| 281 DesktopTaskSwitchMetricRecorderWithShellIntegrationTest:: | |
| 282 DesktopTaskSwitchMetricRecorderWithShellIntegrationTest() { | |
| 283 } | |
| 284 | |
| 285 DesktopTaskSwitchMetricRecorderWithShellIntegrationTest:: | |
| 286 ~DesktopTaskSwitchMetricRecorderWithShellIntegrationTest() { | |
| 287 } | |
| 288 | |
| 289 void DesktopTaskSwitchMetricRecorderWithShellIntegrationTest::SetUp() { | |
| 290 test::AshTestBase::SetUp(); | |
| 291 user_action_tester_.reset(new base::UserActionTester); | |
| 292 } | |
| 293 | |
| 294 void DesktopTaskSwitchMetricRecorderWithShellIntegrationTest::TearDown() { | |
| 295 user_action_tester_.reset(); | |
| 296 test::AshTestBase::TearDown(); | |
| 297 } | |
| 298 | |
| 299 int DesktopTaskSwitchMetricRecorderWithShellIntegrationTest::GetActionCount() | |
| 300 const { | |
| 301 return user_action_tester_->GetActionCount(kDesktopTaskSwitchUserAction); | |
| 302 } | |
| 303 | |
| 304 aura::Window* DesktopTaskSwitchMetricRecorderWithShellIntegrationTest:: | |
| 305 CreatePositionableWindowInShellWithBounds(const gfx::Rect& bounds) { | |
| 306 return CreateTestWindowInShellWithDelegate(&test_window_delegate_, 0, bounds); | |
| 307 } | |
| 308 | |
| 309 // Verify a user action is recorded when a positionable window is activated by | |
| 310 // a INPUT_EVENT. | |
| 311 TEST_F(DesktopTaskSwitchMetricRecorderWithShellIntegrationTest, | |
| 312 ActivatePositionableWindowWithInputEvent) { | |
| 313 aura::Window* positionable_window = | |
| 314 CreatePositionableWindowInShellWithBounds(gfx::Rect(0, 0, 10, 10)); | |
| 315 | |
| 316 ui::test::EventGenerator event_generator(Shell::GetPrimaryRootWindow()); | |
| 317 | |
| 318 event_generator.MoveMouseToCenterOf(positionable_window); | |
| 319 event_generator.ClickLeftButton(); | |
| 320 | |
| 321 EXPECT_EQ(1, GetActionCount()); | |
| 322 } | |
| 323 | |
| 324 // Verify a user action is not recorded when a positionable window is activated | |
| 325 // by a non INPUT_EVENT. | |
| 326 TEST_F(DesktopTaskSwitchMetricRecorderWithShellIntegrationTest, | |
| 327 ActivatePositionableWindowWithNonInputEvent) { | |
| 328 aura::Window* positionable_window = | |
| 329 CreatePositionableWindowInShellWithBounds(gfx::Rect(0, 0, 10, 10)); | |
| 330 | |
| 331 Shell::GetInstance()->activation_client()->ActivateWindow( | |
| 332 positionable_window); | |
| 333 | |
| 334 EXPECT_EQ(0, GetActionCount()); | |
| 335 } | |
| 336 | |
| 337 } // namespace | |
| 338 } // namespace ash | |
| OLD | NEW |