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

Unified Diff: content/common/power_monitor_broadcast_source_unittest.cc

Issue 17074009: Created multi-process-friendly PowerMonitor interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed message export for unit tests Created 7 years, 5 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 side-by-side diff with in-line comments
Download patch
Index: content/common/power_monitor_broadcast_source_unittest.cc
diff --git a/content/common/power_monitor_broadcast_source_unittest.cc b/content/common/power_monitor_broadcast_source_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..667832ec43a29d4197aa6e0815b96617080e0249
--- /dev/null
+++ b/content/common/power_monitor_broadcast_source_unittest.cc
@@ -0,0 +1,93 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/message_loop.h"
+#include "base/power_monitor/power_monitor_test_base.h"
+#include "content/common/power_monitor_broadcast_source.h"
+#include "content/common/power_monitor_messages.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace content {
+
+class PowerMonitorBroadcastSourceTest : public testing::Test {
+ protected:
+ PowerMonitorBroadcastSourceTest() {
+ power_monitor_source_ = new PowerMonitorBroadcastSource();
+ power_monitor_.reset(new base::PowerMonitor(power_monitor_source_));
+ }
+ virtual ~PowerMonitorBroadcastSourceTest() {}
+
+ PowerMonitorBroadcastSource* source() { return power_monitor_source_; }
+ base::PowerMonitor* monitor() { return power_monitor_.get(); }
+
+ base::MessageLoop message_loop_;
+
+ private:
+ PowerMonitorBroadcastSource* power_monitor_source_;
+ scoped_ptr<base::PowerMonitor> power_monitor_;
+
+ DISALLOW_COPY_AND_ASSIGN(PowerMonitorBroadcastSourceTest);
+};
+
+TEST_F(PowerMonitorBroadcastSourceTest, PowerMessageReceiveBroadcast) {
+ IPC::ChannelProxy::MessageFilter* message_filter = source()->MessageFilter();
+
+ base::PowerMonitorTestObserver observer;
+ monitor()->AddObserver(&observer);
+
+ PowerMonitorMsg_Suspend suspend_msg;
+ PowerMonitorMsg_Resume resume_msg;
+
+ // Sending resume when not suspended should have no effect.
+ message_filter->OnMessageReceived(resume_msg);
+ message_loop_.RunUntilIdle();
+ EXPECT_EQ(observer.resumes(), 0);
+
+ // Pretend we suspended.
+ message_filter->OnMessageReceived(suspend_msg);
+ message_loop_.RunUntilIdle();
+ EXPECT_EQ(observer.suspends(), 1);
+
+ // Send a second suspend notification. This should be suppressed.
+ message_filter->OnMessageReceived(suspend_msg);
+ message_loop_.RunUntilIdle();
+ EXPECT_EQ(observer.suspends(), 1);
+
+ // Pretend we were awakened.
+ message_filter->OnMessageReceived(resume_msg);
+ message_loop_.RunUntilIdle();
+ EXPECT_EQ(observer.resumes(), 1);
+
+ // Send a duplicate resume notification. This should be suppressed.
+ message_filter->OnMessageReceived(resume_msg);
+ message_loop_.RunUntilIdle();
+ EXPECT_EQ(observer.resumes(), 1);
+
+ PowerMonitorMsg_PowerStateChange on_battery_msg(true);
+ PowerMonitorMsg_PowerStateChange off_battery_msg(false);
+
+ // Pretend the device has gone on battery power
+ message_filter->OnMessageReceived(on_battery_msg);
+ message_loop_.RunUntilIdle();
+ EXPECT_EQ(observer.power_state_changes(), 1);
+ EXPECT_EQ(observer.last_power_state(), true);
+
+ // Repeated indications the device is on battery power should be suppressed.
+ message_filter->OnMessageReceived(on_battery_msg);
+ message_loop_.RunUntilIdle();
+ EXPECT_EQ(observer.power_state_changes(), 1);
+
+ // Pretend the device has gone off battery power
+ message_filter->OnMessageReceived(off_battery_msg);
+ message_loop_.RunUntilIdle();
+ EXPECT_EQ(observer.power_state_changes(), 2);
+ EXPECT_EQ(observer.last_power_state(), false);
+
+ // Repeated indications the device is off battery power should be suppressed.
+ message_filter->OnMessageReceived(off_battery_msg);
+ message_loop_.RunUntilIdle();
+ EXPECT_EQ(observer.power_state_changes(), 2);
+}
+
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698