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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
// 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/ref_counted.h"
#include "base/waitable_event.h"
#include "media/base/data_buffer.h"
#include "remoting/jingle_glue/jingle_channel.h"
#include "remoting/jingle_glue/jingle_thread.h"
#include "remoting/jingle_glue/mock_objects.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/libjingle/source/talk/base/stream.h"
using testing::_;
using testing::Return;
using testing::Mock;
using testing::SetArgumentPointee;
namespace remoting {
namespace {
// Size of test buffer in bytes.
const size_t kBufferSize = 100;
} // namespace
class MockJingleChannelCallback : public JingleChannel::Callback {
public:
MOCK_METHOD2(OnStateChange, void(JingleChannel*, JingleChannel::State));
MOCK_METHOD2(OnPacketReceived, void(JingleChannel*,
scoped_refptr<media::DataBuffer>));
};
class JingleChannelTest : public testing::Test {
public:
virtual ~JingleChannelTest() { }
// A helper that calls OnStreamEvent(). Need this because we want
// to call it on the jingle thread.
static void StreamEvent(JingleChannel* channel,
talk_base::StreamInterface* stream,
int event, int error,
base::WaitableEvent* done_event) {
channel->OnStreamEvent(stream, event, error);
if (done_event)
done_event->Signal();
}
static void OnClosed(bool* called) {
*called = true;
}
protected:
virtual void SetUp() {
stream_ = new MockStream(); // Freed by the channel.
channel_ = new JingleChannel(&callback_);
channel_->thread_ = &thread_;
channel_->stream_.reset(stream_);
}
JingleThread thread_;
MockStream* stream_;
MockJingleChannelCallback callback_;
scoped_refptr<JingleChannel> channel_;
};
TEST_F(JingleChannelTest, Init) {
EXPECT_CALL(*stream_, GetState())
.Times(1)
.WillRepeatedly(Return(talk_base::SS_OPENING));
EXPECT_CALL(callback_, OnStateChange(channel_.get(),
JingleChannel::CONNECTING))
.Times(1);
thread_.Start();
EXPECT_EQ(JingleChannel::INITIALIZING, channel_->state());
channel_->Init(&thread_, stream_, "user@domain.com");
EXPECT_EQ(JingleChannel::CONNECTING, channel_->state());
channel_->closed_ = true;
thread_.Stop();
}
TEST_F(JingleChannelTest, Write) {
scoped_refptr<media::DataBuffer> data = new media::DataBuffer(kBufferSize);
data->SetDataSize(kBufferSize);
EXPECT_CALL(*stream_, Write(static_cast<const void*>(data->GetData()),
kBufferSize, _, _))
.WillOnce(DoAll(SetArgumentPointee<2>(kBufferSize),
Return(talk_base::SR_SUCCESS)));
channel_->state_ = JingleChannel::OPEN;
thread_.Start();
channel_->Write(data);
thread_.Stop();
channel_->closed_ = true;
}
TEST_F(JingleChannelTest, Read) {
scoped_refptr<media::DataBuffer> data = new media::DataBuffer(kBufferSize);
data->SetDataSize(kBufferSize);
EXPECT_CALL(callback_, OnPacketReceived(channel_.get(), _))
.Times(1);
EXPECT_CALL(*stream_, GetAvailable(_))
.WillOnce(DoAll(SetArgumentPointee<0>(kBufferSize),
Return(true)))
.WillOnce(DoAll(SetArgumentPointee<0>(0),
Return(true)));
EXPECT_CALL(*stream_, Read(_, kBufferSize, _, _))
.WillOnce(DoAll(SetArgumentPointee<2>(kBufferSize),
Return(talk_base::SR_SUCCESS)));
channel_->state_ = JingleChannel::OPEN;
thread_.Start();
base::WaitableEvent done_event(true, false);
thread_.message_loop()->PostTask(FROM_HERE, NewRunnableFunction(
&JingleChannelTest::StreamEvent, channel_.get(), stream_,
talk_base::SE_READ, 0, &done_event));
done_event.Wait();
thread_.Stop();
channel_->closed_ = true;
}
TEST_F(JingleChannelTest, Close) {
EXPECT_CALL(*stream_, Close()).Times(1);
// Don't expect any calls except Close().
EXPECT_CALL(*stream_, GetAvailable(_)).Times(0);
EXPECT_CALL(*stream_, Read(_, _, _, _)).Times(0);
EXPECT_CALL(callback_, OnPacketReceived(_, _)).Times(0);
thread_.Start();
channel_->Close();
// Verify that the channel doesn't call callback anymore.
thread_.message_loop()->PostTask(FROM_HERE, NewRunnableFunction(
&JingleChannelTest::StreamEvent, channel_.get(), stream_,
talk_base::SE_READ, 0, static_cast<base::WaitableEvent*>(NULL)));
thread_.Stop();
}
TEST_F(JingleChannelTest, ClosedTask) {
EXPECT_CALL(*stream_, Close())
.Times(1);
thread_.Start();
bool closed = false;
channel_->Close(NewRunnableFunction(&JingleChannelTest::OnClosed,
&closed));
thread_.Stop();
EXPECT_TRUE(closed);
}
TEST_F(JingleChannelTest, DoubleClose) {
EXPECT_CALL(*stream_, Close())
.Times(1);
thread_.Start();
bool closed1 = false;
channel_->Close(NewRunnableFunction(&JingleChannelTest::OnClosed,
&closed1));
bool closed2 = false;
channel_->Close(NewRunnableFunction(&JingleChannelTest::OnClosed,
&closed2));
thread_.Stop();
EXPECT_TRUE(closed1 && closed2);
}
} // namespace remoting
|