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/plugin_message_filter.h"
7 #include "ppapi/proxy/ppapi_messages.h"
8 #include "ppapi/proxy/ppapi_proxy_test.h"
9 #include "ppapi/proxy/talk_resource.h"
10 #include "ppapi/thunk/thunk.h"
17 template <class ResultType
>
18 class MockCallbackBase
{
20 MockCallbackBase() : called_(false) {
35 static void Callback(void* user_data
, ResultType result
) {
36 MockCallbackBase
* that
= reinterpret_cast<MockCallbackBase
*>(user_data
);
38 that
->result_
= result
;
46 typedef MockCallbackBase
<int32_t> MockCompletionCallback
;
47 typedef MockCallbackBase
<PP_TalkEvent
> TalkEventCallback
;
49 class TalkResourceTest
: public PluginProxyTest
{
53 const IPC::Message
& reply
,
56 ResourceMessageCallParams params
;
57 ASSERT_TRUE(sink().GetFirstResourceCallMatching(id
, ¶ms
, &msg
));
59 ResourceMessageReplyParams
reply_params(params
.pp_resource(),
61 reply_params
.set_result(result
);
62 PluginMessageFilter::DispatchResourceReplyForTest(reply_params
, reply
);
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 PluginMessageFilter::DispatchResourceReplyForTest(
88 reply_params
, PpapiPluginMsg_Talk_RequestPermissionReply());
90 ASSERT_TRUE(callback
.called());
91 ASSERT_EQ(1, callback
.result());
94 TEST_F(TalkResourceTest
, RequestPermission
) {
95 const PPB_Talk_Private_2_0
* talk
= thunk::GetPPB_Talk_Private_2_0_Thunk();
96 LockingResourceReleaser
res(talk
->Create(pp_instance()));
97 MockCompletionCallback callback
;
99 int32_t result
= talk
->RequestPermission(
101 PP_TALKPERMISSION_REMOTING_CONTINUE
,
102 PP_MakeCompletionCallback(&MockCompletionCallback::Callback
, &callback
));
103 ASSERT_EQ(PP_OK_COMPLETIONPENDING
, result
);
105 ResourceMessageCallParams params
;
107 ASSERT_TRUE(sink().GetFirstResourceCallMatching(
108 PpapiHostMsg_Talk_RequestPermission::ID
, ¶ms
, &msg
));
110 ResourceMessageReplyParams
reply_params(params
.pp_resource(),
112 reply_params
.set_result(1);
113 PluginMessageFilter::DispatchResourceReplyForTest(
114 reply_params
, PpapiPluginMsg_Talk_RequestPermissionReply());
116 ASSERT_TRUE(callback
.called());
117 ASSERT_EQ(1, callback
.result());
120 TEST_F(TalkResourceTest
, StartStopRemoting
) {
121 const PPB_Talk_Private_2_0
* talk
= thunk::GetPPB_Talk_Private_2_0_Thunk();
122 LockingResourceReleaser
res(talk
->Create(pp_instance()));
123 MockCompletionCallback callback
;
124 TalkEventCallback event_callback
;
127 int32_t result
= talk
->StartRemoting(
129 &TalkEventCallback::Callback
,
131 PP_MakeCompletionCallback(&MockCompletionCallback::Callback
, &callback
));
132 ASSERT_EQ(PP_OK_COMPLETIONPENDING
, result
);
134 SendReply(PpapiHostMsg_Talk_StartRemoting::ID
,
135 PpapiPluginMsg_Talk_StartRemotingReply(),
138 ASSERT_TRUE(callback
.called());
139 ASSERT_EQ(PP_OK
, callback
.result());
142 ASSERT_FALSE(event_callback
.called());
143 ResourceMessageReplyParams
notify_params(res
.get(), 0);
144 PluginMessageFilter::DispatchResourceReplyForTest(
145 notify_params
, PpapiPluginMsg_Talk_NotifyEvent(PP_TALKEVENT_ERROR
));
146 ASSERT_TRUE(event_callback
.called());
147 ASSERT_EQ(PP_TALKEVENT_ERROR
, event_callback
.result());
151 result
= talk
->StopRemoting(
153 PP_MakeCompletionCallback(&MockCompletionCallback::Callback
, &callback
));
154 ASSERT_EQ(PP_OK_COMPLETIONPENDING
, result
);
156 SendReply(PpapiHostMsg_Talk_StopRemoting::ID
,
157 PpapiPluginMsg_Talk_StopRemotingReply(),
160 ASSERT_TRUE(callback
.called());
161 ASSERT_EQ(PP_OK
, callback
.result());
163 // Events should be discarded at this point
164 event_callback
.Reset();
165 PluginMessageFilter::DispatchResourceReplyForTest(
166 notify_params
, PpapiPluginMsg_Talk_NotifyEvent(PP_TALKEVENT_ERROR
));
167 ASSERT_FALSE(event_callback
.called());