Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(90)

Side by Side Diff: ppapi/tests/test_websocket.cc

Issue 8821010: WebSocket Pepper API: C++ bindings implementation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase to master Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« ppapi/cpp/dev/websocket_dev.cc ('K') | « ppapi/tests/test_websocket.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ppapi/tests/test_websocket.h" 5 #include "ppapi/tests/test_websocket.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 #include <vector>
8 9
9 #include "ppapi/c/dev/ppb_websocket_dev.h" 10 #include "ppapi/c/dev/ppb_websocket_dev.h"
10 #include "ppapi/c/pp_errors.h" 11 #include "ppapi/c/pp_errors.h"
11 #include "ppapi/c/pp_var.h" 12 #include "ppapi/c/pp_var.h"
12 #include "ppapi/c/pp_completion_callback.h" 13 #include "ppapi/c/pp_completion_callback.h"
13 #include "ppapi/c/ppb_core.h" 14 #include "ppapi/c/ppb_core.h"
14 #include "ppapi/c/ppb_var.h" 15 #include "ppapi/c/ppb_var.h"
16 #include "ppapi/cpp/dev/websocket_dev.h"
15 #include "ppapi/cpp/instance.h" 17 #include "ppapi/cpp/instance.h"
16 #include "ppapi/cpp/module.h" 18 #include "ppapi/cpp/module.h"
17 #include "ppapi/tests/test_utils.h" 19 #include "ppapi/tests/test_utils.h"
18 #include "ppapi/tests/testing_instance.h" 20 #include "ppapi/tests/testing_instance.h"
19 21
20 const char kEchoServerURL[] = 22 const char kEchoServerURL[] =
21 "ws://localhost:8880/websocket/tests/hybi/echo"; 23 "ws://localhost:8880/websocket/tests/hybi/echo";
22 24
23 const char kProtocolTestServerURL[] = 25 const char kProtocolTestServerURL[] =
24 "ws://localhost:8880/websocket/tests/hybi/protocol-test?protocol="; 26 "ws://localhost:8880/websocket/tests/hybi/protocol-test?protocol=";
25 27
26 const char* const kInvalidURLs[] = { 28 const char* const kInvalidURLs[] = {
27 "http://www.google.com/invalid_scheme", 29 "http://www.google.com/invalid_scheme",
28 "ws://www.google.com/invalid#fragment", 30 "ws://www.google.com/invalid#fragment",
29 "ws://www.google.com:65535/invalid_port", 31 "ws://www.google.com:65535/invalid_port",
30 NULL 32 NULL
31 }; 33 };
32 34
33 // Connection close code is defined in WebSocket protocol specification. 35 // Connection close code is defined in WebSocket protocol specification.
34 // The magic number 1000 means gracefull closure without any error. 36 // The magic number 1000 means gracefull closure without any error.
35 // See section 7.4.1. of RFC 6455. 37 // See section 7.4.1. of RFC 6455.
36 const uint16_t kCloseCodeNormalClosure = 1000; 38 const uint16_t kCloseCodeNormalClosure = 1000U;
39
40 namespace {
41
42 struct WebSocketEvent {
43 enum EventType {
44 EVENT_OPEN,
45 EVENT_MESSAGE,
46 EVENT_ERROR,
47 EVENT_CLOSE
48 };
49
50 WebSocketEvent(
51 EventType type, bool was_clean, uint16_t code, const pp::Var& var)
52 : event_type(type),
53 was_clean(was_clean),
54 code(code),
55 var(var) {}
56 EventType event_type;
57 bool was_clean;
58 uint16_t code;
59 pp::Var var;
60 };
61
62 class TestWebSocketClient : public pp::WebSocket_Dev {
63 public:
64 TestWebSocketClient(pp::Instance* instance)
65 : pp::WebSocket_Dev(instance),
66 connected_(false),
67 received_(false),
68 closed_(false),
69 wait_for_connected_(false),
70 wait_for_received_(false),
71 wait_for_closed_(false),
72 instance_(instance->pp_instance()) {}
73
74 virtual void OnOpen() {
75 events_.push_back(WebSocketEvent(WebSocketEvent::EVENT_OPEN,
76 true, 0U, pp::Var()));
77 connected_ = true;
78 if (wait_for_connected_) {
79 GetTestingInterface()->QuitMessageLoop(instance_);
80 wait_for_connected_ = false;
81 }
82 }
83
84 virtual void OnMessage(const pp::Var &message) {
85 events_.push_back(WebSocketEvent(WebSocketEvent::EVENT_MESSAGE,
86 true, 0U, message));
87 received_ = true;
88 if (wait_for_received_) {
89 GetTestingInterface()->QuitMessageLoop(instance_);
90 wait_for_received_ = false;
91 received_ = false;
92 }
93 }
94
95 virtual void OnError() {
96 events_.push_back(WebSocketEvent(WebSocketEvent::EVENT_ERROR,
97 true, 0U, pp::Var()));
98 }
99
100 virtual void OnClose(
101 bool was_clean, uint16_t code, const pp::Var& reason) {
102 events_.push_back(WebSocketEvent(WebSocketEvent::EVENT_CLOSE,
103 was_clean, code, reason));
104 connected_ = true;
105 closed_ = true;
106 if (wait_for_connected_ || wait_for_closed_) {
107 GetTestingInterface()->QuitMessageLoop(instance_);
108 wait_for_connected_ = false;
109 wait_for_closed_ = false;
110 }
111 }
112
113 void WaitForConnected() {
114 if (!connected_) {
115 wait_for_connected_ = true;
116 GetTestingInterface()->RunMessageLoop(instance_);
117 }
118 }
119
120 void WaitForReceived() {
121 if (!received_) {
122 wait_for_received_ = true;
123 GetTestingInterface()->RunMessageLoop(instance_);
124 }
125 }
126
127 void WaitForClosed() {
128 if (!closed_) {
129 wait_for_closed_ = true;
130 GetTestingInterface()->RunMessageLoop(instance_);
131 }
132 }
133
134 const std::vector<WebSocketEvent>& GetSeenEvents() const {
135 return events_;
136 }
137
138 private:
139 std::vector<WebSocketEvent> events_;
140 bool connected_;
141 bool received_;
142 bool closed_;
143 bool wait_for_connected_;
144 bool wait_for_received_;
145 bool wait_for_closed_;
146 PP_Instance instance_;
147 };
148
149 } // namespace
37 150
38 REGISTER_TEST_CASE(WebSocket); 151 REGISTER_TEST_CASE(WebSocket);
39 152
40 bool TestWebSocket::Init() { 153 bool TestWebSocket::Init() {
41 websocket_interface_ = static_cast<const PPB_WebSocket_Dev*>( 154 websocket_interface_ = static_cast<const PPB_WebSocket_Dev*>(
42 pp::Module::Get()->GetBrowserInterface(PPB_WEBSOCKET_DEV_INTERFACE)); 155 pp::Module::Get()->GetBrowserInterface(PPB_WEBSOCKET_DEV_INTERFACE));
43 var_interface_ = static_cast<const PPB_Var*>( 156 var_interface_ = static_cast<const PPB_Var*>(
44 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE)); 157 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE));
45 core_interface_ = static_cast<const PPB_Core*>( 158 core_interface_ = static_cast<const PPB_Core*>(
46 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE)); 159 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE));
47 if (!websocket_interface_ || !var_interface_ || !core_interface_) 160 if (!websocket_interface_ || !var_interface_ || !core_interface_)
48 return false; 161 return false;
49 162
50 return true; 163 return true;
51 } 164 }
52 165
53 void TestWebSocket::RunTests(const std::string& filter) { 166 void TestWebSocket::RunTests(const std::string& filter) {
54 RUN_TEST(IsWebSocket, filter); 167 RUN_TEST(IsWebSocket, filter);
55 RUN_TEST(UninitializedPropertiesAccess, filter); 168 RUN_TEST(UninitializedPropertiesAccess, filter);
56 RUN_TEST(InvalidConnect, filter); 169 RUN_TEST(InvalidConnect, filter);
57 RUN_TEST(GetURL, filter); 170 RUN_TEST(GetURL, filter);
58 RUN_TEST(ValidConnect, filter); 171 RUN_TEST(ValidConnect, filter);
59 RUN_TEST(InvalidClose, filter); 172 RUN_TEST(InvalidClose, filter);
60 RUN_TEST(ValidClose, filter); 173 RUN_TEST(ValidClose, filter);
61 RUN_TEST(GetProtocol, filter); 174 RUN_TEST(GetProtocol, filter);
62 RUN_TEST(TextSendReceive, filter); 175 RUN_TEST(TextSendReceive, filter);
176
177 RUN_TEST(CcInvalidConnect, filter);
178 RUN_TEST(CcGetURL, filter);
179 RUN_TEST(CcValidConnect, filter);
180 RUN_TEST(CcGetProtocol, filter);
181 RUN_TEST(CcTextSendReceive, filter);
63 } 182 }
64 183
65 PP_Var TestWebSocket::CreateVar(const char* string) { 184 PP_Var TestWebSocket::CreateVar(const char* string) {
66 return var_interface_->VarFromUtf8( 185 return var_interface_->VarFromUtf8(
67 pp::Module::Get()->pp_module(), string, strlen(string)); 186 pp::Module::Get()->pp_module(), string, strlen(string));
68 } 187 }
69 188
70 void TestWebSocket::ReleaseVar(const PP_Var& var) { 189 void TestWebSocket::ReleaseVar(const PP_Var& var) {
71 var_interface_->Release(var); 190 var_interface_->Release(var);
72 } 191 }
(...skipping 15 matching lines...) Expand all
88 const char* url, int32_t* result, const char* protocol) { 207 const char* url, int32_t* result, const char* protocol) {
89 PP_Var protocols[] = { PP_MakeUndefined() }; 208 PP_Var protocols[] = { PP_MakeUndefined() };
90 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); 209 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance());
91 if (!ws) 210 if (!ws)
92 return 0; 211 return 0;
93 PP_Var url_var = CreateVar(url); 212 PP_Var url_var = CreateVar(url);
94 TestCompletionCallback callback(instance_->pp_instance(), force_async_); 213 TestCompletionCallback callback(instance_->pp_instance(), force_async_);
95 int protocol_count = 0; 214 int protocol_count = 0;
96 if (protocol) { 215 if (protocol) {
97 protocols[0] = CreateVar(protocol); 216 protocols[0] = CreateVar(protocol);
98 protocol_count = 1; 217 protocol_count = 1U;
99 } 218 }
100 *result = websocket_interface_->Connect( 219 *result = websocket_interface_->Connect(
101 ws, url_var, protocols, protocol_count, 220 ws, url_var, protocols, protocol_count,
102 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); 221 static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
103 ReleaseVar(url_var); 222 ReleaseVar(url_var);
104 if (protocol) 223 if (protocol)
105 ReleaseVar(protocols[0]); 224 ReleaseVar(protocols[0]);
106 if (*result == PP_OK_COMPLETIONPENDING) 225 if (*result == PP_OK_COMPLETIONPENDING)
107 *result = callback.WaitForResult(); 226 *result = callback.WaitForResult();
108 return ws; 227 return ws;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 } 278 }
160 279
161 std::string TestWebSocket::TestInvalidConnect() { 280 std::string TestWebSocket::TestInvalidConnect() {
162 PP_Var protocols[] = { PP_MakeUndefined() }; 281 PP_Var protocols[] = { PP_MakeUndefined() };
163 282
164 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); 283 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance());
165 ASSERT_TRUE(ws); 284 ASSERT_TRUE(ws);
166 285
167 TestCompletionCallback callback(instance_->pp_instance(), force_async_); 286 TestCompletionCallback callback(instance_->pp_instance(), force_async_);
168 int32_t result = websocket_interface_->Connect( 287 int32_t result = websocket_interface_->Connect(
169 ws, PP_MakeUndefined(), protocols, 1, 288 ws, PP_MakeUndefined(), protocols, 1U,
170 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); 289 static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
171 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); 290 ASSERT_EQ(PP_ERROR_BADARGUMENT, result);
172 291
173 result = websocket_interface_->Connect( 292 result = websocket_interface_->Connect(
174 ws, PP_MakeUndefined(), protocols, 1, 293 ws, PP_MakeUndefined(), protocols, 1U,
175 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); 294 static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
176 ASSERT_EQ(PP_ERROR_INPROGRESS, result); 295 ASSERT_EQ(PP_ERROR_INPROGRESS, result);
177 296
178 core_interface_->ReleaseResource(ws); 297 core_interface_->ReleaseResource(ws);
179 298
180 for (int i = 0; kInvalidURLs[i]; ++i) { 299 for (int i = 0; kInvalidURLs[i]; ++i) {
181 ws = Connect(kInvalidURLs[i], &result, NULL); 300 ws = Connect(kInvalidURLs[i], &result, NULL);
182 ASSERT_TRUE(ws); 301 ASSERT_TRUE(ws);
183 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); 302 ASSERT_EQ(PP_ERROR_BADARGUMENT, result);
184 303
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 PASS(); 492 PASS();
374 } 493 }
375 494
376 // TODO(toyoshim): Add tests for GetBufferedAmount(). 495 // TODO(toyoshim): Add tests for GetBufferedAmount().
377 // For now, the function doesn't work fine because update callback in WebKit is 496 // For now, the function doesn't work fine because update callback in WebKit is
378 // not landed yet. 497 // not landed yet.
379 498
380 // TODO(toyoshim): Add tests for didReceiveMessageError(). 499 // TODO(toyoshim): Add tests for didReceiveMessageError().
381 500
382 // TODO(toyoshim): Add other function tests. 501 // TODO(toyoshim): Add other function tests.
502
503 std::string TestWebSocket::TestCcInvalidConnect() {
504 const pp::Var protocols[] = { pp::Var() };
505
506 TestWebSocketClient websocket(instance_);
507 int32_t result = websocket.Connect(pp::Var(), protocols, 1U);
508 ASSERT_EQ(PP_ERROR_BADARGUMENT, result);
509 ASSERT_EQ(0U, websocket.GetSeenEvents().size());
510
511 result = websocket.Connect(pp::Var(), protocols, 1U);
512 ASSERT_EQ(PP_ERROR_INPROGRESS, result);
513 ASSERT_EQ(0U, websocket.GetSeenEvents().size());
514
515 for (int i = 0; kInvalidURLs[i]; ++i) {
516 TestWebSocketClient ws(instance_);
517 result = ws.Connect(pp::Var(std::string(kInvalidURLs[i])), protocols, 0U);
518 ASSERT_EQ(PP_ERROR_BADARGUMENT, result);
519 ASSERT_EQ(0U, ws.GetSeenEvents().size());
520 }
521
522 PASS();
523 }
524
525 std::string TestWebSocket::TestCcGetURL() {
526 const pp::Var protocols[] = { pp::Var() };
527
528 for (int i = 0; kInvalidURLs[i]; ++i) {
529 TestWebSocketClient websocket(instance_);
530 int32_t result = websocket.Connect(
531 pp::Var(std::string(kInvalidURLs[i])), protocols, 0U);
532 ASSERT_EQ(PP_ERROR_BADARGUMENT, result);
533 pp::Var url = websocket.GetURL();
534 ASSERT_TRUE(AreEqual(url.pp_var(), kInvalidURLs[i]));
535 ASSERT_EQ(0U, websocket.GetSeenEvents().size());
536 }
537
538 PASS();
539 }
540
541 std::string TestWebSocket::TestCcValidConnect() {
542 const pp::Var protocols[] = { pp::Var() };
543 TestWebSocketClient websocket(instance_);
544 int32_t result = websocket.Connect(
545 pp::Var(std::string(kEchoServerURL)), protocols, 0U);
546 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
547 websocket.WaitForConnected();
548 const std::vector<WebSocketEvent>& events = websocket.GetSeenEvents();
549 ASSERT_EQ(1U, events.size());
550 ASSERT_EQ(WebSocketEvent::EVENT_OPEN, events[0].event_type);
551
552 PASS();
553 }
554
555 std::string TestWebSocket::TestCcGetProtocol() {
556 const std::string protocol("x-chat");
557 const pp::Var protocols[] = { pp::Var(protocol) };
558 std::string url(kProtocolTestServerURL);
559 url += protocol;
560 TestWebSocketClient websocket(instance_);
561 int32_t result = websocket.Connect(pp::Var(url), protocols, 1U);
562 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
563 websocket.WaitForConnected();
564 ASSERT_TRUE(AreEqual(websocket.GetProtocol().pp_var(), protocol.c_str()));
565 const std::vector<WebSocketEvent>& events = websocket.GetSeenEvents();
566 ASSERT_EQ(1U, events.size());
567 ASSERT_EQ(WebSocketEvent::EVENT_OPEN, events[0].event_type);
568
569 PASS();
570 }
571
572 std::string TestWebSocket::TestCcTextSendReceive() {
573 const pp::Var protocols[] = { pp::Var() };
574 TestWebSocketClient websocket(instance_);
575 int32_t result =
576 websocket.Connect(pp::Var(std::string(kEchoServerURL)), protocols, 0U);
577 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
578 websocket.WaitForConnected();
579
580 // Send 'hello pepper'.
581 std::string message1("hello pepper");
582 result = websocket.Send(pp::Var(std::string(message1)));
583 ASSERT_EQ(PP_OK, result);
584
585 // Receive echoed 'hello pepper'.
586 websocket.WaitForReceived();
587
588 // Send 'goodbye pepper'.
589 std::string message2("goodbye pepper");
590 result = websocket.Send(pp::Var(std::string(message2)));
591
592 // Receive echoed 'goodbye pepper'.
593 websocket.WaitForReceived();
594
595 const std::vector<WebSocketEvent>& events = websocket.GetSeenEvents();
596 ASSERT_EQ(3U, events.size());
597 ASSERT_EQ(WebSocketEvent::EVENT_OPEN, events[0].event_type);
598 ASSERT_EQ(WebSocketEvent::EVENT_MESSAGE, events[1].event_type);
599 ASSERT_TRUE(AreEqual(events[1].var.pp_var(), message1.c_str()));
600 ASSERT_EQ(WebSocketEvent::EVENT_MESSAGE, events[2].event_type);
601 ASSERT_TRUE(AreEqual(events[2].var.pp_var(), message2.c_str()));
602
603 PASS();
604 }
OLDNEW
« ppapi/cpp/dev/websocket_dev.cc ('K') | « ppapi/tests/test_websocket.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698