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

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

Issue 8821008: WebSocket Pepper API: Add unit test to call Close() (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: revised 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 8
9 #include "ppapi/c/dev/ppb_websocket_dev.h" 9 #include "ppapi/c/dev/ppb_websocket_dev.h"
10 #include "ppapi/c/pp_errors.h" 10 #include "ppapi/c/pp_errors.h"
11 #include "ppapi/c/pp_var.h" 11 #include "ppapi/c/pp_var.h"
12 #include "ppapi/c/pp_completion_callback.h" 12 #include "ppapi/c/pp_completion_callback.h"
13 #include "ppapi/c/ppb_core.h" 13 #include "ppapi/c/ppb_core.h"
14 #include "ppapi/c/ppb_var.h" 14 #include "ppapi/c/ppb_var.h"
15 #include "ppapi/cpp/instance.h" 15 #include "ppapi/cpp/instance.h"
16 #include "ppapi/cpp/module.h" 16 #include "ppapi/cpp/module.h"
17 #include "ppapi/tests/test_utils.h" 17 #include "ppapi/tests/test_utils.h"
18 #include "ppapi/tests/testing_instance.h" 18 #include "ppapi/tests/testing_instance.h"
19 19
20 static const char kEchoServerURL[] = 20 const char kEchoServerURL[] =
21 "ws://localhost:8880/websocket/tests/hybi/echo"; 21 "ws://localhost:8880/websocket/tests/hybi/echo";
22 22
23 static const char kProtocolTestServerURL[] = 23 const char kProtocolTestServerURL[] =
24 "ws://localhost:8880/websocket/tests/hybi/protocol-test?protocol="; 24 "ws://localhost:8880/websocket/tests/hybi/protocol-test?protocol=";
25 25
26 static const char* kInvalidURLs[] = { 26 const char* kInvalidURLs[] = {
dmichael (off chromium) 2011/12/06 17:07:34 'const char*' -> 'const char* const' (indicating t
Takashi Toyoshima 2011/12/06 17:47:18 Done.
27 "http://www.google.com/invalid_scheme", 27 "http://www.google.com/invalid_scheme",
28 "ws://www.google.com/invalid#fragment", 28 "ws://www.google.com/invalid#fragment",
29 "ws://www.google.com:65535/invalid_port", 29 "ws://www.google.com:65535/invalid_port",
30 NULL 30 NULL
31 }; 31 };
32 32
33 const uint16_t kCloseCodeNormalClosure = 1000;
34
33 REGISTER_TEST_CASE(WebSocket); 35 REGISTER_TEST_CASE(WebSocket);
34 36
35 bool TestWebSocket::Init() { 37 bool TestWebSocket::Init() {
36 websocket_interface_ = static_cast<const PPB_WebSocket_Dev*>( 38 websocket_interface_ = static_cast<const PPB_WebSocket_Dev*>(
37 pp::Module::Get()->GetBrowserInterface(PPB_WEBSOCKET_DEV_INTERFACE)); 39 pp::Module::Get()->GetBrowserInterface(PPB_WEBSOCKET_DEV_INTERFACE));
38 var_interface_ = static_cast<const PPB_Var*>( 40 var_interface_ = static_cast<const PPB_Var*>(
39 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE)); 41 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE));
40 core_interface_ = static_cast<const PPB_Core*>( 42 core_interface_ = static_cast<const PPB_Core*>(
41 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE)); 43 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE));
42 if (!websocket_interface_ || !var_interface_ || !core_interface_) 44 if (!websocket_interface_ || !var_interface_ || !core_interface_)
43 return false; 45 return false;
44 46
45 return true; 47 return true;
46 } 48 }
47 49
48 void TestWebSocket::RunTests(const std::string& filter) { 50 void TestWebSocket::RunTests(const std::string& filter) {
49 RUN_TEST(IsWebSocket, filter); 51 RUN_TEST(IsWebSocket, filter);
50 RUN_TEST(InvalidConnect, filter); 52 RUN_TEST(InvalidConnect, filter);
51 RUN_TEST(GetURL, filter); 53 RUN_TEST(GetURL, filter);
52 RUN_TEST(ValidConnect, filter); 54 RUN_TEST(ValidConnect, filter);
55 RUN_TEST(InvalidClose, filter);
56 RUN_TEST(ValidClose, filter);
53 RUN_TEST(GetProtocol, filter); 57 RUN_TEST(GetProtocol, filter);
54 RUN_TEST(TextSendReceive, filter); 58 RUN_TEST(TextSendReceive, filter);
55 } 59 }
56 60
57 PP_Var TestWebSocket::CreateVar(const char* string) { 61 PP_Var TestWebSocket::CreateVar(const char* string) {
58 return var_interface_->VarFromUtf8( 62 return var_interface_->VarFromUtf8(
59 pp::Module::Get()->pp_module(), string, strlen(string)); 63 pp::Module::Get()->pp_module(), string, strlen(string));
60 } 64 }
61 65
62 void TestWebSocket::ReleaseVar(const PP_Var& var) { 66 void TestWebSocket::ReleaseVar(const PP_Var& var) {
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 std::string TestWebSocket::TestValidConnect() { 175 std::string TestWebSocket::TestValidConnect() {
172 int32_t result; 176 int32_t result;
173 PP_Resource ws = Connect(kEchoServerURL, &result, NULL); 177 PP_Resource ws = Connect(kEchoServerURL, &result, NULL);
174 ASSERT_TRUE(ws); 178 ASSERT_TRUE(ws);
175 ASSERT_EQ(PP_OK, result); 179 ASSERT_EQ(PP_OK, result);
176 core_interface_->ReleaseResource(ws); 180 core_interface_->ReleaseResource(ws);
177 181
178 PASS(); 182 PASS();
179 } 183 }
180 184
185 std::string TestWebSocket::TestInvalidClose() {
186 PP_Var reason = CreateVar("close for test");
187 TestCompletionCallback callback(instance_->pp_instance());
188
189 // Close before connect.
190 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance());
191 int32_t result = websocket_interface_->Close(
192 ws, kCloseCodeNormalClosure, reason,
193 static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
194 ASSERT_EQ(PP_ERROR_FAILED, result);
195 core_interface_->ReleaseResource(ws);
196
197 // Close with bad arguments.
198 ws = Connect(kEchoServerURL, &result, NULL);
199 ASSERT_TRUE(ws);
200 ASSERT_EQ(PP_OK, result);
201 result = websocket_interface_->Close(ws, 1, reason,
202 static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
203 ASSERT_EQ(PP_ERROR_NOACCESS, result);
204 core_interface_->ReleaseResource(ws);
205
206 ReleaseVar(reason);
207
208 PASS();
209 }
210
211 std::string TestWebSocket::TestValidClose() {
212 PP_Var reason = CreateVar("close for test");
213 PP_Var url = CreateVar(kEchoServerURL);
214 PP_Var protocols[] = { PP_MakeUndefined() };
215 TestCompletionCallback callback(instance_->pp_instance());
216 TestCompletionCallback another_callback(instance_->pp_instance());
217
218 // Close.
219 int32_t result;
220 PP_Resource ws = Connect(kEchoServerURL, &result, NULL);
221 ASSERT_TRUE(ws);
222 ASSERT_EQ(PP_OK, result);
223 result = websocket_interface_->Close(ws, kCloseCodeNormalClosure, reason,
224 static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
225 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
226 result = callback.WaitForResult();
dmichael (off chromium) 2011/12/06 17:07:34 Should you check the value of result is PP_OK?
Takashi Toyoshima 2011/12/06 17:47:18 Done.
227 core_interface_->ReleaseResource(ws);
228
229 // Close in connecting.
230 // The ongoing connect failed with PP_ERROR_ABORTED, then the close is done
231 // successfully.
232 ws = websocket_interface_->Create(instance_->pp_instance());
233 result = websocket_interface_->Connect(ws, url, protocols, 0,
234 static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
235 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
236 result = websocket_interface_->Close(ws, kCloseCodeNormalClosure, reason,
237 static_cast<pp::CompletionCallback>(
238 another_callback).pp_completion_callback());
239 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
240 result = callback.WaitForResult();
241 ASSERT_EQ(PP_ERROR_ABORTED, result);
242 result = another_callback.WaitForResult();
243 ASSERT_EQ(PP_OK, result);
244 core_interface_->ReleaseResource(ws);
245
246 // Close in closing.
247 // The first close will be done successfully, then the second one failed with
248 // with PP_ERROR_INPROGRESS immediately.
249 ws = Connect(kEchoServerURL, &result, NULL);
250 ASSERT_TRUE(ws);
251 ASSERT_EQ(PP_OK, result);
252 result = websocket_interface_->Close(ws, kCloseCodeNormalClosure, reason,
253 static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
254 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
255 result = websocket_interface_->Close(ws, kCloseCodeNormalClosure, reason,
256 static_cast<pp::CompletionCallback>(
257 another_callback).pp_completion_callback());
258 ASSERT_EQ(PP_ERROR_INPROGRESS, result);
259 result = callback.WaitForResult();
260 ASSERT_EQ(PP_OK, result);
261 core_interface_->ReleaseResource(ws);
262
263 // Close with ongoing receive message.
264 ws = Connect(kEchoServerURL, &result, NULL);
265 ASSERT_TRUE(ws);
266 ASSERT_EQ(PP_OK, result);
267 PP_Var receive_message_var;
268 result = websocket_interface_->ReceiveMessage(ws, &receive_message_var,
269 static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
270 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
271 result = websocket_interface_->Close(ws, kCloseCodeNormalClosure, reason,
272 static_cast<pp::CompletionCallback>(
273 another_callback).pp_completion_callback());
274 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
275 result = callback.WaitForResult();
276 ASSERT_EQ(PP_ERROR_ABORTED, result);
277 result = another_callback.WaitForResult();
278 ASSERT_EQ(PP_OK, result);
279 core_interface_->ReleaseResource(ws);
280
dmichael (off chromium) 2011/12/06 17:07:34 I think you need to release 'reason' and 'url'
Takashi Toyoshima 2011/12/06 17:47:18 Done.
281 PASS();
282 }
283
181 std::string TestWebSocket::TestGetProtocol() { 284 std::string TestWebSocket::TestGetProtocol() {
182 const char* expected_protocols[] = { 285 const char* expected_protocols[] = {
183 "x-chat", 286 "x-chat",
184 "hoehoe", 287 "hoehoe",
185 NULL 288 NULL
186 }; 289 };
187 for (int i = 0; expected_protocols[i]; ++i) { 290 for (int i = 0; expected_protocols[i]; ++i) {
188 std::string url(kProtocolTestServerURL); 291 std::string url(kProtocolTestServerURL);
189 url += expected_protocols[i]; 292 url += expected_protocols[i];
190 int32_t result; 293 int32_t result;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 PASS(); 335 PASS();
233 } 336 }
234 337
235 // TODO(toyoshim): Add tests for GetBufferedAmount(). 338 // TODO(toyoshim): Add tests for GetBufferedAmount().
236 // For now, the function doesn't work fine because update callback in WebKit is 339 // For now, the function doesn't work fine because update callback in WebKit is
237 // not landed yet. 340 // not landed yet.
238 341
239 // TODO(toyoshim): Add tests for didReceiveMessageError(). 342 // TODO(toyoshim): Add tests for didReceiveMessageError().
240 343
241 // TODO(toyoshim): Add other function tests. 344 // TODO(toyoshim): Add other function tests.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698