| Index: base/power_monitor/power_monitor_unittest.cc
|
| diff --git a/remoting/protocol/connection_to_client_unittest.cc b/base/power_monitor/power_monitor_unittest.cc
|
| similarity index 13%
|
| copy from remoting/protocol/connection_to_client_unittest.cc
|
| copy to base/power_monitor/power_monitor_unittest.cc
|
| index 5638728f0a61f90f9c07fb141dbab120b877a774..d55cf7d144491366a8b02c3bc35be2241fe75cae 100644
|
| --- a/remoting/protocol/connection_to_client_unittest.cc
|
| +++ b/base/power_monitor/power_monitor_unittest.cc
|
| @@ -2,105 +2,99 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| -#include "remoting/protocol/connection_to_client.h"
|
| +#include "base/power_monitor/power_monitor.h"
|
|
|
| -#include "base/bind.h"
|
| #include "base/message_loop.h"
|
| -#include "base/message_loop_proxy.h"
|
| -#include "remoting/base/constants.h"
|
| -#include "remoting/protocol/fake_session.h"
|
| -#include "remoting/protocol/protocol_mock_objects.h"
|
| -#include "testing/gmock/include/gmock/gmock.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
|
|
| -using ::testing::_;
|
| -using ::testing::NotNull;
|
| -using ::testing::StrictMock;
|
| +namespace base {
|
|
|
| -namespace remoting {
|
| -namespace protocol {
|
| -
|
| -class ConnectionToClientTest : public testing::Test {
|
| +class PowerTest : public PowerObserver {
|
| public:
|
| - ConnectionToClientTest() {
|
| + PowerTest()
|
| + : power_state_changes_(0),
|
| + suspends_(0),
|
| + resumes_(0) {
|
| }
|
|
|
| - protected:
|
| - virtual void SetUp() OVERRIDE {
|
| - session_ = new FakeSession();
|
| -
|
| - // Allocate a ClientConnection object with the mock objects.
|
| - viewer_.reset(new ConnectionToClient(session_));
|
| - viewer_->set_clipboard_stub(&clipboard_stub_);
|
| - viewer_->set_host_stub(&host_stub_);
|
| - viewer_->set_input_stub(&input_stub_);
|
| - viewer_->SetEventHandler(&handler_);
|
| - EXPECT_CALL(handler_, OnConnectionAuthenticated(viewer_.get()));
|
| - EXPECT_CALL(handler_, OnConnectionChannelsConnected(viewer_.get()));
|
| - session_->event_handler()->OnSessionStateChange(Session::CONNECTED);
|
| - session_->event_handler()->OnSessionStateChange(Session::AUTHENTICATED);
|
| - message_loop_.RunAllPending();
|
| + // PowerObserver callbacks.
|
| + virtual void OnPowerStateChange(bool on_battery_power) OVERRIDE {
|
| + power_state_changes_++;
|
| }
|
|
|
| - virtual void TearDown() OVERRIDE {
|
| - viewer_.reset();
|
| - message_loop_.RunAllPending();
|
| + virtual void OnSuspend() OVERRIDE {
|
| + suspends_++;
|
| }
|
|
|
| - MessageLoop message_loop_;
|
| - MockConnectionToClientEventHandler handler_;
|
| - MockClipboardStub clipboard_stub_;
|
| - MockHostStub host_stub_;
|
| - MockInputStub input_stub_;
|
| - scoped_ptr<ConnectionToClient> viewer_;
|
| + virtual void OnResume() OVERRIDE {
|
| + resumes_++;
|
| + }
|
|
|
| - // Owned by |viewer_|.
|
| - FakeSession* session_;
|
| + // Test status counts.
|
| + int power_state_changes() { return power_state_changes_; }
|
| + int suspends() { return suspends_; }
|
| + int resumes() { return resumes_; }
|
|
|
| private:
|
| - DISALLOW_COPY_AND_ASSIGN(ConnectionToClientTest);
|
| + int power_state_changes_; // Count of OnPowerStateChange notifications.
|
| + int suspends_; // Count of OnSuspend notifications.
|
| + int resumes_; // Count of OnResume notifications.
|
| };
|
|
|
| -TEST_F(ConnectionToClientTest, SendUpdateStream) {
|
| - scoped_ptr<VideoPacket> packet(new VideoPacket());
|
| - viewer_->video_stub()->ProcessVideoPacket(packet.Pass(), base::Closure());
|
| +class PowerMonitorTest : public testing::Test {
|
| + protected:
|
| + PowerMonitorTest() {
|
| +#if defined(OS_MACOSX)
|
| + // This needs to happen before PowerMonitor's ctor.
|
| + PowerMonitor::AllocateSystemIOPorts();
|
| +#endif
|
| + }
|
| + virtual ~PowerMonitorTest() {}
|
|
|
| - message_loop_.RunAllPending();
|
| + MessageLoop message_loop_;
|
| + PowerMonitor power_monitor_;
|
|
|
| - // Verify that something has been written.
|
| - // TODO(sergeyu): Verify that the correct data has been written.
|
| - ASSERT_TRUE(session_->GetStreamChannel(kVideoChannelName));
|
| - EXPECT_GT(session_->GetStreamChannel(kVideoChannelName)->
|
| - written_data().size(), 0u);
|
| + DISALLOW_COPY_AND_ASSIGN(PowerMonitorTest);
|
| +};
|
|
|
| - // And then close the connection to ConnectionToClient.
|
| - viewer_->Disconnect();
|
| +TEST_F(PowerMonitorTest, PowerNotifications) {
|
| + const int kObservers = 5;
|
|
|
| - message_loop_.RunAllPending();
|
| -}
|
| + PowerTest test[kObservers];
|
| + for (int index = 0; index < kObservers; ++index)
|
| + power_monitor_.AddObserver(&test[index]);
|
| +
|
| + // Send a bunch of power changes. Since the battery power hasn't
|
| + // actually changed, we shouldn't get notifications.
|
| + for (int index = 0; index < 5; index++) {
|
| + power_monitor_.ProcessPowerMessage(PowerMonitor::POWER_STATE_EVENT);
|
| + EXPECT_EQ(test[0].power_state_changes(), 0);
|
| + }
|
|
|
| -TEST_F(ConnectionToClientTest, NoWriteAfterDisconnect) {
|
| - scoped_ptr<VideoPacket> packet(new VideoPacket());
|
| - viewer_->video_stub()->ProcessVideoPacket(packet.Pass(), base::Closure());
|
| + // Sending resume when not suspended should have no effect.
|
| + power_monitor_.ProcessPowerMessage(PowerMonitor::RESUME_EVENT);
|
| + message_loop_.RunAllPending();
|
| + EXPECT_EQ(test[0].resumes(), 0);
|
|
|
| - // And then close the connection to ConnectionToClient.
|
| - viewer_->Disconnect();
|
| + // Pretend we suspended.
|
| + power_monitor_.ProcessPowerMessage(PowerMonitor::SUSPEND_EVENT);
|
| + message_loop_.RunAllPending();
|
| + EXPECT_EQ(test[0].suspends(), 1);
|
|
|
| - // The test will crash if data writer tries to write data to the
|
| - // channel socket.
|
| - // TODO(sergeyu): Use MockSession to verify that no data is written?
|
| + // Send a second suspend notification. This should be suppressed.
|
| + power_monitor_.ProcessPowerMessage(PowerMonitor::SUSPEND_EVENT);
|
| message_loop_.RunAllPending();
|
| -}
|
| + EXPECT_EQ(test[0].suspends(), 1);
|
|
|
| -TEST_F(ConnectionToClientTest, StateChange) {
|
| - EXPECT_CALL(handler_, OnConnectionClosed(viewer_.get(), OK));
|
| - session_->event_handler()->OnSessionStateChange(Session::CLOSED);
|
| + // Pretend we were awakened.
|
| + power_monitor_.ProcessPowerMessage(PowerMonitor::RESUME_EVENT);
|
| message_loop_.RunAllPending();
|
| + EXPECT_EQ(test[0].resumes(), 1);
|
|
|
| - EXPECT_CALL(handler_, OnConnectionClosed(viewer_.get(), SESSION_REJECTED));
|
| - session_->set_error(SESSION_REJECTED);
|
| - session_->event_handler()->OnSessionStateChange(Session::FAILED);
|
| + // Send a duplicate resume notification. This should be suppressed.
|
| + power_monitor_.ProcessPowerMessage(PowerMonitor::RESUME_EVENT);
|
| message_loop_.RunAllPending();
|
| + EXPECT_EQ(test[0].resumes(), 1);
|
| }
|
|
|
| -} // namespace protocol
|
| -} // namespace remoting
|
| +} // namespace base
|
|
|