| 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 <string> |
| 8 #include <vector> |
| 9 |
| 7 #include <base/file_util.h> | 10 #include <base/file_util.h> |
| 8 #include <gtest/gtest.h> | 11 #include <gtest/gtest.h> |
| 9 | 12 |
| 10 #include "counter_mock.h" | 13 #include "counter_mock.h" |
| 11 #include "metrics_daemon.h" | 14 #include "metrics_daemon.h" |
| 12 #include "metrics_library_mock.h" | 15 #include "metrics_library_mock.h" |
| 13 | 16 |
| 14 using base::Time; | 17 using base::Time; |
| 15 using base::TimeTicks; | 18 using base::TimeTicks; |
| 16 using chromeos_metrics::FrequencyCounter; | 19 using chromeos_metrics::FrequencyCounter; |
| 17 using chromeos_metrics::FrequencyCounterMock; | 20 using chromeos_metrics::FrequencyCounterMock; |
| 18 using chromeos_metrics::TaggedCounterMock; | 21 using chromeos_metrics::TaggedCounterMock; |
| 19 using chromeos_metrics::TaggedCounterReporter; | 22 using chromeos_metrics::TaggedCounterReporter; |
| 20 using chromeos_metrics::TaggedCounterReporterMock; | 23 using chromeos_metrics::TaggedCounterReporterMock; |
| 24 using std::string; |
| 25 using std::vector; |
| 21 using ::testing::_; | 26 using ::testing::_; |
| 22 using ::testing::Return; | 27 using ::testing::Return; |
| 23 using ::testing::StrictMock; | 28 using ::testing::StrictMock; |
| 24 | 29 |
| 25 static const int kSecondsPerDay = 24 * 60 * 60; | 30 static const int kSecondsPerDay = 24 * 60 * 60; |
| 26 | 31 |
| 27 static const char kTestDir[] = "test"; | 32 static const char kTestDir[] = "test"; |
| 28 static const char kLastFile[] = "test/last"; | 33 static const char kLastFile[] = "test/last"; |
| 29 static const char kCurrentFile[] = "test/current"; | 34 static const char kCurrentFile[] = "test/current"; |
| 30 | 35 |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 .Times(1) | 163 .Times(1) |
| 159 .RetiresOnSaturation(); | 164 .RetiresOnSaturation(); |
| 160 EXPECT_CALL(*user_crash_interval_, Update(_, _)) | 165 EXPECT_CALL(*user_crash_interval_, Update(_, _)) |
| 161 .Times(1) | 166 .Times(1) |
| 162 .RetiresOnSaturation(); | 167 .RetiresOnSaturation(); |
| 163 ExpectFrequencyFlushCalls(); | 168 ExpectFrequencyFlushCalls(); |
| 164 } | 169 } |
| 165 | 170 |
| 166 // Adds a metrics library mock expectation that the specified metric | 171 // Adds a metrics library mock expectation that the specified metric |
| 167 // will be generated. | 172 // will be generated. |
| 168 void ExpectMetric(const std::string& name, int sample, | 173 void ExpectMetric(const string& name, int sample, |
| 169 int min, int max, int buckets) { | 174 int min, int max, int buckets) { |
| 170 EXPECT_CALL(metrics_lib_, SendToUMA(name, sample, min, max, buckets)) | 175 EXPECT_CALL(metrics_lib_, SendToUMA(name, sample, min, max, buckets)) |
| 171 .Times(1) | 176 .Times(1) |
| 172 .WillOnce(Return(true)) | 177 .WillOnce(Return(true)) |
| 173 .RetiresOnSaturation(); | 178 .RetiresOnSaturation(); |
| 174 } | 179 } |
| 175 | 180 |
| 176 // Adds a metrics library mock expectation that the specified daily | 181 // Adds a metrics library mock expectation that the specified daily |
| 177 // use time metric will be generated. | 182 // use time metric will be generated. |
| 178 void ExpectDailyUseTimeMetric(int sample) { | 183 void ExpectDailyUseTimeMetric(int sample) { |
| 179 ExpectMetric(MetricsDaemon::kMetricDailyUseTimeName, sample, | 184 ExpectMetric(MetricsDaemon::kMetricDailyUseTimeName, sample, |
| 180 MetricsDaemon::kMetricDailyUseTimeMin, | 185 MetricsDaemon::kMetricDailyUseTimeMin, |
| 181 MetricsDaemon::kMetricDailyUseTimeMax, | 186 MetricsDaemon::kMetricDailyUseTimeMax, |
| 182 MetricsDaemon::kMetricDailyUseTimeBuckets); | 187 MetricsDaemon::kMetricDailyUseTimeBuckets); |
| 183 } | 188 } |
| 184 | 189 |
| 185 // Converts from seconds to a Time object. | 190 // Converts from seconds to a Time object. |
| 186 Time TestTime(int64 seconds) { | 191 Time TestTime(int64 seconds) { |
| 187 return Time::FromInternalValue(seconds * Time::kMicrosecondsPerSecond); | 192 return Time::FromInternalValue(seconds * Time::kMicrosecondsPerSecond); |
| 188 } | 193 } |
| 189 | 194 |
| 190 // Creates a new DBus signal message with a single string | 195 // Creates a new DBus signal message with zero or more string arguments. |
| 191 // argument. The message can be deallocated through | 196 // The message can be deallocated through DeleteDBusMessage. |
| 192 // DeleteDBusMessage. | |
| 193 // | 197 // |
| 194 // |path| is the object emitting the signal. | 198 // |path| is the object emitting the signal. |
| 195 // |interface| is the interface the signal is emitted from. | 199 // |interface| is the interface the signal is emitted from. |
| 196 // |name| is the name of the signal. | 200 // |name| is the name of the signal. |
| 197 // |arg_value| is the value of the string argument. | 201 // |arg_values| contains the values of the string arguments. |
| 198 DBusMessage* NewDBusSignalString(const std::string& path, | 202 DBusMessage* NewDBusSignalString(const string& path, |
| 199 const std::string& interface, | 203 const string& interface, |
| 200 const std::string& name, | 204 const string& name, |
| 201 const std::string& arg_value) { | 205 const vector<string>& arg_values) { |
| 202 DBusMessage* msg = dbus_message_new_signal(path.c_str(), | 206 DBusMessage* msg = dbus_message_new_signal(path.c_str(), |
| 203 interface.c_str(), | 207 interface.c_str(), |
| 204 name.c_str()); | 208 name.c_str()); |
| 205 DBusMessageIter iter; | 209 DBusMessageIter iter; |
| 206 dbus_message_iter_init_append(msg, &iter); | 210 dbus_message_iter_init_append(msg, &iter); |
| 207 const char* arg_value_c = arg_value.c_str(); | 211 for (vector<string>::const_iterator it = arg_values.begin(); |
| 208 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &arg_value_c); | 212 it != arg_values.end(); ++it) { |
| 213 const char* str_value = it->c_str(); |
| 214 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &str_value); |
| 215 } |
| 209 return msg; | 216 return msg; |
| 210 } | 217 } |
| 211 | 218 |
| 212 // Deallocates the DBus message |msg| previously allocated through | 219 // Deallocates the DBus message |msg| previously allocated through |
| 213 // dbus_message_new*. | 220 // dbus_message_new*. |
| 214 void DeleteDBusMessage(DBusMessage* msg) { | 221 void DeleteDBusMessage(DBusMessage* msg) { |
| 215 dbus_message_unref(msg); | 222 dbus_message_unref(msg); |
| 216 } | 223 } |
| 217 | 224 |
| 218 // Get the frequency counter for the given name. | 225 // Get the frequency counter for the given name. |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 Update(1)) | 308 Update(1)) |
| 302 .Times(1) | 309 .Times(1) |
| 303 .RetiresOnSaturation(); | 310 .RetiresOnSaturation(); |
| 304 EXPECT_CALL(GetFrequencyMock(MetricsDaemon::kMetricUserCrashesWeeklyName), | 311 EXPECT_CALL(GetFrequencyMock(MetricsDaemon::kMetricUserCrashesWeeklyName), |
| 305 Update(1)) | 312 Update(1)) |
| 306 .Times(1) | 313 .Times(1) |
| 307 .RetiresOnSaturation(); | 314 .RetiresOnSaturation(); |
| 308 EXPECT_CALL(*user_crash_interval_, Flush()) | 315 EXPECT_CALL(*user_crash_interval_, Flush()) |
| 309 .Times(1) | 316 .Times(1) |
| 310 .RetiresOnSaturation(); | 317 .RetiresOnSaturation(); |
| 318 vector<string> signal_args; |
| 311 msg = NewDBusSignalString("/", | 319 msg = NewDBusSignalString("/", |
| 312 "org.chromium.CrashReporter", | 320 "org.chromium.CrashReporter", |
| 313 "UserCrash", | 321 "UserCrash", |
| 314 ""); | 322 signal_args); |
| 315 res = MetricsDaemon::MessageFilter(/* connection */ NULL, msg, &daemon_); | 323 res = MetricsDaemon::MessageFilter(/* connection */ NULL, msg, &daemon_); |
| 316 EXPECT_EQ(DBUS_HANDLER_RESULT_HANDLED, res); | 324 EXPECT_EQ(DBUS_HANDLER_RESULT_HANDLED, res); |
| 317 DeleteDBusMessage(msg); | 325 DeleteDBusMessage(msg); |
| 318 | 326 |
| 327 signal_args.clear(); |
| 328 signal_args.push_back("on"); |
| 319 msg = NewDBusSignalString("/", | 329 msg = NewDBusSignalString("/", |
| 320 "org.chromium.PowerManager", | 330 "org.chromium.PowerManager", |
| 321 "PowerStateChanged", | 331 "PowerStateChanged", |
| 322 "on"); | 332 signal_args); |
| 323 EXPECT_EQ(MetricsDaemon::kUnknownPowerState, daemon_.power_state_); | 333 EXPECT_EQ(MetricsDaemon::kUnknownPowerState, daemon_.power_state_); |
| 324 res = MetricsDaemon::MessageFilter(/* connection */ NULL, msg, &daemon_); | 334 res = MetricsDaemon::MessageFilter(/* connection */ NULL, msg, &daemon_); |
| 325 EXPECT_EQ(MetricsDaemon::kPowerStateOn, daemon_.power_state_); | 335 EXPECT_EQ(MetricsDaemon::kPowerStateOn, daemon_.power_state_); |
| 326 EXPECT_EQ(DBUS_HANDLER_RESULT_HANDLED, res); | 336 EXPECT_EQ(DBUS_HANDLER_RESULT_HANDLED, res); |
| 327 DeleteDBusMessage(msg); | 337 DeleteDBusMessage(msg); |
| 328 | 338 |
| 339 signal_args.clear(); |
| 329 IgnoreActiveUseUpdate(); | 340 IgnoreActiveUseUpdate(); |
| 330 msg = NewDBusSignalString("/", | 341 msg = NewDBusSignalString("/", |
| 331 "org.chromium.PowerManager", | 342 "org.chromium.PowerManager", |
| 332 "ScreenIsUnlocked", | 343 "ScreenIsUnlocked", |
| 333 ""); | 344 signal_args); |
| 334 EXPECT_FALSE(daemon_.user_active_); | 345 EXPECT_FALSE(daemon_.user_active_); |
| 335 res = MetricsDaemon::MessageFilter(/* connection */ NULL, msg, &daemon_); | 346 res = MetricsDaemon::MessageFilter(/* connection */ NULL, msg, &daemon_); |
| 336 EXPECT_TRUE(daemon_.user_active_); | 347 EXPECT_TRUE(daemon_.user_active_); |
| 337 EXPECT_EQ(DBUS_HANDLER_RESULT_HANDLED, res); | 348 EXPECT_EQ(DBUS_HANDLER_RESULT_HANDLED, res); |
| 338 DeleteDBusMessage(msg); | 349 DeleteDBusMessage(msg); |
| 339 | 350 |
| 340 IgnoreActiveUseUpdate(); | 351 IgnoreActiveUseUpdate(); |
| 352 signal_args.clear(); |
| 353 signal_args.push_back("started"); |
| 354 signal_args.push_back("bob"); // arbitrary username |
| 341 msg = NewDBusSignalString("/org/chromium/SessionManager", | 355 msg = NewDBusSignalString("/org/chromium/SessionManager", |
| 342 "org.chromium.SessionManagerInterface", | 356 "org.chromium.SessionManagerInterface", |
| 343 "SessionStateChanged", | 357 "SessionStateChanged", |
| 344 "started"); | 358 signal_args); |
| 345 EXPECT_EQ(MetricsDaemon::kUnknownSessionState, daemon_.session_state_); | 359 EXPECT_EQ(MetricsDaemon::kUnknownSessionState, daemon_.session_state_); |
| 346 res = MetricsDaemon::MessageFilter(/* connection */ NULL, msg, &daemon_); | 360 res = MetricsDaemon::MessageFilter(/* connection */ NULL, msg, &daemon_); |
| 347 EXPECT_EQ(MetricsDaemon::kSessionStateStarted, daemon_.session_state_); | 361 EXPECT_EQ(MetricsDaemon::kSessionStateStarted, daemon_.session_state_); |
| 348 EXPECT_EQ(DBUS_HANDLER_RESULT_HANDLED, res); | 362 EXPECT_EQ(DBUS_HANDLER_RESULT_HANDLED, res); |
| 349 DeleteDBusMessage(msg); | 363 DeleteDBusMessage(msg); |
| 350 | 364 |
| 365 signal_args.clear(); |
| 366 signal_args.push_back("randomstate"); |
| 367 signal_args.push_back("bob"); // arbitrary username |
| 351 msg = NewDBusSignalString("/", | 368 msg = NewDBusSignalString("/", |
| 352 "org.chromium.UnknownService.Manager", | 369 "org.chromium.UnknownService.Manager", |
| 353 "StateChanged", | 370 "StateChanged", |
| 354 "randomstate"); | 371 signal_args); |
| 355 res = MetricsDaemon::MessageFilter(/* connection */ NULL, msg, &daemon_); | 372 res = MetricsDaemon::MessageFilter(/* connection */ NULL, msg, &daemon_); |
| 356 EXPECT_EQ(DBUS_HANDLER_RESULT_NOT_YET_HANDLED, res); | 373 EXPECT_EQ(DBUS_HANDLER_RESULT_NOT_YET_HANDLED, res); |
| 357 DeleteDBusMessage(msg); | 374 DeleteDBusMessage(msg); |
| 358 } | 375 } |
| 359 | 376 |
| 360 TEST_F(MetricsDaemonTest, PowerStateChanged) { | 377 TEST_F(MetricsDaemonTest, PowerStateChanged) { |
| 361 ExpectActiveUseUpdate(7, 0); | 378 ExpectActiveUseUpdate(7, 0); |
| 362 daemon_.SetUserActiveState(/* active */ true, | 379 daemon_.SetUserActiveState(/* active */ true, |
| 363 TestTime(7 * kSecondsPerDay + 15)); | 380 TestTime(7 * kSecondsPerDay + 15)); |
| 364 EXPECT_TRUE(daemon_.user_active_); | 381 EXPECT_TRUE(daemon_.user_active_); |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 513 TEST_F(MetricsDaemonTest, GetHistogramPath) { | 530 TEST_F(MetricsDaemonTest, GetHistogramPath) { |
| 514 EXPECT_EQ("/var/log/metrics/Logging.AnyCrashesDaily", | 531 EXPECT_EQ("/var/log/metrics/Logging.AnyCrashesDaily", |
| 515 daemon_.GetHistogramPath( | 532 daemon_.GetHistogramPath( |
| 516 MetricsDaemon::kMetricAnyCrashesDailyName).value()); | 533 MetricsDaemon::kMetricAnyCrashesDailyName).value()); |
| 517 } | 534 } |
| 518 | 535 |
| 519 int main(int argc, char** argv) { | 536 int main(int argc, char** argv) { |
| 520 testing::InitGoogleTest(&argc, argv); | 537 testing::InitGoogleTest(&argc, argv); |
| 521 return RUN_ALL_TESTS(); | 538 return RUN_ALL_TESTS(); |
| 522 } | 539 } |
| OLD | NEW |