File: app_list_feature_usage_metrics_unittest.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (197 lines) | stat: -rw-r--r-- 7,948 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/app_list/app_list_feature_usage_metrics.h"

#include "ash/app_list/app_list_controller_impl.h"
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_switches.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "base/command_line.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "chromeos/ash/components/feature_usage/feature_usage_metrics.h"
#include "components/user_manager/user_type.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash {
namespace {

// Expanded metric names recorded by FeatureUsageMetrics.
constexpr char kClamshellMetric[] = "ChromeOS.FeatureUsage.ClamshellLauncher";
constexpr char kClamshellUsetimeMetric[] =
    "ChromeOS.FeatureUsage.ClamshellLauncher.Usetime";
constexpr char kTabletMetric[] = "ChromeOS.FeatureUsage.TabletLauncher";
constexpr char kTabletUsetimeMetric[] =
    "ChromeOS.FeatureUsage.TabletLauncher.Usetime";

// Shorten these identifiers for readability.
constexpr int kEligible =
    static_cast<int>(feature_usage::FeatureUsageMetrics::Event::kEligible);
constexpr int kEnabled =
    static_cast<int>(feature_usage::FeatureUsageMetrics::Event::kEnabled);
constexpr int kUsedWithSuccess = static_cast<int>(
    feature_usage::FeatureUsageMetrics::Event::kUsedWithSuccess);

// Uses NoSessionAshTestBase because some tests need to simulate kiosk login.
class AppListFeatureUsageMetricsTest : public NoSessionAshTestBase {
 public:
  AppListFeatureUsageMetricsTest()
      : NoSessionAshTestBase(
            base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
  ~AppListFeatureUsageMetricsTest() override = default;

  // Simulates a device that supports tablet mode.
  void SimulateTabletModeSupport() {
    base::CommandLine::ForCurrentProcess()->AppendSwitch(
        switches::kAshEnableTabletMode);
    auto* tablet_mode_controller = Shell::Get()->tablet_mode_controller();
    tablet_mode_controller->OnECLidAngleDriverStatusChanged(
        /*is_supported=*/true);
    tablet_mode_controller->OnDeviceListsComplete();
  }

  void FastForwardBy(base::TimeDelta delta) {
    task_environment()->FastForwardBy(delta);
  }

  // Fast forwards the clock past the time when FeatureUsageMetrics reports
  // its initial set of eligible/enabled metrics.
  void FastForwardPastMetricsReportingInterval() {
    FastForwardBy(feature_usage::FeatureUsageMetrics::kInitialInterval);
  }

  base::HistogramTester histograms_;
};

TEST_F(AppListFeatureUsageMetricsTest, CountsStartAtZero) {
  SimulateUserLogin("user@gmail.com");

  histograms_.ExpectTotalCount(kClamshellMetric, 0);
  histograms_.ExpectTotalCount(kClamshellUsetimeMetric, 0);
  histograms_.ExpectTotalCount(kTabletMetric, 0);
  histograms_.ExpectTotalCount(kTabletUsetimeMetric, 0);
}

TEST_F(AppListFeatureUsageMetricsTest, InitialMetricsWithoutTabletModeSupport) {
  ASSERT_FALSE(Shell::Get()->tablet_mode_controller()->CanEnterTabletMode());

  SimulateUserLogin("user@gmail.com");
  FastForwardPastMetricsReportingInterval();

  histograms_.ExpectBucketCount(kClamshellMetric, kEligible, 1);
  histograms_.ExpectBucketCount(kClamshellMetric, kEnabled, 1);
  // Not eligible for tablet mode.
  histograms_.ExpectBucketCount(kTabletMetric, kEligible, 0);
  histograms_.ExpectBucketCount(kTabletMetric, kEnabled, 0);
}

TEST_F(AppListFeatureUsageMetricsTest, InitialMetricsWithTabletModeSupport) {
  SimulateTabletModeSupport();
  ASSERT_TRUE(Shell::Get()->tablet_mode_controller()->CanEnterTabletMode());

  SimulateUserLogin("user@gmail.com");
  FastForwardPastMetricsReportingInterval();

  histograms_.ExpectBucketCount(kClamshellMetric, kEligible, 1);
  histograms_.ExpectBucketCount(kClamshellMetric, kEnabled, 1);
  // Eligible for tablet mode.
  histograms_.ExpectBucketCount(kTabletMetric, kEligible, 1);
  histograms_.ExpectBucketCount(kTabletMetric, kEnabled, 1);
}

TEST_F(AppListFeatureUsageMetricsTest, NotEligibleInKioskMode) {
  SimulateKioskMode(user_manager::USER_TYPE_KIOSK_APP);
  FastForwardPastMetricsReportingInterval();

  histograms_.ExpectBucketCount(kClamshellMetric, kEligible, 0);
  histograms_.ExpectBucketCount(kClamshellMetric, kEnabled, 0);
  histograms_.ExpectBucketCount(kTabletMetric, kEligible, 0);
  histograms_.ExpectBucketCount(kTabletMetric, kEnabled, 0);
}

TEST_F(AppListFeatureUsageMetricsTest, ShowAndHideLauncherInClamshell) {
  SimulateUserLogin("user@gmail.com");
  Shell::Get()->app_list_controller()->ShowAppList(
      AppListShowSource::kSearchKey);
  histograms_.ExpectBucketCount(kClamshellMetric, kUsedWithSuccess, 1);

  const base::TimeDelta kUsetime = base::Seconds(2);
  FastForwardBy(kUsetime);
  Shell::Get()->app_list_controller()->DismissAppList();
  histograms_.ExpectTimeBucketCount(kClamshellUsetimeMetric, kUsetime, 1);

  // Tablet usage is not recorded.
  histograms_.ExpectTotalCount(kTabletUsetimeMetric, 0);
}

TEST_F(AppListFeatureUsageMetricsTest, ShowAndHideLauncherInTablet) {
  SimulateTabletModeSupport();
  ASSERT_TRUE(Shell::Get()->tablet_mode_controller()->CanEnterTabletMode());
  SimulateUserLogin("user@gmail.com");

  // Entering tablet mode shows the home screen launcher.
  Shell::Get()->tablet_mode_controller()->SetEnabledForTest(true);
  histograms_.ExpectBucketCount(kTabletMetric, kUsedWithSuccess, 1);

  const base::TimeDelta kUsetime = base::Seconds(2);
  FastForwardBy(kUsetime);
  // Creating a window hides the launcher.
  std::unique_ptr<views::Widget> widget = CreateTestWidget();
  histograms_.ExpectTimeBucketCount(kTabletUsetimeMetric, kUsetime, 1);

  // Clamshell usage is not recorded.
  histograms_.ExpectTotalCount(kClamshellUsetimeMetric, 0);
}

TEST_F(AppListFeatureUsageMetricsTest,
       EnterTabletModeWithLauncherAndWindowOpen) {
  SimulateTabletModeSupport();
  ASSERT_TRUE(Shell::Get()->tablet_mode_controller()->CanEnterTabletMode());
  SimulateUserLogin("user@gmail.com");
  std::unique_ptr<views::Widget> widget = CreateTestWidget();
  Shell::Get()->app_list_controller()->ShowAppList(
      AppListShowSource::kSearchKey);

  // Entering tablet mode with a window open does not show the launcher.
  Shell::Get()->tablet_mode_controller()->SetEnabledForTest(true);
  FastForwardBy(base::Seconds(1));
  histograms_.ExpectTotalCount(kTabletUsetimeMetric, 0);

  // Show the tablet launcher for 1 second.
  Shell::Get()->app_list_controller()->GoHome(GetPrimaryDisplay().id());
  FastForwardBy(base::Seconds(1));
  Shell::Get()->tablet_mode_controller()->SetEnabledForTest(false);

  // Only 1 second of usage is recorded.
  histograms_.ExpectTimeBucketCount(kTabletUsetimeMetric, base::Seconds(1), 1);
}

TEST_F(AppListFeatureUsageMetricsTest, OpenClamshellThenTabletThenExit) {
  SimulateTabletModeSupport();
  SimulateUserLogin("user@gmail.com");

  Shell::Get()->app_list_controller()->ShowAppList(
      AppListShowSource::kSearchKey);
  histograms_.ExpectBucketCount(kClamshellMetric, kUsedWithSuccess, 1);

  // Switching from clamshell to tablet with the launcher open records usage
  // time for clamshell launcher and starts a tablet launcher usage session.
  const base::TimeDelta kClamshellUsetime = base::Seconds(1);
  FastForwardBy(kClamshellUsetime);
  Shell::Get()->tablet_mode_controller()->SetEnabledForTest(true);
  histograms_.ExpectTimeBucketCount(kClamshellUsetimeMetric, kClamshellUsetime,
                                    1);
  histograms_.ExpectBucketCount(kTabletMetric, kUsedWithSuccess, 1);

  // Ending tablet mode records usage time for tablet launcher.
  const base::TimeDelta kTabletUsetime = base::Seconds(2);
  FastForwardBy(kTabletUsetime);
  Shell::Get()->tablet_mode_controller()->SetEnabledForTest(false);
  histograms_.ExpectTimeBucketCount(kTabletUsetimeMetric, kTabletUsetime, 1);
}

}  // namespace
}  // namespace ash