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