[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | f24448db | 2011-01-27 20:40:39 | [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 | |
avi | e029c413 | 2015-12-23 06:45:22 | [diff] [blame] | 5 | #include <stdint.h> |
| 6 | |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 7 | #include <map> |
dcheng | ced9224 | 2016-04-07 00:00:12 | [diff] [blame] | 8 | #include <memory> |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 9 | #include <string> |
| 10 | |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 11 | #include "base/compiler_specific.h" |
avi | e029c413 | 2015-12-23 06:45:22 | [diff] [blame] | 12 | #include "base/macros.h" |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 14 | #include "base/synchronization/waitable_event.h" |
dmichael | b11ca7b | 2015-04-02 16:59:40 | [diff] [blame] | 15 | #include "base/task_runner.h" |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 16 | #include "base/threading/simple_thread.h" |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 17 | #include "base/threading/thread.h" |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 18 | #include "ppapi/c/pp_instance.h" |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 19 | #include "ppapi/proxy/host_dispatcher.h" |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 20 | #include "ppapi/proxy/plugin_dispatcher.h" |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 21 | #include "ppapi/proxy/plugin_globals.h" |
[email protected] | 6fc87e0 | 2011-12-20 19:18:45 | [diff] [blame] | 22 | #include "ppapi/proxy/plugin_proxy_delegate.h" |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 23 | #include "ppapi/proxy/plugin_resource_tracker.h" |
| 24 | #include "ppapi/proxy/plugin_var_tracker.h" |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 25 | #include "ppapi/proxy/resource_message_test_sink.h" |
dmichael | bd96c1e | 2015-02-12 00:58:08 | [diff] [blame] | 26 | #include "ppapi/shared_impl/proxy_lock.h" |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 27 | #include "ppapi/shared_impl/test_globals.h" |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 28 | #include "testing/gtest/include/gtest/gtest.h" |
| 29 | |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 30 | namespace base { |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 31 | class RunLoop; |
skyostil | 23490d4 | 2015-06-12 12:54:26 | [diff] [blame] | 32 | class SingleThreadTaskRunner; |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 33 | } |
| 34 | |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 35 | namespace ppapi { |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 36 | namespace proxy { |
| 37 | |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 38 | class MessageLoopResource; |
| 39 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 40 | // Base class for plugin and host test harnesses. Tests will not use this |
| 41 | // directly. Instead, use the PluginProxyTest, HostProxyTest, or TwoWayTest. |
| 42 | class ProxyTestHarnessBase { |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 43 | public: |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 44 | enum GlobalsConfiguration { |
| 45 | PER_THREAD_GLOBALS, |
| 46 | SINGLETON_GLOBALS |
| 47 | }; |
| 48 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 49 | ProxyTestHarnessBase(); |
| 50 | virtual ~ProxyTestHarnessBase(); |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 51 | |
| 52 | PP_Module pp_module() const { return pp_module_; } |
| 53 | PP_Instance pp_instance() const { return pp_instance_; } |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 54 | ResourceMessageTestSink& sink() { return sink_; } |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 55 | |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 56 | virtual PpapiGlobals* GetGlobals() = 0; |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 57 | // Returns either the plugin or host dispatcher, depending on the test. |
| 58 | virtual Dispatcher* GetDispatcher() = 0; |
| 59 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 60 | // Set up the harness using an IPC::TestSink to capture messages. |
| 61 | virtual void SetUpHarness() = 0; |
| 62 | |
| 63 | // Set up the harness using a real IPC channel. |
skyostil | 23490d4 | 2015-06-12 12:54:26 | [diff] [blame] | 64 | virtual void SetUpHarnessWithChannel( |
| 65 | const IPC::ChannelHandle& channel_handle, |
| 66 | base::SingleThreadTaskRunner* ipc_task_runner, |
| 67 | base::WaitableEvent* shutdown_event, |
| 68 | bool is_client) = 0; |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 69 | |
| 70 | virtual void TearDownHarness() = 0; |
| 71 | |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 72 | // Implementation of GetInterface for the dispatcher. This will |
| 73 | // return NULL for all interfaces unless one is registered by calling |
| 74 | // RegisterTestInterface(); |
| 75 | const void* GetInterface(const char* name); |
| 76 | |
| 77 | // Allows the test to specify an interface implementation for a given |
| 78 | // interface name. This will be returned when any of the proxy logic |
| 79 | // requests a local interface. |
[email protected] | 99ff993 | 2011-09-07 14:14:54 | [diff] [blame] | 80 | void RegisterTestInterface(const char* name, const void* test_interface); |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 81 | |
| 82 | // Sends a "supports interface" message to the current dispatcher and returns |
| 83 | // true if it's supported. This is just for the convenience of tests. |
kareng | 1c62eeb | 2014-11-08 16:35:03 | [diff] [blame] | 84 | bool SupportsInterface(const char* name); |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 85 | |
| 86 | private: |
| 87 | // Destination for IPC messages sent by the test. |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 88 | ResourceMessageTestSink sink_; |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 89 | |
| 90 | // The module and instance ID associated with the plugin dispatcher. |
| 91 | PP_Module pp_module_; |
| 92 | PP_Instance pp_instance_; |
| 93 | |
| 94 | // Stores the data for GetInterface/RegisterTestInterface. |
| 95 | std::map<std::string, const void*> registered_interfaces_; |
| 96 | }; |
| 97 | |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 98 | // Test harness for the plugin side of the proxy. |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 99 | class PluginProxyTestHarness : public ProxyTestHarnessBase { |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 100 | public: |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 101 | explicit PluginProxyTestHarness(GlobalsConfiguration globals_config); |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 102 | virtual ~PluginProxyTestHarness(); |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 103 | |
| 104 | PluginDispatcher* plugin_dispatcher() { return plugin_dispatcher_.get(); } |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 105 | PluginResourceTracker& resource_tracker() { |
[email protected] | d57fd38 | 2012-09-19 00:55:42 | [diff] [blame] | 106 | return *plugin_globals_->plugin_resource_tracker(); |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 107 | } |
| 108 | PluginVarTracker& var_tracker() { |
[email protected] | d57fd38 | 2012-09-19 00:55:42 | [diff] [blame] | 109 | return *plugin_globals_->plugin_var_tracker(); |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 110 | } |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 111 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 112 | // ProxyTestHarnessBase implementation. |
[email protected] | d57fd38 | 2012-09-19 00:55:42 | [diff] [blame] | 113 | virtual PpapiGlobals* GetGlobals(); |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 114 | virtual Dispatcher* GetDispatcher(); |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 115 | virtual void SetUpHarness(); |
skyostil | 23490d4 | 2015-06-12 12:54:26 | [diff] [blame] | 116 | virtual void SetUpHarnessWithChannel( |
| 117 | const IPC::ChannelHandle& channel_handle, |
| 118 | base::SingleThreadTaskRunner* ipc_task_runner, |
| 119 | base::WaitableEvent* shutdown_event, |
| 120 | bool is_client); |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 121 | virtual void TearDownHarness(); |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 122 | |
[email protected] | 6fc87e0 | 2011-12-20 19:18:45 | [diff] [blame] | 123 | class PluginDelegateMock : public PluginDispatcher::PluginDelegate, |
| 124 | public PluginProxyDelegate { |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 125 | public: |
skyostil | 23490d4 | 2015-06-12 12:54:26 | [diff] [blame] | 126 | PluginDelegateMock() : ipc_task_runner_(NULL), shutdown_event_() {} |
nick | b05b81d | 2015-04-23 14:00:50 | [diff] [blame] | 127 | ~PluginDelegateMock() override {} |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 128 | |
skyostil | 23490d4 | 2015-06-12 12:54:26 | [diff] [blame] | 129 | void Init(base::SingleThreadTaskRunner* ipc_task_runner, |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 130 | base::WaitableEvent* shutdown_event) { |
skyostil | 23490d4 | 2015-06-12 12:54:26 | [diff] [blame] | 131 | ipc_task_runner_ = ipc_task_runner; |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 132 | shutdown_event_ = shutdown_event; |
| 133 | } |
| 134 | |
[email protected] | d57fd38 | 2012-09-19 00:55:42 | [diff] [blame] | 135 | void set_browser_sender(IPC::Sender* browser_sender) { |
| 136 | browser_sender_ = browser_sender; |
| 137 | } |
| 138 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 139 | // ProxyChannel::Delegate implementation. |
skyostil | 12262cf | 2015-05-21 14:49:31 | [diff] [blame] | 140 | base::SingleThreadTaskRunner* GetIPCTaskRunner() override; |
nick | b05b81d | 2015-04-23 14:00:50 | [diff] [blame] | 141 | base::WaitableEvent* GetShutdownEvent() override; |
| 142 | IPC::PlatformFileForTransit ShareHandleWithRemote( |
[email protected] | f0ecb55 | 2012-05-11 22:09:11 | [diff] [blame] | 143 | base::PlatformFile handle, |
[email protected] | 108fd34 | 2013-01-04 20:46:54 | [diff] [blame] | 144 | base::ProcessId remote_pid, |
mostynb | 699af3c | 2014-10-06 18:03:34 | [diff] [blame] | 145 | bool should_close_source) override; |
erikchen | 4fc32d5 | 2015-06-02 02:16:38 | [diff] [blame] | 146 | base::SharedMemoryHandle ShareSharedMemoryHandleWithRemote( |
| 147 | const base::SharedMemoryHandle& handle, |
| 148 | base::ProcessId remote_pid) override; |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 149 | |
| 150 | // PluginDispatcher::PluginDelegate implementation. |
nick | b05b81d | 2015-04-23 14:00:50 | [diff] [blame] | 151 | std::set<PP_Instance>* GetGloballySeenInstanceIDSet() override; |
avi | e029c413 | 2015-12-23 06:45:22 | [diff] [blame] | 152 | uint32_t Register(PluginDispatcher* plugin_dispatcher) override; |
| 153 | void Unregister(uint32_t plugin_dispatcher_id) override; |
[email protected] | 6fc87e0 | 2011-12-20 19:18:45 | [diff] [blame] | 154 | |
[email protected] | d57fd38 | 2012-09-19 00:55:42 | [diff] [blame] | 155 | // PluginProxyDelegate implementation. |
nick | b05b81d | 2015-04-23 14:00:50 | [diff] [blame] | 156 | IPC::Sender* GetBrowserSender() override; |
| 157 | std::string GetUILanguage() override; |
mgiuca | 8ca5918 | 2015-07-08 02:10:21 | [diff] [blame] | 158 | void PreCacheFontForFlash(const void* logfontw) override; |
nick | b05b81d | 2015-04-23 14:00:50 | [diff] [blame] | 159 | void SetActiveURL(const std::string& url) override; |
| 160 | PP_Resource CreateBrowserFont( |
[email protected] | 1d14806 | 2013-07-25 20:25:45 | [diff] [blame] | 161 | Connection connection, |
| 162 | PP_Instance instance, |
| 163 | const PP_BrowserFont_Trusted_Description& desc, |
mostynb | 699af3c | 2014-10-06 18:03:34 | [diff] [blame] | 164 | const Preferences& prefs) override; |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 165 | |
| 166 | private: |
skyostil | 23490d4 | 2015-06-12 12:54:26 | [diff] [blame] | 167 | base::SingleThreadTaskRunner* ipc_task_runner_; // Weak |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 168 | base::WaitableEvent* shutdown_event_; // Weak |
| 169 | std::set<PP_Instance> instance_id_set_; |
[email protected] | d57fd38 | 2012-09-19 00:55:42 | [diff] [blame] | 170 | IPC::Sender* browser_sender_; |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 171 | |
| 172 | DISALLOW_COPY_AND_ASSIGN(PluginDelegateMock); |
| 173 | }; |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 174 | |
| 175 | private: |
dmichael | b11ca7b | 2015-04-02 16:59:40 | [diff] [blame] | 176 | void CreatePluginGlobals( |
| 177 | const scoped_refptr<base::TaskRunner>& ipc_task_runner); |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 178 | |
| 179 | GlobalsConfiguration globals_config_; |
dcheng | ced9224 | 2016-04-07 00:00:12 | [diff] [blame] | 180 | std::unique_ptr<PluginGlobals> plugin_globals_; |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 181 | |
dcheng | ced9224 | 2016-04-07 00:00:12 | [diff] [blame] | 182 | std::unique_ptr<PluginDispatcher> plugin_dispatcher_; |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 183 | PluginDelegateMock plugin_delegate_mock_; |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 184 | }; |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 185 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 186 | class PluginProxyTest : public PluginProxyTestHarness, public testing::Test { |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 187 | public: |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 188 | PluginProxyTest(); |
| 189 | virtual ~PluginProxyTest(); |
| 190 | |
| 191 | // testing::Test implementation. |
| 192 | virtual void SetUp(); |
| 193 | virtual void TearDown(); |
[email protected] | 657f4ef | 2011-07-14 16:39:23 | [diff] [blame] | 194 | private: |
[email protected] | d2881d8 | 2013-05-06 19:23:08 | [diff] [blame] | 195 | base::MessageLoop message_loop_; |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 196 | }; |
| 197 | |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 198 | // This class provides support for multi-thread testing. A secondary thread is |
| 199 | // created with a Pepper message loop. |
| 200 | // Subclasses need to implement the two SetUpTestOn*Thread() methods to do the |
| 201 | // actual testing work; and call both PostQuitFor*Thread() when testing is |
| 202 | // done. |
| 203 | class PluginProxyMultiThreadTest |
| 204 | : public PluginProxyTest, |
| 205 | public base::DelegateSimpleThread::Delegate { |
| 206 | public: |
| 207 | PluginProxyMultiThreadTest(); |
nick | b05b81d | 2015-04-23 14:00:50 | [diff] [blame] | 208 | ~PluginProxyMultiThreadTest() override; |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 209 | |
| 210 | // Called before the secondary thread is started, but after all the member |
| 211 | // variables, including |secondary_thread_| and |
| 212 | // |secondary_thread_message_loop_|, are initialized. |
| 213 | virtual void SetUpTestOnMainThread() = 0; |
| 214 | |
| 215 | virtual void SetUpTestOnSecondaryThread() = 0; |
| 216 | |
| 217 | // TEST_F() should call this method. |
| 218 | void RunTest(); |
| 219 | |
| 220 | enum ThreadType { |
| 221 | MAIN_THREAD, |
| 222 | SECONDARY_THREAD |
| 223 | }; |
| 224 | void CheckOnThread(ThreadType thread_type); |
| 225 | |
| 226 | // These can be called on any thread. |
| 227 | void PostQuitForMainThread(); |
| 228 | void PostQuitForSecondaryThread(); |
| 229 | |
| 230 | protected: |
| 231 | scoped_refptr<MessageLoopResource> secondary_thread_message_loop_; |
skyostil | 23490d4 | 2015-06-12 12:54:26 | [diff] [blame] | 232 | scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 233 | |
| 234 | private: |
| 235 | // base::DelegateSimpleThread::Delegate implementation. |
nick | b05b81d | 2015-04-23 14:00:50 | [diff] [blame] | 236 | void Run() override; |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 237 | |
| 238 | void QuitNestedLoop(); |
| 239 | |
| 240 | static void InternalSetUpTestOnSecondaryThread(void* user_data, |
| 241 | int32_t result); |
| 242 | |
dcheng | ced9224 | 2016-04-07 00:00:12 | [diff] [blame] | 243 | std::unique_ptr<base::DelegateSimpleThread> secondary_thread_; |
| 244 | std::unique_ptr<base::RunLoop> nested_main_thread_message_loop_; |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 245 | }; |
| 246 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 247 | class HostProxyTestHarness : public ProxyTestHarnessBase { |
| 248 | public: |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 249 | explicit HostProxyTestHarness(GlobalsConfiguration globals_config); |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 250 | virtual ~HostProxyTestHarness(); |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 251 | |
| 252 | HostDispatcher* host_dispatcher() { return host_dispatcher_.get(); } |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 253 | ResourceTracker& resource_tracker() { |
[email protected] | d57fd38 | 2012-09-19 00:55:42 | [diff] [blame] | 254 | return *host_globals_->GetResourceTracker(); |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 255 | } |
| 256 | VarTracker& var_tracker() { |
[email protected] | d57fd38 | 2012-09-19 00:55:42 | [diff] [blame] | 257 | return *host_globals_->GetVarTracker(); |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 258 | } |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 259 | |
| 260 | // ProxyTestBase implementation. |
[email protected] | d57fd38 | 2012-09-19 00:55:42 | [diff] [blame] | 261 | virtual PpapiGlobals* GetGlobals(); |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 262 | virtual Dispatcher* GetDispatcher(); |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 263 | virtual void SetUpHarness(); |
skyostil | 23490d4 | 2015-06-12 12:54:26 | [diff] [blame] | 264 | virtual void SetUpHarnessWithChannel( |
| 265 | const IPC::ChannelHandle& channel_handle, |
| 266 | base::SingleThreadTaskRunner* ipc_task_runner, |
| 267 | base::WaitableEvent* shutdown_event, |
| 268 | bool is_client); |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 269 | virtual void TearDownHarness(); |
| 270 | |
| 271 | class DelegateMock : public ProxyChannel::Delegate { |
| 272 | public: |
skyostil | 23490d4 | 2015-06-12 12:54:26 | [diff] [blame] | 273 | DelegateMock() : ipc_task_runner_(NULL), shutdown_event_(NULL) {} |
nick | b05b81d | 2015-04-23 14:00:50 | [diff] [blame] | 274 | ~DelegateMock() override {} |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 275 | |
skyostil | 23490d4 | 2015-06-12 12:54:26 | [diff] [blame] | 276 | void Init(base::SingleThreadTaskRunner* ipc_task_runner, |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 277 | base::WaitableEvent* shutdown_event) { |
skyostil | 23490d4 | 2015-06-12 12:54:26 | [diff] [blame] | 278 | ipc_task_runner_ = ipc_task_runner; |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 279 | shutdown_event_ = shutdown_event; |
| 280 | } |
| 281 | |
| 282 | // ProxyChannel::Delegate implementation. |
skyostil | 23490d4 | 2015-06-12 12:54:26 | [diff] [blame] | 283 | base::SingleThreadTaskRunner* GetIPCTaskRunner() override; |
nick | b05b81d | 2015-04-23 14:00:50 | [diff] [blame] | 284 | base::WaitableEvent* GetShutdownEvent() override; |
| 285 | IPC::PlatformFileForTransit ShareHandleWithRemote( |
[email protected] | f0ecb55 | 2012-05-11 22:09:11 | [diff] [blame] | 286 | base::PlatformFile handle, |
[email protected] | 108fd34 | 2013-01-04 20:46:54 | [diff] [blame] | 287 | base::ProcessId remote_pid, |
mostynb | 699af3c | 2014-10-06 18:03:34 | [diff] [blame] | 288 | bool should_close_source) override; |
erikchen | 4fc32d5 | 2015-06-02 02:16:38 | [diff] [blame] | 289 | base::SharedMemoryHandle ShareSharedMemoryHandleWithRemote( |
| 290 | const base::SharedMemoryHandle& handle, |
| 291 | base::ProcessId remote_pid) override; |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 292 | |
| 293 | private: |
skyostil | 23490d4 | 2015-06-12 12:54:26 | [diff] [blame] | 294 | base::SingleThreadTaskRunner* ipc_task_runner_; // Weak |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 295 | base::WaitableEvent* shutdown_event_; // Weak |
| 296 | |
| 297 | DISALLOW_COPY_AND_ASSIGN(DelegateMock); |
| 298 | }; |
| 299 | |
| 300 | private: |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 301 | void CreateHostGlobals(); |
| 302 | |
| 303 | GlobalsConfiguration globals_config_; |
dcheng | ced9224 | 2016-04-07 00:00:12 | [diff] [blame] | 304 | std::unique_ptr<ppapi::TestGlobals> host_globals_; |
| 305 | std::unique_ptr<HostDispatcher> host_dispatcher_; |
dmichael | bd96c1e | 2015-02-12 00:58:08 | [diff] [blame] | 306 | // The host side of the real proxy doesn't lock, so this disables locking for |
| 307 | // the thread the host side of the test runs on. |
dcheng | ced9224 | 2016-04-07 00:00:12 | [diff] [blame] | 308 | std::unique_ptr<ProxyLock::LockingDisablerForTest> disable_locking_; |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 309 | DelegateMock delegate_mock_; |
| 310 | }; |
| 311 | |
| 312 | class HostProxyTest : public HostProxyTestHarness, public testing::Test { |
| 313 | public: |
| 314 | HostProxyTest(); |
| 315 | virtual ~HostProxyTest(); |
| 316 | |
| 317 | // testing::Test implementation. |
| 318 | virtual void SetUp(); |
| 319 | virtual void TearDown(); |
[email protected] | 657f4ef | 2011-07-14 16:39:23 | [diff] [blame] | 320 | private: |
[email protected] | d2881d8 | 2013-05-06 19:23:08 | [diff] [blame] | 321 | base::MessageLoop message_loop_; |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 322 | }; |
| 323 | |
| 324 | // Use this base class to test both sides of a proxy. |
| 325 | class TwoWayTest : public testing::Test { |
| 326 | public: |
| 327 | enum TwoWayTestMode { |
| 328 | TEST_PPP_INTERFACE, |
| 329 | TEST_PPB_INTERFACE |
| 330 | }; |
| 331 | TwoWayTest(TwoWayTestMode test_mode); |
| 332 | virtual ~TwoWayTest(); |
| 333 | |
| 334 | HostProxyTestHarness& host() { return host_; } |
| 335 | PluginProxyTestHarness& plugin() { return plugin_; } |
| 336 | PP_Module pp_module() const { return host_.pp_module(); } |
| 337 | PP_Instance pp_instance() const { return host_.pp_instance(); } |
| 338 | TwoWayTestMode test_mode() { return test_mode_; } |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 339 | |
| 340 | // testing::Test implementation. |
| 341 | virtual void SetUp(); |
| 342 | virtual void TearDown(); |
| 343 | |
[email protected] | d57fd38 | 2012-09-19 00:55:42 | [diff] [blame] | 344 | protected: |
| 345 | // Post a task to the thread where the remote harness lives. This |
| 346 | // is typically used to test the state of the var tracker on the plugin |
| 347 | // thread. This runs the task synchronously for convenience. |
| 348 | void PostTaskOnRemoteHarness(const base::Closure& task); |
| 349 | |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 350 | private: |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 351 | TwoWayTestMode test_mode_; |
| 352 | HostProxyTestHarness host_; |
| 353 | PluginProxyTestHarness plugin_; |
| 354 | // In order to use sync IPC, we need to have an IO thread. |
| 355 | base::Thread io_thread_; |
| 356 | // The plugin side of the proxy runs on its own thread. |
| 357 | base::Thread plugin_thread_; |
| 358 | // The message loop for the main (host) thread. |
[email protected] | d2881d8 | 2013-05-06 19:23:08 | [diff] [blame] | 359 | base::MessageLoop message_loop_; |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 360 | |
| 361 | // Aliases for the host and plugin harnesses; if we're testing a PPP |
| 362 | // interface, remote_harness will point to plugin_, and local_harness |
| 363 | // will point to host_. This makes it convenient when we're starting and |
| 364 | // stopping the harnesses. |
| 365 | ProxyTestHarnessBase* remote_harness_; |
| 366 | ProxyTestHarnessBase* local_harness_; |
| 367 | |
| 368 | base::WaitableEvent channel_created_; |
| 369 | base::WaitableEvent shutdown_event_; |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 370 | }; |
| 371 | |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 372 | // Used during Gtests when you have a PP_Var that you want to EXPECT is equal |
| 373 | // to a certain constant string value: |
| 374 | // |
| 375 | // EXPECT_VAR_IS_STRING("foo", my_var); |
| 376 | #define EXPECT_VAR_IS_STRING(str, var) { \ |
| 377 | StringVar* sv = StringVar::FromPPVar(var); \ |
| 378 | EXPECT_TRUE(sv); \ |
| 379 | if (sv) \ |
| 380 | EXPECT_EQ(str, sv->value()); \ |
| 381 | } |
| 382 | |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 383 | } // namespace proxy |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 384 | } // namespace ppapi |