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

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

Issue 8558017: WebSocket Pepper API: in process API implementation (reland) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix test too 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
« no previous file with comments | « ppapi/tests/test_websocket.h ('k') | webkit/plugins/ppapi/ppb_websocket_impl.h » ('j') | 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>
8
9 #include "base/logging.h"
7 #include "ppapi/c/dev/ppb_websocket_dev.h" 10 #include "ppapi/c/dev/ppb_websocket_dev.h"
11 #include "ppapi/c/pp_errors.h"
12 #include "ppapi/c/pp_var.h"
13 #include "ppapi/c/pp_completion_callback.h"
14 #include "ppapi/c/ppb_core.h"
15 #include "ppapi/c/ppb_var.h"
8 #include "ppapi/cpp/instance.h" 16 #include "ppapi/cpp/instance.h"
9 #include "ppapi/cpp/module.h" 17 #include "ppapi/cpp/module.h"
18 #include "ppapi/tests/test_utils.h"
10 #include "ppapi/tests/testing_instance.h" 19 #include "ppapi/tests/testing_instance.h"
11 20
21 static const char kEchoServerURL[] =
22 "ws://localhost:8880/websocket/tests/hybi/echo";
23
12 REGISTER_TEST_CASE(WebSocket); 24 REGISTER_TEST_CASE(WebSocket);
13 25
14 bool TestWebSocket::Init() { 26 bool TestWebSocket::Init() {
15 websocket_interface_ = reinterpret_cast<PPB_WebSocket_Dev const*>( 27 websocket_interface_ = static_cast<const PPB_WebSocket_Dev*>(
16 pp::Module::Get()->GetBrowserInterface(PPB_WEBSOCKET_DEV_INTERFACE)); 28 pp::Module::Get()->GetBrowserInterface(PPB_WEBSOCKET_DEV_INTERFACE));
17 return !!websocket_interface_; 29 var_interface_ = static_cast<const PPB_Var*>(
30 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE));
31 core_interface_ = static_cast<const PPB_Core*>(
32 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE));
33 if (!websocket_interface_ || !var_interface_ || !core_interface_)
34 return false;
35
36 return true;
18 } 37 }
19 38
20 void TestWebSocket::RunTests(const std::string& filter) { 39 void TestWebSocket::RunTests(const std::string& filter) {
21 instance_->LogTest("Create", TestCreate()); 40 RUN_TEST(IsWebSocket, filter);
22 instance_->LogTest("IsWebSocket", TestIsWebSocket()); 41 RUN_TEST(InvalidConnect, filter);
42 RUN_TEST(ValidConnect, filter);
43 RUN_TEST(TextSendReceive, filter);
23 } 44 }
24 45
25 std::string TestWebSocket::TestCreate() { 46 PP_Var TestWebSocket::CreateVar(const char* string) {
26 PP_Resource rsrc = websocket_interface_->Create(instance_->pp_instance()); 47 return var_interface_->VarFromUtf8(
27 if (!rsrc) 48 pp::Module::Get()->pp_module(), string, strlen(string));
28 return "Could not create websocket via C interface"; 49 }
29 50
30 PASS(); 51 void TestWebSocket::ReleaseVar(const PP_Var& var) {
52 var_interface_->Release(var);
53 }
54
55 bool TestWebSocket::AreEqual(const PP_Var& var, const char* string) {
56 if (var.type != PP_VARTYPE_STRING)
57 return false;
58 uint32_t utf8_length;
59 const char* utf8 = var_interface_->VarToUtf8(var, &utf8_length);
60 uint32_t string_length = strlen(string);
61 if (utf8_length != string_length)
62 return false;
63 if (strncmp(utf8, string, utf8_length))
64 return false;
65 return true;
66 }
67
68 PP_Resource TestWebSocket::Connect() {
69 PP_Var protocols[] = { PP_MakeUndefined() };
70 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance());
71 if (!ws)
72 return 0;
73 PP_Var url = CreateVar(kEchoServerURL);
74 TestCompletionCallback callback(instance_->pp_instance(), force_async_);
75 int32_t result = websocket_interface_->Connect(
76 ws, url, protocols, 0,
77 static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
78 ReleaseVar(url);
79 if (force_async_ && result != PP_OK_COMPLETIONPENDING) {
80 core_interface_->ReleaseResource(ws);
81 return 0;
82 }
83 if (callback.WaitForResult() != PP_OK) {
84 core_interface_->ReleaseResource(ws);
85 return 0;
86 }
87 return ws;
31 } 88 }
32 89
33 std::string TestWebSocket::TestIsWebSocket() { 90 std::string TestWebSocket::TestIsWebSocket() {
34 // Test that a NULL resource isn't a websocket. 91 // Test that a NULL resource isn't a websocket.
35 pp::Resource null_resource; 92 pp::Resource null_resource;
36 if (websocket_interface_->IsWebSocket(null_resource.pp_resource())) 93 PP_Bool result =
37 return "Null resource was reported as a valid websocket"; 94 websocket_interface_->IsWebSocket(null_resource.pp_resource());
95 ASSERT_FALSE(result);
38 96
39 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); 97 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance());
40 if (!websocket_interface_->IsWebSocket(ws)) 98 ASSERT_TRUE(ws);
41 return "websocket was reported as an invalid websocket"; 99
100 result = websocket_interface_->IsWebSocket(ws);
101 ASSERT_TRUE(result);
102
103 core_interface_->ReleaseResource(ws);
42 104
43 PASS(); 105 PASS();
44 } 106 }
107
108 std::string TestWebSocket::TestInvalidConnect() {
109 PP_Var protocols[] = { PP_MakeUndefined() };
110
111 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance());
112 ASSERT_TRUE(ws);
113
114 TestCompletionCallback callback(instance_->pp_instance(), force_async_);
115 int32_t result = websocket_interface_->Connect(
116 ws, PP_MakeUndefined(), protocols, 1,
117 static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
118 ASSERT_EQ(PP_ERROR_BADARGUMENT, result);
119
120 result = websocket_interface_->Connect(
121 ws, PP_MakeUndefined(), protocols, 1,
122 static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
123 ASSERT_EQ(PP_ERROR_INPROGRESS, result);
124
125 core_interface_->ReleaseResource(ws);
126
127 const char* invalid_urls[] = {
128 "http://www.google.com/invalid_scheme",
129 "ws://www.google.com/invalid#fragment",
130 // TODO(toyoshim): Add URL which has invalid port like
131 // ws://www.google.com:65535/invalid_port
132 NULL
133 };
134 for (int i = 0; invalid_urls[i]; ++i) {
135 ws = websocket_interface_->Create(instance_->pp_instance());
136 ASSERT_TRUE(ws);
137 PP_Var invalid_url = CreateVar(invalid_urls[i]);
138 result = websocket_interface_->Connect(
139 ws, invalid_url, protocols, 0,
140 static_cast<pp::CompletionCallback>(
141 callback).pp_completion_callback());
142 ReleaseVar(invalid_url);
143 core_interface_->ReleaseResource(ws);
144 ASSERT_EQ(PP_ERROR_BADARGUMENT, result);
145 }
146
147 // TODO(toyoshim): Add invalid protocols tests
148
149 PASS();
150 }
151
152
153 std::string TestWebSocket::TestValidConnect() {
154 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance());
155 PP_Var url = CreateVar(kEchoServerURL);
156 PP_Var protocols[] = { PP_MakeUndefined() };
157 TestCompletionCallback callback(instance_->pp_instance(), force_async_);
158 int32_t result = websocket_interface_->Connect(
159 ws, url, protocols, 0,
160 static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
161 ReleaseVar(url);
162 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
163 result = callback.WaitForResult();
164 ASSERT_EQ(PP_OK, result);
165 core_interface_->ReleaseResource(ws);
166
167 PASS();
168 }
169
170 // TODO(toyoshim): Add tests to call various interfaces before calling connect.
171
172 std::string TestWebSocket::TestTextSendReceive() {
173 // Connect to test echo server.
174 PP_Resource ws = Connect();
175 ASSERT_TRUE(ws);
176
177 // Send 'hello pepper' text message.
178 const char* message = "hello pepper";
179 PP_Var message_var = CreateVar(message);
180 int32_t result = websocket_interface_->SendMessage(ws, message_var);
181 ReleaseVar(message_var);
182 ASSERT_EQ(PP_OK, result);
183
184 // Receive echoed 'hello pepper'.
185 TestCompletionCallback callback(instance_->pp_instance(), force_async_);
186 PP_Var received_message;
187 result = websocket_interface_->ReceiveMessage(ws, &received_message,
188 static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
189 ASSERT_FALSE(result != PP_OK && result != PP_OK_COMPLETIONPENDING);
190 if (result == PP_OK_COMPLETIONPENDING)
191 result = callback.WaitForResult();
192 ASSERT_EQ(PP_OK, result);
193 ASSERT_TRUE(AreEqual(received_message, message));
194 ReleaseVar(received_message);
195 core_interface_->ReleaseResource(ws);
196
197 PASS();
198 }
199
200 // TODO(toyoshim): Add other function tests.
OLDNEW
« no previous file with comments | « ppapi/tests/test_websocket.h ('k') | webkit/plugins/ppapi/ppb_websocket_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698