| Index: ppapi/tests/test_websocket.cc | 
| diff --git a/ppapi/tests/test_websocket.cc b/ppapi/tests/test_websocket.cc | 
| index eb83f60fbb76067469a32b209a84e94e3296a777..2c85440bf90c61d0166f98d3660f58645051701f 100644 | 
| --- a/ppapi/tests/test_websocket.cc | 
| +++ b/ppapi/tests/test_websocket.cc | 
| @@ -4,41 +4,197 @@ | 
|  | 
| #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", | 
| +    // TODO(toyoshim): Add URL which has invalid port like | 
| +    // 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. | 
|  |