OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "base/macros.h" | |
6 #include "base/message_loop/message_loop.h" | |
7 #include "base/run_loop.h" | |
8 #include "base/test/power_monitor_test_base.h" | |
9 #include "content/browser/power_monitor_message_broadcaster.h" | |
10 #include "mojo/public/cpp/bindings/binding.h" | |
11 #include "testing/gtest/include/gtest/gtest.h" | |
12 | |
13 namespace content { | |
14 | |
15 class FakePowerMonitorClient : public device::mojom::PowerMonitorClient { | |
16 public: | |
17 FakePowerMonitorClient(device::mojom::PowerMonitorClientRequest request) | |
18 : binding_(this, std::move(request)), | |
19 power_state_changes_(0), | |
20 suspends_(0), | |
21 resumes_(0) {} | |
22 ~FakePowerMonitorClient() override {} | |
23 | |
24 // Implement device::mojom::PowerMonitorClient | |
25 void PowerStateChange(bool on_battery_power) override { | |
26 power_state_changes_++; | |
27 } | |
28 void Suspend() override { suspends_++; } | |
29 void Resume() override { resumes_++; } | |
30 | |
31 // Test status counts. | |
32 int power_state_changes() { return power_state_changes_; } | |
33 int suspends() { return suspends_; } | |
34 int resumes() { return resumes_; } | |
35 | |
36 private: | |
37 mojo::Binding<device::mojom::PowerMonitorClient> binding_; | |
38 int power_state_changes_; // Count of OnPowerStateChange notifications. | |
39 int suspends_; // Count of OnSuspend notifications. | |
40 int resumes_; // Count of OnResume notifications. | |
41 }; | |
42 | |
43 class PowerMonitorMessageBroadcasterTest : public testing::Test { | |
44 protected: | |
45 PowerMonitorMessageBroadcasterTest() { | |
46 power_monitor_source_ = new base::PowerMonitorTestSource(); | |
47 power_monitor_.reset(new base::PowerMonitor( | |
48 std::unique_ptr<base::PowerMonitorSource>(power_monitor_source_))); | |
49 } | |
50 ~PowerMonitorMessageBroadcasterTest() override {} | |
51 | |
52 base::PowerMonitorTestSource* source() { return power_monitor_source_; } | |
53 base::PowerMonitor* monitor() { return power_monitor_.get(); } | |
54 | |
55 private: | |
56 base::MessageLoop message_loop_; | |
57 base::PowerMonitorTestSource* power_monitor_source_; | |
58 std::unique_ptr<base::PowerMonitor> power_monitor_; | |
59 | |
60 DISALLOW_COPY_AND_ASSIGN(PowerMonitorMessageBroadcasterTest); | |
61 }; | |
62 | |
63 TEST_F(PowerMonitorMessageBroadcasterTest, PowerMessageBroadcast) { | |
64 device::mojom::PowerMonitorClientPtr proxy; | |
65 FakePowerMonitorClient client(mojo::GetProxy(&proxy)); | |
66 PowerMonitorMessageBroadcaster broadcaster; | |
67 | |
68 // Calling SetClient should invoke a power state change. | |
69 broadcaster.SetClient(std::move(proxy)); | |
70 | |
71 base::RunLoop().RunUntilIdle(); | |
72 EXPECT_EQ(client.power_state_changes(), 1); | |
73 | |
74 // Sending resume when not suspended should have no effect. | |
75 source()->GenerateResumeEvent(); | |
76 EXPECT_EQ(client.resumes(), 0); | |
77 | |
78 // Pretend we suspended. | |
79 source()->GenerateSuspendEvent(); | |
80 EXPECT_EQ(client.suspends(), 1); | |
81 | |
82 // Send a second suspend notification. This should be suppressed. | |
83 source()->GenerateSuspendEvent(); | |
84 EXPECT_EQ(client.suspends(), 1); | |
85 | |
86 // Pretend we were awakened. | |
87 source()->GenerateResumeEvent(); | |
88 EXPECT_EQ(client.resumes(), 1); | |
89 | |
90 // Send a duplicate resume notification. This should be suppressed. | |
91 source()->GenerateResumeEvent(); | |
92 EXPECT_EQ(client.resumes(), 1); | |
93 | |
94 // Pretend the device has gone on battery power | |
95 source()->GeneratePowerStateEvent(true); | |
96 EXPECT_EQ(client.power_state_changes(), 2); | |
97 | |
98 // Repeated indications the device is on battery power should be suppressed. | |
99 source()->GeneratePowerStateEvent(true); | |
100 EXPECT_EQ(client.power_state_changes(), 2); | |
101 | |
102 // Pretend the device has gone off battery power | |
103 source()->GeneratePowerStateEvent(false); | |
104 EXPECT_EQ(client.power_state_changes(), 3); | |
105 | |
106 // Repeated indications the device is off battery power should be suppressed. | |
107 source()->GeneratePowerStateEvent(false); | |
108 EXPECT_EQ(client.power_state_changes(), 3); | |
109 } | |
110 | |
111 } // namespace base | |
OLD | NEW |