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 void setClient(WebRTCDataChannelHandlerClient* client) override { m_client = client; } | |
34 WebString label() override { return ""; } | |
35 bool ordered() const override { return true; } | |
36 unsigned short maxRetransmitTime() const override { return 0; } | |
37 unsigned short maxRetransmits() const override { return 0; } | |
38 WebString protocol() const override { return ""; } | |
39 bool negotiated() const override { return false; } | |
40 unsigned short id() const override { return 0; } | |
41 | |
42 WebRTCDataChannelHandlerClient::ReadyState state() const override { return m _state; } | |
lally
2015/06/10 17:59:41
80col.
| |
43 unsigned long bufferedAmount() override { return m_bufferedAmount; } | |
44 bool sendStringData(const WebString& s) override | |
45 { | |
46 m_bufferedAmount += s.length(); | |
47 return true; | |
48 } | |
49 bool sendRawData(const char* data, size_t length) override | |
50 { | |
51 m_bufferedAmount += length; | |
52 return true; | |
53 } | |
54 void close() override { } | |
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 | |
73 private: | |
74 WebRTCDataChannelHandlerClient* m_client; | |
75 WebRTCDataChannelHandlerClient::ReadyState m_state; | |
76 unsigned long m_bufferedAmount; | |
77 }; | |
78 | |
79 } // namespace | |
80 | |
81 TEST(RTCDataChannelTest, BufferedAmount) | |
82 { | |
83 MockHandler* handler = new MockHandler(); | |
84 RTCDataChannel* channel = RTCDataChannel::create(0, 0, adoptPtr(handler)); | |
85 | |
86 handler->changeState(WebRTCDataChannelHandlerClient::ReadyStateOpen); | |
87 String message(std::string(100, 'A').c_str()); | |
88 channel->send(message, IGNORE_EXCEPTION); | |
89 EXPECT_EQ(100U, channel->bufferedAmount()); | |
90 } | |
91 | |
92 TEST(RTCDataChannelTest, BufferedAmountLow) | |
93 { | |
94 MockHandler* handler = new MockHandler(); | |
95 RTCDataChannel* channel = RTCDataChannel::create(0, 0, adoptPtr(handler)); | |
96 | |
97 // Add and drain 100 bytes | |
98 handler->changeState(WebRTCDataChannelHandlerClient::ReadyStateOpen); | |
99 String message(std::string(100, 'A').c_str()); | |
100 channel->send(message, IGNORE_EXCEPTION); | |
101 EXPECT_EQ(1U, channel->m_scheduledEvents.size()); | |
102 handler->drainBuffer(100); | |
103 EXPECT_EQ(2U, channel->m_scheduledEvents.size()); | |
104 EXPECT_EQ("bufferedamountlow", std::string(channel->m_scheduledEvents.last() ->type().utf8().data())); | |
lally
2015/06/10 17:59:41
Consider checking the buffered amount. Also 80col.
| |
105 | |
106 // Add and drain 1 byte | |
107 channel->send("A", IGNORE_EXCEPTION); | |
108 EXPECT_EQ(2U, channel->m_scheduledEvents.size()); | |
109 handler->drainBuffer(1); | |
110 EXPECT_EQ(3U, channel->m_scheduledEvents.size()); | |
111 EXPECT_EQ("bufferedamountlow", std::string(channel->m_scheduledEvents.last() ->type().utf8().data())); | |
112 | |
113 // Set the threshold to 99 bytes, add 101, and drain 1 byte at a time. | |
114 channel->setBufferedAmountLowThreshold(99U); | |
115 channel->send(message, IGNORE_EXCEPTION); // Add 100 bytes. | |
116 channel->send("A", IGNORE_EXCEPTION); // Add 1 more byte. | |
117 handler->drainBuffer(1); // 100 bytes buffered. | |
118 EXPECT_EQ(3U, channel->m_scheduledEvents.size()); // No new events. | |
119 handler->drainBuffer(1); // 99 bytes buffered; event is emitted. | |
120 EXPECT_EQ(4U, channel->m_scheduledEvents.size()); // One new event. | |
121 EXPECT_EQ("bufferedamountlow", std::string(channel->m_scheduledEvents.last() ->type().utf8().data())); | |
122 handler->drainBuffer(1); // 98 bytes buffered. | |
123 channel->setBufferedAmountLowThreshold(97U); | |
124 EXPECT_EQ(4U, channel->m_scheduledEvents.size()); // No new events. | |
125 handler->drainBuffer(1); // 97 bytes buffered; event is emitted. | |
126 EXPECT_EQ(5U, channel->m_scheduledEvents.size()); // New event. | |
127 EXPECT_EQ("bufferedamountlow", std::string(channel->m_scheduledEvents.last() ->type().utf8().data())); | |
128 } | |
129 | |
130 } // namespace blink | |
OLD | NEW |