1 // Copyright 2014 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.
5 #include "base/memory/scoped_ptr.h"
6 #include "base/test/test_simple_task_runner.h"
7 #include "content/renderer/media/mock_data_channel_impl.h"
8 #include "content/renderer/media/rtc_data_channel_handler.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/WebKit/public/platform/WebRTCDataChannelHandlerClient.h"
14 class MockDataChannelHandlerClient
:
15 public blink::WebRTCDataChannelHandlerClient
{
17 MockDataChannelHandlerClient() : state_(ReadyStateConnecting
) {}
19 void didChangeReadyState(ReadyState state
) override
{ state_
= state
; }
20 void didReceiveStringData(const blink::WebString
& s
) override
{}
21 void didReceiveRawData(const char* data
, size_t size
) override
{}
22 void didDetectError() override
{}
24 ReadyState
ready_state() const { return state_
; }
30 class RtcDataChannelHandlerTest
: public ::testing::Test
{
32 RtcDataChannelHandlerTest() {
33 signaling_thread_
= new base::TestSimpleTaskRunner();
36 void SetUp() override
{
37 channel_
= new rtc::RefCountedObject
<MockDataChannel
>("test", &config
);
40 void TearDown() override
{
43 signaling_thread_
->ClearPendingTasks();
46 webrtc::DataChannelInit config
;
47 scoped_refptr
<base::TestSimpleTaskRunner
> signaling_thread_
;
48 scoped_refptr
<MockDataChannel
> channel_
;
49 scoped_ptr
<RtcDataChannelHandler
> handler_
;
52 // Add a client, change to the open state, and verify that the client has
53 // reached the open state.
54 TEST_F(RtcDataChannelHandlerTest
, SetClient
) {
55 handler_
.reset(new RtcDataChannelHandler(signaling_thread_
, channel_
.get()));
56 MockDataChannelHandlerClient blink_channel
;
57 handler_
->setClient(&blink_channel
);
58 channel_
->changeState(webrtc::DataChannelInterface::kOpen
);
59 signaling_thread_
->RunPendingTasks();
60 EXPECT_EQ(MockDataChannelHandlerClient::ReadyStateOpen
,
61 blink_channel
.ready_state());
64 // Check that state() returns the expected default initial value.
65 TEST_F(RtcDataChannelHandlerTest
, InitialState
) {
66 handler_
.reset(new RtcDataChannelHandler(signaling_thread_
, channel_
.get()));
67 EXPECT_EQ(MockDataChannelHandlerClient::ReadyStateConnecting
,
71 // Check that state() returns the expected value when the channel opens early.
72 TEST_F(RtcDataChannelHandlerTest
, StateEarlyOpen
) {
73 channel_
->changeState(webrtc::DataChannelInterface::kOpen
);
74 signaling_thread_
->RunPendingTasks();
75 handler_
.reset(new RtcDataChannelHandler(signaling_thread_
, channel_
.get()));
76 EXPECT_EQ(MockDataChannelHandlerClient::ReadyStateOpen
,
80 } // namespace content