Chromium Code Reviews| 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> | |
| 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 |
| 12 REGISTER_TEST_CASE(WebSocket); | 21 REGISTER_TEST_CASE(WebSocket); |
| 13 | 22 |
| 14 bool TestWebSocket::Init() { | 23 bool TestWebSocket::Init() { |
| 15 websocket_interface_ = reinterpret_cast<PPB_WebSocket_Dev const*>( | 24 websocket_interface_ = static_cast<const PPB_WebSocket_Dev*>( |
| 16 pp::Module::Get()->GetBrowserInterface(PPB_WEBSOCKET_DEV_INTERFACE)); | 25 pp::Module::Get()->GetBrowserInterface(PPB_WEBSOCKET_DEV_INTERFACE)); |
| 17 return !!websocket_interface_; | 26 var_interface_ = static_cast<const PPB_Var*>( |
| 27 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE)); | |
| 28 core_interface_ = static_cast<const PPB_Core*>( | |
| 29 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE)); | |
| 30 if (!websocket_interface_ || !var_interface_ || !core_interface_) | |
| 31 return false; | |
| 32 | |
| 33 return true; | |
| 18 } | 34 } |
| 19 | 35 |
| 20 void TestWebSocket::RunTests(const std::string& filter) { | 36 void TestWebSocket::RunTests(const std::string& filter) { |
| 21 instance_->LogTest("Create", TestCreate()); | 37 RUN_TEST(IsWebSocket, filter); |
| 22 instance_->LogTest("IsWebSocket", TestIsWebSocket()); | 38 RUN_TEST(InvalidConnect, filter); |
| 39 RUN_TEST(ValidConnect, filter); | |
| 40 RUN_TEST(TextSendReceive, filter); | |
| 23 } | 41 } |
| 24 | 42 |
| 25 std::string TestWebSocket::TestCreate() { | 43 PP_Var TestWebSocket::CreateVar(const char* string) { |
| 26 PP_Resource rsrc = websocket_interface_->Create(instance_->pp_instance()); | 44 return var_interface_->VarFromUtf8( |
| 27 if (!rsrc) | 45 pp::Module::Get()->pp_module(), string, strlen(string)); |
| 28 return "Could not create websocket via C interface"; | 46 } |
| 29 | 47 |
| 30 PASS(); | 48 void TestWebSocket::ReleaseVar(PP_Var& var) { |
| 49 var_interface_->Release(var); | |
| 50 } | |
| 51 | |
| 52 bool TestWebSocket::Compare(PP_Var& var, const char* string) { | |
| 53 if (var.type != PP_VARTYPE_STRING) | |
| 54 return false; | |
| 55 uint32_t utf8_length; | |
| 56 const char* utf8 = var_interface_->VarToUtf8(var, &utf8_length); | |
| 57 uint32_t string_length = strlen(string); | |
| 58 if (utf8_length != string_length) | |
| 59 return false; | |
| 60 if (strncmp(utf8, string, utf8_length)) | |
| 61 return false; | |
| 62 return true; | |
| 63 } | |
| 64 | |
| 65 PP_Resource TestWebSocket::Connect() { | |
| 66 PP_Var empty_protocols[] = {}; | |
| 67 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); | |
| 68 if (!ws) | |
| 69 return 0; | |
| 70 // TODO(toyoshim): For now, following WebSocket server set up manually. | |
|
Yuta Kitamura
2011/11/21 05:49:22
nit: "following WebSocket server must be started m
Takashi Toyoshima
2011/11/21 09:00:46
Done.
| |
| 71 // That's why some unit tests are disabled by default. | |
| 72 const char* echo_url = "ws://localhost:10080/echo"; | |
| 73 PP_Var echo_url_var = CreateVar(echo_url); | |
| 74 TestCompletionCallback callback(instance_->pp_instance(), force_async_); | |
| 75 int32_t result = websocket_interface_->Connect( | |
| 76 ws, echo_url_var, empty_protocols, 0, | |
| 77 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | |
| 78 ReleaseVar(echo_url_var); | |
| 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 undefined_protocols[] = { PP_MakeUndefined() }; | |
| 110 PP_Var empty_protocols[] = {}; | |
| 111 | |
| 112 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); | |
| 113 ASSERT_TRUE(ws); | |
| 114 | |
| 115 int32_t result = websocket_interface_->Connect( | |
| 116 ws, PP_MakeUndefined(), undefined_protocols, 1, PP_BlockUntilComplete()); | |
| 117 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); | |
| 118 | |
| 119 result = websocket_interface_->Connect( | |
| 120 ws, PP_MakeUndefined(), undefined_protocols, 1, PP_BlockUntilComplete()); | |
| 121 ASSERT_EQ(PP_ERROR_INPROGRESS, result); | |
| 122 | |
| 123 core_interface_->ReleaseResource(ws); | |
| 124 | |
| 125 const char* invalid_urls[] = { | |
| 126 "http://www.google.com/invalid_scheme", | |
| 127 "ws://www.google.com/invalid#fragment", | |
| 128 "ws://www.google.com:65535/invalid_port", | |
| 129 NULL | |
| 130 }; | |
| 131 for (int i = 0; invalid_urls[i]; ++i) { | |
| 132 ws = websocket_interface_->Create(instance_->pp_instance()); | |
| 133 ASSERT_TRUE(ws); | |
| 134 PP_Var invalid_url = CreateVar(invalid_urls[i]); | |
| 135 result = websocket_interface_->Connect( | |
| 136 ws, invalid_url, empty_protocols, 0, PP_BlockUntilComplete()); | |
| 137 ReleaseVar(invalid_url); | |
| 138 core_interface_->ReleaseResource(ws); | |
| 139 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); | |
| 140 } | |
| 141 | |
| 142 // TODO(toyoshim): Add invalid protocols tests | |
| 143 | |
| 144 PASS(); | |
| 145 } | |
| 146 | |
| 147 | |
| 148 std::string TestWebSocket::TestValidConnect() { | |
| 149 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); | |
| 150 const char* echo_url = "ws://localhost:10080/echo"; | |
| 151 PP_Var echo_url_var = CreateVar(echo_url); | |
| 152 PP_Var empty_protocols[] = {}; | |
| 153 TestCompletionCallback callback(instance_->pp_instance(), force_async_); | |
| 154 int32_t result = websocket_interface_->Connect( | |
| 155 ws, echo_url_var, empty_protocols, 0, | |
| 156 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | |
| 157 ReleaseVar(echo_url_var); | |
| 158 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); | |
| 159 result = callback.WaitForResult(); | |
| 160 ASSERT_EQ(PP_OK, result); | |
| 161 core_interface_->ReleaseResource(ws); | |
| 162 | |
| 163 PASS(); | |
| 164 } | |
| 165 | |
| 166 // TODO(toyoshim): Add tests to call various interfaces before calling connect. | |
| 167 | |
| 168 std::string TestWebSocket::TestTextSendReceive() { | |
| 169 // Connect to test echo server. | |
| 170 PP_Resource ws = Connect(); | |
| 171 ASSERT_TRUE(ws); | |
| 172 | |
| 173 // Send 'hello pepper' text message. | |
| 174 const char* message = "hello pepper"; | |
| 175 PP_Var message_var = CreateVar(message); | |
| 176 int32_t result = websocket_interface_->SendMessage(ws, message_var); | |
| 177 ReleaseVar(message_var); | |
| 178 ASSERT_EQ(PP_OK, result); | |
| 179 | |
| 180 // Receive echoed 'hello pepper'. | |
| 181 TestCompletionCallback callback(instance_->pp_instance(), force_async_); | |
| 182 PP_Var received_message; | |
| 183 result = websocket_interface_->ReceiveMessage(ws, &received_message, | |
| 184 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | |
| 185 ASSERT_FALSE(result != PP_OK && result != PP_OK_COMPLETIONPENDING); | |
| 186 if (result == PP_OK_COMPLETIONPENDING) | |
| 187 result = callback.WaitForResult(); | |
| 188 ASSERT_EQ(PP_OK, result); | |
| 189 ASSERT_TRUE(Compare(received_message, message)); | |
| 190 ReleaseVar(received_message); | |
| 191 core_interface_->ReleaseResource(ws); | |
| 192 | |
| 193 PASS(); | |
| 194 } | |
| OLD | NEW |