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

Side by Side Diff: src/platform/metrics/metrics_daemon_test.cc

Issue 2079007: Add metrics library tests. Some metrics daemon API cleanup. (Closed) Base URL: ssh://git@chromiumos-git/chromeos
Patch Set: A bit more cleanup. Created 10 years, 7 months 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
« no previous file with comments | « src/platform/metrics/metrics_daemon_main.cc ('k') | src/platform/metrics/metrics_library.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "metrics_daemon.h" 5 #include "metrics_daemon.h"
6 #include "metrics_library_mock.h" 6 #include "metrics_library_mock.h"
7 7
8 #include <sys/file.h> 8 #include <sys/file.h>
9 9
10 #include <base/eintr_wrapper.h> 10 #include <base/eintr_wrapper.h>
11 #include <base/file_util.h> 11 #include <base/file_util.h>
12 #include <base/logging.h> 12 #include <base/logging.h>
13 #include <base/string_util.h> 13 #include <base/string_util.h>
14 #include <gtest/gtest.h> 14 #include <gtest/gtest.h>
15 15
16 using ::testing::Mock; 16 using ::testing::Mock;
17 using ::testing::Return; 17 using ::testing::Return;
18 using ::testing::StrictMock; 18 using ::testing::StrictMock;
19 19
20 static const char kTestDailyUseRecordFile[] = "daily-usage-test"; 20 static const char kTestDailyUseRecordFile[] = "daily-usage-test";
21 static const char kDoesNotExistFile[] = "/does/not/exist"; 21 static const char kDoesNotExistFile[] = "/does/not/exist";
22 22
23 static const int kSecondsPerDay = 24 * 60 * 60; 23 static const int kSecondsPerDay = 24 * 60 * 60;
24 24
25 class MetricsDaemonTest : public testing::Test { 25 class MetricsDaemonTest : public testing::Test {
26 protected: 26 protected:
27 virtual void SetUp() { 27 virtual void SetUp() {
28 EXPECT_EQ(NULL, daemon_.daily_use_record_file_);
28 daemon_.Init(true, &metrics_lib_); 29 daemon_.Init(true, &metrics_lib_);
30 EXPECT_TRUE(NULL != daemon_.daily_use_record_file_);
29 daemon_.daily_use_record_file_ = kTestDailyUseRecordFile; 31 daemon_.daily_use_record_file_ = kTestDailyUseRecordFile;
30 32
31 // The test fixture object will be used by the log message handler. 33 // The test fixture object will be used by the log message handler.
32 daemon_test_ = this; 34 daemon_test_ = this;
33 logging::SetLogMessageHandler(HandleLogMessages); 35 logging::SetLogMessageHandler(HandleLogMessages);
34 } 36 }
35 37
36 virtual void TearDown() { 38 virtual void TearDown() {
37 logging::SetLogMessageHandler(NULL); 39 logging::SetLogMessageHandler(NULL);
38 daemon_test_ = NULL; 40 daemon_test_ = NULL;
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 EXPECT_EQ(7 * kSecondsPerDay + 45, daemon_.user_active_last_); 418 EXPECT_EQ(7 * kSecondsPerDay + 45, daemon_.user_active_last_);
417 EXPECT_PRED_FORMAT2(AssertDailyUseRecord, /* day */ 7, /* seconds */ 30); 419 EXPECT_PRED_FORMAT2(AssertDailyUseRecord, /* day */ 7, /* seconds */ 30);
418 420
419 daemon_.PowerStateChanged("otherstate", 7 * kSecondsPerDay + 185); 421 daemon_.PowerStateChanged("otherstate", 7 * kSecondsPerDay + 185);
420 EXPECT_EQ(MetricsDaemon::kUnknownPowerState, daemon_.power_state_); 422 EXPECT_EQ(MetricsDaemon::kUnknownPowerState, daemon_.power_state_);
421 EXPECT_FALSE(daemon_.user_active_); 423 EXPECT_FALSE(daemon_.user_active_);
422 EXPECT_EQ(7 * kSecondsPerDay + 185, daemon_.user_active_last_); 424 EXPECT_EQ(7 * kSecondsPerDay + 185, daemon_.user_active_last_);
423 EXPECT_PRED_FORMAT2(AssertDailyUseRecord, /* day */ 7, /* seconds */ 30); 425 EXPECT_PRED_FORMAT2(AssertDailyUseRecord, /* day */ 7, /* seconds */ 30);
424 } 426 }
425 427
426 TEST_F(MetricsDaemonTest, PublishMetric) {
427 ExpectMetric("Dummy.Metric", 3, 1, 100, 50);
428 daemon_.PublishMetric("Dummy.Metric", /* sample */ 3,
429 /* min */ 1, /* max */ 100, /* buckets */ 50);
430 }
431
432 TEST_F(MetricsDaemonTest, ScreenSaverStateChanged) { 428 TEST_F(MetricsDaemonTest, ScreenSaverStateChanged) {
433 EXPECT_EQ(MetricsDaemon::kUnknownScreenSaverState, 429 EXPECT_EQ(MetricsDaemon::kUnknownScreenSaverState,
434 daemon_.screensaver_state_); 430 daemon_.screensaver_state_);
435 EXPECT_FALSE(daemon_.user_active_); 431 EXPECT_FALSE(daemon_.user_active_);
436 EXPECT_EQ(0, daemon_.user_active_last_); 432 EXPECT_EQ(0, daemon_.user_active_last_);
437 EXPECT_EQ(0, daemon_.daily_use_day_last_); 433 EXPECT_EQ(0, daemon_.daily_use_day_last_);
438 EXPECT_TRUE(AssertNoOrEmptyUseRecordFile()); 434 EXPECT_TRUE(AssertNoOrEmptyUseRecordFile());
439 435
440 daemon_.ScreenSaverStateChanged("locked", 5 * kSecondsPerDay + 10); 436 daemon_.ScreenSaverStateChanged("locked", 5 * kSecondsPerDay + 10);
441 EXPECT_EQ(MetricsDaemon::kScreenSaverStateLocked, 437 EXPECT_EQ(MetricsDaemon::kScreenSaverStateLocked,
(...skipping 11 matching lines...) Expand all
453 EXPECT_TRUE(AssertNoOrEmptyUseRecordFile()); 449 EXPECT_TRUE(AssertNoOrEmptyUseRecordFile());
454 450
455 daemon_.ScreenSaverStateChanged("otherstate", 5 * kSecondsPerDay + 300); 451 daemon_.ScreenSaverStateChanged("otherstate", 5 * kSecondsPerDay + 300);
456 EXPECT_EQ(MetricsDaemon::kUnknownScreenSaverState, 452 EXPECT_EQ(MetricsDaemon::kUnknownScreenSaverState,
457 daemon_.screensaver_state_); 453 daemon_.screensaver_state_);
458 EXPECT_FALSE(daemon_.user_active_); 454 EXPECT_FALSE(daemon_.user_active_);
459 EXPECT_EQ(5 * kSecondsPerDay + 300, daemon_.user_active_last_); 455 EXPECT_EQ(5 * kSecondsPerDay + 300, daemon_.user_active_last_);
460 EXPECT_PRED_FORMAT2(AssertDailyUseRecord, /* day */ 5, /* seconds */ 200); 456 EXPECT_PRED_FORMAT2(AssertDailyUseRecord, /* day */ 5, /* seconds */ 200);
461 } 457 }
462 458
459 TEST_F(MetricsDaemonTest, SendMetric) {
460 ExpectMetric("Dummy.Metric", 3, 1, 100, 50);
461 daemon_.SendMetric("Dummy.Metric", /* sample */ 3,
462 /* min */ 1, /* max */ 100, /* buckets */ 50);
463 }
464
463 TEST_F(MetricsDaemonTest, SessionStateChanged) { 465 TEST_F(MetricsDaemonTest, SessionStateChanged) {
464 EXPECT_EQ(MetricsDaemon::kUnknownSessionState, daemon_.session_state_); 466 EXPECT_EQ(MetricsDaemon::kUnknownSessionState, daemon_.session_state_);
465 EXPECT_FALSE(daemon_.user_active_); 467 EXPECT_FALSE(daemon_.user_active_);
466 EXPECT_EQ(0, daemon_.user_active_last_); 468 EXPECT_EQ(0, daemon_.user_active_last_);
467 EXPECT_EQ(0, daemon_.daily_use_day_last_); 469 EXPECT_EQ(0, daemon_.daily_use_day_last_);
468 EXPECT_TRUE(AssertNoOrEmptyUseRecordFile()); 470 EXPECT_TRUE(AssertNoOrEmptyUseRecordFile());
469 471
470 daemon_.SessionStateChanged("started", 15 * kSecondsPerDay + 20); 472 daemon_.SessionStateChanged("started", 15 * kSecondsPerDay + 20);
471 EXPECT_EQ(MetricsDaemon::kSessionStateStarted, daemon_.session_state_); 473 EXPECT_EQ(MetricsDaemon::kSessionStateStarted, daemon_.session_state_);
472 EXPECT_TRUE(daemon_.user_active_); 474 EXPECT_TRUE(daemon_.user_active_);
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
554 EXPECT_TRUE(daemon_.user_active_); 556 EXPECT_TRUE(daemon_.user_active_);
555 EXPECT_EQ(9 * kSecondsPerDay + 400, daemon_.user_active_last_); 557 EXPECT_EQ(9 * kSecondsPerDay + 400, daemon_.user_active_last_);
556 EXPECT_EQ(9, daemon_.daily_use_day_last_); 558 EXPECT_EQ(9, daemon_.daily_use_day_last_);
557 EXPECT_PRED_FORMAT2(AssertDailyUseRecord, /* day */ 9, /* seconds */ 800); 559 EXPECT_PRED_FORMAT2(AssertDailyUseRecord, /* day */ 9, /* seconds */ 800);
558 } 560 }
559 561
560 int main(int argc, char **argv) { 562 int main(int argc, char **argv) {
561 testing::InitGoogleTest(&argc, argv); 563 testing::InitGoogleTest(&argc, argv);
562 return RUN_ALL_TESTS(); 564 return RUN_ALL_TESTS();
563 } 565 }
OLDNEW
« no previous file with comments | « src/platform/metrics/metrics_daemon_main.cc ('k') | src/platform/metrics/metrics_library.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698