1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file.
4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ppapi/proxy/locking_resource_releaser.h"
6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ppapi/proxy/ppapi_messages.h"
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ppapi/proxy/ppapi_proxy_test.h"
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ppapi/proxy/talk_resource.h"
9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ppapi/thunk/thunk.h"
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace ppapi {
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace proxy {
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace {
15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)template <class ResultType>
17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class MockCallbackBase {
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  MockCallbackBase() : called_(false) {
20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  bool called() {
23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return called_;
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ResultType result() {
27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return result_;
28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void Reset() {
31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    called_ = false;
32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
33868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
34868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  static void Callback(void* user_data, ResultType result) {
35868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    MockCallbackBase* that = reinterpret_cast<MockCallbackBase*>(user_data);
36868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    that->called_ = true;
37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    that->result_ = result;
38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) private:
41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  bool called_;
42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ResultType result_;
43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)typedef MockCallbackBase<int32_t> MockCompletionCallback;
46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)typedef MockCallbackBase<PP_TalkEvent> TalkEventCallback;
47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class TalkResourceTest : public PluginProxyTest {
49868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void SendReply(
51868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      uint32_t id,
52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      const IPC::Message& reply,
53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      int32_t result) {
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    IPC::Message msg;
55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ResourceMessageCallParams params;
56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ASSERT_TRUE(sink().GetFirstResourceCallMatching(id, &params, &msg));
57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ResourceMessageReplyParams reply_params(params.pp_resource(),
59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                            params.sequence());
60868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    reply_params.set_result(result);
61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    IPC::Message reply_msg = PpapiPluginMsg_ResourceReply(reply_params, reply);
62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived(reply_msg));
63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace
68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(TalkResourceTest, GetPermission) {
70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const PPB_Talk_Private_1_0* talk = thunk::GetPPB_Talk_Private_1_0_Thunk();
71868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  LockingResourceReleaser res(talk->Create(pp_instance()));
72868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  MockCompletionCallback callback;
73868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
74868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int32_t result = talk->GetPermission(
75868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      res.get(),
76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      PP_MakeCompletionCallback(&MockCompletionCallback::Callback, &callback));
77868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
78868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ResourceMessageCallParams params;
80868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  IPC::Message msg;
81868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_TRUE(sink().GetFirstResourceCallMatching(
82868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      PpapiHostMsg_Talk_RequestPermission::ID, &params, &msg));
83868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
84868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ResourceMessageReplyParams reply_params(params.pp_resource(),
85868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                          params.sequence());
86868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  reply_params.set_result(1);
87868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  IPC::Message reply = PpapiPluginMsg_ResourceReply(
88868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      reply_params, PpapiPluginMsg_Talk_RequestPermissionReply());
89868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived(reply));
90868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
91868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_TRUE(callback.called());
92868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_EQ(1, callback.result());
93868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
94868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(TalkResourceTest, RequestPermission) {
96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const PPB_Talk_Private_2_0* talk = thunk::GetPPB_Talk_Private_2_0_Thunk();
97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  LockingResourceReleaser res(talk->Create(pp_instance()));
98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  MockCompletionCallback callback;
99868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
100868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int32_t result = talk->RequestPermission(
101868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      res.get(),
102868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      PP_TALKPERMISSION_REMOTING_CONTINUE,
103868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      PP_MakeCompletionCallback(&MockCompletionCallback::Callback, &callback));
104868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
105868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
106868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ResourceMessageCallParams params;
107868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  IPC::Message msg;
108868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_TRUE(sink().GetFirstResourceCallMatching(
109868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      PpapiHostMsg_Talk_RequestPermission::ID, &params, &msg));
110868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
111868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ResourceMessageReplyParams reply_params(params.pp_resource(),
112868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                          params.sequence());
113868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  reply_params.set_result(1);
114868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  IPC::Message reply = PpapiPluginMsg_ResourceReply(
115868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      reply_params, PpapiPluginMsg_Talk_RequestPermissionReply());
116868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived(reply));
117868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
118868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_TRUE(callback.called());
119868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_EQ(1, callback.result());
120868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
121868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
122868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(TalkResourceTest, StartStopRemoting) {
123868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const PPB_Talk_Private_2_0* talk = thunk::GetPPB_Talk_Private_2_0_Thunk();
124868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  LockingResourceReleaser res(talk->Create(pp_instance()));
125868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  MockCompletionCallback callback;
126868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  TalkEventCallback event_callback;
127868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
128868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Start
129868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int32_t result = talk->StartRemoting(
130868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      res.get(),
131868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      &TalkEventCallback::Callback,
132868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      &event_callback,
133868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      PP_MakeCompletionCallback(&MockCompletionCallback::Callback, &callback));
134868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
135868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
136868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  SendReply(PpapiHostMsg_Talk_StartRemoting::ID,
137868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            PpapiPluginMsg_Talk_StartRemotingReply(),
138868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            PP_OK);
139868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
140868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_TRUE(callback.called());
141868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_EQ(PP_OK, callback.result());
142868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
143868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Receive an event
144868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_FALSE(event_callback.called());
145868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ResourceMessageReplyParams notify_params(res.get(), 0);
146868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  IPC::Message notify = PpapiPluginMsg_ResourceReply(
147868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      notify_params, PpapiPluginMsg_Talk_NotifyEvent(PP_TALKEVENT_ERROR));
148868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived(notify));
149868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_TRUE(event_callback.called());
150868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_EQ(PP_TALKEVENT_ERROR, event_callback.result());
151868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
152868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Stop
153868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  callback.Reset();
154868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  result = talk->StopRemoting(
155868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      res.get(),
156868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      PP_MakeCompletionCallback(&MockCompletionCallback::Callback, &callback));
157868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
158868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
159868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  SendReply(PpapiHostMsg_Talk_StopRemoting::ID,
160868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)           PpapiPluginMsg_Talk_StopRemotingReply(),
161868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)           PP_OK);
162868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
163868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_TRUE(callback.called());
164868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_EQ(PP_OK, callback.result());
165868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
166868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Events should be discarded at this point
167868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  event_callback.Reset();
168868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived(notify));
169868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_FALSE(event_callback.called());
170868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
171868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
172868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace proxy
173868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace ppapi
174