| OLD | NEW |
| 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 #include <vector> |
| 9 | 9 |
| 10 #include "ppapi/c/dev/ppb_testing_dev.h" | 10 #include "ppapi/c/dev/ppb_testing_dev.h" |
| 11 #include "ppapi/c/dev/ppb_var_array_buffer_dev.h" |
| 11 #include "ppapi/c/dev/ppb_websocket_dev.h" | 12 #include "ppapi/c/dev/ppb_websocket_dev.h" |
| 12 #include "ppapi/c/pp_errors.h" | 13 #include "ppapi/c/pp_errors.h" |
| 13 #include "ppapi/c/pp_var.h" | 14 #include "ppapi/c/pp_var.h" |
| 14 #include "ppapi/c/pp_completion_callback.h" | 15 #include "ppapi/c/pp_completion_callback.h" |
| 15 #include "ppapi/c/ppb_core.h" | 16 #include "ppapi/c/ppb_core.h" |
| 16 #include "ppapi/c/ppb_var.h" | 17 #include "ppapi/c/ppb_var.h" |
| 17 #include "ppapi/cpp/dev/websocket_dev.h" | 18 #include "ppapi/cpp/dev/websocket_dev.h" |
| 18 #include "ppapi/cpp/instance.h" | 19 #include "ppapi/cpp/instance.h" |
| 19 #include "ppapi/cpp/module.h" | 20 #include "ppapi/cpp/module.h" |
| 20 #include "ppapi/tests/test_utils.h" | 21 #include "ppapi/tests/test_utils.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 41 // See section 7.4.1. of RFC 6455. | 42 // See section 7.4.1. of RFC 6455. |
| 42 const uint16_t kCloseCodeNormalClosure = 1000U; | 43 const uint16_t kCloseCodeNormalClosure = 1000U; |
| 43 | 44 |
| 44 REGISTER_TEST_CASE(WebSocket); | 45 REGISTER_TEST_CASE(WebSocket); |
| 45 | 46 |
| 46 bool TestWebSocket::Init() { | 47 bool TestWebSocket::Init() { |
| 47 websocket_interface_ = static_cast<const PPB_WebSocket_Dev*>( | 48 websocket_interface_ = static_cast<const PPB_WebSocket_Dev*>( |
| 48 pp::Module::Get()->GetBrowserInterface(PPB_WEBSOCKET_DEV_INTERFACE)); | 49 pp::Module::Get()->GetBrowserInterface(PPB_WEBSOCKET_DEV_INTERFACE)); |
| 49 var_interface_ = static_cast<const PPB_Var*>( | 50 var_interface_ = static_cast<const PPB_Var*>( |
| 50 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE)); | 51 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE)); |
| 52 arraybuffer_interface_ = static_cast<const PPB_VarArrayBuffer_Dev*>( |
| 53 pp::Module::Get()->GetBrowserInterface( |
| 54 PPB_VAR_ARRAY_BUFFER_DEV_INTERFACE)); |
| 51 core_interface_ = static_cast<const PPB_Core*>( | 55 core_interface_ = static_cast<const PPB_Core*>( |
| 52 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE)); | 56 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE)); |
| 53 if (!websocket_interface_ || !var_interface_ || !core_interface_) | 57 if (!websocket_interface_ || !var_interface_ || !arraybuffer_interface_ || |
| 58 !core_interface_) |
| 54 return false; | 59 return false; |
| 55 | 60 |
| 56 return InitTestingInterface(); | 61 return InitTestingInterface(); |
| 57 } | 62 } |
| 58 | 63 |
| 59 void TestWebSocket::RunTests(const std::string& filter) { | 64 void TestWebSocket::RunTests(const std::string& filter) { |
| 60 RUN_TEST_WITH_REFERENCE_CHECK(IsWebSocket, filter); | 65 RUN_TEST_WITH_REFERENCE_CHECK(IsWebSocket, filter); |
| 61 RUN_TEST_WITH_REFERENCE_CHECK(UninitializedPropertiesAccess, filter); | 66 RUN_TEST_WITH_REFERENCE_CHECK(UninitializedPropertiesAccess, filter); |
| 62 RUN_TEST_WITH_REFERENCE_CHECK(InvalidConnect, filter); | 67 RUN_TEST_WITH_REFERENCE_CHECK(InvalidConnect, filter); |
| 63 RUN_TEST_WITH_REFERENCE_CHECK(Protocols, filter); | 68 RUN_TEST_WITH_REFERENCE_CHECK(Protocols, filter); |
| 64 RUN_TEST_WITH_REFERENCE_CHECK(GetURL, filter); | 69 RUN_TEST_WITH_REFERENCE_CHECK(GetURL, filter); |
| 65 RUN_TEST_WITH_REFERENCE_CHECK(ValidConnect, filter); | 70 RUN_TEST_WITH_REFERENCE_CHECK(ValidConnect, filter); |
| 66 RUN_TEST_WITH_REFERENCE_CHECK(InvalidClose, filter); | 71 RUN_TEST_WITH_REFERENCE_CHECK(InvalidClose, filter); |
| 67 RUN_TEST_WITH_REFERENCE_CHECK(ValidClose, filter); | 72 RUN_TEST_WITH_REFERENCE_CHECK(ValidClose, filter); |
| 68 RUN_TEST_WITH_REFERENCE_CHECK(GetProtocol, filter); | 73 RUN_TEST_WITH_REFERENCE_CHECK(GetProtocol, filter); |
| 69 RUN_TEST_WITH_REFERENCE_CHECK(TextSendReceive, filter); | 74 RUN_TEST_WITH_REFERENCE_CHECK(TextSendReceive, filter); |
| 75 RUN_TEST_WITH_REFERENCE_CHECK(BinarySendReceive, filter); |
| 70 | 76 |
| 71 RUN_TEST_WITH_REFERENCE_CHECK(CcInterfaces, filter); | 77 RUN_TEST_WITH_REFERENCE_CHECK(CcInterfaces, filter); |
| 72 } | 78 } |
| 73 | 79 |
| 74 PP_Var TestWebSocket::CreateVar(const char* string) { | 80 PP_Var TestWebSocket::CreateVarString(const char* string) { |
| 75 return var_interface_->VarFromUtf8(string, strlen(string)); | 81 return var_interface_->VarFromUtf8(string, strlen(string)); |
| 76 } | 82 } |
| 77 | 83 |
| 84 PP_Var TestWebSocket::CreateVarBinary(const uint8_t* data, uint32_t size) { |
| 85 PP_Var var = arraybuffer_interface_->Create(size); |
| 86 void* var_data = arraybuffer_interface_->Map(var); |
| 87 memcpy(var_data, data, size); |
| 88 return var; |
| 89 } |
| 90 |
| 78 void TestWebSocket::ReleaseVar(const PP_Var& var) { | 91 void TestWebSocket::ReleaseVar(const PP_Var& var) { |
| 79 var_interface_->Release(var); | 92 var_interface_->Release(var); |
| 80 } | 93 } |
| 81 | 94 |
| 82 bool TestWebSocket::AreEqual(const PP_Var& var, const char* string) { | 95 bool TestWebSocket::AreEqualWithString(const PP_Var& var, const char* string) { |
| 83 if (var.type != PP_VARTYPE_STRING) | 96 if (var.type != PP_VARTYPE_STRING) |
| 84 return false; | 97 return false; |
| 85 uint32_t utf8_length; | 98 uint32_t utf8_length; |
| 86 const char* utf8 = var_interface_->VarToUtf8(var, &utf8_length); | 99 const char* utf8 = var_interface_->VarToUtf8(var, &utf8_length); |
| 87 uint32_t string_length = strlen(string); | 100 uint32_t string_length = strlen(string); |
| 88 if (utf8_length != string_length) | 101 if (utf8_length != string_length) |
| 89 return false; | 102 return false; |
| 90 if (strncmp(utf8, string, utf8_length)) | 103 if (strncmp(utf8, string, utf8_length)) |
| 91 return false; | 104 return false; |
| 92 return true; | 105 return true; |
| 93 } | 106 } |
| 94 | 107 |
| 108 bool TestWebSocket::AreEqualWithBinary(const PP_Var& var, |
| 109 const uint8_t* data, |
| 110 uint32_t size) { |
| 111 if (arraybuffer_interface_->ByteLength(var) != size) |
| 112 return false; |
| 113 if (memcmp(arraybuffer_interface_->Map(var), data, size)) |
| 114 return false; |
| 115 return true; |
| 116 } |
| 117 |
| 95 PP_Resource TestWebSocket::Connect( | 118 PP_Resource TestWebSocket::Connect( |
| 96 const char* url, int32_t* result, const char* protocol) { | 119 const char* url, int32_t* result, const char* protocol) { |
| 97 PP_Var protocols[] = { PP_MakeUndefined() }; | 120 PP_Var protocols[] = { PP_MakeUndefined() }; |
| 98 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); | 121 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); |
| 99 if (!ws) | 122 if (!ws) |
| 100 return 0; | 123 return 0; |
| 101 PP_Var url_var = CreateVar(url); | 124 PP_Var url_var = CreateVarString(url); |
| 102 TestCompletionCallback callback(instance_->pp_instance(), force_async_); | 125 TestCompletionCallback callback(instance_->pp_instance(), force_async_); |
| 103 uint32_t protocol_count = 0U; | 126 uint32_t protocol_count = 0U; |
| 104 if (protocol) { | 127 if (protocol) { |
| 105 protocols[0] = CreateVar(protocol); | 128 protocols[0] = CreateVarString(protocol); |
| 106 protocol_count = 1U; | 129 protocol_count = 1U; |
| 107 } | 130 } |
| 108 *result = websocket_interface_->Connect( | 131 *result = websocket_interface_->Connect( |
| 109 ws, url_var, protocols, protocol_count, | 132 ws, url_var, protocols, protocol_count, |
| 110 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | 133 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); |
| 111 ReleaseVar(url_var); | 134 ReleaseVar(url_var); |
| 112 if (protocol) | 135 if (protocol) |
| 113 ReleaseVar(protocols[0]); | 136 ReleaseVar(protocols[0]); |
| 114 if (*result == PP_OK_COMPLETIONPENDING) | 137 if (*result == PP_OK_COMPLETIONPENDING) |
| 115 *result = callback.WaitForResult(); | 138 *result = callback.WaitForResult(); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 138 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); | 161 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); |
| 139 ASSERT_TRUE(ws); | 162 ASSERT_TRUE(ws); |
| 140 | 163 |
| 141 uint64_t bufferedAmount = websocket_interface_->GetBufferedAmount(ws); | 164 uint64_t bufferedAmount = websocket_interface_->GetBufferedAmount(ws); |
| 142 ASSERT_EQ(0U, bufferedAmount); | 165 ASSERT_EQ(0U, bufferedAmount); |
| 143 | 166 |
| 144 uint16_t close_code = websocket_interface_->GetCloseCode(ws); | 167 uint16_t close_code = websocket_interface_->GetCloseCode(ws); |
| 145 ASSERT_EQ(0U, close_code); | 168 ASSERT_EQ(0U, close_code); |
| 146 | 169 |
| 147 PP_Var close_reason = websocket_interface_->GetCloseReason(ws); | 170 PP_Var close_reason = websocket_interface_->GetCloseReason(ws); |
| 148 ASSERT_TRUE(AreEqual(close_reason, "")); | 171 ASSERT_TRUE(AreEqualWithString(close_reason, "")); |
| 149 ReleaseVar(close_reason); | 172 ReleaseVar(close_reason); |
| 150 | 173 |
| 151 PP_Bool close_was_clean = websocket_interface_->GetCloseWasClean(ws); | 174 PP_Bool close_was_clean = websocket_interface_->GetCloseWasClean(ws); |
| 152 ASSERT_EQ(PP_FALSE, close_was_clean); | 175 ASSERT_EQ(PP_FALSE, close_was_clean); |
| 153 | 176 |
| 154 PP_Var extensions = websocket_interface_->GetExtensions(ws); | 177 PP_Var extensions = websocket_interface_->GetExtensions(ws); |
| 155 ASSERT_TRUE(AreEqual(extensions, "")); | 178 ASSERT_TRUE(AreEqualWithString(extensions, "")); |
| 156 ReleaseVar(extensions); | 179 ReleaseVar(extensions); |
| 157 | 180 |
| 158 PP_Var protocol = websocket_interface_->GetProtocol(ws); | 181 PP_Var protocol = websocket_interface_->GetProtocol(ws); |
| 159 ASSERT_TRUE(AreEqual(protocol, "")); | 182 ASSERT_TRUE(AreEqualWithString(protocol, "")); |
| 160 ReleaseVar(protocol); | 183 ReleaseVar(protocol); |
| 161 | 184 |
| 162 PP_WebSocketReadyState_Dev ready_state = | 185 PP_WebSocketReadyState_Dev ready_state = |
| 163 websocket_interface_->GetReadyState(ws); | 186 websocket_interface_->GetReadyState(ws); |
| 164 ASSERT_EQ(PP_WEBSOCKETREADYSTATE_INVALID_DEV, ready_state); | 187 ASSERT_EQ(PP_WEBSOCKETREADYSTATE_INVALID_DEV, ready_state); |
| 165 | 188 |
| 166 PP_Var url = websocket_interface_->GetURL(ws); | 189 PP_Var url = websocket_interface_->GetURL(ws); |
| 167 ASSERT_TRUE(AreEqual(url, "")); | 190 ASSERT_TRUE(AreEqualWithString(url, "")); |
| 168 ReleaseVar(url); | 191 ReleaseVar(url); |
| 169 | 192 |
| 170 core_interface_->ReleaseResource(ws); | 193 core_interface_->ReleaseResource(ws); |
| 171 | 194 |
| 172 PASS(); | 195 PASS(); |
| 173 } | 196 } |
| 174 | 197 |
| 175 std::string TestWebSocket::TestInvalidConnect() { | 198 std::string TestWebSocket::TestInvalidConnect() { |
| 176 PP_Var protocols[] = { PP_MakeUndefined() }; | 199 PP_Var protocols[] = { PP_MakeUndefined() }; |
| 177 | 200 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 196 ASSERT_TRUE(ws); | 219 ASSERT_TRUE(ws); |
| 197 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); | 220 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); |
| 198 | 221 |
| 199 core_interface_->ReleaseResource(ws); | 222 core_interface_->ReleaseResource(ws); |
| 200 } | 223 } |
| 201 | 224 |
| 202 PASS(); | 225 PASS(); |
| 203 } | 226 } |
| 204 | 227 |
| 205 std::string TestWebSocket::TestProtocols() { | 228 std::string TestWebSocket::TestProtocols() { |
| 206 PP_Var url = CreateVar(kEchoServerURL); | 229 PP_Var url = CreateVarString(kEchoServerURL); |
| 207 PP_Var bad_protocols[] = { CreateVar("x-test"), CreateVar("x-test") }; | 230 PP_Var bad_protocols[] = { |
| 208 PP_Var good_protocols[] = { CreateVar("x-test"), CreateVar("x-yatest") }; | 231 CreateVarString("x-test"), |
| 232 CreateVarString("x-test") |
| 233 }; |
| 234 PP_Var good_protocols[] = { |
| 235 CreateVarString("x-test"), |
| 236 CreateVarString("x-yatest") |
| 237 }; |
| 209 | 238 |
| 210 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); | 239 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); |
| 211 ASSERT_TRUE(ws); | 240 ASSERT_TRUE(ws); |
| 212 int32_t result = websocket_interface_->Connect( | 241 int32_t result = websocket_interface_->Connect( |
| 213 ws, url, bad_protocols, 2U, PP_BlockUntilComplete()); | 242 ws, url, bad_protocols, 2U, PP_BlockUntilComplete()); |
| 214 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); | 243 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); |
| 215 core_interface_->ReleaseResource(ws); | 244 core_interface_->ReleaseResource(ws); |
| 216 | 245 |
| 217 ws = websocket_interface_->Create(instance_->pp_instance()); | 246 ws = websocket_interface_->Create(instance_->pp_instance()); |
| 218 ASSERT_TRUE(ws); | 247 ASSERT_TRUE(ws); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 230 | 259 |
| 231 PASS(); | 260 PASS(); |
| 232 } | 261 } |
| 233 | 262 |
| 234 std::string TestWebSocket::TestGetURL() { | 263 std::string TestWebSocket::TestGetURL() { |
| 235 for (int i = 0; kInvalidURLs[i]; ++i) { | 264 for (int i = 0; kInvalidURLs[i]; ++i) { |
| 236 int32_t result; | 265 int32_t result; |
| 237 PP_Resource ws = Connect(kInvalidURLs[i], &result, NULL); | 266 PP_Resource ws = Connect(kInvalidURLs[i], &result, NULL); |
| 238 ASSERT_TRUE(ws); | 267 ASSERT_TRUE(ws); |
| 239 PP_Var url = websocket_interface_->GetURL(ws); | 268 PP_Var url = websocket_interface_->GetURL(ws); |
| 240 ASSERT_TRUE(AreEqual(url, kInvalidURLs[i])); | 269 ASSERT_TRUE(AreEqualWithString(url, kInvalidURLs[i])); |
| 241 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); | 270 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); |
| 242 | 271 |
| 243 ReleaseVar(url); | 272 ReleaseVar(url); |
| 244 core_interface_->ReleaseResource(ws); | 273 core_interface_->ReleaseResource(ws); |
| 245 } | 274 } |
| 246 | 275 |
| 247 PASS(); | 276 PASS(); |
| 248 } | 277 } |
| 249 | 278 |
| 250 std::string TestWebSocket::TestValidConnect() { | 279 std::string TestWebSocket::TestValidConnect() { |
| 251 int32_t result; | 280 int32_t result; |
| 252 PP_Resource ws = Connect(kEchoServerURL, &result, NULL); | 281 PP_Resource ws = Connect(kEchoServerURL, &result, NULL); |
| 253 ASSERT_TRUE(ws); | 282 ASSERT_TRUE(ws); |
| 254 ASSERT_EQ(PP_OK, result); | 283 ASSERT_EQ(PP_OK, result); |
| 255 core_interface_->ReleaseResource(ws); | 284 core_interface_->ReleaseResource(ws); |
| 256 | 285 |
| 257 PASS(); | 286 PASS(); |
| 258 } | 287 } |
| 259 | 288 |
| 260 std::string TestWebSocket::TestInvalidClose() { | 289 std::string TestWebSocket::TestInvalidClose() { |
| 261 PP_Var reason = CreateVar("close for test"); | 290 PP_Var reason = CreateVarString("close for test"); |
| 262 TestCompletionCallback callback(instance_->pp_instance()); | 291 TestCompletionCallback callback(instance_->pp_instance()); |
| 263 | 292 |
| 264 // Close before connect. | 293 // Close before connect. |
| 265 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); | 294 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); |
| 266 int32_t result = websocket_interface_->Close( | 295 int32_t result = websocket_interface_->Close( |
| 267 ws, kCloseCodeNormalClosure, reason, | 296 ws, kCloseCodeNormalClosure, reason, |
| 268 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | 297 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); |
| 269 ASSERT_EQ(PP_ERROR_FAILED, result); | 298 ASSERT_EQ(PP_ERROR_FAILED, result); |
| 270 core_interface_->ReleaseResource(ws); | 299 core_interface_->ReleaseResource(ws); |
| 271 | 300 |
| 272 // Close with bad arguments. | 301 // Close with bad arguments. |
| 273 ws = Connect(kEchoServerURL, &result, NULL); | 302 ws = Connect(kEchoServerURL, &result, NULL); |
| 274 ASSERT_TRUE(ws); | 303 ASSERT_TRUE(ws); |
| 275 ASSERT_EQ(PP_OK, result); | 304 ASSERT_EQ(PP_OK, result); |
| 276 result = websocket_interface_->Close(ws, 1U, reason, | 305 result = websocket_interface_->Close(ws, 1U, reason, |
| 277 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | 306 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); |
| 278 ASSERT_EQ(PP_ERROR_NOACCESS, result); | 307 ASSERT_EQ(PP_ERROR_NOACCESS, result); |
| 279 core_interface_->ReleaseResource(ws); | 308 core_interface_->ReleaseResource(ws); |
| 280 | 309 |
| 281 ReleaseVar(reason); | 310 ReleaseVar(reason); |
| 282 | 311 |
| 283 PASS(); | 312 PASS(); |
| 284 } | 313 } |
| 285 | 314 |
| 286 std::string TestWebSocket::TestValidClose() { | 315 std::string TestWebSocket::TestValidClose() { |
| 287 PP_Var reason = CreateVar("close for test"); | 316 PP_Var reason = CreateVarString("close for test"); |
| 288 PP_Var url = CreateVar(kEchoServerURL); | 317 PP_Var url = CreateVarString(kEchoServerURL); |
| 289 PP_Var protocols[] = { PP_MakeUndefined() }; | 318 PP_Var protocols[] = { PP_MakeUndefined() }; |
| 290 TestCompletionCallback callback(instance_->pp_instance()); | 319 TestCompletionCallback callback(instance_->pp_instance()); |
| 291 TestCompletionCallback another_callback(instance_->pp_instance()); | 320 TestCompletionCallback another_callback(instance_->pp_instance()); |
| 292 | 321 |
| 293 // Close. | 322 // Close. |
| 294 int32_t result; | 323 int32_t result; |
| 295 PP_Resource ws = Connect(kEchoServerURL, &result, NULL); | 324 PP_Resource ws = Connect(kEchoServerURL, &result, NULL); |
| 296 ASSERT_TRUE(ws); | 325 ASSERT_TRUE(ws); |
| 297 ASSERT_EQ(PP_OK, result); | 326 ASSERT_EQ(PP_OK, result); |
| 298 result = websocket_interface_->Close(ws, kCloseCodeNormalClosure, reason, | 327 result = websocket_interface_->Close(ws, kCloseCodeNormalClosure, reason, |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 }; | 397 }; |
| 369 for (int i = 0; expected_protocols[i]; ++i) { | 398 for (int i = 0; expected_protocols[i]; ++i) { |
| 370 std::string url(kProtocolTestServerURL); | 399 std::string url(kProtocolTestServerURL); |
| 371 url += expected_protocols[i]; | 400 url += expected_protocols[i]; |
| 372 int32_t result; | 401 int32_t result; |
| 373 PP_Resource ws = Connect(url.c_str(), &result, expected_protocols[i]); | 402 PP_Resource ws = Connect(url.c_str(), &result, expected_protocols[i]); |
| 374 ASSERT_TRUE(ws); | 403 ASSERT_TRUE(ws); |
| 375 ASSERT_EQ(PP_OK, result); | 404 ASSERT_EQ(PP_OK, result); |
| 376 | 405 |
| 377 PP_Var protocol = websocket_interface_->GetProtocol(ws); | 406 PP_Var protocol = websocket_interface_->GetProtocol(ws); |
| 378 ASSERT_TRUE(AreEqual(protocol, expected_protocols[i])); | 407 ASSERT_TRUE(AreEqualWithString(protocol, expected_protocols[i])); |
| 379 | 408 |
| 380 ReleaseVar(protocol); | 409 ReleaseVar(protocol); |
| 381 core_interface_->ReleaseResource(ws); | 410 core_interface_->ReleaseResource(ws); |
| 382 } | 411 } |
| 383 | 412 |
| 384 PASS(); | 413 PASS(); |
| 385 } | 414 } |
| 386 | 415 |
| 387 std::string TestWebSocket::TestTextSendReceive() { | 416 std::string TestWebSocket::TestTextSendReceive() { |
| 388 // Connect to test echo server. | 417 // Connect to test echo server. |
| 389 int32_t connect_result; | 418 int32_t connect_result; |
| 390 PP_Resource ws = Connect(kEchoServerURL, &connect_result, NULL); | 419 PP_Resource ws = Connect(kEchoServerURL, &connect_result, NULL); |
| 391 ASSERT_TRUE(ws); | 420 ASSERT_TRUE(ws); |
| 392 ASSERT_EQ(PP_OK, connect_result); | 421 ASSERT_EQ(PP_OK, connect_result); |
| 393 | 422 |
| 394 // Send 'hello pepper' text message. | 423 // Send 'hello pepper' text message. |
| 395 const char* message = "hello pepper"; | 424 const char* message = "hello pepper"; |
| 396 PP_Var message_var = CreateVar(message); | 425 PP_Var message_var = CreateVarString(message); |
| 397 int32_t result = websocket_interface_->SendMessage(ws, message_var); | 426 int32_t result = websocket_interface_->SendMessage(ws, message_var); |
| 398 ReleaseVar(message_var); | 427 ReleaseVar(message_var); |
| 399 ASSERT_EQ(PP_OK, result); | 428 ASSERT_EQ(PP_OK, result); |
| 400 | 429 |
| 401 // Receive echoed 'hello pepper'. | 430 // Receive echoed 'hello pepper'. |
| 402 TestCompletionCallback callback(instance_->pp_instance(), force_async_); | 431 TestCompletionCallback callback(instance_->pp_instance(), force_async_); |
| 403 PP_Var received_message; | 432 PP_Var received_message; |
| 404 result = websocket_interface_->ReceiveMessage(ws, &received_message, | 433 result = websocket_interface_->ReceiveMessage(ws, &received_message, |
| 405 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | 434 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); |
| 406 ASSERT_FALSE(result != PP_OK && result != PP_OK_COMPLETIONPENDING); | 435 ASSERT_FALSE(result != PP_OK && result != PP_OK_COMPLETIONPENDING); |
| 407 if (result == PP_OK_COMPLETIONPENDING) | 436 if (result == PP_OK_COMPLETIONPENDING) |
| 408 result = callback.WaitForResult(); | 437 result = callback.WaitForResult(); |
| 409 ASSERT_EQ(PP_OK, result); | 438 ASSERT_EQ(PP_OK, result); |
| 410 ASSERT_TRUE(AreEqual(received_message, message)); | 439 ASSERT_TRUE(AreEqualWithString(received_message, message)); |
| 411 ReleaseVar(received_message); | 440 ReleaseVar(received_message); |
| 412 core_interface_->ReleaseResource(ws); | 441 core_interface_->ReleaseResource(ws); |
| 413 | 442 |
| 443 PASS(); |
| 444 } |
| 445 |
| 446 std::string TestWebSocket::TestBinarySendReceive() { |
| 447 // Connect to test echo server. |
| 448 int32_t connect_result; |
| 449 PP_Resource ws = Connect(kEchoServerURL, &connect_result, NULL); |
| 450 ASSERT_TRUE(ws); |
| 451 ASSERT_EQ(PP_OK, connect_result); |
| 452 |
| 453 // Send binary message. |
| 454 uint32_t len = 256; |
| 455 uint8_t data[256]; |
| 456 for (uint32_t i = 0; i < len; ++i) |
| 457 data[i] = i; |
| 458 PP_Var message_var = CreateVarBinary(data, len); |
| 459 int32_t result = websocket_interface_->SendMessage(ws, message_var); |
| 460 ReleaseVar(message_var); |
| 461 ASSERT_EQ(PP_OK, result); |
| 462 |
| 463 // Receive echoed binary. |
| 464 TestCompletionCallback callback(instance_->pp_instance(), force_async_); |
| 465 PP_Var received_message; |
| 466 result = websocket_interface_->ReceiveMessage(ws, &received_message, |
| 467 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); |
| 468 ASSERT_TRUE(result == PP_OK || result == PP_OK_COMPLETIONPENDING); |
| 469 if (result == PP_OK_COMPLETIONPENDING) |
| 470 result = callback.WaitForResult(); |
| 471 ASSERT_EQ(PP_OK, result); |
| 472 ASSERT_TRUE(AreEqualWithBinary(received_message, data, len)); |
| 473 ReleaseVar(received_message); |
| 474 core_interface_->ReleaseResource(ws); |
| 475 |
| 414 PASS(); | 476 PASS(); |
| 415 } | 477 } |
| 416 | 478 |
| 417 // TODO(toyoshim): Add tests for GetBufferedAmount(). | 479 // TODO(toyoshim): Add tests for GetBufferedAmount(). |
| 418 // For now, the function doesn't work fine because update callback in WebKit is | 480 // For now, the function doesn't work fine because update callback in WebKit is |
| 419 // not landed yet. | 481 // not landed yet. |
| 420 | 482 |
| 421 // TODO(toyoshim): Add tests for didReceiveMessageError(). | 483 // TODO(toyoshim): Add tests for didReceiveMessageError(). |
| 422 | 484 |
| 423 // TODO(toyoshim): Add other function tests. | 485 // TODO(toyoshim): Add other function tests. |
| 424 | 486 |
| 425 std::string TestWebSocket::TestCcInterfaces() { | 487 std::string TestWebSocket::TestCcInterfaces() { |
| 426 // C++ bindings is simple straightforward, then just verifies interfaces work | 488 // C++ bindings is simple straightforward, then just verifies interfaces work |
| 427 // as a interface bridge fine. | 489 // as a interface bridge fine. |
| 428 pp::WebSocket_Dev ws(instance_); | 490 pp::WebSocket_Dev ws(instance_); |
| 429 | 491 |
| 430 // Check uninitialized properties access. | 492 // Check uninitialized properties access. |
| 431 ASSERT_EQ(0, ws.GetBufferedAmount()); | 493 ASSERT_EQ(0, ws.GetBufferedAmount()); |
| 432 ASSERT_EQ(0, ws.GetCloseCode()); | 494 ASSERT_EQ(0, ws.GetCloseCode()); |
| 433 ASSERT_TRUE(AreEqual(ws.GetCloseReason().pp_var(), "")); | 495 ASSERT_TRUE(AreEqualWithString(ws.GetCloseReason().pp_var(), "")); |
| 434 ASSERT_EQ(false, ws.GetCloseWasClean()); | 496 ASSERT_EQ(false, ws.GetCloseWasClean()); |
| 435 ASSERT_TRUE(AreEqual(ws.GetExtensions().pp_var(), "")); | 497 ASSERT_TRUE(AreEqualWithString(ws.GetExtensions().pp_var(), "")); |
| 436 ASSERT_TRUE(AreEqual(ws.GetProtocol().pp_var(), "")); | 498 ASSERT_TRUE(AreEqualWithString(ws.GetProtocol().pp_var(), "")); |
| 437 ASSERT_EQ(PP_WEBSOCKETREADYSTATE_INVALID_DEV, ws.GetReadyState()); | 499 ASSERT_EQ(PP_WEBSOCKETREADYSTATE_INVALID_DEV, ws.GetReadyState()); |
| 438 ASSERT_TRUE(AreEqual(ws.GetURL().pp_var(), "")); | 500 ASSERT_TRUE(AreEqualWithString(ws.GetURL().pp_var(), "")); |
| 439 | 501 |
| 440 // Check communication interfaces (connect, send, receive, and close). | 502 // Check communication interfaces (connect, send, receive, and close). |
| 441 TestCompletionCallback connect_callback(instance_->pp_instance()); | 503 TestCompletionCallback connect_callback(instance_->pp_instance()); |
| 442 int32_t result = ws.Connect(pp::Var(std::string(kCloseServerURL)), NULL, 0U, | 504 int32_t result = ws.Connect(pp::Var(std::string(kCloseServerURL)), NULL, 0U, |
| 443 connect_callback); | 505 connect_callback); |
| 444 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); | 506 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); |
| 445 result = connect_callback.WaitForResult(); | 507 result = connect_callback.WaitForResult(); |
| 446 ASSERT_EQ(PP_OK, result); | 508 ASSERT_EQ(PP_OK, result); |
| 447 | 509 |
| 448 std::string message("hello C++"); | 510 std::string text_message("hello C++"); |
| 449 result = ws.SendMessage(pp::Var(message)); | 511 result = ws.SendMessage(pp::Var(text_message)); |
| 450 ASSERT_EQ(PP_OK, result); | 512 ASSERT_EQ(PP_OK, result); |
| 451 | 513 |
| 452 pp::Var receive_var; | 514 uint32_t binary_length = 256; |
| 453 TestCompletionCallback receive_callback(instance_->pp_instance()); | 515 uint8_t binary_message[256]; |
| 454 result = ws.ReceiveMessage(&receive_var, receive_callback); | 516 for (uint32_t i = 0; i < binary_length; ++i) |
| 517 binary_message[i] = i; |
| 518 result = ws.SendMessage(pp::Var( |
| 519 pp::Var::PassRef(), CreateVarBinary(binary_message, binary_length))); |
| 520 ASSERT_EQ(PP_OK, result); |
| 521 |
| 522 pp::Var text_receive_var; |
| 523 TestCompletionCallback text_receive_callback(instance_->pp_instance()); |
| 524 result = ws.ReceiveMessage(&text_receive_var, text_receive_callback); |
| 455 if (result == PP_OK_COMPLETIONPENDING) | 525 if (result == PP_OK_COMPLETIONPENDING) |
| 456 result = receive_callback.WaitForResult(); | 526 result = text_receive_callback.WaitForResult(); |
| 457 ASSERT_EQ(PP_OK, result); | 527 ASSERT_EQ(PP_OK, result); |
| 458 ASSERT_TRUE(AreEqual(receive_var.pp_var(), message.c_str())); | 528 ASSERT_TRUE( |
| 529 AreEqualWithString(text_receive_var.pp_var(), text_message.c_str())); |
| 530 |
| 531 pp::Var binary_receive_var; |
| 532 TestCompletionCallback binary_receive_callback(instance_->pp_instance()); |
| 533 result = ws.ReceiveMessage(&binary_receive_var, binary_receive_callback); |
| 534 if (result == PP_OK_COMPLETIONPENDING) |
| 535 result = binary_receive_callback.WaitForResult(); |
| 536 ASSERT_EQ(PP_OK, result); |
| 537 ASSERT_TRUE(AreEqualWithBinary( |
| 538 binary_receive_var.pp_var(), binary_message, binary_length)); |
| 459 | 539 |
| 460 TestCompletionCallback close_callback(instance_->pp_instance()); | 540 TestCompletionCallback close_callback(instance_->pp_instance()); |
| 461 std::string reason("bye"); | 541 std::string reason("bye"); |
| 462 result = ws.Close(kCloseCodeNormalClosure, pp::Var(reason), close_callback); | 542 result = ws.Close(kCloseCodeNormalClosure, pp::Var(reason), close_callback); |
| 463 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); | 543 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); |
| 464 result = close_callback.WaitForResult(); | 544 result = close_callback.WaitForResult(); |
| 465 ASSERT_EQ(PP_OK, result); | 545 ASSERT_EQ(PP_OK, result); |
| 466 | 546 |
| 467 // Check initialized properties access. | 547 // Check initialized properties access. |
| 468 ASSERT_EQ(0, ws.GetBufferedAmount()); | 548 ASSERT_EQ(0, ws.GetBufferedAmount()); |
| 469 ASSERT_EQ(kCloseCodeNormalClosure, ws.GetCloseCode()); | 549 ASSERT_EQ(kCloseCodeNormalClosure, ws.GetCloseCode()); |
| 470 ASSERT_TRUE(AreEqual(ws.GetCloseReason().pp_var(), reason.c_str())); | 550 ASSERT_TRUE(AreEqualWithString(ws.GetCloseReason().pp_var(), reason.c_str())); |
| 471 ASSERT_EQ(true, ws.GetCloseWasClean()); | 551 ASSERT_EQ(true, ws.GetCloseWasClean()); |
| 472 ASSERT_TRUE(AreEqual(ws.GetExtensions().pp_var(), "")); | 552 ASSERT_TRUE(AreEqualWithString(ws.GetExtensions().pp_var(), "")); |
| 473 ASSERT_TRUE(AreEqual(ws.GetProtocol().pp_var(), "")); | 553 ASSERT_TRUE(AreEqualWithString(ws.GetProtocol().pp_var(), "")); |
| 474 ASSERT_EQ(PP_WEBSOCKETREADYSTATE_CLOSED_DEV, ws.GetReadyState()); | 554 ASSERT_EQ(PP_WEBSOCKETREADYSTATE_CLOSED_DEV, ws.GetReadyState()); |
| 475 ASSERT_TRUE(AreEqual(ws.GetURL().pp_var(), kCloseServerURL)); | 555 ASSERT_TRUE(AreEqualWithString(ws.GetURL().pp_var(), kCloseServerURL)); |
| 476 | 556 |
| 477 PASS(); | 557 PASS(); |
| 478 } | 558 } |
| OLD | NEW |