Fix iOS build for XCode 4.6.
[chromium-blink-merge.git] / ppapi / proxy / websocket_resource_unittest.cc
blob56012b33f6781889e03e47575ca32b1daea70805
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.h"
7 #include "ppapi/c/pp_errors.h"
8 #include "ppapi/c/ppb_websocket.h"
9 #include "ppapi/c/ppb_var.h"
10 #include "ppapi/proxy/websocket_resource.h"
11 #include "ppapi/proxy/ppapi_messages.h"
12 #include "ppapi/proxy/ppapi_proxy_test.h"
13 #include "ppapi/shared_impl/ppb_var_shared.h"
14 #include "ppapi/shared_impl/scoped_pp_resource.h"
15 #include "ppapi/shared_impl/scoped_pp_var.h"
16 #include "ppapi/shared_impl/tracked_callback.h"
17 #include "ppapi/shared_impl/var.h"
18 #include "ppapi/thunk/thunk.h"
20 namespace ppapi {
21 namespace proxy {
23 namespace {
25 typedef PluginProxyTest WebSocketResourceTest;
27 bool g_callback_called;
28 int32_t g_callback_result;
29 const PPB_Var* ppb_var_ = NULL;
31 void Callback(void* user_data, int32_t result) {
32 g_callback_called = true;
33 g_callback_result = result;
36 PP_CompletionCallback MakeCallback() {
37 g_callback_called = false;
38 g_callback_result = PP_OK;
39 return PP_MakeCompletionCallback(Callback, NULL);
42 PP_Var MakeStringVar(const std::string& string) {
43 if (!ppb_var_)
44 ppb_var_ = ppapi::PPB_Var_Shared::GetVarInterface1_1();
45 return ppb_var_->VarFromUtf8(string.c_str(), string.length());
48 } // namespace
51 // Does a test of Connect().
52 TEST_F(WebSocketResourceTest, Connect) {
53 const PPB_WebSocket_1_0* websocket_iface =
54 thunk::GetPPB_WebSocket_1_0_Thunk();
56 std::string url("ws://ws.google.com");
57 std::string protocol0("x-foo");
58 std::string protocol1("x-bar");
59 PP_Var url_var = MakeStringVar(url);
60 PP_Var protocols[] = { MakeStringVar(protocol0), MakeStringVar(protocol1) };
62 ScopedPPResource res(ScopedPPResource::PassRef(),
63 websocket_iface->Create(pp_instance()));
65 int32_t result =
66 websocket_iface->Connect(res, url_var, protocols, 2, MakeCallback());
67 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
69 // Should be sent a "Connect" message.
70 ResourceMessageCallParams params;
71 IPC::Message msg;
72 ASSERT_TRUE(sink().GetFirstResourceCallMatching(
73 PpapiHostMsg_WebSocket_Connect::ID, &params, &msg));
74 PpapiHostMsg_WebSocket_Connect::Schema::Param p;
75 PpapiHostMsg_WebSocket_Connect::Read(&msg, &p);
76 EXPECT_EQ(url, p.a);
77 EXPECT_EQ(protocol0, p.b[0]);
78 EXPECT_EQ(protocol1, p.b[1]);
80 // Synthesize a response.
81 ResourceMessageReplyParams reply_params(params.pp_resource(),
82 params.sequence());
83 reply_params.set_result(PP_OK);
84 ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived(
85 PpapiPluginMsg_ResourceReply(reply_params,
86 PpapiPluginMsg_WebSocket_ConnectReply(url, protocol1))));
88 EXPECT_EQ(PP_OK, g_callback_result);
89 EXPECT_EQ(true, g_callback_called);
92 // Does a test for unsolicited replies.
93 TEST_F(WebSocketResourceTest, UnsolicitedReplies) {
94 const PPB_WebSocket_1_0* websocket_iface =
95 thunk::GetPPB_WebSocket_1_0_Thunk();
97 ScopedPPResource res(ScopedPPResource::PassRef(),
98 websocket_iface->Create(pp_instance()));
100 // Check if BufferedAmountReply is handled.
101 ResourceMessageReplyParams reply_params(res, 0);
102 reply_params.set_result(PP_OK);
103 ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived(
104 PpapiPluginMsg_ResourceReply(
105 reply_params,
106 PpapiPluginMsg_WebSocket_BufferedAmountReply(19760227u))));
108 uint64_t amount = websocket_iface->GetBufferedAmount(res);
109 EXPECT_EQ(19760227u, amount);
111 // Check if StateReply is handled.
112 ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived(
113 PpapiPluginMsg_ResourceReply(
114 reply_params,
115 PpapiPluginMsg_WebSocket_StateReply(
116 static_cast<int32_t>(PP_WEBSOCKETREADYSTATE_CLOSING)))));
118 PP_WebSocketReadyState state = websocket_iface->GetReadyState(res);
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 ScopedPPResource res(ScopedPPResource::PassRef(),
130 websocket_iface->Create(pp_instance()));
132 // Establish the connection virtually.
133 int32_t result =
134 websocket_iface->Connect(res, url_var, NULL, 0, MakeCallback());
135 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
137 ResourceMessageCallParams params;
138 IPC::Message msg;
139 ASSERT_TRUE(sink().GetFirstResourceCallMatching(
140 PpapiHostMsg_WebSocket_Connect::ID, &params, &msg));
142 ResourceMessageReplyParams connect_reply_params(params.pp_resource(),
143 params.sequence());
144 connect_reply_params.set_result(PP_OK);
145 ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived(
146 PpapiPluginMsg_ResourceReply(connect_reply_params,
147 PpapiPluginMsg_WebSocket_ConnectReply(url, std::string()))));
149 EXPECT_EQ(PP_OK, g_callback_result);
150 EXPECT_TRUE(g_callback_called);
152 PP_Var message;
153 result = websocket_iface->ReceiveMessage(res, &message, MakeCallback());
154 EXPECT_FALSE(g_callback_called);
156 // Synthesize a WebSocket_ErrorReply message.
157 ResourceMessageReplyParams error_reply_params(res, 0);
158 error_reply_params.set_result(PP_OK);
159 ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived(
160 PpapiPluginMsg_ResourceReply(error_reply_params,
161 PpapiPluginMsg_WebSocket_ErrorReply())));
163 EXPECT_EQ(PP_ERROR_FAILED, g_callback_result);
164 EXPECT_TRUE(g_callback_called);
167 } // namespace proxy
168 } // namespace ppapi