pointer_metrics_recorder_unittest.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. // Copyright 2016 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/pointer_metrics_recorder.h"
  5. #include <memory>
  6. #include "ash/constants/app_types.h"
  7. #include "ash/display/screen_orientation_controller_test_api.h"
  8. #include "ash/public/cpp/shell_window_ids.h"
  9. #include "ash/shell.h"
  10. #include "ash/test/ash_test_base.h"
  11. #include "ash/wm/tablet_mode/tablet_mode_controller.h"
  12. #include "base/test/metrics/histogram_tester.h"
  13. #include "ui/aura/client/aura_constants.h"
  14. #include "ui/aura/window.h"
  15. #include "ui/display/test/display_manager_test_api.h"
  16. #include "ui/events/event.h"
  17. #include "ui/views/widget/widget.h"
  18. namespace ash {
  19. namespace {
  20. const char kCombinationHistogramName[] =
  21. "Event.DownEventCount.PerInputFormFactorDestinationCombination2";
  22. // Test fixture for the PointerMetricsRecorder class.
  23. class PointerMetricsRecorderTest : public AshTestBase {
  24. public:
  25. PointerMetricsRecorderTest();
  26. PointerMetricsRecorderTest(const PointerMetricsRecorderTest&) = delete;
  27. PointerMetricsRecorderTest& operator=(const PointerMetricsRecorderTest&) =
  28. delete;
  29. ~PointerMetricsRecorderTest() override;
  30. // AshTestBase:
  31. void SetUp() override;
  32. void TearDown() override;
  33. void CreateDownEvent(ui::EventPointerType pointer_type,
  34. DownEventFormFactor form_factor,
  35. AppType destination);
  36. protected:
  37. // The test target.
  38. std::unique_ptr<PointerMetricsRecorder> pointer_metrics_recorder_;
  39. // Used to verify recorded data.
  40. std::unique_ptr<base::HistogramTester> histogram_tester_;
  41. // Where down events are dispatched to.
  42. std::unique_ptr<views::Widget> widget_;
  43. };
  44. PointerMetricsRecorderTest::PointerMetricsRecorderTest() = default;
  45. PointerMetricsRecorderTest::~PointerMetricsRecorderTest() = default;
  46. void PointerMetricsRecorderTest::SetUp() {
  47. AshTestBase::SetUp();
  48. pointer_metrics_recorder_ = std::make_unique<PointerMetricsRecorder>();
  49. histogram_tester_ = std::make_unique<base::HistogramTester>();
  50. widget_ = CreateTestWidget();
  51. }
  52. void PointerMetricsRecorderTest::TearDown() {
  53. widget_.reset();
  54. pointer_metrics_recorder_.reset();
  55. AshTestBase::TearDown();
  56. }
  57. void PointerMetricsRecorderTest::CreateDownEvent(
  58. ui::EventPointerType pointer_type,
  59. DownEventFormFactor form_factor,
  60. AppType destination) {
  61. aura::Window* window = widget_->GetNativeWindow();
  62. CHECK(window);
  63. window->SetProperty(aura::client::kAppType, static_cast<int>(destination));
  64. if (form_factor == DownEventFormFactor::kClamshell) {
  65. Shell::Get()->tablet_mode_controller()->SetEnabledForTest(false);
  66. } else {
  67. Shell::Get()->tablet_mode_controller()->SetEnabledForTest(true);
  68. display::Display::Rotation rotation =
  69. (form_factor == DownEventFormFactor::kTabletModeLandscape)
  70. ? display::Display::ROTATE_0
  71. : display::Display::ROTATE_90;
  72. ScreenOrientationControllerTestApi test_api(
  73. Shell::Get()->screen_orientation_controller());
  74. // Set the screen orientation.
  75. test_api.SetDisplayRotation(rotation,
  76. display::Display::RotationSource::ACTIVE);
  77. }
  78. if (pointer_type == ui::EventPointerType::kMouse) {
  79. ui::MouseEvent mouse_down(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(),
  80. base::TimeTicks(), 0, 0);
  81. ui::Event::DispatcherApi(&mouse_down).set_target(window);
  82. pointer_metrics_recorder_->OnMouseEvent(&mouse_down);
  83. } else {
  84. // Pen and eraser events are touch events.
  85. ui::TouchEvent touch_down(ui::ET_TOUCH_PRESSED, gfx::Point(),
  86. base::TimeTicks(),
  87. ui::PointerDetails(pointer_type, 0));
  88. ui::Event::DispatcherApi(&touch_down).set_target(window);
  89. pointer_metrics_recorder_->OnTouchEvent(&touch_down);
  90. }
  91. }
  92. } // namespace
  93. // Verifies that histogram is not recorded when receiving events that are not
  94. // down events.
  95. TEST_F(PointerMetricsRecorderTest, NonDownEventsInAllPointerHistogram) {
  96. ui::MouseEvent mouse_up(ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(),
  97. base::TimeTicks(), 0, 0);
  98. pointer_metrics_recorder_->OnMouseEvent(&mouse_up);
  99. histogram_tester_->ExpectTotalCount(kCombinationHistogramName, 0);
  100. }
  101. // Verifies that down events from different combination of input type, form
  102. // factor and destination are recorded.
  103. TEST_F(PointerMetricsRecorderTest, DownEventPerCombination) {
  104. int64_t display_id = display::Screen::GetScreen()->GetPrimaryDisplay().id();
  105. display::DisplayManager* display_manager = Shell::Get()->display_manager();
  106. display::test::ScopedSetInternalDisplayId set_internal(display_manager,
  107. display_id);
  108. CreateDownEvent(ui::EventPointerType::kMouse, DownEventFormFactor::kClamshell,
  109. AppType::NON_APP);
  110. histogram_tester_->ExpectBucketCount(
  111. kCombinationHistogramName,
  112. static_cast<int>(DownEventMetric2::kNonAppMouseClamshell), 1);
  113. CreateDownEvent(ui::EventPointerType::kMouse,
  114. DownEventFormFactor::kTabletModeLandscape, AppType::NON_APP);
  115. histogram_tester_->ExpectBucketCount(
  116. kCombinationHistogramName,
  117. static_cast<int>(DownEventMetric2::kNonAppMouseTabletLandscape), 1);
  118. CreateDownEvent(ui::EventPointerType::kMouse,
  119. DownEventFormFactor::kTabletModePortrait, AppType::NON_APP);
  120. histogram_tester_->ExpectBucketCount(
  121. kCombinationHistogramName,
  122. static_cast<int>(DownEventMetric2::kNonAppMouseTabletPortrait), 1);
  123. CreateDownEvent(ui::EventPointerType::kPen, DownEventFormFactor::kClamshell,
  124. AppType::NON_APP);
  125. histogram_tester_->ExpectBucketCount(
  126. kCombinationHistogramName,
  127. static_cast<int>(DownEventMetric2::kNonAppStylusClamshell), 1);
  128. CreateDownEvent(ui::EventPointerType::kPen,
  129. DownEventFormFactor::kTabletModeLandscape, AppType::NON_APP);
  130. histogram_tester_->ExpectBucketCount(
  131. kCombinationHistogramName,
  132. static_cast<int>(DownEventMetric2::kNonAppStylusTabletLandscape), 1);
  133. CreateDownEvent(ui::EventPointerType::kPen,
  134. DownEventFormFactor::kTabletModePortrait, AppType::NON_APP);
  135. histogram_tester_->ExpectBucketCount(
  136. kCombinationHistogramName,
  137. static_cast<int>(DownEventMetric2::kNonAppStylusTabletPortrait), 1);
  138. CreateDownEvent(ui::EventPointerType::kTouch, DownEventFormFactor::kClamshell,
  139. AppType::NON_APP);
  140. histogram_tester_->ExpectBucketCount(
  141. kCombinationHistogramName,
  142. static_cast<int>(DownEventMetric2::kNonAppStylusClamshell), 1);
  143. CreateDownEvent(ui::EventPointerType::kTouch,
  144. DownEventFormFactor::kTabletModeLandscape, AppType::NON_APP);
  145. histogram_tester_->ExpectBucketCount(
  146. kCombinationHistogramName,
  147. static_cast<int>(DownEventMetric2::kNonAppStylusTabletLandscape), 1);
  148. CreateDownEvent(ui::EventPointerType::kTouch,
  149. DownEventFormFactor::kTabletModePortrait, AppType::NON_APP);
  150. histogram_tester_->ExpectBucketCount(
  151. kCombinationHistogramName,
  152. static_cast<int>(DownEventMetric2::kNonAppStylusTabletPortrait), 1);
  153. CreateDownEvent(ui::EventPointerType::kMouse, DownEventFormFactor::kClamshell,
  154. AppType::BROWSER);
  155. histogram_tester_->ExpectBucketCount(
  156. kCombinationHistogramName,
  157. static_cast<int>(DownEventMetric2::kBrowserMouseClamshell), 1);
  158. CreateDownEvent(ui::EventPointerType::kMouse,
  159. DownEventFormFactor::kTabletModeLandscape, AppType::BROWSER);
  160. histogram_tester_->ExpectBucketCount(
  161. kCombinationHistogramName,
  162. static_cast<int>(DownEventMetric2::kBrowserMouseTabletLandscape), 1);
  163. CreateDownEvent(ui::EventPointerType::kMouse,
  164. DownEventFormFactor::kTabletModePortrait, AppType::BROWSER);
  165. histogram_tester_->ExpectBucketCount(
  166. kCombinationHistogramName,
  167. static_cast<int>(DownEventMetric2::kBrowserMouseTabletPortrait), 1);
  168. CreateDownEvent(ui::EventPointerType::kPen, DownEventFormFactor::kClamshell,
  169. AppType::BROWSER);
  170. histogram_tester_->ExpectBucketCount(
  171. kCombinationHistogramName,
  172. static_cast<int>(DownEventMetric2::kBrowserStylusClamshell), 1);
  173. CreateDownEvent(ui::EventPointerType::kPen,
  174. DownEventFormFactor::kTabletModeLandscape, AppType::BROWSER);
  175. histogram_tester_->ExpectBucketCount(
  176. kCombinationHistogramName,
  177. static_cast<int>(DownEventMetric2::kBrowserStylusTabletLandscape), 1);
  178. CreateDownEvent(ui::EventPointerType::kPen,
  179. DownEventFormFactor::kTabletModePortrait, AppType::BROWSER);
  180. histogram_tester_->ExpectBucketCount(
  181. kCombinationHistogramName,
  182. static_cast<int>(DownEventMetric2::kBrowserStylusTabletPortrait), 1);
  183. CreateDownEvent(ui::EventPointerType::kTouch, DownEventFormFactor::kClamshell,
  184. AppType::BROWSER);
  185. histogram_tester_->ExpectBucketCount(
  186. kCombinationHistogramName,
  187. static_cast<int>(DownEventMetric2::kBrowserStylusClamshell), 1);
  188. CreateDownEvent(ui::EventPointerType::kTouch,
  189. DownEventFormFactor::kTabletModeLandscape, AppType::BROWSER);
  190. histogram_tester_->ExpectBucketCount(
  191. kCombinationHistogramName,
  192. static_cast<int>(DownEventMetric2::kBrowserStylusTabletLandscape), 1);
  193. CreateDownEvent(ui::EventPointerType::kTouch,
  194. DownEventFormFactor::kTabletModePortrait, AppType::BROWSER);
  195. histogram_tester_->ExpectBucketCount(
  196. kCombinationHistogramName,
  197. static_cast<int>(DownEventMetric2::kBrowserStylusTabletPortrait), 1);
  198. CreateDownEvent(ui::EventPointerType::kMouse, DownEventFormFactor::kClamshell,
  199. AppType::CHROME_APP);
  200. histogram_tester_->ExpectBucketCount(
  201. kCombinationHistogramName,
  202. static_cast<int>(DownEventMetric2::kChromeAppMouseClamshell), 1);
  203. CreateDownEvent(ui::EventPointerType::kMouse,
  204. DownEventFormFactor::kTabletModeLandscape,
  205. AppType::CHROME_APP);
  206. histogram_tester_->ExpectBucketCount(
  207. kCombinationHistogramName,
  208. static_cast<int>(DownEventMetric2::kChromeAppMouseTabletLandscape), 1);
  209. CreateDownEvent(ui::EventPointerType::kMouse,
  210. DownEventFormFactor::kTabletModePortrait,
  211. AppType::CHROME_APP);
  212. histogram_tester_->ExpectBucketCount(
  213. kCombinationHistogramName,
  214. static_cast<int>(DownEventMetric2::kChromeAppMouseTabletPortrait), 1);
  215. CreateDownEvent(ui::EventPointerType::kPen, DownEventFormFactor::kClamshell,
  216. AppType::CHROME_APP);
  217. histogram_tester_->ExpectBucketCount(
  218. kCombinationHistogramName,
  219. static_cast<int>(DownEventMetric2::kChromeAppStylusClamshell), 1);
  220. CreateDownEvent(ui::EventPointerType::kPen,
  221. DownEventFormFactor::kTabletModeLandscape,
  222. AppType::CHROME_APP);
  223. histogram_tester_->ExpectBucketCount(
  224. kCombinationHistogramName,
  225. static_cast<int>(DownEventMetric2::kChromeAppStylusTabletLandscape), 1);
  226. CreateDownEvent(ui::EventPointerType::kPen,
  227. DownEventFormFactor::kTabletModePortrait,
  228. AppType::CHROME_APP);
  229. histogram_tester_->ExpectBucketCount(
  230. kCombinationHistogramName,
  231. static_cast<int>(DownEventMetric2::kChromeAppStylusTabletPortrait), 1);
  232. CreateDownEvent(ui::EventPointerType::kTouch, DownEventFormFactor::kClamshell,
  233. AppType::CHROME_APP);
  234. histogram_tester_->ExpectBucketCount(
  235. kCombinationHistogramName,
  236. static_cast<int>(DownEventMetric2::kChromeAppStylusClamshell), 1);
  237. CreateDownEvent(ui::EventPointerType::kTouch,
  238. DownEventFormFactor::kTabletModeLandscape,
  239. AppType::CHROME_APP);
  240. histogram_tester_->ExpectBucketCount(
  241. kCombinationHistogramName,
  242. static_cast<int>(DownEventMetric2::kChromeAppStylusTabletLandscape), 1);
  243. CreateDownEvent(ui::EventPointerType::kTouch,
  244. DownEventFormFactor::kTabletModePortrait,
  245. AppType::CHROME_APP);
  246. histogram_tester_->ExpectBucketCount(
  247. kCombinationHistogramName,
  248. static_cast<int>(DownEventMetric2::kChromeAppStylusTabletPortrait), 1);
  249. CreateDownEvent(ui::EventPointerType::kMouse, DownEventFormFactor::kClamshell,
  250. AppType::ARC_APP);
  251. histogram_tester_->ExpectBucketCount(
  252. kCombinationHistogramName,
  253. static_cast<int>(DownEventMetric2::kArcAppMouseClamshell), 1);
  254. CreateDownEvent(ui::EventPointerType::kMouse,
  255. DownEventFormFactor::kTabletModeLandscape, AppType::ARC_APP);
  256. histogram_tester_->ExpectBucketCount(
  257. kCombinationHistogramName,
  258. static_cast<int>(DownEventMetric2::kArcAppMouseTabletLandscape), 1);
  259. CreateDownEvent(ui::EventPointerType::kMouse,
  260. DownEventFormFactor::kTabletModePortrait, AppType::ARC_APP);
  261. histogram_tester_->ExpectBucketCount(
  262. kCombinationHistogramName,
  263. static_cast<int>(DownEventMetric2::kArcAppMouseTabletPortrait), 1);
  264. CreateDownEvent(ui::EventPointerType::kPen, DownEventFormFactor::kClamshell,
  265. AppType::ARC_APP);
  266. histogram_tester_->ExpectBucketCount(
  267. kCombinationHistogramName,
  268. static_cast<int>(DownEventMetric2::kArcAppStylusClamshell), 1);
  269. CreateDownEvent(ui::EventPointerType::kPen,
  270. DownEventFormFactor::kTabletModeLandscape, AppType::ARC_APP);
  271. histogram_tester_->ExpectBucketCount(
  272. kCombinationHistogramName,
  273. static_cast<int>(DownEventMetric2::kArcAppStylusTabletLandscape), 1);
  274. CreateDownEvent(ui::EventPointerType::kPen,
  275. DownEventFormFactor::kTabletModePortrait, AppType::ARC_APP);
  276. histogram_tester_->ExpectBucketCount(
  277. kCombinationHistogramName,
  278. static_cast<int>(DownEventMetric2::kArcAppStylusTabletPortrait), 1);
  279. CreateDownEvent(ui::EventPointerType::kTouch, DownEventFormFactor::kClamshell,
  280. AppType::ARC_APP);
  281. histogram_tester_->ExpectBucketCount(
  282. kCombinationHistogramName,
  283. static_cast<int>(DownEventMetric2::kArcAppStylusClamshell), 1);
  284. CreateDownEvent(ui::EventPointerType::kTouch,
  285. DownEventFormFactor::kTabletModeLandscape, AppType::ARC_APP);
  286. histogram_tester_->ExpectBucketCount(
  287. kCombinationHistogramName,
  288. static_cast<int>(DownEventMetric2::kArcAppStylusTabletLandscape), 1);
  289. CreateDownEvent(ui::EventPointerType::kTouch,
  290. DownEventFormFactor::kTabletModePortrait, AppType::ARC_APP);
  291. histogram_tester_->ExpectBucketCount(
  292. kCombinationHistogramName,
  293. static_cast<int>(DownEventMetric2::kArcAppStylusTabletPortrait), 1);
  294. CreateDownEvent(ui::EventPointerType::kMouse, DownEventFormFactor::kClamshell,
  295. AppType::CROSTINI_APP);
  296. histogram_tester_->ExpectBucketCount(
  297. kCombinationHistogramName,
  298. static_cast<int>(DownEventMetric2::kCrostiniAppMouseClamshell), 1);
  299. CreateDownEvent(ui::EventPointerType::kMouse,
  300. DownEventFormFactor::kTabletModeLandscape,
  301. AppType::CROSTINI_APP);
  302. histogram_tester_->ExpectBucketCount(
  303. kCombinationHistogramName,
  304. static_cast<int>(DownEventMetric2::kCrostiniAppMouseTabletLandscape), 1);
  305. CreateDownEvent(ui::EventPointerType::kMouse,
  306. DownEventFormFactor::kTabletModePortrait,
  307. AppType::CROSTINI_APP);
  308. histogram_tester_->ExpectBucketCount(
  309. kCombinationHistogramName,
  310. static_cast<int>(DownEventMetric2::kCrostiniAppMouseTabletPortrait), 1);
  311. CreateDownEvent(ui::EventPointerType::kPen, DownEventFormFactor::kClamshell,
  312. AppType::CROSTINI_APP);
  313. histogram_tester_->ExpectBucketCount(
  314. kCombinationHistogramName,
  315. static_cast<int>(DownEventMetric2::kCrostiniAppStylusClamshell), 1);
  316. CreateDownEvent(ui::EventPointerType::kPen,
  317. DownEventFormFactor::kTabletModeLandscape,
  318. AppType::CROSTINI_APP);
  319. histogram_tester_->ExpectBucketCount(
  320. kCombinationHistogramName,
  321. static_cast<int>(DownEventMetric2::kCrostiniAppStylusTabletLandscape), 1);
  322. CreateDownEvent(ui::EventPointerType::kPen,
  323. DownEventFormFactor::kTabletModePortrait,
  324. AppType::CROSTINI_APP);
  325. histogram_tester_->ExpectBucketCount(
  326. kCombinationHistogramName,
  327. static_cast<int>(DownEventMetric2::kCrostiniAppStylusTabletPortrait), 1);
  328. CreateDownEvent(ui::EventPointerType::kTouch, DownEventFormFactor::kClamshell,
  329. AppType::CROSTINI_APP);
  330. histogram_tester_->ExpectBucketCount(
  331. kCombinationHistogramName,
  332. static_cast<int>(DownEventMetric2::kCrostiniAppStylusClamshell), 1);
  333. CreateDownEvent(ui::EventPointerType::kTouch,
  334. DownEventFormFactor::kTabletModeLandscape,
  335. AppType::CROSTINI_APP);
  336. histogram_tester_->ExpectBucketCount(
  337. kCombinationHistogramName,
  338. static_cast<int>(DownEventMetric2::kCrostiniAppStylusTabletLandscape), 1);
  339. CreateDownEvent(ui::EventPointerType::kTouch,
  340. DownEventFormFactor::kTabletModePortrait,
  341. AppType::CROSTINI_APP);
  342. histogram_tester_->ExpectBucketCount(
  343. kCombinationHistogramName,
  344. static_cast<int>(DownEventMetric2::kCrostiniAppStylusTabletPortrait), 1);
  345. histogram_tester_->ExpectTotalCount(kCombinationHistogramName, 45);
  346. }
  347. } // namespace ash