1 // Copyright 2013 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 "ppapi/proxy/locking_resource_releaser.h"
6 #include "ppapi/proxy/ppapi_messages.h"
7 #include "ppapi/proxy/ppapi_proxy_test.h"
8 #include "ppapi/proxy/talk_resource.h"
9 #include "ppapi/thunk/thunk.h"
16 template <class ResultType>
17 class MockCallbackBase {
19 MockCallbackBase() : called_(false) {
34 static void Callback(void* user_data, ResultType result) {
35 MockCallbackBase* that = reinterpret_cast<MockCallbackBase*>(user_data);
37 that->result_ = result;
45 typedef MockCallbackBase<int32_t> MockCompletionCallback;
46 typedef MockCallbackBase<PP_TalkEvent> TalkEventCallback;
48 class TalkResourceTest : public PluginProxyTest {
52 const IPC::Message& reply,
55 ResourceMessageCallParams params;
56 ASSERT_TRUE(sink().GetFirstResourceCallMatching(id, ¶ms, &msg));
58 ResourceMessageReplyParams reply_params(params.pp_resource(),
60 reply_params.set_result(result);
61 IPC::Message reply_msg = PpapiPluginMsg_ResourceReply(reply_params, reply);
62 ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived(reply_msg));
69 TEST_F(TalkResourceTest, GetPermission) {
70 const PPB_Talk_Private_1_0* talk = thunk::GetPPB_Talk_Private_1_0_Thunk();
71 LockingResourceReleaser res(talk->Create(pp_instance()));
72 MockCompletionCallback callback;
74 int32_t result = talk->GetPermission(
76 PP_MakeCompletionCallback(&MockCompletionCallback::Callback, &callback));
77 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
79 ResourceMessageCallParams params;
81 ASSERT_TRUE(sink().GetFirstResourceCallMatching(
82 PpapiHostMsg_Talk_RequestPermission::ID, ¶ms, &msg));
84 ResourceMessageReplyParams reply_params(params.pp_resource(),
86 reply_params.set_result(1);
87 IPC::Message reply = PpapiPluginMsg_ResourceReply(
88 reply_params, PpapiPluginMsg_Talk_RequestPermissionReply());
89 ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived(reply));
91 ASSERT_TRUE(callback.called());
92 ASSERT_EQ(1, callback.result());
95 TEST_F(TalkResourceTest, RequestPermission) {
96 const PPB_Talk_Private_2_0* talk = thunk::GetPPB_Talk_Private_2_0_Thunk();
97 LockingResourceReleaser res(talk->Create(pp_instance()));
98 MockCompletionCallback callback;
100 int32_t result = talk->RequestPermission(
102 PP_TALKPERMISSION_REMOTING_CONTINUE,
103 PP_MakeCompletionCallback(&MockCompletionCallback::Callback, &callback));
104 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
106 ResourceMessageCallParams params;
108 ASSERT_TRUE(sink().GetFirstResourceCallMatching(
109 PpapiHostMsg_Talk_RequestPermission::ID, ¶ms, &msg));
111 ResourceMessageReplyParams reply_params(params.pp_resource(),
113 reply_params.set_result(1);
114 IPC::Message reply = PpapiPluginMsg_ResourceReply(
115 reply_params, PpapiPluginMsg_Talk_RequestPermissionReply());
116 ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived(reply));
118 ASSERT_TRUE(callback.called());
119 ASSERT_EQ(1, callback.result());
122 TEST_F(TalkResourceTest, StartStopRemoting) {
123 const PPB_Talk_Private_2_0* talk = thunk::GetPPB_Talk_Private_2_0_Thunk();
124 LockingResourceReleaser res(talk->Create(pp_instance()));
125 MockCompletionCallback callback;
126 TalkEventCallback event_callback;
129 int32_t result = talk->StartRemoting(
131 &TalkEventCallback::Callback,
133 PP_MakeCompletionCallback(&MockCompletionCallback::Callback, &callback));
134 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
136 SendReply(PpapiHostMsg_Talk_StartRemoting::ID,
137 PpapiPluginMsg_Talk_StartRemotingReply(),
140 ASSERT_TRUE(callback.called());
141 ASSERT_EQ(PP_OK, callback.result());
144 ASSERT_FALSE(event_callback.called());
145 ResourceMessageReplyParams notify_params(res.get(), 0);
146 IPC::Message notify = PpapiPluginMsg_ResourceReply(
147 notify_params, PpapiPluginMsg_Talk_NotifyEvent(PP_TALKEVENT_ERROR));
148 ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived(notify));
149 ASSERT_TRUE(event_callback.called());
150 ASSERT_EQ(PP_TALKEVENT_ERROR, event_callback.result());
154 result = talk->StopRemoting(
156 PP_MakeCompletionCallback(&MockCompletionCallback::Callback, &callback));
157 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
159 SendReply(PpapiHostMsg_Talk_StopRemoting::ID,
160 PpapiPluginMsg_Talk_StopRemotingReply(),
163 ASSERT_TRUE(callback.called());
164 ASSERT_EQ(PP_OK, callback.result());
166 // Events should be discarded at this point
167 event_callback.Reset();
168 ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived(notify));
169 ASSERT_FALSE(event_callback.called());