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
base / test / metrics / user_action_tester_unittest.cc [blame]
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/test/metrics/user_action_tester.h"
#include "base/metrics/user_metrics.h"
#include "base/metrics/user_metrics_action.h"
#include "base/test/scoped_mock_clock_override.h"
#include "base/time/time.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::UnorderedElementsAre;
namespace base {
namespace {
const char kUserAction1[] = "user.action.1";
const char kUserAction2[] = "user.action.2";
const char kUserAction3[] = "user.action.3";
// Record an action and cause all ActionCallback observers to be notified.
void RecordAction(const char user_action[]) {
base::RecordAction(base::UserMetricsAction(user_action));
}
} // namespace
// Verify user action counts are zero initially.
TEST(UserActionTesterTest, GetActionCountWhenNoActionsHaveBeenRecorded) {
UserActionTester user_action_tester;
EXPECT_EQ(0, user_action_tester.GetActionCount(kUserAction1));
}
// Verify user action counts are zero initially.
TEST(UserActionTesterTest, GetActionTimesWhenNoActionsHaveBeenRecorded) {
UserActionTester user_action_tester;
EXPECT_TRUE(user_action_tester.GetActionTimes(kUserAction1).empty());
}
// Verify user action counts are tracked properly.
TEST(UserActionTesterTest, GetActionCountWhenActionsHaveBeenRecorded) {
UserActionTester user_action_tester;
RecordAction(kUserAction1);
RecordAction(kUserAction2);
RecordAction(kUserAction2);
EXPECT_EQ(1, user_action_tester.GetActionCount(kUserAction1));
EXPECT_EQ(2, user_action_tester.GetActionCount(kUserAction2));
EXPECT_EQ(0, user_action_tester.GetActionCount(kUserAction3));
}
// Verify user action times are tracked properly.
TEST(UserActionTesterTest, GetActionTimesWhenActionsHaveBeenRecorded) {
ScopedMockClockOverride clock;
UserActionTester user_action_tester;
TimeTicks t1 = TimeTicks::Now();
RecordAction(kUserAction1);
clock.Advance(Minutes(10));
TimeTicks t2 = TimeTicks::Now();
RecordAction(kUserAction2);
clock.Advance(Minutes(20));
TimeTicks t3 = TimeTicks::Now();
RecordAction(kUserAction3);
EXPECT_THAT(user_action_tester.GetActionTimes(kUserAction1),
UnorderedElementsAre(t1));
EXPECT_THAT(user_action_tester.GetActionTimes(kUserAction2),
UnorderedElementsAre(t2));
EXPECT_THAT(user_action_tester.GetActionTimes(kUserAction3),
UnorderedElementsAre(t3));
}
// Verify no seg faults occur when resetting action counts when none have been
// recorded.
TEST(UserActionTesterTest, ResetCountsWhenNoActionsHaveBeenRecorded) {
UserActionTester user_action_tester;
user_action_tester.ResetCounts();
}
// Verify user action counts are set to zero on a ResetCounts.
TEST(UserActionTesterTest, ResetCountsWhenActionsHaveBeenRecorded) {
UserActionTester user_action_tester;
RecordAction(kUserAction1);
RecordAction(kUserAction1);
RecordAction(kUserAction2);
user_action_tester.ResetCounts();
EXPECT_EQ(0, user_action_tester.GetActionCount(kUserAction1));
EXPECT_EQ(0, user_action_tester.GetActionCount(kUserAction2));
EXPECT_EQ(0, user_action_tester.GetActionCount(kUserAction3));
}
// Verify user action times are cleared on a ResetCounts.
TEST(UserActionTesterTest, ResetTimesWhenActionsHaveBeenRecorded) {
UserActionTester user_action_tester;
RecordAction(kUserAction1);
RecordAction(kUserAction1);
RecordAction(kUserAction2);
user_action_tester.ResetCounts();
EXPECT_TRUE(user_action_tester.GetActionTimes(kUserAction1).empty());
EXPECT_TRUE(user_action_tester.GetActionTimes(kUserAction2).empty());
EXPECT_TRUE(user_action_tester.GetActionTimes(kUserAction3).empty());
}
// Verify the UserActionsTester is notified when base::RecordAction is called.
TEST(UserActionTesterTest, VerifyUserActionTesterListensForUserActions) {
ScopedMockClockOverride clock;
UserActionTester user_action_tester;
TimeTicks time = TimeTicks::Now();
base::RecordAction(base::UserMetricsAction(kUserAction1));
EXPECT_EQ(1, user_action_tester.GetActionCount(kUserAction1));
EXPECT_THAT(user_action_tester.GetActionTimes(kUserAction1),
UnorderedElementsAre(time));
}
// Verify the UserActionsTester is notified when base::RecordComputedAction is
// called.
TEST(UserActionTesterTest,
VerifyUserActionTesterListensForComputedUserActions) {
ScopedMockClockOverride clock;
UserActionTester user_action_tester;
TimeTicks time = TimeTicks::Now();
base::RecordComputedAction(kUserAction1);
EXPECT_EQ(1, user_action_tester.GetActionCount(kUserAction1));
EXPECT_THAT(user_action_tester.GetActionTimes(kUserAction1),
UnorderedElementsAre(time));
}
// Verify the UserActionsTester is notified when base::RecordComputedActionAt is
// called.
TEST(UserActionTesterTest,
VerifyUserActionTesterListensForComputedUserActionAt) {
UserActionTester user_action_tester;
TimeTicks time = TimeTicks::Now() - Minutes(10);
base::RecordComputedActionAt(kUserAction1, time);
EXPECT_EQ(1, user_action_tester.GetActionCount(kUserAction1));
EXPECT_THAT(user_action_tester.GetActionTimes(kUserAction1),
UnorderedElementsAre(time));
}
// Verify the UserActionsTester is notified when base::RecordComputedActionSince
// is called.
TEST(UserActionTesterTest,
VerifyUserActionTesterListensForComputedUserActionSince) {
ScopedMockClockOverride clock;
UserActionTester user_action_tester;
TimeTicks time = TimeTicks::Now();
base::RecordComputedActionSince(kUserAction1, Minutes(20));
TimeTicks expected_time = time - Minutes(20);
EXPECT_EQ(1, user_action_tester.GetActionCount(kUserAction1));
EXPECT_THAT(user_action_tester.GetActionTimes(kUserAction1),
UnorderedElementsAre(expected_time));
}
} // namespace base