OLD | NEW |
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <utime.h> | 5 #include <utime.h> |
6 | 6 |
7 #include <base/file_util.h> | 7 #include <base/file_util.h> |
8 #include <gtest/gtest.h> | 8 #include <gtest/gtest.h> |
9 | 9 |
10 #include "counter_mock.h" | 10 #include "counter_mock.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 // This class allows a TimeTicks object to be initialized with seconds | 31 // This class allows a TimeTicks object to be initialized with seconds |
32 // (rather than microseconds) through the protected TimeTicks(int64) | 32 // (rather than microseconds) through the protected TimeTicks(int64) |
33 // constructor. | 33 // constructor. |
34 class TestTicks : public TimeTicks { | 34 class TestTicks : public TimeTicks { |
35 public: | 35 public: |
36 TestTicks(int64 seconds) | 36 TestTicks(int64 seconds) |
37 : TimeTicks(seconds * Time::kMicrosecondsPerSecond) {} | 37 : TimeTicks(seconds * Time::kMicrosecondsPerSecond) {} |
38 }; | 38 }; |
39 | 39 |
40 // Overloaded for test failure printing purposes. | 40 // Overloaded for test failure printing purposes. |
41 static std::ostream& operator<<(std::ostream& o, const TimeTicks& ticks) { | |
42 o << ticks.ToInternalValue() << "us"; | |
43 return o; | |
44 }; | |
45 | |
46 // Overloaded for test failure printing purposes. | |
47 static std::ostream& operator<<(std::ostream& o, const Time& time) { | 41 static std::ostream& operator<<(std::ostream& o, const Time& time) { |
48 o << time.ToInternalValue() << "us"; | 42 o << time.ToInternalValue() << "us"; |
49 return o; | 43 return o; |
50 }; | 44 }; |
51 | 45 |
52 class MetricsDaemonTest : public testing::Test { | 46 class MetricsDaemonTest : public testing::Test { |
53 protected: | 47 protected: |
54 virtual void SetUp() { | 48 virtual void SetUp() { |
55 EXPECT_EQ(NULL, daemon_.daily_use_.get()); | 49 EXPECT_EQ(NULL, daemon_.daily_use_.get()); |
56 EXPECT_EQ(NULL, daemon_.kernel_crash_interval_.get()); | 50 EXPECT_EQ(NULL, daemon_.kernel_crash_interval_.get()); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 // Reset all frequency counter reporters to mocks for further testing. | 101 // Reset all frequency counter reporters to mocks for further testing. |
108 MetricsDaemon::FrequencyCounters::iterator i; | 102 MetricsDaemon::FrequencyCounters::iterator i; |
109 for (i = daemon_.frequency_counters_.begin(); | 103 for (i = daemon_.frequency_counters_.begin(); |
110 i != daemon_.frequency_counters_.end(); ++i) { | 104 i != daemon_.frequency_counters_.end(); ++i) { |
111 delete i->second; | 105 delete i->second; |
112 i->second = new StrictMock<FrequencyCounterMock>(); | 106 i->second = new StrictMock<FrequencyCounterMock>(); |
113 } | 107 } |
114 | 108 |
115 EXPECT_FALSE(daemon_.user_active_); | 109 EXPECT_FALSE(daemon_.user_active_); |
116 EXPECT_TRUE(daemon_.user_active_last_.is_null()); | 110 EXPECT_TRUE(daemon_.user_active_last_.is_null()); |
117 EXPECT_EQ(MetricsDaemon::kUnknownNetworkState, daemon_.network_state_); | |
118 EXPECT_TRUE(daemon_.network_state_last_.is_null()); | |
119 EXPECT_EQ(MetricsDaemon::kUnknownPowerState, daemon_.power_state_); | 111 EXPECT_EQ(MetricsDaemon::kUnknownPowerState, daemon_.power_state_); |
120 EXPECT_EQ(MetricsDaemon::kUnknownSessionState, daemon_.session_state_); | 112 EXPECT_EQ(MetricsDaemon::kUnknownSessionState, daemon_.session_state_); |
121 | 113 |
122 file_util::Delete(FilePath(kTestDir), true); | 114 file_util::Delete(FilePath(kTestDir), true); |
123 file_util::CreateDirectory(FilePath(kTestDir)); | 115 file_util::CreateDirectory(FilePath(kTestDir)); |
124 } | 116 } |
125 | 117 |
126 virtual void TearDown() {} | 118 virtual void TearDown() {} |
127 | 119 |
128 const TaggedCounterReporter* | 120 const TaggedCounterReporter* |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 | 175 |
184 // Adds a metrics library mock expectation that the specified daily | 176 // Adds a metrics library mock expectation that the specified daily |
185 // use time metric will be generated. | 177 // use time metric will be generated. |
186 void ExpectDailyUseTimeMetric(int sample) { | 178 void ExpectDailyUseTimeMetric(int sample) { |
187 ExpectMetric(MetricsDaemon::kMetricDailyUseTimeName, sample, | 179 ExpectMetric(MetricsDaemon::kMetricDailyUseTimeName, sample, |
188 MetricsDaemon::kMetricDailyUseTimeMin, | 180 MetricsDaemon::kMetricDailyUseTimeMin, |
189 MetricsDaemon::kMetricDailyUseTimeMax, | 181 MetricsDaemon::kMetricDailyUseTimeMax, |
190 MetricsDaemon::kMetricDailyUseTimeBuckets); | 182 MetricsDaemon::kMetricDailyUseTimeBuckets); |
191 } | 183 } |
192 | 184 |
193 // Adds a metrics library mock expectation that the specified time | |
194 // to network dropping metric will be generated. | |
195 void ExpectTimeToNetworkDropMetric(int sample) { | |
196 ExpectMetric(MetricsDaemon::kMetricTimeToNetworkDropName, sample, | |
197 MetricsDaemon::kMetricTimeToNetworkDropMin, | |
198 MetricsDaemon::kMetricTimeToNetworkDropMax, | |
199 MetricsDaemon::kMetricTimeToNetworkDropBuckets); | |
200 } | |
201 | |
202 // Converts from seconds to a Time object. | 185 // Converts from seconds to a Time object. |
203 Time TestTime(int64 seconds) { | 186 Time TestTime(int64 seconds) { |
204 return Time::FromInternalValue(seconds * Time::kMicrosecondsPerSecond); | 187 return Time::FromInternalValue(seconds * Time::kMicrosecondsPerSecond); |
205 } | 188 } |
206 | 189 |
207 // Creates a new DBus signal message with a single string | 190 // Creates a new DBus signal message with a single string |
208 // argument. The message can be deallocated through | 191 // argument. The message can be deallocated through |
209 // DeleteDBusMessage. | 192 // DeleteDBusMessage. |
210 // | 193 // |
211 // |path| is the object emitting the signal. | 194 // |path| is the object emitting the signal. |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 MetricsDaemon::ReportDailyUse(&daemon_, /* tag */ 20, /* count */ 90); | 256 MetricsDaemon::ReportDailyUse(&daemon_, /* tag */ 20, /* count */ 90); |
274 | 257 |
275 ExpectDailyUseTimeMetric(/* sample */ 1); | 258 ExpectDailyUseTimeMetric(/* sample */ 1); |
276 MetricsDaemon::ReportDailyUse(&daemon_, /* tag */ 23, /* count */ 89); | 259 MetricsDaemon::ReportDailyUse(&daemon_, /* tag */ 23, /* count */ 89); |
277 | 260 |
278 // There should be no metrics generated for the calls below. | 261 // There should be no metrics generated for the calls below. |
279 MetricsDaemon::ReportDailyUse(&daemon_, /* tag */ 50, /* count */ 0); | 262 MetricsDaemon::ReportDailyUse(&daemon_, /* tag */ 50, /* count */ 0); |
280 MetricsDaemon::ReportDailyUse(&daemon_, /* tag */ 60, /* count */ -5); | 263 MetricsDaemon::ReportDailyUse(&daemon_, /* tag */ 60, /* count */ -5); |
281 } | 264 } |
282 | 265 |
283 TEST_F(MetricsDaemonTest, LookupNetworkState) { | |
284 EXPECT_EQ(MetricsDaemon::kNetworkStateOnline, | |
285 daemon_.LookupNetworkState("online")); | |
286 EXPECT_EQ(MetricsDaemon::kNetworkStateOffline, | |
287 daemon_.LookupNetworkState("offline")); | |
288 EXPECT_EQ(MetricsDaemon::kUnknownNetworkState, | |
289 daemon_.LookupNetworkState("somestate")); | |
290 } | |
291 | |
292 TEST_F(MetricsDaemonTest, LookupPowerState) { | 266 TEST_F(MetricsDaemonTest, LookupPowerState) { |
293 EXPECT_EQ(MetricsDaemon::kPowerStateOn, | 267 EXPECT_EQ(MetricsDaemon::kPowerStateOn, |
294 daemon_.LookupPowerState("on")); | 268 daemon_.LookupPowerState("on")); |
295 EXPECT_EQ(MetricsDaemon::kPowerStateMem, | 269 EXPECT_EQ(MetricsDaemon::kPowerStateMem, |
296 daemon_.LookupPowerState("mem")); | 270 daemon_.LookupPowerState("mem")); |
297 EXPECT_EQ(MetricsDaemon::kUnknownPowerState, | 271 EXPECT_EQ(MetricsDaemon::kUnknownPowerState, |
298 daemon_.LookupPowerState("somestate")); | 272 daemon_.LookupPowerState("somestate")); |
299 } | 273 } |
300 | 274 |
301 TEST_F(MetricsDaemonTest, LookupSessionState) { | 275 TEST_F(MetricsDaemonTest, LookupSessionState) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 .RetiresOnSaturation(); | 310 .RetiresOnSaturation(); |
337 msg = NewDBusSignalString("/", | 311 msg = NewDBusSignalString("/", |
338 "org.chromium.CrashReporter", | 312 "org.chromium.CrashReporter", |
339 "UserCrash", | 313 "UserCrash", |
340 ""); | 314 ""); |
341 res = MetricsDaemon::MessageFilter(/* connection */ NULL, msg, &daemon_); | 315 res = MetricsDaemon::MessageFilter(/* connection */ NULL, msg, &daemon_); |
342 EXPECT_EQ(DBUS_HANDLER_RESULT_HANDLED, res); | 316 EXPECT_EQ(DBUS_HANDLER_RESULT_HANDLED, res); |
343 DeleteDBusMessage(msg); | 317 DeleteDBusMessage(msg); |
344 | 318 |
345 msg = NewDBusSignalString("/", | 319 msg = NewDBusSignalString("/", |
346 "org.chromium.flimflam.Manager", | |
347 "StateChanged", | |
348 "online"); | |
349 EXPECT_EQ(MetricsDaemon::kUnknownNetworkState, daemon_.network_state_); | |
350 res = MetricsDaemon::MessageFilter(/* connection */ NULL, msg, &daemon_); | |
351 EXPECT_EQ(MetricsDaemon::kNetworkStateOnline, daemon_.network_state_); | |
352 EXPECT_EQ(DBUS_HANDLER_RESULT_HANDLED, res); | |
353 DeleteDBusMessage(msg); | |
354 | |
355 msg = NewDBusSignalString("/", | |
356 "org.chromium.PowerManager", | 320 "org.chromium.PowerManager", |
357 "PowerStateChanged", | 321 "PowerStateChanged", |
358 "on"); | 322 "on"); |
359 EXPECT_EQ(MetricsDaemon::kUnknownPowerState, daemon_.power_state_); | 323 EXPECT_EQ(MetricsDaemon::kUnknownPowerState, daemon_.power_state_); |
360 res = MetricsDaemon::MessageFilter(/* connection */ NULL, msg, &daemon_); | 324 res = MetricsDaemon::MessageFilter(/* connection */ NULL, msg, &daemon_); |
361 EXPECT_EQ(MetricsDaemon::kPowerStateOn, daemon_.power_state_); | 325 EXPECT_EQ(MetricsDaemon::kPowerStateOn, daemon_.power_state_); |
362 EXPECT_EQ(DBUS_HANDLER_RESULT_HANDLED, res); | 326 EXPECT_EQ(DBUS_HANDLER_RESULT_HANDLED, res); |
363 DeleteDBusMessage(msg); | 327 DeleteDBusMessage(msg); |
364 | 328 |
365 IgnoreActiveUseUpdate(); | 329 IgnoreActiveUseUpdate(); |
(...skipping 20 matching lines...) Expand all Loading... |
386 | 350 |
387 msg = NewDBusSignalString("/", | 351 msg = NewDBusSignalString("/", |
388 "org.chromium.UnknownService.Manager", | 352 "org.chromium.UnknownService.Manager", |
389 "StateChanged", | 353 "StateChanged", |
390 "randomstate"); | 354 "randomstate"); |
391 res = MetricsDaemon::MessageFilter(/* connection */ NULL, msg, &daemon_); | 355 res = MetricsDaemon::MessageFilter(/* connection */ NULL, msg, &daemon_); |
392 EXPECT_EQ(DBUS_HANDLER_RESULT_NOT_YET_HANDLED, res); | 356 EXPECT_EQ(DBUS_HANDLER_RESULT_NOT_YET_HANDLED, res); |
393 DeleteDBusMessage(msg); | 357 DeleteDBusMessage(msg); |
394 } | 358 } |
395 | 359 |
396 TEST_F(MetricsDaemonTest, NetStateChangedSimpleDrop) { | |
397 daemon_.NetStateChanged("online", TestTicks(10)); | |
398 EXPECT_EQ(MetricsDaemon::kNetworkStateOnline, daemon_.network_state_); | |
399 EXPECT_EQ(TestTicks(10), daemon_.network_state_last_); | |
400 | |
401 ExpectTimeToNetworkDropMetric(20); | |
402 daemon_.NetStateChanged("offline", TestTicks(30)); | |
403 EXPECT_EQ(MetricsDaemon::kNetworkStateOffline, daemon_.network_state_); | |
404 EXPECT_EQ(TestTicks(30), daemon_.network_state_last_); | |
405 } | |
406 | |
407 TEST_F(MetricsDaemonTest, NetStateChangedSuspend) { | |
408 daemon_.NetStateChanged("offline", TestTicks(30)); | |
409 EXPECT_EQ(MetricsDaemon::kNetworkStateOffline, daemon_.network_state_); | |
410 EXPECT_EQ(TestTicks(30), daemon_.network_state_last_); | |
411 | |
412 daemon_.NetStateChanged("online", TestTicks(60)); | |
413 EXPECT_EQ(MetricsDaemon::kNetworkStateOnline, daemon_.network_state_); | |
414 EXPECT_EQ(TestTicks(60), daemon_.network_state_last_); | |
415 | |
416 daemon_.power_state_ = MetricsDaemon::kPowerStateMem; | |
417 daemon_.NetStateChanged("offline", TestTicks(85)); | |
418 EXPECT_EQ(MetricsDaemon::kNetworkStateOffline, daemon_.network_state_); | |
419 EXPECT_EQ(TestTicks(85), daemon_.network_state_last_); | |
420 | |
421 daemon_.NetStateChanged("somestate", TestTicks(90)); | |
422 EXPECT_EQ(MetricsDaemon::kUnknownNetworkState, daemon_.network_state_); | |
423 EXPECT_EQ(TestTicks(90), daemon_.network_state_last_); | |
424 | |
425 daemon_.NetStateChanged("offline", TestTicks(95)); | |
426 EXPECT_EQ(MetricsDaemon::kNetworkStateOffline, daemon_.network_state_); | |
427 EXPECT_EQ(TestTicks(95), daemon_.network_state_last_); | |
428 | |
429 daemon_.power_state_ = MetricsDaemon::kPowerStateOn; | |
430 daemon_.NetStateChanged("online", TestTicks(105)); | |
431 EXPECT_EQ(MetricsDaemon::kNetworkStateOnline, daemon_.network_state_); | |
432 EXPECT_EQ(TestTicks(105), daemon_.network_state_last_); | |
433 | |
434 ExpectTimeToNetworkDropMetric(3); | |
435 daemon_.NetStateChanged("offline", TestTicks(108)); | |
436 EXPECT_EQ(MetricsDaemon::kNetworkStateOffline, daemon_.network_state_); | |
437 EXPECT_EQ(TestTicks(108), daemon_.network_state_last_); | |
438 } | |
439 | |
440 TEST_F(MetricsDaemonTest, PowerStateChanged) { | 360 TEST_F(MetricsDaemonTest, PowerStateChanged) { |
441 ExpectActiveUseUpdate(7, 0); | 361 ExpectActiveUseUpdate(7, 0); |
442 daemon_.SetUserActiveState(/* active */ true, | 362 daemon_.SetUserActiveState(/* active */ true, |
443 TestTime(7 * kSecondsPerDay + 15)); | 363 TestTime(7 * kSecondsPerDay + 15)); |
444 EXPECT_TRUE(daemon_.user_active_); | 364 EXPECT_TRUE(daemon_.user_active_); |
445 EXPECT_EQ(TestTime(7 * kSecondsPerDay + 15), daemon_.user_active_last_); | 365 EXPECT_EQ(TestTime(7 * kSecondsPerDay + 15), daemon_.user_active_last_); |
446 | 366 |
447 ExpectActiveUseUpdate(7, 30); | 367 ExpectActiveUseUpdate(7, 30); |
448 daemon_.PowerStateChanged("mem", TestTime(7 * kSecondsPerDay + 45)); | 368 daemon_.PowerStateChanged("mem", TestTime(7 * kSecondsPerDay + 45)); |
449 EXPECT_EQ(MetricsDaemon::kPowerStateMem, daemon_.power_state_); | 369 EXPECT_EQ(MetricsDaemon::kPowerStateMem, daemon_.power_state_); |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
593 TEST_F(MetricsDaemonTest, GetHistogramPath) { | 513 TEST_F(MetricsDaemonTest, GetHistogramPath) { |
594 EXPECT_EQ("/var/log/metrics/Logging.AnyCrashesDaily", | 514 EXPECT_EQ("/var/log/metrics/Logging.AnyCrashesDaily", |
595 daemon_.GetHistogramPath( | 515 daemon_.GetHistogramPath( |
596 MetricsDaemon::kMetricAnyCrashesDailyName).value()); | 516 MetricsDaemon::kMetricAnyCrashesDailyName).value()); |
597 } | 517 } |
598 | 518 |
599 int main(int argc, char** argv) { | 519 int main(int argc, char** argv) { |
600 testing::InitGoogleTest(&argc, argv); | 520 testing::InitGoogleTest(&argc, argv); |
601 return RUN_ALL_TESTS(); | 521 return RUN_ALL_TESTS(); |
602 } | 522 } |
OLD | NEW |