desktop_task_switch_metric_recorder_unittest.cc 12 KB

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