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