[Mac] Implement Ambient Light API
[chromium-blink-merge.git] / content / browser / power_monitor_message_broadcaster_unittest.cc
blob32deb1ea09b7b70eeda124989a236e11535415a9
1 // Copyright 2013 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.
5 #include "base/test/power_monitor_test_base.h"
6 #include "content/browser/power_monitor_message_broadcaster.h"
7 #include "content/common/power_monitor_messages.h"
8 #include "ipc/ipc_sender.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace content {
13 class PowerMonitorMessageSender : public IPC::Sender {
14 public:
15 PowerMonitorMessageSender()
16 : power_state_changes_(0),
17 suspends_(0),
18 resumes_(0) {
20 ~PowerMonitorMessageSender() override {}
22 bool Send(IPC::Message* msg) override {
23 switch (msg->type()) {
24 case PowerMonitorMsg_Suspend::ID:
25 suspends_++;
26 break;
27 case PowerMonitorMsg_Resume::ID:
28 resumes_++;
29 break;
30 case PowerMonitorMsg_PowerStateChange::ID:
31 power_state_changes_++;
32 break;
34 delete msg;
35 return true;
38 // Test status counts.
39 int power_state_changes() { return power_state_changes_; }
40 int suspends() { return suspends_; }
41 int resumes() { return resumes_; }
43 private:
44 int power_state_changes_; // Count of OnPowerStateChange notifications.
45 int suspends_; // Count of OnSuspend notifications.
46 int resumes_; // Count of OnResume notifications.
49 class PowerMonitorMessageBroadcasterTest : public testing::Test {
50 protected:
51 PowerMonitorMessageBroadcasterTest() {
52 power_monitor_source_ = new base::PowerMonitorTestSource();
53 power_monitor_.reset(new base::PowerMonitor(
54 scoped_ptr<base::PowerMonitorSource>(power_monitor_source_)));
56 ~PowerMonitorMessageBroadcasterTest() override {}
58 base::PowerMonitorTestSource* source() { return power_monitor_source_; }
59 base::PowerMonitor* monitor() { return power_monitor_.get(); }
61 private:
62 base::PowerMonitorTestSource* power_monitor_source_;
63 scoped_ptr<base::PowerMonitor> power_monitor_;
65 DISALLOW_COPY_AND_ASSIGN(PowerMonitorMessageBroadcasterTest);
68 TEST_F(PowerMonitorMessageBroadcasterTest, PowerMessageBroadcast) {
69 PowerMonitorMessageSender sender;
70 PowerMonitorMessageBroadcaster broadcaster(&sender);
72 // Calling Init should invoke a power state change.
73 broadcaster.Init();
74 EXPECT_EQ(sender.power_state_changes(), 1);
76 // Sending resume when not suspended should have no effect.
77 source()->GenerateResumeEvent();
78 EXPECT_EQ(sender.resumes(), 0);
80 // Pretend we suspended.
81 source()->GenerateSuspendEvent();
82 EXPECT_EQ(sender.suspends(), 1);
84 // Send a second suspend notification. This should be suppressed.
85 source()->GenerateSuspendEvent();
86 EXPECT_EQ(sender.suspends(), 1);
88 // Pretend we were awakened.
89 source()->GenerateResumeEvent();
90 EXPECT_EQ(sender.resumes(), 1);
92 // Send a duplicate resume notification. This should be suppressed.
93 source()->GenerateResumeEvent();
94 EXPECT_EQ(sender.resumes(), 1);
96 // Pretend the device has gone on battery power
97 source()->GeneratePowerStateEvent(true);
98 EXPECT_EQ(sender.power_state_changes(), 2);
100 // Repeated indications the device is on battery power should be suppressed.
101 source()->GeneratePowerStateEvent(true);
102 EXPECT_EQ(sender.power_state_changes(), 2);
104 // Pretend the device has gone off battery power
105 source()->GeneratePowerStateEvent(false);
106 EXPECT_EQ(sender.power_state_changes(), 3);
108 // Repeated indications the device is off battery power should be suppressed.
109 source()->GeneratePowerStateEvent(false);
110 EXPECT_EQ(sender.power_state_changes(), 3);
113 } // namespace base