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

Side by Side Diff: base/power_monitor/power_monitor_unittest.cc

Issue 10959020: SystemMonitor refactoring: move power state monitor into a separate class called PowerMonitor (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Update patch per vandebo's comments Created 8 years, 2 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium 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 "remoting/protocol/connection_to_client.h" 5 #include "base/power_monitor/power_monitor.h"
6 6
7 #include "base/bind.h"
8 #include "base/message_loop.h" 7 #include "base/message_loop.h"
9 #include "base/message_loop_proxy.h" 8 #include "testing/gtest/include/gtest/gtest.h"
10 #include "remoting/base/constants.h"
11 #include "remoting/protocol/fake_session.h"
12 #include "remoting/protocol/protocol_mock_objects.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 9
15 using ::testing::_; 10 namespace base {
16 using ::testing::NotNull;
17 using ::testing::StrictMock;
18 11
19 namespace remoting { 12 class PowerTest : public PowerObserver {
20 namespace protocol {
21
22 class ConnectionToClientTest : public testing::Test {
23 public: 13 public:
24 ConnectionToClientTest() { 14 PowerTest()
15 : power_state_changes_(0),
16 suspends_(0),
17 resumes_(0) {
25 } 18 }
26 19
27 protected: 20 // PowerObserver callbacks.
28 virtual void SetUp() OVERRIDE { 21 virtual void OnPowerStateChange(bool on_battery_power) OVERRIDE {
29 session_ = new FakeSession(); 22 power_state_changes_++;
30
31 // Allocate a ClientConnection object with the mock objects.
32 viewer_.reset(new ConnectionToClient(session_));
33 viewer_->set_clipboard_stub(&clipboard_stub_);
34 viewer_->set_host_stub(&host_stub_);
35 viewer_->set_input_stub(&input_stub_);
36 viewer_->SetEventHandler(&handler_);
37 EXPECT_CALL(handler_, OnConnectionAuthenticated(viewer_.get()));
38 EXPECT_CALL(handler_, OnConnectionChannelsConnected(viewer_.get()));
39 session_->event_handler()->OnSessionStateChange(Session::CONNECTED);
40 session_->event_handler()->OnSessionStateChange(Session::AUTHENTICATED);
41 message_loop_.RunAllPending();
42 } 23 }
43 24
44 virtual void TearDown() OVERRIDE { 25 virtual void OnSuspend() OVERRIDE {
45 viewer_.reset(); 26 suspends_++;
46 message_loop_.RunAllPending();
47 } 27 }
48 28
49 MessageLoop message_loop_; 29 virtual void OnResume() OVERRIDE {
50 MockConnectionToClientEventHandler handler_; 30 resumes_++;
51 MockClipboardStub clipboard_stub_; 31 }
52 MockHostStub host_stub_;
53 MockInputStub input_stub_;
54 scoped_ptr<ConnectionToClient> viewer_;
55 32
56 // Owned by |viewer_|. 33 // Test status counts.
57 FakeSession* session_; 34 int power_state_changes() { return power_state_changes_; }
35 int suspends() { return suspends_; }
36 int resumes() { return resumes_; }
58 37
59 private: 38 private:
60 DISALLOW_COPY_AND_ASSIGN(ConnectionToClientTest); 39 int power_state_changes_; // Count of OnPowerStateChange notifications.
40 int suspends_; // Count of OnSuspend notifications.
41 int resumes_; // Count of OnResume notifications.
61 }; 42 };
62 43
63 TEST_F(ConnectionToClientTest, SendUpdateStream) { 44 class PowerMonitorTest : public testing::Test {
64 scoped_ptr<VideoPacket> packet(new VideoPacket()); 45 protected:
65 viewer_->video_stub()->ProcessVideoPacket(packet.Pass(), base::Closure()); 46 PowerMonitorTest() {
47 #if defined(OS_MACOSX)
48 // This needs to happen before PowerMonitor's ctor.
49 PowerMonitor::AllocateSystemIOPorts();
50 #endif
51 }
52 virtual ~PowerMonitorTest() {}
66 53
54 MessageLoop message_loop_;
55 PowerMonitor power_monitor_;
56
57 DISALLOW_COPY_AND_ASSIGN(PowerMonitorTest);
58 };
59
60 TEST_F(PowerMonitorTest, PowerNotifications) {
61 const int kObservers = 5;
62
63 PowerTest test[kObservers];
64 for (int index = 0; index < kObservers; ++index)
65 power_monitor_.AddObserver(&test[index]);
66
67 // Send a bunch of power changes. Since the battery power hasn't
68 // actually changed, we shouldn't get notifications.
69 for (int index = 0; index < 5; index++) {
70 power_monitor_.ProcessPowerMessage(PowerMonitor::POWER_STATE_EVENT);
71 EXPECT_EQ(test[0].power_state_changes(), 0);
72 }
73
74 // Sending resume when not suspended should have no effect.
75 power_monitor_.ProcessPowerMessage(PowerMonitor::RESUME_EVENT);
67 message_loop_.RunAllPending(); 76 message_loop_.RunAllPending();
77 EXPECT_EQ(test[0].resumes(), 0);
68 78
69 // Verify that something has been written. 79 // Pretend we suspended.
70 // TODO(sergeyu): Verify that the correct data has been written. 80 power_monitor_.ProcessPowerMessage(PowerMonitor::SUSPEND_EVENT);
71 ASSERT_TRUE(session_->GetStreamChannel(kVideoChannelName)); 81 message_loop_.RunAllPending();
72 EXPECT_GT(session_->GetStreamChannel(kVideoChannelName)-> 82 EXPECT_EQ(test[0].suspends(), 1);
73 written_data().size(), 0u);
74 83
75 // And then close the connection to ConnectionToClient. 84 // Send a second suspend notification. This should be suppressed.
76 viewer_->Disconnect(); 85 power_monitor_.ProcessPowerMessage(PowerMonitor::SUSPEND_EVENT);
86 message_loop_.RunAllPending();
87 EXPECT_EQ(test[0].suspends(), 1);
77 88
89 // Pretend we were awakened.
90 power_monitor_.ProcessPowerMessage(PowerMonitor::RESUME_EVENT);
78 message_loop_.RunAllPending(); 91 message_loop_.RunAllPending();
92 EXPECT_EQ(test[0].resumes(), 1);
93
94 // Send a duplicate resume notification. This should be suppressed.
95 power_monitor_.ProcessPowerMessage(PowerMonitor::RESUME_EVENT);
96 message_loop_.RunAllPending();
97 EXPECT_EQ(test[0].resumes(), 1);
79 } 98 }
80 99
81 TEST_F(ConnectionToClientTest, NoWriteAfterDisconnect) { 100 } // namespace base
82 scoped_ptr<VideoPacket> packet(new VideoPacket());
83 viewer_->video_stub()->ProcessVideoPacket(packet.Pass(), base::Closure());
84
85 // And then close the connection to ConnectionToClient.
86 viewer_->Disconnect();
87
88 // The test will crash if data writer tries to write data to the
89 // channel socket.
90 // TODO(sergeyu): Use MockSession to verify that no data is written?
91 message_loop_.RunAllPending();
92 }
93
94 TEST_F(ConnectionToClientTest, StateChange) {
95 EXPECT_CALL(handler_, OnConnectionClosed(viewer_.get(), OK));
96 session_->event_handler()->OnSessionStateChange(Session::CLOSED);
97 message_loop_.RunAllPending();
98
99 EXPECT_CALL(handler_, OnConnectionClosed(viewer_.get(), SESSION_REJECTED));
100 session_->set_error(SESSION_REJECTED);
101 session_->event_handler()->OnSessionStateChange(Session::FAILED);
102 message_loop_.RunAllPending();
103 }
104
105 } // namespace protocol
106 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698