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
3 // found in the LICENSE file.
5 #include "base/memory/ref_counted.h"
6 #include "base/message_loop/message_loop.h"
7 #include "ppapi/c/pp_errors.h"
8 #include "ppapi/c/ppb_var.h"
9 #include "ppapi/c/ppb_websocket.h"
10 #include "ppapi/proxy/locking_resource_releaser.h"
11 #include "ppapi/proxy/plugin_message_filter.h"
12 #include "ppapi/proxy/ppapi_messages.h"
13 #include "ppapi/proxy/ppapi_proxy_test.h"
14 #include "ppapi/proxy/websocket_resource.h"
15 #include "ppapi/shared_impl/ppapi_globals.h"
16 #include "ppapi/shared_impl/ppb_var_shared.h"
17 #include "ppapi/shared_impl/proxy_lock.h"
18 #include "ppapi/shared_impl/resource_tracker.h"
19 #include "ppapi/shared_impl/scoped_pp_resource.h"
20 #include "ppapi/shared_impl/scoped_pp_var.h"
21 #include "ppapi/shared_impl/tracked_callback.h"
22 #include "ppapi/shared_impl/var.h"
23 #include "ppapi/thunk/thunk.h"
30 typedef PluginProxyTest WebSocketResourceTest
;
32 bool g_callback_called
;
33 int32_t g_callback_result
;
34 const PPB_Var
* ppb_var_
= NULL
;
36 void Callback(void* user_data
, int32_t result
) {
37 g_callback_called
= true;
38 g_callback_result
= result
;
41 PP_CompletionCallback
MakeCallback() {
42 g_callback_called
= false;
43 g_callback_result
= PP_OK
;
44 return PP_MakeCompletionCallback(Callback
, NULL
);
47 PP_Var
MakeStringVar(const std::string
& string
) {
49 ppb_var_
= ppapi::PPB_Var_Shared::GetVarInterface1_2();
50 return ppb_var_
->VarFromUtf8(string
.c_str(),
51 static_cast<uint32_t>(string
.length()));
57 // Does a test of Connect().
58 TEST_F(WebSocketResourceTest
, Connect
) {
59 const PPB_WebSocket_1_0
* websocket_iface
=
60 thunk::GetPPB_WebSocket_1_0_Thunk();
62 std::string
url("ws://ws.google.com");
63 std::string
protocol0("x-foo");
64 std::string
protocol1("x-bar");
65 PP_Var url_var
= MakeStringVar(url
);
66 PP_Var protocols
[] = { MakeStringVar(protocol0
), MakeStringVar(protocol1
) };
68 LockingResourceReleaser
res(websocket_iface
->Create(pp_instance()));
70 int32_t result
= websocket_iface
->Connect(res
.get(), url_var
, protocols
, 2,
72 ASSERT_EQ(PP_OK_COMPLETIONPENDING
, result
);
74 // Should be sent a "Connect" message.
75 ResourceMessageCallParams params
;
77 ASSERT_TRUE(sink().GetFirstResourceCallMatching(
78 PpapiHostMsg_WebSocket_Connect::ID
, ¶ms
, &msg
));
79 PpapiHostMsg_WebSocket_Connect::Schema::Param p
;
80 PpapiHostMsg_WebSocket_Connect::Read(&msg
, &p
);
81 EXPECT_EQ(url
, base::get
<0>(p
));
82 EXPECT_EQ(protocol0
, base::get
<1>(p
)[0]);
83 EXPECT_EQ(protocol1
, base::get
<1>(p
)[1]);
85 // Synthesize a response.
86 ResourceMessageReplyParams
reply_params(params
.pp_resource(),
88 reply_params
.set_result(PP_OK
);
89 PluginMessageFilter::DispatchResourceReplyForTest(
90 reply_params
, PpapiPluginMsg_WebSocket_ConnectReply(url
, protocol1
));
92 EXPECT_EQ(PP_OK
, g_callback_result
);
93 EXPECT_EQ(true, g_callback_called
);
96 // Does a test for unsolicited replies.
97 TEST_F(WebSocketResourceTest
, UnsolicitedReplies
) {
98 const PPB_WebSocket_1_0
* websocket_iface
=
99 thunk::GetPPB_WebSocket_1_0_Thunk();
101 LockingResourceReleaser
res(websocket_iface
->Create(pp_instance()));
103 // Check if BufferedAmountReply is handled.
104 ResourceMessageReplyParams
reply_params(res
.get(), 0);
105 reply_params
.set_result(PP_OK
);
106 PluginMessageFilter::DispatchResourceReplyForTest(
107 reply_params
, PpapiPluginMsg_WebSocket_BufferedAmountReply(19760227u));
109 uint64_t amount
= websocket_iface
->GetBufferedAmount(res
.get());
110 EXPECT_EQ(19760227u, amount
);
112 // Check if StateReply is handled.
113 PluginMessageFilter::DispatchResourceReplyForTest(
115 PpapiPluginMsg_WebSocket_StateReply(
116 static_cast<int32_t>(PP_WEBSOCKETREADYSTATE_CLOSING
)));
118 PP_WebSocketReadyState state
= websocket_iface
->GetReadyState(res
.get());
119 EXPECT_EQ(PP_WEBSOCKETREADYSTATE_CLOSING
, state
);
122 TEST_F(WebSocketResourceTest
, MessageError
) {
123 const PPB_WebSocket_1_0
* websocket_iface
=
124 thunk::GetPPB_WebSocket_1_0_Thunk();
126 std::string
url("ws://ws.google.com");
127 PP_Var url_var
= MakeStringVar(url
);
129 LockingResourceReleaser
res(websocket_iface
->Create(pp_instance()));
131 // Establish the connection virtually.
133 websocket_iface
->Connect(res
.get(), url_var
, NULL
, 0, MakeCallback());
134 ASSERT_EQ(PP_OK_COMPLETIONPENDING
, result
);
136 ResourceMessageCallParams params
;
138 ASSERT_TRUE(sink().GetFirstResourceCallMatching(
139 PpapiHostMsg_WebSocket_Connect::ID
, ¶ms
, &msg
));
141 ResourceMessageReplyParams
connect_reply_params(params
.pp_resource(),
143 connect_reply_params
.set_result(PP_OK
);
144 PluginMessageFilter::DispatchResourceReplyForTest(
145 connect_reply_params
,
146 PpapiPluginMsg_WebSocket_ConnectReply(url
, std::string()));
148 EXPECT_EQ(PP_OK
, g_callback_result
);
149 EXPECT_TRUE(g_callback_called
);
152 result
= websocket_iface
->ReceiveMessage(res
.get(), &message
, MakeCallback());
153 EXPECT_FALSE(g_callback_called
);
155 // Synthesize a WebSocket_ErrorReply message.
156 ResourceMessageReplyParams
error_reply_params(res
.get(), 0);
157 error_reply_params
.set_result(PP_OK
);
158 PluginMessageFilter::DispatchResourceReplyForTest(
159 error_reply_params
, PpapiPluginMsg_WebSocket_ErrorReply());
161 EXPECT_EQ(PP_ERROR_FAILED
, g_callback_result
);
162 EXPECT_TRUE(g_callback_called
);