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 <base/file_util.h> |
5 #include <gtest/gtest.h> | 6 #include <gtest/gtest.h> |
6 | 7 |
7 #include "counter_mock.h" | 8 #include "counter_mock.h" |
8 #include "metrics_daemon.h" | 9 #include "metrics_daemon.h" |
9 #include "metrics_library_mock.h" | 10 #include "metrics_library_mock.h" |
10 | 11 |
11 using base::Time; | 12 using base::Time; |
12 using base::TimeTicks; | 13 using base::TimeTicks; |
13 using chromeos_metrics::TaggedCounterMock; | 14 using chromeos_metrics::TaggedCounterMock; |
14 using ::testing::_; | 15 using ::testing::_; |
(...skipping 20 matching lines...) Expand all Loading... |
35 // Overloaded for test failure printing purposes. | 36 // Overloaded for test failure printing purposes. |
36 static std::ostream& operator<<(std::ostream& o, const Time& time) { | 37 static std::ostream& operator<<(std::ostream& o, const Time& time) { |
37 o << time.ToInternalValue() << "us"; | 38 o << time.ToInternalValue() << "us"; |
38 return o; | 39 return o; |
39 }; | 40 }; |
40 | 41 |
41 class MetricsDaemonTest : public testing::Test { | 42 class MetricsDaemonTest : public testing::Test { |
42 protected: | 43 protected: |
43 virtual void SetUp() { | 44 virtual void SetUp() { |
44 EXPECT_EQ(NULL, daemon_.daily_use_.get()); | 45 EXPECT_EQ(NULL, daemon_.daily_use_.get()); |
| 46 EXPECT_EQ(NULL, daemon_.kernel_crash_interval_.get()); |
45 EXPECT_EQ(NULL, daemon_.user_crash_interval_.get()); | 47 EXPECT_EQ(NULL, daemon_.user_crash_interval_.get()); |
46 daemon_.Init(true, &metrics_lib_); | 48 daemon_.Init(true, &metrics_lib_); |
47 | 49 |
48 // Tests constructor initialization. Switches to mock counters. | 50 // Tests constructor initialization. Switches to mock counters. |
49 EXPECT_TRUE(NULL != daemon_.daily_use_.get()); | 51 EXPECT_TRUE(NULL != daemon_.daily_use_.get()); |
| 52 EXPECT_TRUE(NULL != daemon_.kernel_crash_interval_.get()); |
50 EXPECT_TRUE(NULL != daemon_.user_crash_interval_.get()); | 53 EXPECT_TRUE(NULL != daemon_.user_crash_interval_.get()); |
51 | 54 |
52 // Allocates mock counter and transfers ownership. | 55 // Allocates mock counter and transfers ownership. |
53 daily_use_ = new StrictMock<TaggedCounterMock>(); | 56 daily_use_ = new StrictMock<TaggedCounterMock>(); |
54 daemon_.daily_use_.reset(daily_use_); | 57 daemon_.daily_use_.reset(daily_use_); |
| 58 kernel_crash_interval_ = new StrictMock<TaggedCounterMock>(); |
| 59 daemon_.kernel_crash_interval_.reset(kernel_crash_interval_); |
55 user_crash_interval_ = new StrictMock<TaggedCounterMock>(); | 60 user_crash_interval_ = new StrictMock<TaggedCounterMock>(); |
56 daemon_.user_crash_interval_.reset(user_crash_interval_); | 61 daemon_.user_crash_interval_.reset(user_crash_interval_); |
57 | 62 |
58 EXPECT_FALSE(daemon_.user_active_); | 63 EXPECT_FALSE(daemon_.user_active_); |
59 EXPECT_TRUE(daemon_.user_active_last_.is_null()); | 64 EXPECT_TRUE(daemon_.user_active_last_.is_null()); |
60 EXPECT_EQ(MetricsDaemon::kUnknownNetworkState, daemon_.network_state_); | 65 EXPECT_EQ(MetricsDaemon::kUnknownNetworkState, daemon_.network_state_); |
61 EXPECT_TRUE(daemon_.network_state_last_.is_null()); | 66 EXPECT_TRUE(daemon_.network_state_last_.is_null()); |
62 EXPECT_EQ(MetricsDaemon::kUnknownPowerState, daemon_.power_state_); | 67 EXPECT_EQ(MetricsDaemon::kUnknownPowerState, daemon_.power_state_); |
63 EXPECT_EQ(MetricsDaemon::kUnknownSessionState, daemon_.session_state_); | 68 EXPECT_EQ(MetricsDaemon::kUnknownSessionState, daemon_.session_state_); |
64 } | 69 } |
65 | 70 |
66 virtual void TearDown() {} | 71 virtual void TearDown() {} |
67 | 72 |
68 // Adds active use aggregation counters update expectations that the | 73 // Adds active use aggregation counters update expectations that the |
69 // specified tag/count update will be generated. | 74 // specified tag/count update will be generated. |
70 void ExpectActiveUseUpdate(int daily_tag, int count) { | 75 void ExpectActiveUseUpdate(int daily_tag, int count) { |
71 EXPECT_CALL(*daily_use_, Update(daily_tag, count)) | 76 EXPECT_CALL(*daily_use_, Update(daily_tag, count)) |
72 .Times(1) | 77 .Times(1) |
73 .RetiresOnSaturation(); | 78 .RetiresOnSaturation(); |
| 79 EXPECT_CALL(*kernel_crash_interval_, Update(0, count)) |
| 80 .Times(1) |
| 81 .RetiresOnSaturation(); |
74 EXPECT_CALL(*user_crash_interval_, Update(0, count)) | 82 EXPECT_CALL(*user_crash_interval_, Update(0, count)) |
75 .Times(1) | 83 .Times(1) |
76 .RetiresOnSaturation(); | 84 .RetiresOnSaturation(); |
77 } | 85 } |
78 | 86 |
79 // Adds active use aggregation counters update expectations that | 87 // Adds active use aggregation counters update expectations that |
80 // ignore the update arguments. | 88 // ignore the update arguments. |
81 void IgnoreActiveUseUpdate() { | 89 void IgnoreActiveUseUpdate() { |
82 EXPECT_CALL(*daily_use_, Update(_, _)) | 90 EXPECT_CALL(*daily_use_, Update(_, _)) |
83 .Times(1) | 91 .Times(1) |
84 .RetiresOnSaturation(); | 92 .RetiresOnSaturation(); |
| 93 EXPECT_CALL(*kernel_crash_interval_, Update(_, _)) |
| 94 .Times(1) |
| 95 .RetiresOnSaturation(); |
85 EXPECT_CALL(*user_crash_interval_, Update(_, _)) | 96 EXPECT_CALL(*user_crash_interval_, Update(_, _)) |
86 .Times(1) | 97 .Times(1) |
87 .RetiresOnSaturation(); | 98 .RetiresOnSaturation(); |
88 } | 99 } |
89 | 100 |
90 // Adds a metrics library mock expectation that the specified metric | 101 // Adds a metrics library mock expectation that the specified metric |
91 // will be generated. | 102 // will be generated. |
92 void ExpectMetric(const std::string& name, int sample, | 103 void ExpectMetric(const std::string& name, int sample, |
93 int min, int max, int buckets) { | 104 int min, int max, int buckets) { |
94 EXPECT_CALL(metrics_lib_, SendToUMA(name, sample, min, max, buckets)) | 105 EXPECT_CALL(metrics_lib_, SendToUMA(name, sample, min, max, buckets)) |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 MetricsDaemon daemon_; | 163 MetricsDaemon daemon_; |
153 | 164 |
154 // Metrics library mock. It's a strict mock so that all unexpected | 165 // Metrics library mock. It's a strict mock so that all unexpected |
155 // metric generation calls are marked as failures. | 166 // metric generation calls are marked as failures. |
156 StrictMock<MetricsLibraryMock> metrics_lib_; | 167 StrictMock<MetricsLibraryMock> metrics_lib_; |
157 | 168 |
158 // Counter mocks. They are strict mocks so that all unexpected | 169 // Counter mocks. They are strict mocks so that all unexpected |
159 // update calls are marked as failures. They are pointers so that | 170 // update calls are marked as failures. They are pointers so that |
160 // they can replace the scoped_ptr's allocated by the daemon. | 171 // they can replace the scoped_ptr's allocated by the daemon. |
161 StrictMock<TaggedCounterMock>* daily_use_; | 172 StrictMock<TaggedCounterMock>* daily_use_; |
| 173 StrictMock<TaggedCounterMock>* kernel_crash_interval_; |
162 StrictMock<TaggedCounterMock>* user_crash_interval_; | 174 StrictMock<TaggedCounterMock>* user_crash_interval_; |
163 }; | 175 }; |
164 | 176 |
| 177 TEST_F(MetricsDaemonTest, CheckKernelCrash) { |
| 178 static const char kKernelCrashDetected[] = "test-kernel-crash-detected"; |
| 179 daemon_.CheckKernelCrash(kKernelCrashDetected); |
| 180 |
| 181 FilePath crash_detected(kKernelCrashDetected); |
| 182 file_util::WriteFile(crash_detected, "", 0); |
| 183 IgnoreActiveUseUpdate(); |
| 184 EXPECT_CALL(*kernel_crash_interval_, Flush()) |
| 185 .Times(1) |
| 186 .RetiresOnSaturation(); |
| 187 daemon_.CheckKernelCrash(kKernelCrashDetected); |
| 188 file_util::Delete(crash_detected, false); |
| 189 } |
| 190 |
165 TEST_F(MetricsDaemonTest, DailyUseReporter) { | 191 TEST_F(MetricsDaemonTest, DailyUseReporter) { |
166 ExpectDailyUseTimeMetric(/* sample */ 2); | 192 ExpectDailyUseTimeMetric(/* sample */ 2); |
167 MetricsDaemon::DailyUseReporter(&daemon_, /* tag */ 20, /* count */ 90); | 193 MetricsDaemon::DailyUseReporter(&daemon_, /* tag */ 20, /* count */ 90); |
168 | 194 |
169 ExpectDailyUseTimeMetric(/* sample */ 1); | 195 ExpectDailyUseTimeMetric(/* sample */ 1); |
170 MetricsDaemon::DailyUseReporter(&daemon_, /* tag */ 23, /* count */ 89); | 196 MetricsDaemon::DailyUseReporter(&daemon_, /* tag */ 23, /* count */ 89); |
171 | 197 |
172 // There should be no metrics generated for the calls below. | 198 // There should be no metrics generated for the calls below. |
173 MetricsDaemon::DailyUseReporter(&daemon_, /* tag */ 50, /* count */ 0); | 199 MetricsDaemon::DailyUseReporter(&daemon_, /* tag */ 50, /* count */ 0); |
174 MetricsDaemon::DailyUseReporter(&daemon_, /* tag */ 60, /* count */ -5); | 200 MetricsDaemon::DailyUseReporter(&daemon_, /* tag */ 60, /* count */ -5); |
175 } | 201 } |
176 | 202 |
| 203 TEST_F(MetricsDaemonTest, KernelCrashIntervalReporter) { |
| 204 ExpectMetric(MetricsDaemon::kMetricKernelCrashIntervalName, 50, |
| 205 MetricsDaemon::kMetricKernelCrashIntervalMin, |
| 206 MetricsDaemon::kMetricKernelCrashIntervalMax, |
| 207 MetricsDaemon::kMetricKernelCrashIntervalBuckets); |
| 208 MetricsDaemon::KernelCrashIntervalReporter(&daemon_, 0, 50); |
| 209 } |
| 210 |
177 TEST_F(MetricsDaemonTest, LookupNetworkState) { | 211 TEST_F(MetricsDaemonTest, LookupNetworkState) { |
178 EXPECT_EQ(MetricsDaemon::kNetworkStateOnline, | 212 EXPECT_EQ(MetricsDaemon::kNetworkStateOnline, |
179 daemon_.LookupNetworkState("online")); | 213 daemon_.LookupNetworkState("online")); |
180 EXPECT_EQ(MetricsDaemon::kNetworkStateOffline, | 214 EXPECT_EQ(MetricsDaemon::kNetworkStateOffline, |
181 daemon_.LookupNetworkState("offline")); | 215 daemon_.LookupNetworkState("offline")); |
182 EXPECT_EQ(MetricsDaemon::kUnknownNetworkState, | 216 EXPECT_EQ(MetricsDaemon::kUnknownNetworkState, |
183 daemon_.LookupNetworkState("somestate")); | 217 daemon_.LookupNetworkState("somestate")); |
184 } | 218 } |
185 | 219 |
186 TEST_F(MetricsDaemonTest, LookupPowerState) { | 220 TEST_F(MetricsDaemonTest, LookupPowerState) { |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
333 EXPECT_FALSE(daemon_.user_active_); | 367 EXPECT_FALSE(daemon_.user_active_); |
334 EXPECT_EQ(TestTime(7 * kSecondsPerDay + 45), daemon_.user_active_last_); | 368 EXPECT_EQ(TestTime(7 * kSecondsPerDay + 45), daemon_.user_active_last_); |
335 | 369 |
336 ExpectActiveUseUpdate(7, 0); | 370 ExpectActiveUseUpdate(7, 0); |
337 daemon_.PowerStateChanged("otherstate", TestTime(7 * kSecondsPerDay + 185)); | 371 daemon_.PowerStateChanged("otherstate", TestTime(7 * kSecondsPerDay + 185)); |
338 EXPECT_EQ(MetricsDaemon::kUnknownPowerState, daemon_.power_state_); | 372 EXPECT_EQ(MetricsDaemon::kUnknownPowerState, daemon_.power_state_); |
339 EXPECT_FALSE(daemon_.user_active_); | 373 EXPECT_FALSE(daemon_.user_active_); |
340 EXPECT_EQ(TestTime(7 * kSecondsPerDay + 185), daemon_.user_active_last_); | 374 EXPECT_EQ(TestTime(7 * kSecondsPerDay + 185), daemon_.user_active_last_); |
341 } | 375 } |
342 | 376 |
| 377 TEST_F(MetricsDaemonTest, ProcessKernelCrash) { |
| 378 IgnoreActiveUseUpdate(); |
| 379 EXPECT_CALL(*kernel_crash_interval_, Flush()) |
| 380 .Times(1) |
| 381 .RetiresOnSaturation(); |
| 382 daemon_.ProcessKernelCrash(); |
| 383 } |
| 384 |
343 TEST_F(MetricsDaemonTest, ProcessUserCrash) { | 385 TEST_F(MetricsDaemonTest, ProcessUserCrash) { |
344 IgnoreActiveUseUpdate(); | 386 IgnoreActiveUseUpdate(); |
345 EXPECT_CALL(*user_crash_interval_, Flush()) | 387 EXPECT_CALL(*user_crash_interval_, Flush()) |
346 .Times(1) | 388 .Times(1) |
347 .RetiresOnSaturation(); | 389 .RetiresOnSaturation(); |
348 daemon_.ProcessUserCrash(); | 390 daemon_.ProcessUserCrash(); |
349 } | 391 } |
350 | 392 |
351 TEST_F(MetricsDaemonTest, SendMetric) { | 393 TEST_F(MetricsDaemonTest, SendMetric) { |
352 ExpectMetric("Dummy.Metric", 3, 1, 100, 50); | 394 ExpectMetric("Dummy.Metric", 3, 1, 100, 50); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
432 MetricsDaemon::kMetricUserCrashIntervalMin, | 474 MetricsDaemon::kMetricUserCrashIntervalMin, |
433 MetricsDaemon::kMetricUserCrashIntervalMax, | 475 MetricsDaemon::kMetricUserCrashIntervalMax, |
434 MetricsDaemon::kMetricUserCrashIntervalBuckets); | 476 MetricsDaemon::kMetricUserCrashIntervalBuckets); |
435 MetricsDaemon::UserCrashIntervalReporter(&daemon_, 0, 50); | 477 MetricsDaemon::UserCrashIntervalReporter(&daemon_, 0, 50); |
436 } | 478 } |
437 | 479 |
438 int main(int argc, char** argv) { | 480 int main(int argc, char** argv) { |
439 testing::InitGoogleTest(&argc, argv); | 481 testing::InitGoogleTest(&argc, argv); |
440 return RUN_ALL_TESTS(); | 482 return RUN_ALL_TESTS(); |
441 } | 483 } |
OLD | NEW |