[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 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" |
[email protected] | f2ebdce | 2011-07-07 20:48:49 | [diff] [blame] | 7 | #include "ppapi/c/pp_var.h" |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 8 | #include "ppapi/c/ppb_core.h" |
[email protected] | 6570e56 | 2011-10-14 21:59:17 | [diff] [blame] | 9 | #include "ppapi/c/ppb_fullscreen.h" |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 10 | #include "ppapi/c/ppb_url_loader.h" |
| 11 | #include "ppapi/c/ppp_instance.h" |
[email protected] | 725c051 | 2011-09-23 20:01:21 | [diff] [blame] | 12 | #include "ppapi/c/private/ppb_flash_fullscreen.h" |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 13 | #include "ppapi/proxy/ppapi_messages.h" |
| 14 | #include "ppapi/proxy/ppapi_proxy_test.h" |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 15 | #include "ppapi/shared_impl/ppb_view_shared.h" |
| 16 | #include "ppapi/shared_impl/scoped_pp_resource.h" |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 17 | |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 18 | namespace ppapi { |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 19 | namespace proxy { |
| 20 | |
| 21 | namespace { |
| 22 | // This is a poor man's mock of PPP_Instance using global variables. Eventually |
| 23 | // we should generalize making PPAPI interface mocks by using IDL or macro/ |
| 24 | // template magic. |
| 25 | PP_Instance received_instance; |
| 26 | uint32_t received_argc; |
| 27 | std::vector<std::string> received_argn; |
| 28 | std::vector<std::string> received_argv; |
| 29 | PP_Bool bool_to_return; |
| 30 | PP_Bool DidCreate(PP_Instance instance, uint32_t argc, const char* argn[], |
| 31 | const char* argv[]) { |
| 32 | received_instance = instance; |
| 33 | received_argc = argc; |
| 34 | received_argn.clear(); |
| 35 | received_argn.insert(received_argn.begin(), argn, argn + argc); |
| 36 | received_argv.clear(); |
| 37 | received_argv.insert(received_argv.begin(), argv, argv + argc); |
| 38 | return bool_to_return; |
| 39 | } |
| 40 | |
| 41 | void DidDestroy(PP_Instance instance) { |
| 42 | received_instance = instance; |
| 43 | } |
| 44 | |
| 45 | PP_Rect received_position; |
| 46 | PP_Rect received_clip; |
| 47 | // DidChangeView is asynchronous. We wait until the call has completed before |
| 48 | // proceeding on to the next test. |
| 49 | base::WaitableEvent did_change_view_called(false, false); |
| 50 | void DidChangeView(PP_Instance instance, const PP_Rect* position, |
| 51 | const PP_Rect* clip) { |
| 52 | received_instance = instance; |
| 53 | received_position = *position; |
| 54 | received_clip = *clip; |
| 55 | did_change_view_called.Signal(); |
| 56 | } |
| 57 | |
| 58 | PP_Bool received_has_focus; |
| 59 | base::WaitableEvent did_change_focus_called(false, false); |
| 60 | void DidChangeFocus(PP_Instance instance, PP_Bool has_focus) { |
| 61 | received_instance = instance; |
| 62 | received_has_focus = has_focus; |
| 63 | did_change_focus_called.Signal(); |
| 64 | } |
| 65 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 66 | PP_Bool HandleDocumentLoad(PP_Instance instance, PP_Resource url_loader) { |
| 67 | // This one requires use of the PPB_URLLoader proxy and PPB_Core, plus a |
| 68 | // resource tracker for the url_loader resource. |
| 69 | // TODO(dmichael): Mock those out and test this function. |
| 70 | NOTREACHED(); |
| 71 | return PP_FALSE; |
| 72 | } |
| 73 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 74 | // Clear all the 'received' values for our mock. Call this before you expect |
| 75 | // one of the functions to be invoked. TODO(dmichael): It would be better to |
| 76 | // have a flag also for each function, so we know the right one got called. |
| 77 | void ResetReceived() { |
| 78 | received_instance = 0; |
| 79 | received_argc = 0; |
| 80 | received_argn.clear(); |
| 81 | received_argv.clear(); |
| 82 | memset(&received_position, 0, sizeof(received_position)); |
| 83 | memset(&received_clip, 0, sizeof(received_clip)); |
| 84 | received_has_focus = PP_FALSE; |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 85 | } |
| 86 | |
[email protected] | 13a8f49 | 2011-07-20 19:55:39 | [diff] [blame] | 87 | PPP_Instance_1_0 ppp_instance_1_0 = { |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 88 | &DidCreate, |
| 89 | &DidDestroy, |
| 90 | &DidChangeView, |
| 91 | &DidChangeFocus, |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 92 | &HandleDocumentLoad |
| 93 | }; |
| 94 | |
[email protected] | 06e0a34 | 2011-09-27 04:24:30 | [diff] [blame] | 95 | // PPP_Instance_Proxy::DidChangeView relies on PPB_(Flash)Fullscreen being |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 96 | // available with a valid implementation of IsFullScreen, so we mock it. |
| 97 | PP_Bool IsFullscreen(PP_Instance instance) { |
| 98 | return PP_FALSE; |
| 99 | } |
[email protected] | 6570e56 | 2011-10-14 21:59:17 | [diff] [blame] | 100 | PPB_Fullscreen ppb_fullscreen = { &IsFullscreen }; |
[email protected] | 725c051 | 2011-09-23 20:01:21 | [diff] [blame] | 101 | PPB_FlashFullscreen ppb_flash_fullscreen = { &IsFullscreen }; |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 102 | |
| 103 | } // namespace |
| 104 | |
| 105 | class PPP_Instance_ProxyTest : public TwoWayTest { |
| 106 | public: |
| 107 | PPP_Instance_ProxyTest() |
| 108 | : TwoWayTest(TwoWayTest::TEST_PPP_INTERFACE) { |
| 109 | } |
| 110 | }; |
| 111 | |
[email protected] | 13a8f49 | 2011-07-20 19:55:39 | [diff] [blame] | 112 | TEST_F(PPP_Instance_ProxyTest, PPPInstance1_0) { |
| 113 | plugin().RegisterTestInterface(PPP_INSTANCE_INTERFACE_1_0, &ppp_instance_1_0); |
[email protected] | 725c051 | 2011-09-23 20:01:21 | [diff] [blame] | 114 | host().RegisterTestInterface(PPB_FLASHFULLSCREEN_INTERFACE, |
| 115 | &ppb_flash_fullscreen); |
[email protected] | 6570e56 | 2011-10-14 21:59:17 | [diff] [blame] | 116 | host().RegisterTestInterface(PPB_FULLSCREEN_INTERFACE, |
[email protected] | 06e0a34 | 2011-09-27 04:24:30 | [diff] [blame] | 117 | &ppb_fullscreen); |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 118 | |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 119 | // Grab the host-side proxy for the interface. The browser only speaks 1.1, |
| 120 | // while the proxy ensures support for the 1.0 version on the plugin side. |
| 121 | const PPP_Instance_1_1* ppp_instance = static_cast<const PPP_Instance_1_1*>( |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 122 | host().host_dispatcher()->GetProxiedInterface( |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 123 | PPP_INSTANCE_INTERFACE_1_1)); |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 124 | |
| 125 | // Call each function in turn, make sure we get the expected values and |
| 126 | // returns. |
| 127 | // |
| 128 | // We don't test DidDestroy, because it has the side-effect of removing the |
| 129 | // PP_Instance from the PluginDispatcher, which will cause a failure later |
| 130 | // when the test is torn down. |
| 131 | PP_Instance expected_instance = pp_instance(); |
| 132 | std::vector<std::string> expected_argn, expected_argv; |
| 133 | expected_argn.push_back("Hello"); |
| 134 | expected_argn.push_back("world."); |
| 135 | expected_argv.push_back("elloHay"); |
| 136 | expected_argv.push_back("orldway."); |
| 137 | std::vector<const char*> argn_to_pass, argv_to_pass; |
| 138 | CHECK(expected_argn.size() == expected_argv.size()); |
| 139 | for (size_t i = 0; i < expected_argn.size(); ++i) { |
| 140 | argn_to_pass.push_back(expected_argn[i].c_str()); |
| 141 | argv_to_pass.push_back(expected_argv[i].c_str()); |
| 142 | } |
| 143 | uint32_t expected_argc = expected_argn.size(); |
| 144 | bool_to_return = PP_TRUE; |
| 145 | ResetReceived(); |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 146 | // Tell the host resource tracker about the instance. |
| 147 | host().resource_tracker().DidCreateInstance(expected_instance); |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 148 | EXPECT_EQ(bool_to_return, ppp_instance->DidCreate(expected_instance, |
| 149 | expected_argc, |
| 150 | &argn_to_pass[0], |
| 151 | &argv_to_pass[0])); |
| 152 | EXPECT_EQ(received_instance, expected_instance); |
| 153 | EXPECT_EQ(received_argc, expected_argc); |
| 154 | EXPECT_EQ(received_argn, expected_argn); |
| 155 | EXPECT_EQ(received_argv, expected_argv); |
| 156 | |
| 157 | PP_Rect expected_position = { {1, 2}, {3, 4} }; |
| 158 | PP_Rect expected_clip = { {5, 6}, {7, 8} }; |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 159 | ViewData data; |
| 160 | data.rect = expected_position; |
| 161 | data.is_fullscreen = false; |
| 162 | data.is_page_visible = true; |
| 163 | data.clip_rect = expected_clip; |
[email protected] | 1410930 | 2012-11-13 07:18:53 | [diff] [blame^] | 164 | data.device_scale = 1.0f; |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 165 | ResetReceived(); |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 166 | ScopedPPResource view_resource( |
| 167 | ScopedPPResource::PassRef(), |
[email protected] | 00d320a | 2012-02-14 00:27:04 | [diff] [blame] | 168 | (new PPB_View_Shared(OBJECT_IS_IMPL, |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 169 | expected_instance, data))->GetReference()); |
| 170 | ppp_instance->DidChangeView(expected_instance, view_resource); |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 171 | did_change_view_called.Wait(); |
| 172 | EXPECT_EQ(received_instance, expected_instance); |
| 173 | EXPECT_EQ(received_position.point.x, expected_position.point.x); |
| 174 | EXPECT_EQ(received_position.point.y, expected_position.point.y); |
| 175 | EXPECT_EQ(received_position.size.width, expected_position.size.width); |
| 176 | EXPECT_EQ(received_position.size.height, expected_position.size.height); |
| 177 | EXPECT_EQ(received_clip.point.x, expected_clip.point.x); |
| 178 | EXPECT_EQ(received_clip.point.y, expected_clip.point.y); |
| 179 | EXPECT_EQ(received_clip.size.width, expected_clip.size.width); |
| 180 | EXPECT_EQ(received_clip.size.height, expected_clip.size.height); |
| 181 | |
| 182 | PP_Bool expected_has_focus = PP_TRUE; |
| 183 | ResetReceived(); |
| 184 | ppp_instance->DidChangeFocus(expected_instance, expected_has_focus); |
| 185 | did_change_focus_called.Wait(); |
| 186 | EXPECT_EQ(received_instance, expected_instance); |
| 187 | EXPECT_EQ(received_has_focus, expected_has_focus); |
| 188 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 189 | // TODO(dmichael): Need to mock out a resource Tracker to be able to test |
| 190 | // HandleResourceLoad. It also requires |
| 191 | // PPB_Core.AddRefResource and for PPB_URLLoader to be |
| 192 | // registered. |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 193 | |
| 194 | host().resource_tracker().DidDeleteInstance(expected_instance); |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | } // namespace proxy |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 198 | } // namespace ppapi |