blob: c49f1a27a77c25a2ee9b8e76c32e655a2102169b [file] [log] [blame]
[email protected]912f3d6c2011-06-29 18:26:361// Copyright (c) 2011 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 "base/synchronization/waitable_event.h"
6#include "ipc/ipc_message_utils.h"
7#include "ppapi/c/dev/ppb_fullscreen_dev.h"
[email protected]f2ebdce2011-07-07 20:48:498#include "ppapi/c/pp_var.h"
[email protected]912f3d6c2011-06-29 18:26:369#include "ppapi/c/ppb_core.h"
10#include "ppapi/c/ppb_url_loader.h"
11#include "ppapi/c/ppp_instance.h"
12#include "ppapi/proxy/ppapi_messages.h"
13#include "ppapi/proxy/ppapi_proxy_test.h"
14
15namespace pp {
16namespace proxy {
17
18namespace {
19// This is a poor man's mock of PPP_Instance using global variables. Eventually
20// we should generalize making PPAPI interface mocks by using IDL or macro/
21// template magic.
22PP_Instance received_instance;
23uint32_t received_argc;
24std::vector<std::string> received_argn;
25std::vector<std::string> received_argv;
26PP_Bool bool_to_return;
27PP_Bool DidCreate(PP_Instance instance, uint32_t argc, const char* argn[],
28 const char* argv[]) {
29 received_instance = instance;
30 received_argc = argc;
31 received_argn.clear();
32 received_argn.insert(received_argn.begin(), argn, argn + argc);
33 received_argv.clear();
34 received_argv.insert(received_argv.begin(), argv, argv + argc);
35 return bool_to_return;
36}
37
38void DidDestroy(PP_Instance instance) {
39 received_instance = instance;
40}
41
42PP_Rect received_position;
43PP_Rect received_clip;
44// DidChangeView is asynchronous. We wait until the call has completed before
45// proceeding on to the next test.
46base::WaitableEvent did_change_view_called(false, false);
47void DidChangeView(PP_Instance instance, const PP_Rect* position,
48 const PP_Rect* clip) {
49 received_instance = instance;
50 received_position = *position;
51 received_clip = *clip;
52 did_change_view_called.Signal();
53}
54
55PP_Bool received_has_focus;
56base::WaitableEvent did_change_focus_called(false, false);
57void DidChangeFocus(PP_Instance instance, PP_Bool has_focus) {
58 received_instance = instance;
59 received_has_focus = has_focus;
60 did_change_focus_called.Signal();
61}
62
[email protected]912f3d6c2011-06-29 18:26:3663PP_Bool HandleDocumentLoad(PP_Instance instance, PP_Resource url_loader) {
64 // This one requires use of the PPB_URLLoader proxy and PPB_Core, plus a
65 // resource tracker for the url_loader resource.
66 // TODO(dmichael): Mock those out and test this function.
67 NOTREACHED();
68 return PP_FALSE;
69}
70
71PP_Var var_to_return;
72PP_Var GetInstanceObject(PP_Instance instance) {
73 received_instance = instance;
74 return var_to_return;
75}
76
77// Clear all the 'received' values for our mock. Call this before you expect
78// one of the functions to be invoked. TODO(dmichael): It would be better to
79// have a flag also for each function, so we know the right one got called.
80void ResetReceived() {
81 received_instance = 0;
82 received_argc = 0;
83 received_argn.clear();
84 received_argv.clear();
85 memset(&received_position, 0, sizeof(received_position));
86 memset(&received_clip, 0, sizeof(received_clip));
87 received_has_focus = PP_FALSE;
[email protected]912f3d6c2011-06-29 18:26:3688}
89
[email protected]13a8f492011-07-20 19:55:3990PPP_Instance_1_0 ppp_instance_1_0 = {
[email protected]912f3d6c2011-06-29 18:26:3691 &DidCreate,
92 &DidDestroy,
93 &DidChangeView,
94 &DidChangeFocus,
[email protected]912f3d6c2011-06-29 18:26:3695 &HandleDocumentLoad
96};
97
98// PPP_Instance_Proxy::DidChangeView relies on PPB_FullscreenDev being
99// available with a valid implementation of IsFullScreen, so we mock it.
100PP_Bool IsFullscreen(PP_Instance instance) {
101 return PP_FALSE;
102}
103PPB_Fullscreen_Dev ppb_fullscreen_dev = { &IsFullscreen };
104
105} // namespace
106
107class PPP_Instance_ProxyTest : public TwoWayTest {
108 public:
109 PPP_Instance_ProxyTest()
110 : TwoWayTest(TwoWayTest::TEST_PPP_INTERFACE) {
111 }
112};
113
[email protected]13a8f492011-07-20 19:55:39114TEST_F(PPP_Instance_ProxyTest, PPPInstance1_0) {
115 plugin().RegisterTestInterface(PPP_INSTANCE_INTERFACE_1_0, &ppp_instance_1_0);
[email protected]912f3d6c2011-06-29 18:26:36116 host().RegisterTestInterface(PPB_FULLSCREEN_DEV_INTERFACE,
117 &ppb_fullscreen_dev);
118
[email protected]13a8f492011-07-20 19:55:39119 // Grab the host-side proxy for the 1.0 interface.
120 const PPP_Instance_1_0* ppp_instance = static_cast<const PPP_Instance_1_0*>(
[email protected]912f3d6c2011-06-29 18:26:36121 host().host_dispatcher()->GetProxiedInterface(
[email protected]13a8f492011-07-20 19:55:39122 PPP_INSTANCE_INTERFACE_1_0));
[email protected]912f3d6c2011-06-29 18:26:36123
124 // Call each function in turn, make sure we get the expected values and
125 // returns.
126 //
127 // We don't test DidDestroy, because it has the side-effect of removing the
128 // PP_Instance from the PluginDispatcher, which will cause a failure later
129 // when the test is torn down.
130 PP_Instance expected_instance = pp_instance();
131 std::vector<std::string> expected_argn, expected_argv;
132 expected_argn.push_back("Hello");
133 expected_argn.push_back("world.");
134 expected_argv.push_back("elloHay");
135 expected_argv.push_back("orldway.");
136 std::vector<const char*> argn_to_pass, argv_to_pass;
137 CHECK(expected_argn.size() == expected_argv.size());
138 for (size_t i = 0; i < expected_argn.size(); ++i) {
139 argn_to_pass.push_back(expected_argn[i].c_str());
140 argv_to_pass.push_back(expected_argv[i].c_str());
141 }
142 uint32_t expected_argc = expected_argn.size();
143 bool_to_return = PP_TRUE;
144 ResetReceived();
145 EXPECT_EQ(bool_to_return, ppp_instance->DidCreate(expected_instance,
146 expected_argc,
147 &argn_to_pass[0],
148 &argv_to_pass[0]));
149 EXPECT_EQ(received_instance, expected_instance);
150 EXPECT_EQ(received_argc, expected_argc);
151 EXPECT_EQ(received_argn, expected_argn);
152 EXPECT_EQ(received_argv, expected_argv);
153
154 PP_Rect expected_position = { {1, 2}, {3, 4} };
155 PP_Rect expected_clip = { {5, 6}, {7, 8} };
156 ResetReceived();
157 ppp_instance->DidChangeView(expected_instance, &expected_position,
158 &expected_clip);
159 did_change_view_called.Wait();
160 EXPECT_EQ(received_instance, expected_instance);
161 EXPECT_EQ(received_position.point.x, expected_position.point.x);
162 EXPECT_EQ(received_position.point.y, expected_position.point.y);
163 EXPECT_EQ(received_position.size.width, expected_position.size.width);
164 EXPECT_EQ(received_position.size.height, expected_position.size.height);
165 EXPECT_EQ(received_clip.point.x, expected_clip.point.x);
166 EXPECT_EQ(received_clip.point.y, expected_clip.point.y);
167 EXPECT_EQ(received_clip.size.width, expected_clip.size.width);
168 EXPECT_EQ(received_clip.size.height, expected_clip.size.height);
169
170 PP_Bool expected_has_focus = PP_TRUE;
171 ResetReceived();
172 ppp_instance->DidChangeFocus(expected_instance, expected_has_focus);
173 did_change_focus_called.Wait();
174 EXPECT_EQ(received_instance, expected_instance);
175 EXPECT_EQ(received_has_focus, expected_has_focus);
176
[email protected]912f3d6c2011-06-29 18:26:36177 // TODO(dmichael): Need to mock out a resource Tracker to be able to test
178 // HandleResourceLoad. It also requires
179 // PPB_Core.AddRefResource and for PPB_URLLoader to be
180 // registered.
181}
182
183} // namespace proxy
184} // namespace pp
185