Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(455)

Side by Side Diff: chrome/browser/policy/device_status_collector_unittest.cc

Issue 8702009: Add device status reports to policy requests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove some now-dead code. Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 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/policy/device_status_collector.h"
6
7 #include "base/message_loop.h"
8 #include "base/time.h"
9 #include "chrome/browser/idle.h"
10 #include "chrome/browser/policy/proto/device_management_backend.pb.h"
11 #include "chrome/browser/prefs/pref_service.h"
12 #include "chrome/test/base/testing_pref_service.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 using base::TimeDelta;
16 using base::Time;
17
18 namespace em = enterprise_management;
19
20 namespace {
21
22 class TestingDeviceStatusCollector : public policy::DeviceStatusCollector {
23 public:
24 explicit TestingDeviceStatusCollector(PrefService* local_state)
25 : policy::DeviceStatusCollector(local_state),
26 local_state_(local_state),
27 baseline_time_(Time::Now()) {
28 }
29
30 void Simulate(IdleState* states, int len) {
31 for (int i = 0; i < len; i++)
32 IdleStateCallback(states[i]);
33 }
34
35 void SimulateWithSleep(IdleState* states, int len, int ) {
36 for (int i = 0; i < len; i++)
37 IdleStateCallback(states[i]);
38 }
39
40 void set_max_stored_active_periods(unsigned int value) {
41 max_stored_active_periods_ = value;
42 }
43
44 protected:
45 virtual void CheckIdleState() OVERRIDE {
46 // This should never be called in testing, as it results in a dbus call.
47 NOTREACHED();
48 }
49
50 // Each time this is called, returns a time that is a fixed increment
51 // later than the previous time.
52 virtual Time GetCurrentTime() OVERRIDE {
53 static int call_count = 0;
54 return baseline_time_ + TimeDelta::FromSeconds(
55 policy::DeviceStatusCollector::kPollIntervalSeconds * call_count++);
56 }
57
58 private:
59 PrefService* local_state_;
60
61 // Baseline time for the fake times returned from GetCurrentTime().
62 // It doesn't really matter what this is, as long as it stays the same over
63 // the lifetime of the object.
64 Time baseline_time_;
65 };
66
67 // Return the total number of active milliseconds contained in a device
68 // status report.
69 int64 GetActiveMilliseconds(em::DeviceStatusReportRequest& status) {
70 int64 active_milliseconds = 0;
71 for (int i = 0; i < status.active_time_size(); i++) {
72 const em::TimePeriod& period = status.active_time(i);
73 active_milliseconds += period.end_timestamp() - period.start_timestamp();
74 }
75 return active_milliseconds;
76 }
77
78 } // namespace
79
80 namespace policy {
81
82 class DeviceStatusCollectorTest : public testing::Test {
83 public:
84 DeviceStatusCollectorTest()
85 : message_loop_(new MessageLoop),
86 prefs_(),
87 status_collector_(&prefs_) {
88 DeviceStatusCollector::RegisterPrefs(&prefs_);
89 }
90
91 protected:
92 // Convenience method.
93 int64 ActivePeriodMilliseconds() {
94 return policy::DeviceStatusCollector::kPollIntervalSeconds * 1000;
95 }
96
97 scoped_ptr<MessageLoop> message_loop_;
98
99 TestingPrefService prefs_;
100 TestingDeviceStatusCollector status_collector_;
101 em::DeviceStatusReportRequest status_;
102 };
103
104 TEST_F(DeviceStatusCollectorTest, AllIdle) {
105 IdleState test_states[] = {
106 IDLE_STATE_IDLE,
107 IDLE_STATE_IDLE,
108 IDLE_STATE_IDLE
109 };
110 // Test reporting with no data.
111 status_collector_.GetStatus(&status_);
112 EXPECT_EQ(0, status_.active_time_size());
113 EXPECT_EQ(0, GetActiveMilliseconds(status_));
114
115 // Test reporting with a single idle sample.
116 status_collector_.Simulate(test_states, 1);
117 status_collector_.GetStatus(&status_);
118 EXPECT_EQ(0, status_.active_time_size());
119 EXPECT_EQ(0, GetActiveMilliseconds(status_));
120
121 // Test reporting with multiple consecutive idle samples.
122 status_collector_.Simulate(test_states,
123 sizeof(test_states) / sizeof(IdleState));
124 status_collector_.GetStatus(&status_);
125 EXPECT_EQ(0, status_.active_time_size());
126 EXPECT_EQ(0, GetActiveMilliseconds(status_));
127 }
128
129 TEST_F(DeviceStatusCollectorTest, AllActive) {
130 IdleState test_states[] = {
131 IDLE_STATE_ACTIVE,
132 IDLE_STATE_ACTIVE,
133 IDLE_STATE_ACTIVE
134 };
135 // Test a single active sample.
136 status_collector_.Simulate(test_states, 1);
137 status_collector_.GetStatus(&status_);
138 EXPECT_EQ(1, status_.active_time_size());
139 EXPECT_EQ(1 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_));
140 status_.clear_active_time(); // Clear the result protobuf.
141
142 // Test multiple consecutive active samples -- they should be coalesced
143 // into a single active period.
144 status_collector_.Simulate(test_states,
145 sizeof(test_states) / sizeof(IdleState));
146 status_collector_.GetStatus(&status_);
147 EXPECT_EQ(1, status_.active_time_size());
148 EXPECT_EQ(3 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_));
149 }
150
151 TEST_F(DeviceStatusCollectorTest, MixedStates) {
152 IdleState test_states[] = {
153 IDLE_STATE_ACTIVE,
154 IDLE_STATE_IDLE,
155 IDLE_STATE_ACTIVE,
156 IDLE_STATE_ACTIVE,
157 IDLE_STATE_IDLE,
158 IDLE_STATE_IDLE,
159 IDLE_STATE_ACTIVE
160 };
161 status_collector_.Simulate(test_states,
162 sizeof(test_states) / sizeof(IdleState));
163 status_collector_.GetStatus(&status_);
164 EXPECT_EQ(3, status_.active_time_size());
165 EXPECT_EQ(4 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_));
166 }
167
168 TEST_F(DeviceStatusCollectorTest, StateKeptInPref) {
169 IdleState test_states[] = {
170 IDLE_STATE_ACTIVE,
171 IDLE_STATE_IDLE,
172 IDLE_STATE_ACTIVE,
173 IDLE_STATE_ACTIVE,
174 IDLE_STATE_IDLE,
175 IDLE_STATE_IDLE
176 };
177 status_collector_.Simulate(test_states,
178 sizeof(test_states) / sizeof(IdleState));
179
180 // Process the list a second time with a different collector.
181 // It should be able to count the active periods found by the first
182 // collector, because the results are stored in a pref.
183 TestingDeviceStatusCollector second_collector(&prefs_);
184 second_collector.Simulate(test_states,
185 sizeof(test_states) / sizeof(IdleState));
186
187 second_collector.GetStatus(&status_);
188 EXPECT_EQ(4, status_.active_time_size());
189 EXPECT_EQ(6 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_));
190 }
191
192 TEST_F(DeviceStatusCollectorTest, Times) {
193 IdleState test_states[] = {
194 IDLE_STATE_ACTIVE,
195 IDLE_STATE_IDLE,
196 IDLE_STATE_ACTIVE,
197 IDLE_STATE_ACTIVE,
198 IDLE_STATE_IDLE,
199 IDLE_STATE_IDLE
200 };
201 status_collector_.Simulate(test_states,
202 sizeof(test_states) / sizeof(IdleState));
203 status_collector_.GetStatus(&status_);
204 EXPECT_EQ(2, status_.active_time_size());
205
206 EXPECT_EQ(3 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_));
207 }
208
209 TEST_F(DeviceStatusCollectorTest, MaxStoredPeriods) {
210 IdleState test_states[] = {
211 IDLE_STATE_ACTIVE,
212 IDLE_STATE_IDLE
213 };
214 unsigned int max_periods = 10;
215
216 status_collector_.set_max_stored_active_periods(max_periods);
217
218 // Simulate 12 active periods.
219 for (int i = 0; i < 12; i++) {
220 status_collector_.Simulate(test_states,
221 sizeof(test_states) / sizeof(IdleState));
222 }
223
224 // Check that we don't exceed the max number of periods.
225 status_collector_.GetStatus(&status_);
226 EXPECT_EQ(static_cast<int>(max_periods), status_.active_time_size());
227 }
228
229 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/device_status_collector.cc ('k') | chrome/browser/policy/enterprise_metrics_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698