blob: 6c696a786d310042ed244a9f5cfdfd0236d3bd6a [file] [log] [blame]
[email protected]73097562012-01-12 19:38:551// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]912f3d6c2011-06-29 18:26:362// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
avie029c4132015-12-23 06:45:225#include <stddef.h>
6#include <stdint.h>
7
[email protected]912f3d6c2011-06-29 18:26:368#include "base/synchronization/waitable_event.h"
9#include "ipc/ipc_message_utils.h"
[email protected]f2ebdce2011-07-07 20:48:4910#include "ppapi/c/pp_var.h"
[email protected]912f3d6c2011-06-29 18:26:3611#include "ppapi/c/ppb_core.h"
[email protected]6570e562011-10-14 21:59:1712#include "ppapi/c/ppb_fullscreen.h"
[email protected]912f3d6c2011-06-29 18:26:3613#include "ppapi/c/ppb_url_loader.h"
14#include "ppapi/c/ppp_instance.h"
[email protected]725c0512011-09-23 20:01:2115#include "ppapi/c/private/ppb_flash_fullscreen.h"
[email protected]b73c6592013-03-30 17:08:1316#include "ppapi/proxy/locking_resource_releaser.h"
[email protected]912f3d6c2011-06-29 18:26:3617#include "ppapi/proxy/ppapi_messages.h"
18#include "ppapi/proxy/ppapi_proxy_test.h"
[email protected]73097562012-01-12 19:38:5519#include "ppapi/shared_impl/ppb_view_shared.h"
[email protected]912f3d6c2011-06-29 18:26:3620
[email protected]4d2efd22011-08-18 21:58:0221namespace ppapi {
[email protected]912f3d6c2011-06-29 18:26:3622namespace proxy {
23
24namespace {
25// This is a poor man's mock of PPP_Instance using global variables. Eventually
26// we should generalize making PPAPI interface mocks by using IDL or macro/
27// template magic.
28PP_Instance received_instance;
29uint32_t received_argc;
30std::vector<std::string> received_argn;
31std::vector<std::string> received_argv;
32PP_Bool bool_to_return;
33PP_Bool DidCreate(PP_Instance instance, uint32_t argc, const char* argn[],
34 const char* argv[]) {
35 received_instance = instance;
36 received_argc = argc;
37 received_argn.clear();
38 received_argn.insert(received_argn.begin(), argn, argn + argc);
39 received_argv.clear();
40 received_argv.insert(received_argv.begin(), argv, argv + argc);
41 return bool_to_return;
42}
43
44void DidDestroy(PP_Instance instance) {
45 received_instance = instance;
46}
47
48PP_Rect received_position;
49PP_Rect received_clip;
50// DidChangeView is asynchronous. We wait until the call has completed before
51// proceeding on to the next test.
gabf40c0a5e2016-06-01 20:10:4652base::WaitableEvent did_change_view_called(
53 base::WaitableEvent::ResetPolicy::AUTOMATIC,
54 base::WaitableEvent::InitialState::NOT_SIGNALED);
[email protected]912f3d6c2011-06-29 18:26:3655void DidChangeView(PP_Instance instance, const PP_Rect* position,
56 const PP_Rect* clip) {
57 received_instance = instance;
58 received_position = *position;
59 received_clip = *clip;
60 did_change_view_called.Signal();
61}
62
63PP_Bool received_has_focus;
gabf40c0a5e2016-06-01 20:10:4664base::WaitableEvent did_change_focus_called(
65 base::WaitableEvent::ResetPolicy::AUTOMATIC,
66 base::WaitableEvent::InitialState::NOT_SIGNALED);
[email protected]912f3d6c2011-06-29 18:26:3667void DidChangeFocus(PP_Instance instance, PP_Bool has_focus) {
68 received_instance = instance;
69 received_has_focus = has_focus;
70 did_change_focus_called.Signal();
71}
72
[email protected]912f3d6c2011-06-29 18:26:3673PP_Bool HandleDocumentLoad(PP_Instance instance, PP_Resource url_loader) {
74 // This one requires use of the PPB_URLLoader proxy and PPB_Core, plus a
75 // resource tracker for the url_loader resource.
76 // TODO(dmichael): Mock those out and test this function.
77 NOTREACHED();
78 return PP_FALSE;
79}
80
[email protected]912f3d6c2011-06-29 18:26:3681// Clear all the 'received' values for our mock. Call this before you expect
82// one of the functions to be invoked. TODO(dmichael): It would be better to
83// have a flag also for each function, so we know the right one got called.
84void ResetReceived() {
85 received_instance = 0;
86 received_argc = 0;
87 received_argn.clear();
88 received_argv.clear();
89 memset(&received_position, 0, sizeof(received_position));
90 memset(&received_clip, 0, sizeof(received_clip));
91 received_has_focus = PP_FALSE;
[email protected]912f3d6c2011-06-29 18:26:3692}
93
[email protected]13a8f492011-07-20 19:55:3994PPP_Instance_1_0 ppp_instance_1_0 = {
[email protected]912f3d6c2011-06-29 18:26:3695 &DidCreate,
96 &DidDestroy,
97 &DidChangeView,
98 &DidChangeFocus,
[email protected]912f3d6c2011-06-29 18:26:3699 &HandleDocumentLoad
100};
101
[email protected]06e0a342011-09-27 04:24:30102// PPP_Instance_Proxy::DidChangeView relies on PPB_(Flash)Fullscreen being
[email protected]912f3d6c2011-06-29 18:26:36103// available with a valid implementation of IsFullScreen, so we mock it.
104PP_Bool IsFullscreen(PP_Instance instance) {
105 return PP_FALSE;
106}
[email protected]6570e562011-10-14 21:59:17107PPB_Fullscreen ppb_fullscreen = { &IsFullscreen };
[email protected]725c0512011-09-23 20:01:21108PPB_FlashFullscreen ppb_flash_fullscreen = { &IsFullscreen };
[email protected]912f3d6c2011-06-29 18:26:36109
110} // namespace
111
112class PPP_Instance_ProxyTest : public TwoWayTest {
113 public:
114 PPP_Instance_ProxyTest()
115 : TwoWayTest(TwoWayTest::TEST_PPP_INTERFACE) {
116 }
117};
118
[email protected]13a8f492011-07-20 19:55:39119TEST_F(PPP_Instance_ProxyTest, PPPInstance1_0) {
120 plugin().RegisterTestInterface(PPP_INSTANCE_INTERFACE_1_0, &ppp_instance_1_0);
[email protected]725c0512011-09-23 20:01:21121 host().RegisterTestInterface(PPB_FLASHFULLSCREEN_INTERFACE,
122 &ppb_flash_fullscreen);
[email protected]6570e562011-10-14 21:59:17123 host().RegisterTestInterface(PPB_FULLSCREEN_INTERFACE,
[email protected]06e0a342011-09-27 04:24:30124 &ppb_fullscreen);
[email protected]912f3d6c2011-06-29 18:26:36125
[email protected]73097562012-01-12 19:38:55126 // Grab the host-side proxy for the interface. The browser only speaks 1.1,
127 // while the proxy ensures support for the 1.0 version on the plugin side.
128 const PPP_Instance_1_1* ppp_instance = static_cast<const PPP_Instance_1_1*>(
[email protected]912f3d6c2011-06-29 18:26:36129 host().host_dispatcher()->GetProxiedInterface(
[email protected]73097562012-01-12 19:38:55130 PPP_INSTANCE_INTERFACE_1_1));
[email protected]912f3d6c2011-06-29 18:26:36131
132 // Call each function in turn, make sure we get the expected values and
133 // returns.
134 //
135 // We don't test DidDestroy, because it has the side-effect of removing the
136 // PP_Instance from the PluginDispatcher, which will cause a failure later
137 // when the test is torn down.
138 PP_Instance expected_instance = pp_instance();
139 std::vector<std::string> expected_argn, expected_argv;
140 expected_argn.push_back("Hello");
141 expected_argn.push_back("world.");
142 expected_argv.push_back("elloHay");
143 expected_argv.push_back("orldway.");
144 std::vector<const char*> argn_to_pass, argv_to_pass;
145 CHECK(expected_argn.size() == expected_argv.size());
146 for (size_t i = 0; i < expected_argn.size(); ++i) {
147 argn_to_pass.push_back(expected_argn[i].c_str());
148 argv_to_pass.push_back(expected_argv[i].c_str());
149 }
brettw669d47b12015-02-13 21:17:38150 uint32_t expected_argc = static_cast<uint32_t>(expected_argn.size());
[email protected]912f3d6c2011-06-29 18:26:36151 bool_to_return = PP_TRUE;
152 ResetReceived();
[email protected]73097562012-01-12 19:38:55153 // Tell the host resource tracker about the instance.
154 host().resource_tracker().DidCreateInstance(expected_instance);
[email protected]912f3d6c2011-06-29 18:26:36155 EXPECT_EQ(bool_to_return, ppp_instance->DidCreate(expected_instance,
156 expected_argc,
157 &argn_to_pass[0],
158 &argv_to_pass[0]));
159 EXPECT_EQ(received_instance, expected_instance);
160 EXPECT_EQ(received_argc, expected_argc);
161 EXPECT_EQ(received_argn, expected_argn);
162 EXPECT_EQ(received_argv, expected_argv);
163
164 PP_Rect expected_position = { {1, 2}, {3, 4} };
165 PP_Rect expected_clip = { {5, 6}, {7, 8} };
[email protected]73097562012-01-12 19:38:55166 ViewData data;
167 data.rect = expected_position;
168 data.is_fullscreen = false;
169 data.is_page_visible = true;
170 data.clip_rect = expected_clip;
[email protected]14109302012-11-13 07:18:53171 data.device_scale = 1.0f;
[email protected]912f3d6c2011-06-29 18:26:36172 ResetReceived();
[email protected]b73c6592013-03-30 17:08:13173 LockingResourceReleaser view_resource(
[email protected]00d320a2012-02-14 00:27:04174 (new PPB_View_Shared(OBJECT_IS_IMPL,
[email protected]73097562012-01-12 19:38:55175 expected_instance, data))->GetReference());
[email protected]b73c6592013-03-30 17:08:13176 ppp_instance->DidChangeView(expected_instance, view_resource.get());
[email protected]912f3d6c2011-06-29 18:26:36177 did_change_view_called.Wait();
178 EXPECT_EQ(received_instance, expected_instance);
179 EXPECT_EQ(received_position.point.x, expected_position.point.x);
180 EXPECT_EQ(received_position.point.y, expected_position.point.y);
181 EXPECT_EQ(received_position.size.width, expected_position.size.width);
182 EXPECT_EQ(received_position.size.height, expected_position.size.height);
183 EXPECT_EQ(received_clip.point.x, expected_clip.point.x);
184 EXPECT_EQ(received_clip.point.y, expected_clip.point.y);
185 EXPECT_EQ(received_clip.size.width, expected_clip.size.width);
186 EXPECT_EQ(received_clip.size.height, expected_clip.size.height);
187
188 PP_Bool expected_has_focus = PP_TRUE;
189 ResetReceived();
190 ppp_instance->DidChangeFocus(expected_instance, expected_has_focus);
191 did_change_focus_called.Wait();
192 EXPECT_EQ(received_instance, expected_instance);
193 EXPECT_EQ(received_has_focus, expected_has_focus);
194
[email protected]912f3d6c2011-06-29 18:26:36195 // TODO(dmichael): Need to mock out a resource Tracker to be able to test
196 // HandleResourceLoad. It also requires
197 // PPB_Core.AddRefResource and for PPB_URLLoader to be
198 // registered.
[email protected]73097562012-01-12 19:38:55199
200 host().resource_tracker().DidDeleteInstance(expected_instance);
[email protected]912f3d6c2011-06-29 18:26:36201}
202
203} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02204} // namespace ppapi