talk_resource_unittest.cc revision 868fa2fe829687343ffae624259930155e16dbd8
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.
4
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"
10
11namespace ppapi {
12namespace proxy {
13
14namespace {
15
16template <class ResultType>
17class MockCallbackBase {
18 public:
19  MockCallbackBase() : called_(false) {
20  }
21
22  bool called() {
23    return called_;
24  }
25
26  ResultType result() {
27    return result_;
28  }
29
30  void Reset() {
31    called_ = false;
32  }
33
34  static void Callback(void* user_data, ResultType result) {
35    MockCallbackBase* that = reinterpret_cast<MockCallbackBase*>(user_data);
36    that->called_ = true;
37    that->result_ = result;
38  }
39
40 private:
41  bool called_;
42  ResultType result_;
43};
44
45typedef MockCallbackBase<int32_t> MockCompletionCallback;
46typedef MockCallbackBase<PP_TalkEvent> TalkEventCallback;
47
48class TalkResourceTest : public PluginProxyTest {
49 public:
50  void SendReply(
51      uint32_t id,
52      const IPC::Message& reply,
53      int32_t result) {
54    IPC::Message msg;
55    ResourceMessageCallParams params;
56    ASSERT_TRUE(sink().GetFirstResourceCallMatching(id, &params, &msg));
57
58    ResourceMessageReplyParams reply_params(params.pp_resource(),
59                                            params.sequence());
60    reply_params.set_result(result);
61    IPC::Message reply_msg = PpapiPluginMsg_ResourceReply(reply_params, reply);
62    ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived(reply_msg));
63  }
64};
65
66
67}  // namespace
68
69TEST_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;
73
74  int32_t result = talk->GetPermission(
75      res.get(),
76      PP_MakeCompletionCallback(&MockCompletionCallback::Callback, &callback));
77  ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
78
79  ResourceMessageCallParams params;
80  IPC::Message msg;
81  ASSERT_TRUE(sink().GetFirstResourceCallMatching(
82      PpapiHostMsg_Talk_RequestPermission::ID, &params, &msg));
83
84  ResourceMessageReplyParams reply_params(params.pp_resource(),
85                                          params.sequence());
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));
90
91  ASSERT_TRUE(callback.called());
92  ASSERT_EQ(1, callback.result());
93}
94
95TEST_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;
99
100  int32_t result = talk->RequestPermission(
101      res.get(),
102      PP_TALKPERMISSION_REMOTING_CONTINUE,
103      PP_MakeCompletionCallback(&MockCompletionCallback::Callback, &callback));
104  ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
105
106  ResourceMessageCallParams params;
107  IPC::Message msg;
108  ASSERT_TRUE(sink().GetFirstResourceCallMatching(
109      PpapiHostMsg_Talk_RequestPermission::ID, &params, &msg));
110
111  ResourceMessageReplyParams reply_params(params.pp_resource(),
112                                          params.sequence());
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));
117
118  ASSERT_TRUE(callback.called());
119  ASSERT_EQ(1, callback.result());
120}
121
122TEST_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;
127
128  // Start
129  int32_t result = talk->StartRemoting(
130      res.get(),
131      &TalkEventCallback::Callback,
132      &event_callback,
133      PP_MakeCompletionCallback(&MockCompletionCallback::Callback, &callback));
134  ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
135
136  SendReply(PpapiHostMsg_Talk_StartRemoting::ID,
137            PpapiPluginMsg_Talk_StartRemotingReply(),
138            PP_OK);
139
140  ASSERT_TRUE(callback.called());
141  ASSERT_EQ(PP_OK, callback.result());
142
143  // Receive an event
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());
151
152  // Stop
153  callback.Reset();
154  result = talk->StopRemoting(
155      res.get(),
156      PP_MakeCompletionCallback(&MockCompletionCallback::Callback, &callback));
157  ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
158
159  SendReply(PpapiHostMsg_Talk_StopRemoting::ID,
160           PpapiPluginMsg_Talk_StopRemotingReply(),
161           PP_OK);
162
163  ASSERT_TRUE(callback.called());
164  ASSERT_EQ(PP_OK, callback.result());
165
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());
170}
171
172}  // namespace proxy
173}  // namespace ppapi
174