app_list_feature_usage_metrics_unittest.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // Copyright 2021 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/app_list/app_list_feature_usage_metrics.h"
  5. #include "ash/app_list/app_list_controller_impl.h"
  6. #include "ash/constants/ash_features.h"
  7. #include "ash/constants/ash_switches.h"
  8. #include "ash/shell.h"
  9. #include "ash/test/ash_test_base.h"
  10. #include "base/command_line.h"
  11. #include "base/test/metrics/histogram_tester.h"
  12. #include "base/test/scoped_feature_list.h"
  13. #include "chromeos/ash/components/feature_usage/feature_usage_metrics.h"
  14. #include "components/user_manager/user_type.h"
  15. #include "testing/gtest/include/gtest/gtest.h"
  16. namespace ash {
  17. namespace {
  18. // Expanded metric names recorded by FeatureUsageMetrics.
  19. constexpr char kClamshellMetric[] = "ChromeOS.FeatureUsage.ClamshellLauncher";
  20. constexpr char kClamshellUsetimeMetric[] =
  21. "ChromeOS.FeatureUsage.ClamshellLauncher.Usetime";
  22. constexpr char kTabletMetric[] = "ChromeOS.FeatureUsage.TabletLauncher";
  23. constexpr char kTabletUsetimeMetric[] =
  24. "ChromeOS.FeatureUsage.TabletLauncher.Usetime";
  25. // Shorten these identifiers for readability.
  26. constexpr int kEligible =
  27. static_cast<int>(feature_usage::FeatureUsageMetrics::Event::kEligible);
  28. constexpr int kEnabled =
  29. static_cast<int>(feature_usage::FeatureUsageMetrics::Event::kEnabled);
  30. constexpr int kUsedWithSuccess = static_cast<int>(
  31. feature_usage::FeatureUsageMetrics::Event::kUsedWithSuccess);
  32. // Uses NoSessionAshTestBase because some tests need to simulate kiosk login.
  33. // Tests are optionally parameterized by feature ProductivityLauncher.
  34. class AppListFeatureUsageMetricsTest
  35. : public NoSessionAshTestBase,
  36. public testing::WithParamInterface<bool> {
  37. public:
  38. AppListFeatureUsageMetricsTest()
  39. : NoSessionAshTestBase(
  40. base::test::TaskEnvironment::TimeSource::MOCK_TIME) {
  41. // Support both TEST_F and TEST_P.
  42. if (testing::UnitTest::GetInstance()->current_test_info()->value_param()) {
  43. feature_list_.InitWithFeatureState(features::kProductivityLauncher,
  44. GetParam());
  45. }
  46. }
  47. ~AppListFeatureUsageMetricsTest() override = default;
  48. // Simulates a device that supports tablet mode.
  49. void SimulateTabletModeSupport() {
  50. base::CommandLine::ForCurrentProcess()->AppendSwitch(
  51. switches::kAshEnableTabletMode);
  52. Shell::Get()->tablet_mode_controller()->OnECLidAngleDriverStatusChanged(
  53. /*is_supported=*/true);
  54. }
  55. void FastForwardBy(base::TimeDelta delta) {
  56. task_environment()->FastForwardBy(delta);
  57. }
  58. // Fast forwards the clock past the time when FeatureUsageMetrics reports
  59. // its initial set of eligible/enabled metrics.
  60. void FastForwardPastMetricsReportingInterval() {
  61. FastForwardBy(feature_usage::FeatureUsageMetrics::kInitialInterval);
  62. }
  63. base::test::ScopedFeatureList feature_list_;
  64. base::HistogramTester histograms_;
  65. };
  66. INSTANTIATE_TEST_SUITE_P(ProductivityLauncher,
  67. AppListFeatureUsageMetricsTest,
  68. testing::Bool());
  69. TEST_F(AppListFeatureUsageMetricsTest, CountsStartAtZero) {
  70. SimulateUserLogin("user@gmail.com");
  71. histograms_.ExpectTotalCount(kClamshellMetric, 0);
  72. histograms_.ExpectTotalCount(kClamshellUsetimeMetric, 0);
  73. histograms_.ExpectTotalCount(kTabletMetric, 0);
  74. histograms_.ExpectTotalCount(kTabletUsetimeMetric, 0);
  75. }
  76. TEST_F(AppListFeatureUsageMetricsTest, InitialMetricsWithoutTabletModeSupport) {
  77. ASSERT_FALSE(Shell::Get()->tablet_mode_controller()->CanEnterTabletMode());
  78. SimulateUserLogin("user@gmail.com");
  79. FastForwardPastMetricsReportingInterval();
  80. histograms_.ExpectBucketCount(kClamshellMetric, kEligible, 1);
  81. histograms_.ExpectBucketCount(kClamshellMetric, kEnabled, 1);
  82. // Not eligible for tablet mode.
  83. histograms_.ExpectBucketCount(kTabletMetric, kEligible, 0);
  84. histograms_.ExpectBucketCount(kTabletMetric, kEnabled, 0);
  85. }
  86. TEST_F(AppListFeatureUsageMetricsTest, InitialMetricsWithTabletModeSupport) {
  87. SimulateTabletModeSupport();
  88. ASSERT_TRUE(Shell::Get()->tablet_mode_controller()->CanEnterTabletMode());
  89. SimulateUserLogin("user@gmail.com");
  90. FastForwardPastMetricsReportingInterval();
  91. histograms_.ExpectBucketCount(kClamshellMetric, kEligible, 1);
  92. histograms_.ExpectBucketCount(kClamshellMetric, kEnabled, 1);
  93. // Eligible for tablet mode.
  94. histograms_.ExpectBucketCount(kTabletMetric, kEligible, 1);
  95. histograms_.ExpectBucketCount(kTabletMetric, kEnabled, 1);
  96. }
  97. TEST_F(AppListFeatureUsageMetricsTest, NotEligibleInKioskMode) {
  98. SimulateKioskMode(user_manager::USER_TYPE_KIOSK_APP);
  99. FastForwardPastMetricsReportingInterval();
  100. histograms_.ExpectBucketCount(kClamshellMetric, kEligible, 0);
  101. histograms_.ExpectBucketCount(kClamshellMetric, kEnabled, 0);
  102. histograms_.ExpectBucketCount(kTabletMetric, kEligible, 0);
  103. histograms_.ExpectBucketCount(kTabletMetric, kEnabled, 0);
  104. }
  105. TEST_P(AppListFeatureUsageMetricsTest, ShowAndHideLauncherInClamshell) {
  106. SimulateUserLogin("user@gmail.com");
  107. Shell::Get()->app_list_controller()->ShowAppList();
  108. histograms_.ExpectBucketCount(kClamshellMetric, kUsedWithSuccess, 1);
  109. const base::TimeDelta kUsetime = base::Seconds(2);
  110. FastForwardBy(kUsetime);
  111. Shell::Get()->app_list_controller()->DismissAppList();
  112. histograms_.ExpectTimeBucketCount(kClamshellUsetimeMetric, kUsetime, 1);
  113. // Tablet usage is not recorded.
  114. histograms_.ExpectTotalCount(kTabletUsetimeMetric, 0);
  115. }
  116. TEST_P(AppListFeatureUsageMetricsTest, ShowAndHideLauncherInTablet) {
  117. SimulateTabletModeSupport();
  118. ASSERT_TRUE(Shell::Get()->tablet_mode_controller()->CanEnterTabletMode());
  119. SimulateUserLogin("user@gmail.com");
  120. // Entering tablet mode shows the home screen launcher.
  121. Shell::Get()->tablet_mode_controller()->SetEnabledForTest(true);
  122. histograms_.ExpectBucketCount(kTabletMetric, kUsedWithSuccess, 1);
  123. const base::TimeDelta kUsetime = base::Seconds(2);
  124. FastForwardBy(kUsetime);
  125. // Creating a window hides the launcher.
  126. std::unique_ptr<views::Widget> widget = CreateTestWidget();
  127. histograms_.ExpectTimeBucketCount(kTabletUsetimeMetric, kUsetime, 1);
  128. // Clamshell usage is not recorded.
  129. histograms_.ExpectTotalCount(kClamshellUsetimeMetric, 0);
  130. }
  131. TEST_P(AppListFeatureUsageMetricsTest,
  132. EnterTabletModeWithLauncherAndWindowOpen) {
  133. SimulateTabletModeSupport();
  134. ASSERT_TRUE(Shell::Get()->tablet_mode_controller()->CanEnterTabletMode());
  135. SimulateUserLogin("user@gmail.com");
  136. std::unique_ptr<views::Widget> widget = CreateTestWidget();
  137. Shell::Get()->app_list_controller()->ShowAppList();
  138. // Entering tablet mode with a window open does not show the launcher.
  139. Shell::Get()->tablet_mode_controller()->SetEnabledForTest(true);
  140. FastForwardBy(base::Seconds(1));
  141. histograms_.ExpectTotalCount(kTabletUsetimeMetric, 0);
  142. // Show the tablet launcher for 1 second.
  143. Shell::Get()->app_list_controller()->GoHome(GetPrimaryDisplay().id());
  144. FastForwardBy(base::Seconds(1));
  145. Shell::Get()->tablet_mode_controller()->SetEnabledForTest(false);
  146. // Only 1 second of usage is recorded.
  147. histograms_.ExpectTimeBucketCount(kTabletUsetimeMetric, base::Seconds(1), 1);
  148. }
  149. TEST_P(AppListFeatureUsageMetricsTest, OpenClamshellThenTabletThenExit) {
  150. SimulateTabletModeSupport();
  151. SimulateUserLogin("user@gmail.com");
  152. Shell::Get()->app_list_controller()->ShowAppList();
  153. histograms_.ExpectBucketCount(kClamshellMetric, kUsedWithSuccess, 1);
  154. // Switching from clamshell to tablet with the launcher open records usage
  155. // time for clamshell launcher and starts a tablet launcher usage session.
  156. const base::TimeDelta kClamshellUsetime = base::Seconds(1);
  157. FastForwardBy(kClamshellUsetime);
  158. Shell::Get()->tablet_mode_controller()->SetEnabledForTest(true);
  159. histograms_.ExpectTimeBucketCount(kClamshellUsetimeMetric, kClamshellUsetime,
  160. 1);
  161. histograms_.ExpectBucketCount(kTabletMetric, kUsedWithSuccess, 1);
  162. // Ending tablet mode records usage time for tablet launcher.
  163. const base::TimeDelta kTabletUsetime = base::Seconds(2);
  164. FastForwardBy(kTabletUsetime);
  165. Shell::Get()->tablet_mode_controller()->SetEnabledForTest(false);
  166. histograms_.ExpectTimeBucketCount(kTabletUsetimeMetric, kTabletUsetime, 1);
  167. }
  168. } // namespace
  169. } // namespace ash