| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/memory/ref_counted.h" | 5 #include "base/memory/ref_counted.h" |
| 6 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
| 7 #include "ppapi/c/pp_errors.h" | 7 #include "ppapi/c/pp_errors.h" |
| 8 #include "ppapi/c/ppb_var.h" | 8 #include "ppapi/c/ppb_var.h" |
| 9 #include "ppapi/c/ppb_websocket.h" | 9 #include "ppapi/c/ppb_websocket.h" |
| 10 #include "ppapi/proxy/locking_resource_releaser.h" | 10 #include "ppapi/proxy/locking_resource_releaser.h" |
| 11 #include "ppapi/proxy/plugin_message_filter.h" |
| 11 #include "ppapi/proxy/ppapi_messages.h" | 12 #include "ppapi/proxy/ppapi_messages.h" |
| 12 #include "ppapi/proxy/ppapi_proxy_test.h" | 13 #include "ppapi/proxy/ppapi_proxy_test.h" |
| 13 #include "ppapi/proxy/websocket_resource.h" | 14 #include "ppapi/proxy/websocket_resource.h" |
| 14 #include "ppapi/shared_impl/ppapi_globals.h" | 15 #include "ppapi/shared_impl/ppapi_globals.h" |
| 15 #include "ppapi/shared_impl/ppb_var_shared.h" | 16 #include "ppapi/shared_impl/ppb_var_shared.h" |
| 16 #include "ppapi/shared_impl/proxy_lock.h" | 17 #include "ppapi/shared_impl/proxy_lock.h" |
| 17 #include "ppapi/shared_impl/resource_tracker.h" | 18 #include "ppapi/shared_impl/resource_tracker.h" |
| 18 #include "ppapi/shared_impl/scoped_pp_resource.h" | 19 #include "ppapi/shared_impl/scoped_pp_resource.h" |
| 19 #include "ppapi/shared_impl/scoped_pp_var.h" | 20 #include "ppapi/shared_impl/scoped_pp_var.h" |
| 20 #include "ppapi/shared_impl/tracked_callback.h" | 21 #include "ppapi/shared_impl/tracked_callback.h" |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 PpapiHostMsg_WebSocket_Connect::Schema::Param p; | 78 PpapiHostMsg_WebSocket_Connect::Schema::Param p; |
| 78 PpapiHostMsg_WebSocket_Connect::Read(&msg, &p); | 79 PpapiHostMsg_WebSocket_Connect::Read(&msg, &p); |
| 79 EXPECT_EQ(url, p.a); | 80 EXPECT_EQ(url, p.a); |
| 80 EXPECT_EQ(protocol0, p.b[0]); | 81 EXPECT_EQ(protocol0, p.b[0]); |
| 81 EXPECT_EQ(protocol1, p.b[1]); | 82 EXPECT_EQ(protocol1, p.b[1]); |
| 82 | 83 |
| 83 // Synthesize a response. | 84 // Synthesize a response. |
| 84 ResourceMessageReplyParams reply_params(params.pp_resource(), | 85 ResourceMessageReplyParams reply_params(params.pp_resource(), |
| 85 params.sequence()); | 86 params.sequence()); |
| 86 reply_params.set_result(PP_OK); | 87 reply_params.set_result(PP_OK); |
| 87 ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived( | 88 PluginMessageFilter::DispatchResourceReplyForTest( |
| 88 PpapiPluginMsg_ResourceReply(reply_params, | 89 reply_params, PpapiPluginMsg_WebSocket_ConnectReply(url, protocol1)); |
| 89 PpapiPluginMsg_WebSocket_ConnectReply(url, protocol1)))); | |
| 90 | 90 |
| 91 EXPECT_EQ(PP_OK, g_callback_result); | 91 EXPECT_EQ(PP_OK, g_callback_result); |
| 92 EXPECT_EQ(true, g_callback_called); | 92 EXPECT_EQ(true, g_callback_called); |
| 93 } | 93 } |
| 94 | 94 |
| 95 // Does a test for unsolicited replies. | 95 // Does a test for unsolicited replies. |
| 96 TEST_F(WebSocketResourceTest, UnsolicitedReplies) { | 96 TEST_F(WebSocketResourceTest, UnsolicitedReplies) { |
| 97 const PPB_WebSocket_1_0* websocket_iface = | 97 const PPB_WebSocket_1_0* websocket_iface = |
| 98 thunk::GetPPB_WebSocket_1_0_Thunk(); | 98 thunk::GetPPB_WebSocket_1_0_Thunk(); |
| 99 | 99 |
| 100 LockingResourceReleaser res(websocket_iface->Create(pp_instance())); | 100 LockingResourceReleaser res(websocket_iface->Create(pp_instance())); |
| 101 | 101 |
| 102 // Check if BufferedAmountReply is handled. | 102 // Check if BufferedAmountReply is handled. |
| 103 ResourceMessageReplyParams reply_params(res.get(), 0); | 103 ResourceMessageReplyParams reply_params(res.get(), 0); |
| 104 reply_params.set_result(PP_OK); | 104 reply_params.set_result(PP_OK); |
| 105 ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived( | 105 PluginMessageFilter::DispatchResourceReplyForTest( |
| 106 PpapiPluginMsg_ResourceReply( | 106 reply_params, PpapiPluginMsg_WebSocket_BufferedAmountReply(19760227u)); |
| 107 reply_params, | |
| 108 PpapiPluginMsg_WebSocket_BufferedAmountReply(19760227u)))); | |
| 109 | 107 |
| 110 uint64_t amount = websocket_iface->GetBufferedAmount(res.get()); | 108 uint64_t amount = websocket_iface->GetBufferedAmount(res.get()); |
| 111 EXPECT_EQ(19760227u, amount); | 109 EXPECT_EQ(19760227u, amount); |
| 112 | 110 |
| 113 // Check if StateReply is handled. | 111 // Check if StateReply is handled. |
| 114 ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived( | 112 PluginMessageFilter::DispatchResourceReplyForTest( |
| 115 PpapiPluginMsg_ResourceReply( | 113 reply_params, |
| 116 reply_params, | 114 PpapiPluginMsg_WebSocket_StateReply( |
| 117 PpapiPluginMsg_WebSocket_StateReply( | 115 static_cast<int32_t>(PP_WEBSOCKETREADYSTATE_CLOSING))); |
| 118 static_cast<int32_t>(PP_WEBSOCKETREADYSTATE_CLOSING))))); | |
| 119 | 116 |
| 120 PP_WebSocketReadyState state = websocket_iface->GetReadyState(res.get()); | 117 PP_WebSocketReadyState state = websocket_iface->GetReadyState(res.get()); |
| 121 EXPECT_EQ(PP_WEBSOCKETREADYSTATE_CLOSING, state); | 118 EXPECT_EQ(PP_WEBSOCKETREADYSTATE_CLOSING, state); |
| 122 } | 119 } |
| 123 | 120 |
| 124 TEST_F(WebSocketResourceTest, MessageError) { | 121 TEST_F(WebSocketResourceTest, MessageError) { |
| 125 const PPB_WebSocket_1_0* websocket_iface = | 122 const PPB_WebSocket_1_0* websocket_iface = |
| 126 thunk::GetPPB_WebSocket_1_0_Thunk(); | 123 thunk::GetPPB_WebSocket_1_0_Thunk(); |
| 127 | 124 |
| 128 std::string url("ws://ws.google.com"); | 125 std::string url("ws://ws.google.com"); |
| 129 PP_Var url_var = MakeStringVar(url); | 126 PP_Var url_var = MakeStringVar(url); |
| 130 | 127 |
| 131 LockingResourceReleaser res(websocket_iface->Create(pp_instance())); | 128 LockingResourceReleaser res(websocket_iface->Create(pp_instance())); |
| 132 | 129 |
| 133 // Establish the connection virtually. | 130 // Establish the connection virtually. |
| 134 int32_t result = | 131 int32_t result = |
| 135 websocket_iface->Connect(res.get(), url_var, NULL, 0, MakeCallback()); | 132 websocket_iface->Connect(res.get(), url_var, NULL, 0, MakeCallback()); |
| 136 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); | 133 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); |
| 137 | 134 |
| 138 ResourceMessageCallParams params; | 135 ResourceMessageCallParams params; |
| 139 IPC::Message msg; | 136 IPC::Message msg; |
| 140 ASSERT_TRUE(sink().GetFirstResourceCallMatching( | 137 ASSERT_TRUE(sink().GetFirstResourceCallMatching( |
| 141 PpapiHostMsg_WebSocket_Connect::ID, ¶ms, &msg)); | 138 PpapiHostMsg_WebSocket_Connect::ID, ¶ms, &msg)); |
| 142 | 139 |
| 143 ResourceMessageReplyParams connect_reply_params(params.pp_resource(), | 140 ResourceMessageReplyParams connect_reply_params(params.pp_resource(), |
| 144 params.sequence()); | 141 params.sequence()); |
| 145 connect_reply_params.set_result(PP_OK); | 142 connect_reply_params.set_result(PP_OK); |
| 146 ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived( | 143 PluginMessageFilter::DispatchResourceReplyForTest( |
| 147 PpapiPluginMsg_ResourceReply(connect_reply_params, | 144 connect_reply_params, |
| 148 PpapiPluginMsg_WebSocket_ConnectReply(url, std::string())))); | 145 PpapiPluginMsg_WebSocket_ConnectReply(url, std::string())); |
| 149 | 146 |
| 150 EXPECT_EQ(PP_OK, g_callback_result); | 147 EXPECT_EQ(PP_OK, g_callback_result); |
| 151 EXPECT_TRUE(g_callback_called); | 148 EXPECT_TRUE(g_callback_called); |
| 152 | 149 |
| 153 PP_Var message; | 150 PP_Var message; |
| 154 result = websocket_iface->ReceiveMessage(res.get(), &message, MakeCallback()); | 151 result = websocket_iface->ReceiveMessage(res.get(), &message, MakeCallback()); |
| 155 EXPECT_FALSE(g_callback_called); | 152 EXPECT_FALSE(g_callback_called); |
| 156 | 153 |
| 157 // Synthesize a WebSocket_ErrorReply message. | 154 // Synthesize a WebSocket_ErrorReply message. |
| 158 ResourceMessageReplyParams error_reply_params(res.get(), 0); | 155 ResourceMessageReplyParams error_reply_params(res.get(), 0); |
| 159 error_reply_params.set_result(PP_OK); | 156 error_reply_params.set_result(PP_OK); |
| 160 ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived( | 157 PluginMessageFilter::DispatchResourceReplyForTest( |
| 161 PpapiPluginMsg_ResourceReply(error_reply_params, | 158 error_reply_params, PpapiPluginMsg_WebSocket_ErrorReply()); |
| 162 PpapiPluginMsg_WebSocket_ErrorReply()))); | |
| 163 | 159 |
| 164 EXPECT_EQ(PP_ERROR_FAILED, g_callback_result); | 160 EXPECT_EQ(PP_ERROR_FAILED, g_callback_result); |
| 165 EXPECT_TRUE(g_callback_called); | 161 EXPECT_TRUE(g_callback_called); |
| 166 } | 162 } |
| 167 | 163 |
| 168 } // namespace proxy | 164 } // namespace proxy |
| 169 } // namespace ppapi | 165 } // namespace ppapi |
| OLD | NEW |