summaryrefslogtreecommitdiffstats
path: root/ppapi
diff options
context:
space:
mode:
authortoyoshim@chromium.org <toyoshim@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-11-25 14:50:58 +0000
committertoyoshim@chromium.org <toyoshim@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-11-25 14:50:58 +0000
commit3138146004b13a2f1a249da15531d96a53f3e2d2 (patch)
tree5adf3ed0d951b465a7bb9d6d914773bb9089a1b1 /ppapi
parent9da4b1b94613562bf00cb3bc4a0b80af3c2fdc94 (diff)
downloadchromium_src-3138146004b13a2f1a249da15531d96a53f3e2d2.zip
chromium_src-3138146004b13a2f1a249da15531d96a53f3e2d2.tar.gz
chromium_src-3138146004b13a2f1a249da15531d96a53f3e2d2.tar.bz2
WebSocket Pepper API: in process API implementation
This change enable in process Websocket Pepper API. For now, some unit tests are disabled because they need external a WebSocket server. These disabled tests run manually with external pywebsocket and --gtest_also_run_disabled_tests flag as follows. These tests will enabled in a few days. TBR=dmichael@chromium.org BUG=87310 TEST=ui_tests --gtest_filter='PPAPITest.WebSocket*' Review URL: http://codereview.chromium.org/8558017 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@111596 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ppapi')
-rw-r--r--ppapi/tests/test_websocket.cc181
-rw-r--r--ppapi/tests/test_websocket.h14
2 files changed, 181 insertions, 14 deletions
diff --git a/ppapi/tests/test_websocket.cc b/ppapi/tests/test_websocket.cc
index eb83f60..cb30686 100644
--- a/ppapi/tests/test_websocket.cc
+++ b/ppapi/tests/test_websocket.cc
@@ -4,41 +4,196 @@
#include "ppapi/tests/test_websocket.h"
+#include <string.h>
+
+#include "base/logging.h"
#include "ppapi/c/dev/ppb_websocket_dev.h"
+#include "ppapi/c/pp_errors.h"
+#include "ppapi/c/pp_var.h"
+#include "ppapi/c/pp_completion_callback.h"
+#include "ppapi/c/ppb_core.h"
+#include "ppapi/c/ppb_var.h"
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/module.h"
+#include "ppapi/tests/test_utils.h"
#include "ppapi/tests/testing_instance.h"
+static const char kEchoServerURL[] =
+ "ws://localhost:8880/websocket/tests/hybi/echo";
+
REGISTER_TEST_CASE(WebSocket);
bool TestWebSocket::Init() {
- websocket_interface_ = reinterpret_cast<PPB_WebSocket_Dev const*>(
+ websocket_interface_ = static_cast<const PPB_WebSocket_Dev*>(
pp::Module::Get()->GetBrowserInterface(PPB_WEBSOCKET_DEV_INTERFACE));
- return !!websocket_interface_;
+ var_interface_ = static_cast<const PPB_Var*>(
+ pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE));
+ core_interface_ = static_cast<const PPB_Core*>(
+ pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE));
+ if (!websocket_interface_ || !var_interface_ || !core_interface_)
+ return false;
+
+ return true;
}
void TestWebSocket::RunTests(const std::string& filter) {
- instance_->LogTest("Create", TestCreate());
- instance_->LogTest("IsWebSocket", TestIsWebSocket());
+ RUN_TEST(IsWebSocket, filter);
+ RUN_TEST(InvalidConnect, filter);
+ RUN_TEST(ValidConnect, filter);
+ RUN_TEST(TextSendReceive, filter);
}
-std::string TestWebSocket::TestCreate() {
- PP_Resource rsrc = websocket_interface_->Create(instance_->pp_instance());
- if (!rsrc)
- return "Could not create websocket via C interface";
+PP_Var TestWebSocket::CreateVar(const char* string) {
+ return var_interface_->VarFromUtf8(
+ pp::Module::Get()->pp_module(), string, strlen(string));
+}
- PASS();
+void TestWebSocket::ReleaseVar(const PP_Var& var) {
+ var_interface_->Release(var);
+}
+
+bool TestWebSocket::AreEqual(const PP_Var& var, const char* string) {
+ if (var.type != PP_VARTYPE_STRING)
+ return false;
+ uint32_t utf8_length;
+ const char* utf8 = var_interface_->VarToUtf8(var, &utf8_length);
+ uint32_t string_length = strlen(string);
+ if (utf8_length != string_length)
+ return false;
+ if (strncmp(utf8, string, utf8_length))
+ return false;
+ return true;
+}
+
+PP_Resource TestWebSocket::Connect() {
+ PP_Var protocols[] = { PP_MakeUndefined() };
+ PP_Resource ws = websocket_interface_->Create(instance_->pp_instance());
+ if (!ws)
+ return 0;
+ PP_Var url = CreateVar(kEchoServerURL);
+ TestCompletionCallback callback(instance_->pp_instance(), force_async_);
+ int32_t result = websocket_interface_->Connect(
+ ws, url, protocols, 0,
+ static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
+ ReleaseVar(url);
+ if (force_async_ && result != PP_OK_COMPLETIONPENDING) {
+ core_interface_->ReleaseResource(ws);
+ return 0;
+ }
+ if (callback.WaitForResult() != PP_OK) {
+ core_interface_->ReleaseResource(ws);
+ return 0;
+ }
+ return ws;
}
std::string TestWebSocket::TestIsWebSocket() {
// Test that a NULL resource isn't a websocket.
pp::Resource null_resource;
- if (websocket_interface_->IsWebSocket(null_resource.pp_resource()))
- return "Null resource was reported as a valid websocket";
+ PP_Bool result =
+ websocket_interface_->IsWebSocket(null_resource.pp_resource());
+ ASSERT_FALSE(result);
PP_Resource ws = websocket_interface_->Create(instance_->pp_instance());
- if (!websocket_interface_->IsWebSocket(ws))
- return "websocket was reported as an invalid websocket";
+ ASSERT_TRUE(ws);
+
+ result = websocket_interface_->IsWebSocket(ws);
+ ASSERT_TRUE(result);
+
+ core_interface_->ReleaseResource(ws);
PASS();
}
+
+std::string TestWebSocket::TestInvalidConnect() {
+ PP_Var protocols[] = { PP_MakeUndefined() };
+
+ PP_Resource ws = websocket_interface_->Create(instance_->pp_instance());
+ ASSERT_TRUE(ws);
+
+ TestCompletionCallback callback(instance_->pp_instance(), force_async_);
+ int32_t result = websocket_interface_->Connect(
+ ws, PP_MakeUndefined(), protocols, 1,
+ static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
+ ASSERT_EQ(PP_ERROR_BADARGUMENT, result);
+
+ result = websocket_interface_->Connect(
+ ws, PP_MakeUndefined(), protocols, 1,
+ static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
+ ASSERT_EQ(PP_ERROR_INPROGRESS, result);
+
+ core_interface_->ReleaseResource(ws);
+
+ const char* invalid_urls[] = {
+ "http://www.google.com/invalid_scheme",
+ "ws://www.google.com/invalid#fragment",
+ "ws://www.google.com:65535/invalid_port",
+ NULL
+ };
+ for (int i = 0; invalid_urls[i]; ++i) {
+ ws = websocket_interface_->Create(instance_->pp_instance());
+ ASSERT_TRUE(ws);
+ PP_Var invalid_url = CreateVar(invalid_urls[i]);
+ result = websocket_interface_->Connect(
+ ws, invalid_url, protocols, 0,
+ static_cast<pp::CompletionCallback>(
+ callback).pp_completion_callback());
+ ReleaseVar(invalid_url);
+ core_interface_->ReleaseResource(ws);
+ ASSERT_EQ(PP_ERROR_BADARGUMENT, result);
+ }
+
+ // TODO(toyoshim): Add invalid protocols tests
+
+ PASS();
+}
+
+
+std::string TestWebSocket::TestValidConnect() {
+ PP_Resource ws = websocket_interface_->Create(instance_->pp_instance());
+ PP_Var url = CreateVar(kEchoServerURL);
+ PP_Var protocols[] = { PP_MakeUndefined() };
+ TestCompletionCallback callback(instance_->pp_instance(), force_async_);
+ int32_t result = websocket_interface_->Connect(
+ ws, url, protocols, 0,
+ static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
+ ReleaseVar(url);
+ ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
+ result = callback.WaitForResult();
+ ASSERT_EQ(PP_OK, result);
+ core_interface_->ReleaseResource(ws);
+
+ PASS();
+}
+
+// TODO(toyoshim): Add tests to call various interfaces before calling connect.
+
+std::string TestWebSocket::TestTextSendReceive() {
+ // Connect to test echo server.
+ PP_Resource ws = Connect();
+ ASSERT_TRUE(ws);
+
+ // Send 'hello pepper' text message.
+ const char* message = "hello pepper";
+ PP_Var message_var = CreateVar(message);
+ int32_t result = websocket_interface_->SendMessage(ws, message_var);
+ ReleaseVar(message_var);
+ ASSERT_EQ(PP_OK, result);
+
+ // Receive echoed 'hello pepper'.
+ TestCompletionCallback callback(instance_->pp_instance(), force_async_);
+ PP_Var received_message;
+ result = websocket_interface_->ReceiveMessage(ws, &received_message,
+ static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
+ ASSERT_FALSE(result != PP_OK && result != PP_OK_COMPLETIONPENDING);
+ if (result == PP_OK_COMPLETIONPENDING)
+ result = callback.WaitForResult();
+ ASSERT_EQ(PP_OK, result);
+ ASSERT_TRUE(AreEqual(received_message, message));
+ ReleaseVar(received_message);
+ core_interface_->ReleaseResource(ws);
+
+ PASS();
+}
+
+// TODO(toyoshim): Add other function tests.
diff --git a/ppapi/tests/test_websocket.h b/ppapi/tests/test_websocket.h
index 60eeead..f6daf7b 100644
--- a/ppapi/tests/test_websocket.h
+++ b/ppapi/tests/test_websocket.h
@@ -9,6 +9,8 @@
#include "ppapi/tests/test_case.h"
+struct PPB_Core;
+struct PPB_Var;
struct PPB_WebSocket_Dev;
class TestWebSocket : public TestCase {
@@ -20,11 +22,21 @@ class TestWebSocket : public TestCase {
virtual void RunTests(const std::string& filter);
private:
- std::string TestCreate();
+ PP_Var CreateVar(const char* string);
+ void ReleaseVar(const PP_Var& var);
+ bool AreEqual(const PP_Var& var, const char* string);
+
+ PP_Resource Connect();
+
std::string TestIsWebSocket();
+ std::string TestInvalidConnect();
+ std::string TestValidConnect();
+ std::string TestTextSendReceive();
// Used by the tests that access the C API directly.
const PPB_WebSocket_Dev* websocket_interface_;
+ const PPB_Var* var_interface_;
+ const PPB_Core* core_interface_;
};
#endif // PAPPI_TESTS_TEST_WEBSOCKET_H_