1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25 #include "MockWebRTCDataChannelHandler.h"
26
27 #include "public/platform/WebRTCDataChannelHandlerClient.h"
28 #include "public/testing/WebTestDelegate.h"
29 #include <assert.h>
30
31 using namespace blink;
32
33 namespace WebTestRunner {
34
35 class DataChannelReadyStateTask : public WebMethodTask<MockWebRTCDataChannelHandler> {
36 public:
DataChannelReadyStateTask(MockWebRTCDataChannelHandler * object,WebRTCDataChannelHandlerClient * dataChannelClient,WebRTCDataChannelHandlerClient::ReadyState state)37 DataChannelReadyStateTask(MockWebRTCDataChannelHandler* object, WebRTCDataChannelHandlerClient* dataChannelClient, WebRTCDataChannelHandlerClient::ReadyState state)
38 : WebMethodTask<MockWebRTCDataChannelHandler>(object)
39 , m_dataChannelClient(dataChannelClient)
40 , m_state(state)
41 {
42 }
43
runIfValid()44 virtual void runIfValid() OVERRIDE
45 {
46 m_dataChannelClient->didChangeReadyState(m_state);
47 }
48
49 private:
50 WebRTCDataChannelHandlerClient* m_dataChannelClient;
51 WebRTCDataChannelHandlerClient::ReadyState m_state;
52 };
53
54 /////////////////////
55
MockWebRTCDataChannelHandler(WebString label,const WebRTCDataChannelInit & init,WebTestDelegate * delegate)56 MockWebRTCDataChannelHandler::MockWebRTCDataChannelHandler(WebString label, const WebRTCDataChannelInit& init, WebTestDelegate* delegate)
57 : m_client(0)
58 , m_label(label)
59 , m_init(init)
60 , m_delegate(delegate)
61 {
62 m_reliable = (init.ordered && init.maxRetransmits == -1 && init.maxRetransmitTime == -1);
63 }
64
setClient(WebRTCDataChannelHandlerClient * client)65 void MockWebRTCDataChannelHandler::setClient(WebRTCDataChannelHandlerClient* client)
66 {
67 m_client = client;
68 if (m_client)
69 m_delegate->postTask(new DataChannelReadyStateTask(this, m_client, WebRTCDataChannelHandlerClient::ReadyStateOpen));
70 }
71
ordered() const72 bool MockWebRTCDataChannelHandler::ordered() const
73 {
74 return m_init.ordered;
75 }
76
maxRetransmitTime() const77 unsigned short MockWebRTCDataChannelHandler::maxRetransmitTime() const
78 {
79 return m_init.maxRetransmitTime;
80 }
81
maxRetransmits() const82 unsigned short MockWebRTCDataChannelHandler::maxRetransmits() const
83 {
84 return m_init.maxRetransmits;
85 }
86
protocol() const87 WebString MockWebRTCDataChannelHandler::protocol() const
88 {
89 return m_init.protocol;
90 }
91
negotiated() const92 bool MockWebRTCDataChannelHandler::negotiated() const
93 {
94 return m_init.negotiated;
95 }
96
id() const97 unsigned short MockWebRTCDataChannelHandler::id() const
98 {
99 return m_init.id;
100 }
101
bufferedAmount()102 unsigned long MockWebRTCDataChannelHandler::bufferedAmount()
103 {
104 return 0;
105 }
106
sendStringData(const WebString & data)107 bool MockWebRTCDataChannelHandler::sendStringData(const WebString& data)
108 {
109 assert(m_client);
110 m_client->didReceiveStringData(data);
111 return true;
112 }
113
sendRawData(const char * data,size_t size)114 bool MockWebRTCDataChannelHandler::sendRawData(const char* data, size_t size)
115 {
116 assert(m_client);
117 m_client->didReceiveRawData(data, size);
118 return true;
119 }
120
close()121 void MockWebRTCDataChannelHandler::close()
122 {
123 assert(m_client);
124 m_delegate->postTask(new DataChannelReadyStateTask(this, m_client, WebRTCDataChannelHandlerClient::ReadyStateClosed));
125 }
126
127 }
128