summaryrefslogtreecommitdiffstats
path: root/remoting/host/client_connection_unittest.cc
blob: b7cb10cb574f201ae809a1acb9396d40f1fee2ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (c) 2010 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 "media/base/data_buffer.h"
#include "remoting/base/mock_objects.h"
#include "remoting/host/client_connection.h"
#include "remoting/host/mock_objects.h"
#include "remoting/jingle_glue/mock_objects.h"
#include "testing/gmock/include/gmock/gmock.h"

using ::testing::_;
using ::testing::NotNull;
using ::testing::StrictMock;

namespace remoting {

class ClientConnectionTest : public testing::Test {
 public:
  ClientConnectionTest() {
  }

 protected:
  virtual void SetUp() {
    decoder_ = new MockProtocolDecoder();
    channel_ = new StrictMock<MockJingleChannel>();

    // Allocate a ClientConnection object with the mock objects. we give the
    // ownership of decoder to the viewer.
    viewer_ = new ClientConnection(&message_loop_,
                                   decoder_,
                                   &handler_);

    viewer_->set_jingle_channel(channel_.get());
  }

  MessageLoop message_loop_;
  MockProtocolDecoder* decoder_;
  MockClientConnectionEventHandler handler_;
  scoped_refptr<ClientConnection> viewer_;

  // |channel_| is wrapped with StrictMock because we limit strictly the calls
  // to it.
  scoped_refptr<StrictMock<MockJingleChannel> > channel_;

 private:
  DISALLOW_COPY_AND_ASSIGN(ClientConnectionTest);
};

TEST_F(ClientConnectionTest, SendUpdateStream) {
  // Tell the viewer we are starting an update stream.
  EXPECT_CALL(*channel_, Write(_));
  viewer_->SendBeginUpdateStreamMessage();

  // Then send the actual data.
  EXPECT_CALL(*channel_, Write(_));
  scoped_ptr<UpdateStreamPacketHeader> header(new UpdateStreamPacketHeader);
  header->set_x(0);
  header->set_y(0);
  header->set_width(640);
  header->set_height(480);

  scoped_refptr<media::DataBuffer> data = new media::DataBuffer(10);
  viewer_->SendUpdateStreamPacketMessage(header.get(), data);

  // Send the end of update message.
  EXPECT_CALL(*channel_, Write(_));
  viewer_->SendEndUpdateStreamMessage();

  // And then close the connection to ClientConnection.
  EXPECT_CALL(*channel_, Close());
  viewer_->Disconnect();
}

TEST_F(ClientConnectionTest, StateChange) {
  EXPECT_CALL(handler_, OnConnectionOpened(viewer_.get()));
  viewer_->OnStateChange(channel_.get(), JingleChannel::OPEN);
  message_loop_.RunAllPending();

  EXPECT_CALL(handler_, OnConnectionClosed(viewer_.get()));
  viewer_->OnStateChange(channel_.get(), JingleChannel::CLOSED);
  message_loop_.RunAllPending();

  EXPECT_CALL(handler_, OnConnectionFailed(viewer_.get()));
  viewer_->OnStateChange(channel_.get(), JingleChannel::FAILED);
  message_loop_.RunAllPending();
}

TEST_F(ClientConnectionTest, ParseMessages) {
  scoped_refptr<media::DataBuffer> data;

  // Give the data to the ClientConnection, it will use ProtocolDecoder to
  // decode the messages.
  EXPECT_CALL(*decoder_, ParseClientMessages(data, NotNull()));
  EXPECT_CALL(handler_, HandleMessages(viewer_.get(), NotNull()));

  viewer_->OnPacketReceived(channel_.get(), data);
  message_loop_.RunAllPending();
}

// Test that we can close client connection more than once and operations
// after the connection is closed has no effect.
TEST_F(ClientConnectionTest, Close) {
  EXPECT_CALL(*channel_, Close());
  viewer_->Disconnect();

  viewer_->SendBeginUpdateStreamMessage();
  scoped_ptr<UpdateStreamPacketHeader> header(new UpdateStreamPacketHeader);
  header->set_x(0);
  header->set_y(0);
  header->set_width(640);
  header->set_height(480);

  scoped_refptr<media::DataBuffer> data = new media::DataBuffer(10);
  viewer_->SendUpdateStreamPacketMessage(header.get(), data);
  viewer_->SendEndUpdateStreamMessage();
  viewer_->Disconnect();
}

}  // namespace remoting