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 |
| 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::Compare(const PP_Var& var, const char* string) { | |
|
dmichael (off chromium)
2011/11/23 17:41:24
nit: Maybe AreEqual would be a better name, so it'
Takashi Toyoshima
2011/11/24 03:55:49
Done.
| |
| 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 empty_protocols[] = {}; | |
| 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, empty_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 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()); | |
|
dmichael (off chromium)
2011/11/23 17:41:24
PP_BlockUntilComplete is not allowed on the main t
Takashi Toyoshima
2011/11/24 03:55:49
This callback is totally dummy and never used in w
dmichael (off chromium)
2011/11/24 04:32:36
I understand it's a dummy, but the blocking comple
| |
| 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); | |
|
dmichael (off chromium)
2011/11/23 17:41:24
Should it really return 'INPROGRESS'? I would have
Takashi Toyoshima
2011/11/24 03:55:49
We define that Connect must be called at most once
| |
| 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 PP_Var url = CreateVar(kEchoServerURL); | |
| 151 PP_Var empty_protocols[] = {}; | |
| 152 TestCompletionCallback callback(instance_->pp_instance(), force_async_); | |
| 153 int32_t result = websocket_interface_->Connect( | |
| 154 ws, url, empty_protocols, 0, | |
| 155 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | |
| 156 ReleaseVar(url); | |
| 157 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); | |
| 158 result = callback.WaitForResult(); | |
| 159 ASSERT_EQ(PP_OK, result); | |
| 160 core_interface_->ReleaseResource(ws); | |
| 161 | |
| 162 PASS(); | |
| 163 } | |
| 164 | |
| 165 // TODO(toyoshim): Add tests to call various interfaces before calling connect. | |
| 166 | |
| 167 std::string TestWebSocket::TestTextSendReceive() { | |
| 168 // Connect to test echo server. | |
| 169 PP_Resource ws = Connect(); | |
| 170 ASSERT_TRUE(ws); | |
| 171 | |
| 172 // Send 'hello pepper' text message. | |
| 173 const char* message = "hello pepper"; | |
| 174 PP_Var message_var = CreateVar(message); | |
|
dmichael (off chromium)
2011/11/23 17:41:24
Optional: You might save a little bit of coding if
Takashi Toyoshima
2011/11/24 03:55:49
Thank you for good advice.
I'm planning to prepare
| |
| 175 int32_t result = websocket_interface_->SendMessage(ws, message_var); | |
| 176 ReleaseVar(message_var); | |
| 177 ASSERT_EQ(PP_OK, result); | |
| 178 | |
| 179 // Receive echoed 'hello pepper'. | |
| 180 TestCompletionCallback callback(instance_->pp_instance(), force_async_); | |
| 181 PP_Var received_message; | |
| 182 result = websocket_interface_->ReceiveMessage(ws, &received_message, | |
| 183 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | |
| 184 ASSERT_FALSE(result != PP_OK && result != PP_OK_COMPLETIONPENDING); | |
| 185 if (result == PP_OK_COMPLETIONPENDING) | |
| 186 result = callback.WaitForResult(); | |
| 187 ASSERT_EQ(PP_OK, result); | |
| 188 ASSERT_TRUE(Compare(received_message, message)); | |
| 189 ReleaseVar(received_message); | |
| 190 core_interface_->ReleaseResource(ws); | |
| 191 | |
| 192 PASS(); | |
| 193 } | |
| 194 | |
| 195 // TODO(toyoshim): Add other function tests. | |
| OLD | NEW |