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

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: steps against compilation error in other platforms 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
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 static 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 static 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 static const char* kInvalidURLs[] = { 28 const char* 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
35 const uint16_t kCloseCodeNormalClosure = 1000U;
36
37 namespace {
38
39 struct WebSocketEvent {
40 enum EventType {
41 EVENT_OPEN,
42 EVENT_MESSAGE,
43 EVENT_ERROR,
44 EVENT_CLOSE
45 };
46
47 WebSocketEvent(
48 EventType type, bool was_clean, uint16_t code, const pp::Var& var)
49 : event_type(type),
50 was_clean(was_clean),
51 code(code),
52 var(var) {}
53 EventType event_type;
54 bool was_clean;
55 uint16_t code;
56 pp::Var var;
57 };
58
59 class TestWebSocketClient : public pp::WebSocket_Dev {
60 public:
61 TestWebSocketClient(pp::Instance* instance)
62 : pp::WebSocket_Dev(instance),
63 connected_(false),
64 received_(false),
65 closed_(false),
66 wait_for_connected_(false),
67 wait_for_received_(false),
68 wait_for_closed_(false),
69 instance_(instance->pp_instance()) {}
70
71 virtual void OnOpen() {
72 events_.push_back(WebSocketEvent(WebSocketEvent::EVENT_OPEN,
73 true, 0U, pp::Var()));
74 connected_ = true;
75 if (wait_for_connected_) {
76 GetTestingInterface()->QuitMessageLoop(instance_);
77 wait_for_connected_ = false;
78 }
79 }
80
81 virtual void OnMessage(const pp::Var &message) {
82 events_.push_back(WebSocketEvent(WebSocketEvent::EVENT_MESSAGE,
83 true, 0U, message));
84 received_ = true;
85 if (wait_for_received_) {
86 GetTestingInterface()->QuitMessageLoop(instance_);
87 wait_for_received_ = false;
88 received_ = false;
89 }
90 }
91
92 virtual void OnError() {
93 events_.push_back(WebSocketEvent(WebSocketEvent::EVENT_ERROR,
94 true, 0U, pp::Var()));
95 }
96
97 virtual void OnClose(
98 bool was_clean, uint16_t code, const pp::Var& reason) {
99 events_.push_back(WebSocketEvent(WebSocketEvent::EVENT_CLOSE,
100 was_clean, code, reason));
101 connected_ = true;
102 closed_ = true;
103 if (wait_for_connected_ || wait_for_closed_) {
104 GetTestingInterface()->QuitMessageLoop(instance_);
105 wait_for_connected_ = false;
106 wait_for_closed_ = false;
107 }
108 }
109
110 void WaitForConnected() {
111 if (!connected_) {
112 wait_for_connected_ = true;
113 GetTestingInterface()->RunMessageLoop(instance_);
114 }
115 }
116
117 void WaitForReceived() {
118 if (!received_) {
119 wait_for_received_ = true;
120 GetTestingInterface()->RunMessageLoop(instance_);
121 }
122 }
123
124 void WaitForClosed() {
125 if (!closed_) {
126 wait_for_closed_ = true;
127 GetTestingInterface()->RunMessageLoop(instance_);
128 }
129 }
130
131 const std::vector<WebSocketEvent>& GetSeenEvents() const {
132 return events_;
133 }
134
135 private:
136 std::vector<WebSocketEvent> events_;
137 bool connected_;
138 bool received_;
139 bool closed_;
140 bool wait_for_connected_;
141 bool wait_for_received_;
142 bool wait_for_closed_;
143 PP_Instance instance_;
144 };
145
146 } // namespace
147
33 REGISTER_TEST_CASE(WebSocket); 148 REGISTER_TEST_CASE(WebSocket);
34 149
35 bool TestWebSocket::Init() { 150 bool TestWebSocket::Init() {
36 websocket_interface_ = static_cast<const PPB_WebSocket_Dev*>( 151 websocket_interface_ = static_cast<const PPB_WebSocket_Dev*>(
37 pp::Module::Get()->GetBrowserInterface(PPB_WEBSOCKET_DEV_INTERFACE)); 152 pp::Module::Get()->GetBrowserInterface(PPB_WEBSOCKET_DEV_INTERFACE));
38 var_interface_ = static_cast<const PPB_Var*>( 153 var_interface_ = static_cast<const PPB_Var*>(
39 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE)); 154 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE));
40 core_interface_ = static_cast<const PPB_Core*>( 155 core_interface_ = static_cast<const PPB_Core*>(
41 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE)); 156 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE));
42 if (!websocket_interface_ || !var_interface_ || !core_interface_) 157 if (!websocket_interface_ || !var_interface_ || !core_interface_)
43 return false; 158 return false;
44 159
45 return true; 160 return true;
46 } 161 }
47 162
48 void TestWebSocket::RunTests(const std::string& filter) { 163 void TestWebSocket::RunTests(const std::string& filter) {
49 RUN_TEST(IsWebSocket, filter); 164 RUN_TEST(IsWebSocket, filter);
50 RUN_TEST(InvalidConnect, filter); 165 RUN_TEST(InvalidConnect, filter);
51 RUN_TEST(GetURL, filter); 166 RUN_TEST(GetURL, filter);
52 RUN_TEST(ValidConnect, filter); 167 RUN_TEST(ValidConnect, filter);
53 RUN_TEST(GetProtocol, filter); 168 RUN_TEST(GetProtocol, filter);
54 RUN_TEST(TextSendReceive, filter); 169 RUN_TEST(TextSendReceive, filter);
170
171 RUN_TEST(CcInvalidConnect, filter);
172 RUN_TEST(CcGetURL, filter);
173 RUN_TEST(CcValidConnect, filter);
174 RUN_TEST(CcGetProtocol, filter);
175 RUN_TEST(CcTextSendReceive, filter);
55 } 176 }
56 177
57 PP_Var TestWebSocket::CreateVar(const char* string) { 178 PP_Var TestWebSocket::CreateVar(const char* string) {
58 return var_interface_->VarFromUtf8( 179 return var_interface_->VarFromUtf8(
59 pp::Module::Get()->pp_module(), string, strlen(string)); 180 pp::Module::Get()->pp_module(), string, strlen(string));
60 } 181 }
61 182
62 void TestWebSocket::ReleaseVar(const PP_Var& var) { 183 void TestWebSocket::ReleaseVar(const PP_Var& var) {
63 var_interface_->Release(var); 184 var_interface_->Release(var);
64 } 185 }
(...skipping 15 matching lines...) Expand all
80 const char* url, int32_t* result, const char* protocol) { 201 const char* url, int32_t* result, const char* protocol) {
81 PP_Var protocols[] = { PP_MakeUndefined() }; 202 PP_Var protocols[] = { PP_MakeUndefined() };
82 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); 203 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance());
83 if (!ws) 204 if (!ws)
84 return 0; 205 return 0;
85 PP_Var url_var = CreateVar(url); 206 PP_Var url_var = CreateVar(url);
86 TestCompletionCallback callback(instance_->pp_instance(), force_async_); 207 TestCompletionCallback callback(instance_->pp_instance(), force_async_);
87 int protocol_count = 0; 208 int protocol_count = 0;
88 if (protocol) { 209 if (protocol) {
89 protocols[0] = CreateVar(protocol); 210 protocols[0] = CreateVar(protocol);
90 protocol_count = 1; 211 protocol_count = 1U;
91 } 212 }
92 *result = websocket_interface_->Connect( 213 *result = websocket_interface_->Connect(
93 ws, url_var, protocols, protocol_count, 214 ws, url_var, protocols, protocol_count,
94 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); 215 static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
95 ReleaseVar(url_var); 216 ReleaseVar(url_var);
96 if (protocol) 217 if (protocol)
97 ReleaseVar(protocols[0]); 218 ReleaseVar(protocols[0]);
98 if (*result == PP_OK_COMPLETIONPENDING) 219 if (*result == PP_OK_COMPLETIONPENDING)
99 *result = callback.WaitForResult(); 220 *result = callback.WaitForResult();
100 return ws; 221 return ws;
(...skipping 20 matching lines...) Expand all
121 // TODO(toyoshim): Add tests to call various interfaces before calling connect. 242 // TODO(toyoshim): Add tests to call various interfaces before calling connect.
122 243
123 std::string TestWebSocket::TestInvalidConnect() { 244 std::string TestWebSocket::TestInvalidConnect() {
124 PP_Var protocols[] = { PP_MakeUndefined() }; 245 PP_Var protocols[] = { PP_MakeUndefined() };
125 246
126 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); 247 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance());
127 ASSERT_TRUE(ws); 248 ASSERT_TRUE(ws);
128 249
129 TestCompletionCallback callback(instance_->pp_instance(), force_async_); 250 TestCompletionCallback callback(instance_->pp_instance(), force_async_);
130 int32_t result = websocket_interface_->Connect( 251 int32_t result = websocket_interface_->Connect(
131 ws, PP_MakeUndefined(), protocols, 1, 252 ws, PP_MakeUndefined(), protocols, 1U,
132 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); 253 static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
133 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); 254 ASSERT_EQ(PP_ERROR_BADARGUMENT, result);
134 255
135 result = websocket_interface_->Connect( 256 result = websocket_interface_->Connect(
136 ws, PP_MakeUndefined(), protocols, 1, 257 ws, PP_MakeUndefined(), protocols, 1U,
137 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); 258 static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
138 ASSERT_EQ(PP_ERROR_INPROGRESS, result); 259 ASSERT_EQ(PP_ERROR_INPROGRESS, result);
139 260
140 core_interface_->ReleaseResource(ws); 261 core_interface_->ReleaseResource(ws);
141 262
142 for (int i = 0; kInvalidURLs[i]; ++i) { 263 for (int i = 0; kInvalidURLs[i]; ++i) {
143 ws = Connect(kInvalidURLs[i], &result, NULL); 264 ws = Connect(kInvalidURLs[i], &result, NULL);
144 ASSERT_TRUE(ws); 265 ASSERT_TRUE(ws);
145 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); 266 ASSERT_EQ(PP_ERROR_BADARGUMENT, result);
146 267
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 PASS(); 353 PASS();
233 } 354 }
234 355
235 // TODO(toyoshim): Add tests for GetBufferedAmount(). 356 // TODO(toyoshim): Add tests for GetBufferedAmount().
236 // For now, the function doesn't work fine because update callback in WebKit is 357 // For now, the function doesn't work fine because update callback in WebKit is
237 // not landed yet. 358 // not landed yet.
238 359
239 // TODO(toyoshim): Add tests for didReceiveMessageError(). 360 // TODO(toyoshim): Add tests for didReceiveMessageError().
240 361
241 // TODO(toyoshim): Add other function tests. 362 // TODO(toyoshim): Add other function tests.
363
364 std::string TestWebSocket::TestCcInvalidConnect() {
365 const pp::Var protocols[] = { pp::Var() };
366
367 TestWebSocketClient websocket(instance_);
368 int32_t result = websocket.Connect(pp::Var(), protocols, 1U);
369 ASSERT_EQ(PP_ERROR_BADARGUMENT, result);
370 ASSERT_EQ(0U, websocket.GetSeenEvents().size());
371
372 result = websocket.Connect(pp::Var(), protocols, 1U);
373 ASSERT_EQ(PP_ERROR_INPROGRESS, result);
374 ASSERT_EQ(0U, websocket.GetSeenEvents().size());
375
376 for (int i = 0; kInvalidURLs[i]; ++i) {
377 TestWebSocketClient ws(instance_);
378 result = ws.Connect(pp::Var(std::string(kInvalidURLs[i])), protocols, 0U);
379 ASSERT_EQ(PP_ERROR_BADARGUMENT, result);
380 ASSERT_EQ(0U, ws.GetSeenEvents().size());
381 }
382
383 PASS();
384 }
385
386 std::string TestWebSocket::TestCcGetURL() {
387 const pp::Var protocols[] = { pp::Var() };
388
389 for (int i = 0; kInvalidURLs[i]; ++i) {
390 TestWebSocketClient websocket(instance_);
391 int32_t result = websocket.Connect(
392 pp::Var(std::string(kInvalidURLs[i])), protocols, 0U);
393 ASSERT_EQ(PP_ERROR_BADARGUMENT, result);
394 pp::Var url = websocket.GetURL();
395 ASSERT_TRUE(AreEqual(url.pp_var(), kInvalidURLs[i]));
396 ASSERT_EQ(0U, websocket.GetSeenEvents().size());
397 }
398
399 PASS();
400 }
401
402 std::string TestWebSocket::TestCcValidConnect() {
403 const pp::Var protocols[] = { pp::Var() };
404 TestWebSocketClient websocket(instance_);
405 int32_t result = websocket.Connect(
406 pp::Var(std::string(kEchoServerURL)), protocols, 0U);
407 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
408 websocket.WaitForConnected();
409 const std::vector<WebSocketEvent>& events = websocket.GetSeenEvents();
410 ASSERT_EQ(1U, events.size());
411 ASSERT_EQ(WebSocketEvent::EVENT_OPEN, events[0].event_type);
412
413 PASS();
414 }
415
416 std::string TestWebSocket::TestCcGetProtocol() {
417 const std::string protocol("x-chat");
418 const pp::Var protocols[] = { pp::Var(protocol) };
419 std::string url(kProtocolTestServerURL);
420 url += protocol;
421 TestWebSocketClient websocket(instance_);
422 int32_t result = websocket.Connect(pp::Var(url), protocols, 1U);
423 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
424 websocket.WaitForConnected();
425 ASSERT_TRUE(AreEqual(websocket.GetProtocol().pp_var(), protocol.c_str()));
426 const std::vector<WebSocketEvent>& events = websocket.GetSeenEvents();
427 ASSERT_EQ(1U, events.size());
428 ASSERT_EQ(WebSocketEvent::EVENT_OPEN, events[0].event_type);
429
430 PASS();
431 }
432
433 std::string TestWebSocket::TestCcTextSendReceive() {
434 const pp::Var protocols[] = { pp::Var() };
435 TestWebSocketClient websocket(instance_);
436 int32_t result =
437 websocket.Connect(pp::Var(std::string(kEchoServerURL)), protocols, 0U);
438 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
439 websocket.WaitForConnected();
440
441 // Send 'hello pepper'.
442 std::string message1("hello pepper");
443 result = websocket.Send(pp::Var(std::string(message1)));
444 ASSERT_EQ(PP_OK, result);
445
446 // Receive echoed 'hello pepper'.
447 websocket.WaitForReceived();
448
449 // Send 'goodbye pepper'.
450 std::string message2("goodbye pepper");
451 result = websocket.Send(pp::Var(std::string(message2)));
452
453 // Receive echoed 'goodbye pepper'.
454 websocket.WaitForReceived();
455
456 const std::vector<WebSocketEvent>& events = websocket.GetSeenEvents();
457 ASSERT_EQ(3U, events.size());
458 ASSERT_EQ(WebSocketEvent::EVENT_OPEN, events[0].event_type);
459 ASSERT_EQ(WebSocketEvent::EVENT_MESSAGE, events[1].event_type);
460 ASSERT_TRUE(AreEqual(events[1].var.pp_var(), message1.c_str()));
461 ASSERT_EQ(WebSocketEvent::EVENT_MESSAGE, events[2].event_type);
462 ASSERT_TRUE(AreEqual(events[2].var.pp_var(), message2.c_str()));
463
464 PASS();
465 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698