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