OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 "ash/metrics/user_metrics_recorder.h" | |
6 | |
7 #include "ash/shelf/shelf_layout_manager.h" | |
8 #include "ash/shell.h" | |
9 #include "ash/test/ash_test_base.h" | |
10 #include "ash/test/test_session_state_delegate.h" | |
11 #include "base/test/histogram_tester.h" | |
12 #include "ui/aura/window.h" | |
13 | |
14 namespace ash { | |
15 namespace { | |
16 | |
17 const char kAsh_ShelfAlignmentOverTime[] = "Ash.ShelfAlignmentOverTime"; | |
18 | |
19 const char kAsh_NumberOfVisibleWindowsInPrimaryDisplay[] = | |
20 "Ash.NumberOfVisibleWindowsInPrimaryDisplay"; | |
21 | |
22 const char kAsh_ActiveWindowShowTypeOverTime[] = | |
23 "Ash.ActiveWindowShowTypeOverTime"; | |
24 | |
25 // Test specific subclass of a UserMetricsRecorder that promotes the visibility | |
26 // of some protected level functions to enable testing. The repeating timer of | |
27 // the UserMetricsRecorder will NOT be started. Use the |RecordPeriodicMetrics| | |
28 // method to trigger a recording. | |
29 class TestUserMetricsRecorder : public UserMetricsRecorder { | |
pkotwicz
2015/04/17 13:45:38
Extending the real class is a common pattern in Ch
bruthig
2015/04/17 14:41:23
Out of curiosity when would it be necessary?
pkotwicz
2015/04/17 17:45:16
Extending the real class is necessary when the tes
sadrul
2015/04/17 22:55:39
In a number of places, we friend a TestApi class t
bruthig
2015/04/21 14:34:05
I've reworked it to use the TestAPI approach and I
| |
30 public: | |
31 TestUserMetricsRecorder(); | |
32 ~TestUserMetricsRecorder() override; | |
33 | |
34 using UserMetricsRecorder::RecordPeriodicMetrics; | |
35 using UserMetricsRecorder::UserIsActive; | |
36 | |
37 private: | |
38 DISALLOW_COPY_AND_ASSIGN(TestUserMetricsRecorder); | |
39 }; | |
40 | |
41 TestUserMetricsRecorder::TestUserMetricsRecorder() | |
42 : UserMetricsRecorder(false) { | |
43 } | |
44 | |
45 TestUserMetricsRecorder::~TestUserMetricsRecorder() { | |
46 } | |
47 | |
48 } // namespace | |
49 | |
50 // Test fixture for the UserMetricsRecorder class. | |
51 class UserMetricsRecorderTest : public test::AshTestBase { | |
52 public: | |
53 UserMetricsRecorderTest(); | |
54 ~UserMetricsRecorderTest() override; | |
55 | |
56 void SetUp() override; | |
57 void TearDown() override; | |
58 | |
59 // Convenience function to trigger the periodic metrics recording of the test | |
60 // target. | |
61 void RecordPeriodicMetrics(); | |
62 | |
63 // Sets the current user session to be active. | |
64 void SetUserActive(); | |
65 | |
66 // Sets the current user session to be inactive. | |
67 void SetUserNotActive(); | |
68 | |
69 TestUserMetricsRecorder& user_metrics_recorder() { | |
70 return user_metrics_recorder_; | |
71 } | |
72 | |
73 base::HistogramTester& histograms() { return histograms_; } | |
74 | |
75 test::TestSessionStateDelegate* session_state_delegate() { | |
76 return session_state_delegate_; | |
77 } | |
78 | |
79 private: | |
80 // The test target. | |
81 TestUserMetricsRecorder user_metrics_recorder_; | |
82 | |
83 // Histogram value verifier. | |
84 base::HistogramTester histograms_; | |
85 | |
86 // The current SessionStateDelegate used by the test fixture. Not owned. | |
87 test::TestSessionStateDelegate* session_state_delegate_; | |
88 | |
89 DISALLOW_COPY_AND_ASSIGN(UserMetricsRecorderTest); | |
90 }; | |
91 | |
92 UserMetricsRecorderTest::UserMetricsRecorderTest() { | |
93 } | |
94 | |
95 UserMetricsRecorderTest::~UserMetricsRecorderTest() { | |
96 } | |
97 | |
98 void UserMetricsRecorderTest::SetUp() { | |
99 test::AshTestBase::SetUp(); | |
100 | |
101 session_state_delegate_ = static_cast<ash::test::TestSessionStateDelegate*>( | |
102 ash::Shell::GetInstance()->session_state_delegate()); | |
103 } | |
104 | |
105 void UserMetricsRecorderTest::TearDown() { | |
106 session_state_delegate_ = nullptr; | |
107 | |
108 test::AshTestBase::TearDown(); | |
109 } | |
110 | |
111 void UserMetricsRecorderTest::RecordPeriodicMetrics() { | |
112 user_metrics_recorder_.RecordPeriodicMetrics(); | |
113 } | |
114 | |
115 void UserMetricsRecorderTest::SetUserActive() { | |
116 SetSessionStarted(true); | |
117 session_state_delegate()->UnlockScreen(); | |
118 ASSERT_TRUE(user_metrics_recorder().UserIsActive()); | |
119 } | |
120 | |
121 void UserMetricsRecorderTest::SetUserNotActive() { | |
122 SetSessionStarted(false); | |
123 session_state_delegate()->UnlockScreen(); | |
124 ASSERT_FALSE(user_metrics_recorder().UserIsActive()); | |
125 } | |
126 | |
127 // Verify that the user is active when logged in and the screen is unlocked. | |
128 TEST_F(UserMetricsRecorderTest, VerifyUserIsActiveWhenLoggedIn) { | |
129 SetSessionStarted(true); | |
130 session_state_delegate()->UnlockScreen(); | |
131 EXPECT_TRUE(user_metrics_recorder().UserIsActive()); | |
132 } | |
133 | |
134 // Verify that the user is not active when not logged in. | |
135 TEST_F(UserMetricsRecorderTest, VerifyUserIsNotActiveWhenNotLoggedIn) { | |
136 SetSessionStarted(false); | |
137 session_state_delegate()->UnlockScreen(); | |
138 EXPECT_FALSE(user_metrics_recorder().UserIsActive()); | |
139 } | |
140 | |
141 // Verify that the user is not active when logged in but the screen is locked. | |
142 TEST_F(UserMetricsRecorderTest, VerifyUserIsNotActiveWhenAccountIsLocked) { | |
143 SetSessionStarted(true); | |
144 session_state_delegate()->LockScreen(); | |
145 EXPECT_FALSE(user_metrics_recorder().UserIsActive()); | |
146 } | |
147 | |
148 // Verifies that the stats dependent on the presence of a ShelfLayoutManager | |
149 // are recorded when a ShelfLayoutManager exists. | |
150 // TODO(bruthig): Add a test to verify the stats are not recorded when a | |
151 // ShelfLayoutManager does not exist. | |
pkotwicz
2015/04/17 13:45:38
When does the ShelfLayoutManager not exist?
bruthig
2015/04/17 14:41:23
I'm not sure, I'm not familiar with this and only
pkotwicz
2015/04/17 17:45:16
I think that the test would 10x more useful if it
bruthig
2015/04/21 14:34:05
To the best of my knowledge a ShelfLayoutManager m
| |
152 TEST_F(UserMetricsRecorderTest, | |
153 VerifyStatsRecordedWhenAShelfLayoutManagerExists) { | |
154 SetUserActive(); | |
155 ASSERT_TRUE(ShelfLayoutManager::ForShelf(Shell::GetPrimaryRootWindow())); | |
156 RecordPeriodicMetrics(); | |
157 | |
158 histograms().ExpectTotalCount(kAsh_ShelfAlignmentOverTime, 1); | |
159 } | |
160 | |
161 // Verifies that the UserIsActive() dependent stats are not recorded when a | |
162 // user is not active. | |
163 TEST_F(UserMetricsRecorderTest, VerifyStatsRecordedWhenUserIsNotActive) { | |
164 SetUserNotActive(); | |
165 RecordPeriodicMetrics(); | |
166 | |
167 histograms().ExpectTotalCount(kAsh_NumberOfVisibleWindowsInPrimaryDisplay, 0); | |
168 } | |
169 | |
170 // Verifies that the UserIsActive() dependent stats are recorded when a user | |
171 // is active. | |
172 TEST_F(UserMetricsRecorderTest, VerifyStatsRecordedWhenUserIsActive) { | |
173 SetUserActive(); | |
174 RecordPeriodicMetrics(); | |
175 | |
176 histograms().ExpectTotalCount(kAsh_NumberOfVisibleWindowsInPrimaryDisplay, 1); | |
177 } | |
178 | |
179 // Verifies which stats are always recorded and are not dependent on conditional | |
180 // logic for when they are recorded. | |
181 TEST_F(UserMetricsRecorderTest, VerifyStatsRecordedByRecordPeriodicMetrics) { | |
182 SetUserActive(); | |
183 RecordPeriodicMetrics(); | |
184 | |
185 histograms().ExpectTotalCount(kAsh_ActiveWindowShowTypeOverTime, 1); | |
186 } | |
187 | |
188 } // namespace ash | |
OLD | NEW |