OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/ref_counted.h" |
| 6 #include "media/base/data_buffer.h" |
| 7 #include "remoting/jingle_glue/jingle_channel.h" |
| 8 #include "remoting/jingle_glue/jingle_thread.h" |
| 9 #include "talk/base/stream.h" |
| 10 #include "testing/gmock/include/gmock/gmock.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 using testing::_; |
| 14 using testing::Return; |
| 15 using testing::Mock; |
| 16 using testing::SetArgumentPointee; |
| 17 |
| 18 namespace remoting { |
| 19 |
| 20 namespace { |
| 21 const size_t kBufferSize = 100; |
| 22 } // namespace |
| 23 |
| 24 class MockCallback : public JingleChannel::Callback { |
| 25 public: |
| 26 MOCK_METHOD2(OnStateChange, void(JingleChannel*, JingleChannel::State)); |
| 27 MOCK_METHOD2(OnPacketReceived, void(JingleChannel*, |
| 28 scoped_refptr<media::DataBuffer>)); |
| 29 }; |
| 30 |
| 31 class MockStream : public talk_base::StreamInterface { |
| 32 public: |
| 33 virtual ~MockStream() {} |
| 34 MOCK_CONST_METHOD0(GetState, talk_base::StreamState ()); |
| 35 |
| 36 MOCK_METHOD4(Read, talk_base::StreamResult (void*, size_t, |
| 37 size_t*, int*)); |
| 38 MOCK_METHOD4(Write, talk_base::StreamResult (const void*, size_t, |
| 39 size_t*, int*)); |
| 40 MOCK_CONST_METHOD1(GetAvailable, bool (size_t *)); |
| 41 MOCK_METHOD0(Close, void ()); |
| 42 |
| 43 MOCK_METHOD3(PostEvent, void (talk_base::Thread*, int, int)); |
| 44 MOCK_METHOD2(PostEvent, void (int, int)); |
| 45 }; |
| 46 |
| 47 TEST(JingleChannelTest, Init) { |
| 48 JingleThread thread; |
| 49 |
| 50 MockStream *stream = new MockStream(); |
| 51 MockCallback callback; |
| 52 |
| 53 EXPECT_CALL(*stream, GetState()) |
| 54 .Times(1) |
| 55 .WillRepeatedly(Return(talk_base::SS_OPENING)); |
| 56 |
| 57 scoped_refptr<JingleChannel> channel = new JingleChannel(&callback); |
| 58 |
| 59 EXPECT_CALL(callback, OnStateChange(channel.get(), JingleChannel::CONNECTING)) |
| 60 .Times(1); |
| 61 |
| 62 thread.Start(); |
| 63 |
| 64 EXPECT_EQ(JingleChannel::INITIALIZING, channel->state()); |
| 65 channel->Init(&thread, stream, "user@domain.com"); |
| 66 EXPECT_EQ(JingleChannel::CONNECTING, channel->state()); |
| 67 channel->state_ = JingleChannel::CLOSED; |
| 68 |
| 69 thread.Stop(); |
| 70 } |
| 71 |
| 72 TEST(JingleChannelTest, Write) { |
| 73 JingleThread thread; |
| 74 MockStream* stream = new MockStream(); // Freed by the channel. |
| 75 MockCallback callback; |
| 76 |
| 77 scoped_refptr<media::DataBuffer> data = new media::DataBuffer(kBufferSize); |
| 78 data->SetDataSize(kBufferSize); |
| 79 |
| 80 EXPECT_CALL(*stream, Write(static_cast<const void*>(data->GetData()), |
| 81 kBufferSize, _, _)) |
| 82 .WillOnce(DoAll(SetArgumentPointee<2>(kBufferSize), |
| 83 Return(talk_base::SR_SUCCESS))); |
| 84 |
| 85 scoped_refptr<JingleChannel> channel = new JingleChannel(&callback); |
| 86 |
| 87 channel->thread_ = &thread; |
| 88 channel->stream_.reset(stream); |
| 89 channel->state_ = JingleChannel::OPEN; |
| 90 thread.Start(); |
| 91 channel->Write(data); |
| 92 thread.Stop(); |
| 93 channel->state_ = JingleChannel::CLOSED; |
| 94 } |
| 95 |
| 96 TEST(JingleChannelTest, Read) { |
| 97 JingleThread thread; |
| 98 MockStream* stream = new MockStream(); // Freed by the channel. |
| 99 MockCallback callback; |
| 100 |
| 101 scoped_refptr<media::DataBuffer> data = new media::DataBuffer(kBufferSize); |
| 102 data->SetDataSize(kBufferSize); |
| 103 |
| 104 scoped_refptr<JingleChannel> channel = new JingleChannel(&callback); |
| 105 |
| 106 EXPECT_CALL(callback, OnPacketReceived(channel.get(), _)) |
| 107 .Times(1); |
| 108 |
| 109 EXPECT_CALL(*stream, GetAvailable(_)) |
| 110 .WillOnce(DoAll(SetArgumentPointee<0>(kBufferSize), |
| 111 Return(true))) |
| 112 .WillOnce(DoAll(SetArgumentPointee<0>(0), |
| 113 Return(true))); |
| 114 |
| 115 EXPECT_CALL(*stream, Read(_, kBufferSize, _, _)) |
| 116 .WillOnce(DoAll(SetArgumentPointee<2>(kBufferSize), |
| 117 Return(talk_base::SR_SUCCESS))); |
| 118 |
| 119 channel->thread_ = &thread; |
| 120 channel->stream_.reset(stream); |
| 121 channel->state_ = JingleChannel::OPEN; |
| 122 thread.Start(); |
| 123 channel->OnStreamEvent(stream, talk_base::SE_READ, 0); |
| 124 thread.Stop(); |
| 125 channel->state_ = JingleChannel::CLOSED; |
| 126 } |
| 127 |
| 128 TEST(JingleChannelTest, Close) { |
| 129 JingleThread thread; |
| 130 MockStream* stream = new MockStream(); // Freed by the channel. |
| 131 MockCallback callback; |
| 132 |
| 133 EXPECT_CALL(*stream, Close()) |
| 134 .Times(1); |
| 135 |
| 136 scoped_refptr<JingleChannel> channel = new JingleChannel(&callback); |
| 137 |
| 138 channel->thread_ = &thread; |
| 139 channel->stream_.reset(stream); |
| 140 channel->state_ = JingleChannel::OPEN; |
| 141 thread.Start(); |
| 142 channel->Close(); |
| 143 thread.Stop(); |
| 144 } |
| 145 |
| 146 } // namespace remoting |
OLD | NEW |