1 // Copyright 2020 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 "chrome/browser/ui/webui/settings/chromeos/search/per_session_settings_user_action_tracker.h"
6 
7 #include "base/test/metrics/histogram_tester.h"
8 #include "base/test/task_environment.h"
9 #include "base/time/time.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 
12 namespace chromeos {
13 namespace settings {
14 
15 class PerSessionSettingsUserActionTrackerTest : public testing::Test {
16  protected:
17   PerSessionSettingsUserActionTrackerTest() = default;
18   ~PerSessionSettingsUserActionTrackerTest() override = default;
19 
20   base::test::TaskEnvironment task_environment_{
21       base::test::TaskEnvironment::TimeSource::MOCK_TIME};
22   base::HistogramTester histogram_tester_;
23   PerSessionSettingsUserActionTracker tracker_;
24 };
25 
TEST_F(PerSessionSettingsUserActionTrackerTest,TestRecordMetrics)26 TEST_F(PerSessionSettingsUserActionTrackerTest, TestRecordMetrics) {
27   // Focus the page, perform some tasks, and change a setting.
28   tracker_.RecordPageFocus();
29   tracker_.RecordClick();
30   tracker_.RecordNavigation();
31   tracker_.RecordSearch();
32   task_environment_.FastForwardBy(base::TimeDelta::FromSeconds(10));
33   tracker_.RecordSettingChange();
34 
35   // The "first change" metrics should have been logged.
36   histogram_tester_.ExpectTotalCount(
37       "ChromeOS.Settings.NumClicksUntilChange.FirstChange",
38       /*count=*/1);
39   histogram_tester_.ExpectTotalCount(
40       "ChromeOS.Settings.NumNavigationsUntilChange.FirstChange",
41       /*count=*/1);
42   histogram_tester_.ExpectTotalCount(
43       "ChromeOS.Settings.NumSearchesUntilChange.FirstChange",
44       /*count=*/1);
45   histogram_tester_.ExpectTimeBucketCount(
46       "ChromeOS.Settings.TimeUntilChange.FirstChange",
47       /*sample=*/base::TimeDelta::FromSeconds(10),
48       /*count=*/1);
49 
50   // Without leaving the page, perform some more tasks, and change another
51   // setting.
52   tracker_.RecordClick();
53   tracker_.RecordNavigation();
54   tracker_.RecordSearch();
55   task_environment_.FastForwardBy(base::TimeDelta::FromSeconds(10));
56   tracker_.RecordSettingChange();
57 
58   // The "subsequent change" metrics should have been logged.
59   histogram_tester_.ExpectTotalCount(
60       "ChromeOS.Settings.NumClicksUntilChange.SubsequentChange",
61       /*count=*/1);
62   histogram_tester_.ExpectTotalCount(
63       "ChromeOS.Settings.NumNavigationsUntilChange.SubsequentChange",
64       /*count=*/1);
65   histogram_tester_.ExpectTotalCount(
66       "ChromeOS.Settings.NumSearchesUntilChange.SubsequentChange",
67       /*count=*/1);
68   histogram_tester_.ExpectTimeBucketCount(
69       "ChromeOS.Settings.TimeUntilChange.SubsequentChange",
70       /*sample=*/base::TimeDelta::FromSeconds(10),
71       /*count=*/1);
72 
73   // Repeat this, but only after 100ms. This is lower than the minimum value
74   // required for this metric, so it should be ignored.
75   tracker_.RecordClick();
76   tracker_.RecordNavigation();
77   tracker_.RecordSearch();
78   task_environment_.FastForwardBy(base::TimeDelta::FromMilliseconds(100));
79   tracker_.RecordSettingChange();
80 
81   // No additional logging should have occurred, so make the same verifications
82   // as above.
83   histogram_tester_.ExpectTotalCount(
84       "ChromeOS.Settings.NumClicksUntilChange.SubsequentChange",
85       /*count=*/1);
86   histogram_tester_.ExpectTotalCount(
87       "ChromeOS.Settings.NumNavigationsUntilChange.SubsequentChange",
88       /*count=*/1);
89   histogram_tester_.ExpectTotalCount(
90       "ChromeOS.Settings.NumSearchesUntilChange.SubsequentChange",
91       /*count=*/1);
92   histogram_tester_.ExpectTimeBucketCount(
93       "ChromeOS.Settings.TimeUntilChange.SubsequentChange",
94       /*sample=*/base::TimeDelta::FromSeconds(10),
95       /*count=*/1);
96 
97   // Repeat this once more, and verify that the counts increased.
98   tracker_.RecordClick();
99   tracker_.RecordNavigation();
100   tracker_.RecordSearch();
101   task_environment_.FastForwardBy(base::TimeDelta::FromSeconds(10));
102   tracker_.RecordSettingChange();
103 
104   // The "subsequent change" metrics should have been logged.
105   histogram_tester_.ExpectTotalCount(
106       "ChromeOS.Settings.NumClicksUntilChange.SubsequentChange",
107       /*count=*/2);
108   histogram_tester_.ExpectTotalCount(
109       "ChromeOS.Settings.NumNavigationsUntilChange.SubsequentChange",
110       /*count=*/2);
111   histogram_tester_.ExpectTotalCount(
112       "ChromeOS.Settings.NumSearchesUntilChange.SubsequentChange",
113       /*count=*/2);
114   histogram_tester_.ExpectTimeBucketCount(
115       "ChromeOS.Settings.TimeUntilChange.SubsequentChange",
116       /*sample=*/base::TimeDelta::FromSeconds(10),
117       /*count=*/2);
118 }
119 
TEST_F(PerSessionSettingsUserActionTrackerTest,TestBlurAndFocus)120 TEST_F(PerSessionSettingsUserActionTrackerTest, TestBlurAndFocus) {
121   // Focus the page, click, and change a setting.
122   tracker_.RecordPageFocus();
123   tracker_.RecordClick();
124   task_environment_.FastForwardBy(base::TimeDelta::FromSeconds(1));
125   tracker_.RecordSettingChange();
126   histogram_tester_.ExpectTotalCount(
127       "ChromeOS.Settings.NumClicksUntilChange.FirstChange",
128       /*count=*/1);
129   histogram_tester_.ExpectTimeBucketCount(
130       "ChromeOS.Settings.TimeUntilChange.FirstChange",
131       /*sample=*/base::TimeDelta::FromSeconds(1),
132       /*count=*/1);
133 
134   // Blur for 59 seconds (not quite a minute), click, and change a setting.
135   // Since the blur was under a minute, this should count for the "subsequent
136   // change" metrics.
137   tracker_.RecordPageBlur();
138   task_environment_.FastForwardBy(base::TimeDelta::FromSeconds(59));
139   tracker_.RecordPageFocus();
140   tracker_.RecordClick();
141   tracker_.RecordSettingChange();
142   histogram_tester_.ExpectTimeBucketCount(
143       "ChromeOS.Settings.BlurredWindowDuration",
144       /*sample=*/base::TimeDelta::FromSeconds(59),
145       /*count=*/1);
146   histogram_tester_.ExpectTotalCount(
147       "ChromeOS.Settings.NumClicksUntilChange.SubsequentChange",
148       /*count=*/1);
149   histogram_tester_.ExpectTimeBucketCount(
150       "ChromeOS.Settings.TimeUntilChange.SubsequentChange",
151       /*sample=*/base::TimeDelta::FromSeconds(59),
152       /*count=*/1);
153 
154   // Now, blur for a full minute, click, and change a setting. Since the blur
155   // was a full minute, this should count for the "first change" metrics.
156   tracker_.RecordPageBlur();
157   task_environment_.FastForwardBy(base::TimeDelta::FromMinutes(1));
158   tracker_.RecordPageFocus();
159   task_environment_.FastForwardBy(base::TimeDelta::FromSeconds(5));
160   tracker_.RecordClick();
161   tracker_.RecordSettingChange();
162   histogram_tester_.ExpectTimeBucketCount(
163       "ChromeOS.Settings.BlurredWindowDuration",
164       /*sample=*/base::TimeDelta::FromMinutes(1),
165       /*count=*/2);
166   histogram_tester_.ExpectTotalCount(
167       "ChromeOS.Settings.NumClicksUntilChange.FirstChange",
168       /*count=*/2);
169   histogram_tester_.ExpectTimeBucketCount(
170       "ChromeOS.Settings.TimeUntilChange.FirstChange",
171       /*sample=*/base::TimeDelta::FromSeconds(5),
172       /*count=*/1);
173 }
174 
175 }  // namespace settings.
176 }  // namespace chromeos.
177