OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 "config.h" | |
6 #include "modules/mediastream/RTCDataChannel.h" | |
7 | |
8 #include "bindings/core/v8/ExceptionStatePlaceholder.h" | |
9 #include "core/dom/DOMArrayBuffer.h" | |
10 #include "core/dom/DOMException.h" | |
11 #include "core/dom/ExceptionCode.h" | |
12 #include "core/events/Event.h" | |
13 #include "platform/heap/Heap.h" | |
14 #include "public/platform/WebRTCDataChannelHandler.h" | |
15 #include "public/platform/WebUnitTestSupport.h" | |
16 #include "public/platform/WebVector.h" | |
17 #include "wtf/RefPtr.h" | |
18 #include "wtf/testing/WTFTestHelpers.h" | |
19 #include "wtf/text/WTFString.h" | |
20 #include <gtest/gtest.h> | |
21 | |
22 namespace blink { | |
23 namespace { | |
24 | |
25 class MockHandler final : public WebRTCDataChannelHandler { | |
26 public: | |
27 MockHandler() | |
28 : m_client(0) | |
29 , m_state(WebRTCDataChannelHandlerClient::ReadyStateConnecting) | |
30 , m_bufferedAmount(0) | |
31 { | |
32 } | |
33 virtual void setClient(WebRTCDataChannelHandlerClient* client) { m_client = client; } | |
tkent
2015/06/09 23:47:27
Remove |virtual|, and add |override| or |final|.
S
| |
34 virtual WebString label() { return ""; } | |
35 virtual bool ordered() const { return true; } | |
36 virtual unsigned short maxRetransmitTime() const { return 0; } | |
37 virtual unsigned short maxRetransmits() const { return 0; } | |
38 virtual WebString protocol() const { return ""; } | |
39 virtual bool negotiated() const { return false; } | |
40 virtual unsigned short id() const { return 0; } | |
41 | |
42 virtual WebRTCDataChannelHandlerClient::ReadyState state() const { return m_ state; } | |
43 virtual unsigned long bufferedAmount() { return m_bufferedAmount; } | |
44 virtual bool sendStringData(const WebString& s) | |
45 { | |
46 m_bufferedAmount += s.length(); | |
47 return true; | |
48 } | |
49 virtual bool sendRawData(const char* data, size_t length) | |
50 { | |
51 m_bufferedAmount += length; | |
52 return true; | |
53 } | |
54 virtual void close() { } | |
55 | |
56 // Methods for testing. | |
57 void changeState(WebRTCDataChannelHandlerClient::ReadyState state) | |
58 { | |
59 m_state = state; | |
60 if (m_client) { | |
61 m_client->didChangeReadyState(m_state); | |
62 } | |
63 } | |
64 void drainBuffer(unsigned long bytes) | |
65 { | |
66 unsigned long oldBufferedAmount = m_bufferedAmount; | |
67 m_bufferedAmount -= bytes; | |
68 if (m_client) { | |
69 m_client->didDecreaseBufferedAmount(oldBufferedAmount); | |
70 } | |
71 } | |
72 private: | |
tkent
2015/06/09 23:47:27
nit: add a blank line before |private:|
| |
73 WebRTCDataChannelHandlerClient* m_client; | |
74 WebRTCDataChannelHandlerClient::ReadyState m_state; | |
75 unsigned long m_bufferedAmount; | |
76 }; | |
77 | |
78 } // namespace | |
79 | |
80 TEST(RTCDataChannelTest, BufferedAmount) | |
81 { | |
82 MockHandler* handler = new MockHandler(); | |
83 RTCDataChannel* channel = RTCDataChannel::create(0, 0, adoptPtr(handler)); | |
84 | |
85 handler->changeState(WebRTCDataChannelHandlerClient::ReadyStateOpen); | |
86 String message(std::string(100, 'A').c_str()); | |
87 channel->send(message, IGNORE_EXCEPTION); | |
88 EXPECT_EQ(100U, channel->bufferedAmount()); | |
89 } | |
90 | |
91 TEST(RTCDataChannelTest, BufferedAmountLow) | |
92 { | |
93 MockHandler* handler = new MockHandler(); | |
94 RTCDataChannel* channel = RTCDataChannel::create(0, 0, adoptPtr(handler)); | |
95 | |
96 // Add and drain 100 bytes | |
97 handler->changeState(WebRTCDataChannelHandlerClient::ReadyStateOpen); | |
98 String message(std::string(100, 'A').c_str()); | |
99 channel->send(message, IGNORE_EXCEPTION); | |
100 EXPECT_EQ(1U, channel->m_scheduledEvents.size()); | |
101 handler->drainBuffer(100); | |
102 EXPECT_EQ(2U, channel->m_scheduledEvents.size()); | |
103 EXPECT_EQ("bufferedamountlow", std::string(channel->m_scheduledEvents.last() ->type().utf8().data())); | |
104 | |
105 // Add and drain 1 byte | |
106 channel->send("A", IGNORE_EXCEPTION); | |
107 EXPECT_EQ(2U, channel->m_scheduledEvents.size()); | |
108 handler->drainBuffer(1); | |
109 EXPECT_EQ(3U, channel->m_scheduledEvents.size()); | |
110 EXPECT_EQ("bufferedamountlow", std::string(channel->m_scheduledEvents.last() ->type().utf8().data())); | |
111 | |
112 // Set the threshold to 99 bytes, add 101, and drain 1 byte at a time. | |
113 channel->setBufferedAmountLowThreshold(99U); | |
114 channel->send(message, IGNORE_EXCEPTION); // Add 100 bytes. | |
115 channel->send("A", IGNORE_EXCEPTION); // Add 1 more byte. | |
116 handler->drainBuffer(1); // 100 bytes buffered. | |
117 EXPECT_EQ(3U, channel->m_scheduledEvents.size()); // No new events. | |
118 handler->drainBuffer(1); // 99 bytes buffered; event is emitted. | |
119 EXPECT_EQ(4U, channel->m_scheduledEvents.size()); // One new event. | |
120 EXPECT_EQ("bufferedamountlow", std::string(channel->m_scheduledEvents.last() ->type().utf8().data())); | |
121 handler->drainBuffer(1); // 98 bytes buffered. | |
122 channel->setBufferedAmountLowThreshold(97U); | |
123 EXPECT_EQ(4U, channel->m_scheduledEvents.size()); // No new events. | |
124 handler->drainBuffer(1); // 97 bytes buffered; event is emitted. | |
125 EXPECT_EQ(5U, channel->m_scheduledEvents.size()); // New event. | |
126 EXPECT_EQ("bufferedamountlow", std::string(channel->m_scheduledEvents.last() ->type().utf8().data())); | |
127 } | |
128 | |
129 } // namespace blink | |
OLD | NEW |