1 // Copyright (c) 2012 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.
8 #include "base/compiler_specific.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/synchronization/waitable_event.h"
12 #include "base/task_runner.h"
13 #include "base/threading/simple_thread.h"
14 #include "base/threading/thread.h"
15 #include "ppapi/c/pp_instance.h"
16 #include "ppapi/proxy/host_dispatcher.h"
17 #include "ppapi/proxy/plugin_dispatcher.h"
18 #include "ppapi/proxy/plugin_globals.h"
19 #include "ppapi/proxy/plugin_proxy_delegate.h"
20 #include "ppapi/proxy/plugin_resource_tracker.h"
21 #include "ppapi/proxy/plugin_var_tracker.h"
22 #include "ppapi/proxy/resource_message_test_sink.h"
23 #include "ppapi/shared_impl/proxy_lock.h"
24 #include "ppapi/shared_impl/test_globals.h"
25 #include "testing/gtest/include/gtest/gtest.h"
29 class SingleThreadTaskRunner
;
35 class MessageLoopResource
;
37 // Base class for plugin and host test harnesses. Tests will not use this
38 // directly. Instead, use the PluginProxyTest, HostProxyTest, or TwoWayTest.
39 class ProxyTestHarnessBase
{
41 enum GlobalsConfiguration
{
46 ProxyTestHarnessBase();
47 virtual ~ProxyTestHarnessBase();
49 PP_Module
pp_module() const { return pp_module_
; }
50 PP_Instance
pp_instance() const { return pp_instance_
; }
51 ResourceMessageTestSink
& sink() { return sink_
; }
53 virtual PpapiGlobals
* GetGlobals() = 0;
54 // Returns either the plugin or host dispatcher, depending on the test.
55 virtual Dispatcher
* GetDispatcher() = 0;
57 // Set up the harness using an IPC::TestSink to capture messages.
58 virtual void SetUpHarness() = 0;
60 // Set up the harness using a real IPC channel.
61 virtual void SetUpHarnessWithChannel(
62 const IPC::ChannelHandle
& channel_handle
,
63 base::SingleThreadTaskRunner
* ipc_task_runner
,
64 base::WaitableEvent
* shutdown_event
,
67 virtual void TearDownHarness() = 0;
69 // Implementation of GetInterface for the dispatcher. This will
70 // return NULL for all interfaces unless one is registered by calling
71 // RegisterTestInterface();
72 const void* GetInterface(const char* name
);
74 // Allows the test to specify an interface implementation for a given
75 // interface name. This will be returned when any of the proxy logic
76 // requests a local interface.
77 void RegisterTestInterface(const char* name
, const void* test_interface
);
79 // Sends a "supports interface" message to the current dispatcher and returns
80 // true if it's supported. This is just for the convenience of tests.
81 bool SupportsInterface(const char* name
);
84 // Destination for IPC messages sent by the test.
85 ResourceMessageTestSink sink_
;
87 // The module and instance ID associated with the plugin dispatcher.
89 PP_Instance pp_instance_
;
91 // Stores the data for GetInterface/RegisterTestInterface.
92 std::map
<std::string
, const void*> registered_interfaces_
;
95 // Test harness for the plugin side of the proxy.
96 class PluginProxyTestHarness
: public ProxyTestHarnessBase
{
98 explicit PluginProxyTestHarness(GlobalsConfiguration globals_config
);
99 virtual ~PluginProxyTestHarness();
101 PluginDispatcher
* plugin_dispatcher() { return plugin_dispatcher_
.get(); }
102 PluginResourceTracker
& resource_tracker() {
103 return *plugin_globals_
->plugin_resource_tracker();
105 PluginVarTracker
& var_tracker() {
106 return *plugin_globals_
->plugin_var_tracker();
109 // ProxyTestHarnessBase implementation.
110 virtual PpapiGlobals
* GetGlobals();
111 virtual Dispatcher
* GetDispatcher();
112 virtual void SetUpHarness();
113 virtual void SetUpHarnessWithChannel(
114 const IPC::ChannelHandle
& channel_handle
,
115 base::SingleThreadTaskRunner
* ipc_task_runner
,
116 base::WaitableEvent
* shutdown_event
,
118 virtual void TearDownHarness();
120 class PluginDelegateMock
: public PluginDispatcher::PluginDelegate
,
121 public PluginProxyDelegate
{
123 PluginDelegateMock() : ipc_task_runner_(NULL
), shutdown_event_() {}
124 ~PluginDelegateMock() override
{}
126 void Init(base::SingleThreadTaskRunner
* ipc_task_runner
,
127 base::WaitableEvent
* shutdown_event
) {
128 ipc_task_runner_
= ipc_task_runner
;
129 shutdown_event_
= shutdown_event
;
132 void set_browser_sender(IPC::Sender
* browser_sender
) {
133 browser_sender_
= browser_sender
;
136 // ProxyChannel::Delegate implementation.
137 base::SingleThreadTaskRunner
* GetIPCTaskRunner() override
;
138 base::WaitableEvent
* GetShutdownEvent() override
;
139 IPC::PlatformFileForTransit
ShareHandleWithRemote(
140 base::PlatformFile handle
,
141 base::ProcessId remote_pid
,
142 bool should_close_source
) override
;
143 base::SharedMemoryHandle
ShareSharedMemoryHandleWithRemote(
144 const base::SharedMemoryHandle
& handle
,
145 base::ProcessId remote_pid
) override
;
147 // PluginDispatcher::PluginDelegate implementation.
148 std::set
<PP_Instance
>* GetGloballySeenInstanceIDSet() override
;
149 uint32
Register(PluginDispatcher
* plugin_dispatcher
) override
;
150 void Unregister(uint32 plugin_dispatcher_id
) override
;
152 // PluginProxyDelegate implementation.
153 IPC::Sender
* GetBrowserSender() override
;
154 std::string
GetUILanguage() override
;
155 void PreCacheFontForFlash(const void* logfontw
) override
;
156 void SetActiveURL(const std::string
& url
) override
;
157 PP_Resource
CreateBrowserFont(
158 Connection connection
,
159 PP_Instance instance
,
160 const PP_BrowserFont_Trusted_Description
& desc
,
161 const Preferences
& prefs
) override
;
164 base::SingleThreadTaskRunner
* ipc_task_runner_
; // Weak
165 base::WaitableEvent
* shutdown_event_
; // Weak
166 std::set
<PP_Instance
> instance_id_set_
;
167 IPC::Sender
* browser_sender_
;
169 DISALLOW_COPY_AND_ASSIGN(PluginDelegateMock
);
173 void CreatePluginGlobals(
174 const scoped_refptr
<base::TaskRunner
>& ipc_task_runner
);
176 GlobalsConfiguration globals_config_
;
177 scoped_ptr
<PluginGlobals
> plugin_globals_
;
179 scoped_ptr
<PluginDispatcher
> plugin_dispatcher_
;
180 PluginDelegateMock plugin_delegate_mock_
;
183 class PluginProxyTest
: public PluginProxyTestHarness
, public testing::Test
{
186 virtual ~PluginProxyTest();
188 // testing::Test implementation.
189 virtual void SetUp();
190 virtual void TearDown();
192 base::MessageLoop message_loop_
;
195 // This class provides support for multi-thread testing. A secondary thread is
196 // created with a Pepper message loop.
197 // Subclasses need to implement the two SetUpTestOn*Thread() methods to do the
198 // actual testing work; and call both PostQuitFor*Thread() when testing is
200 class PluginProxyMultiThreadTest
201 : public PluginProxyTest
,
202 public base::DelegateSimpleThread::Delegate
{
204 PluginProxyMultiThreadTest();
205 ~PluginProxyMultiThreadTest() override
;
207 // Called before the secondary thread is started, but after all the member
208 // variables, including |secondary_thread_| and
209 // |secondary_thread_message_loop_|, are initialized.
210 virtual void SetUpTestOnMainThread() = 0;
212 virtual void SetUpTestOnSecondaryThread() = 0;
214 // TEST_F() should call this method.
221 void CheckOnThread(ThreadType thread_type
);
223 // These can be called on any thread.
224 void PostQuitForMainThread();
225 void PostQuitForSecondaryThread();
228 scoped_refptr
<MessageLoopResource
> secondary_thread_message_loop_
;
229 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner_
;
232 // base::DelegateSimpleThread::Delegate implementation.
235 void QuitNestedLoop();
237 static void InternalSetUpTestOnSecondaryThread(void* user_data
,
240 scoped_ptr
<base::DelegateSimpleThread
> secondary_thread_
;
241 scoped_ptr
<base::RunLoop
> nested_main_thread_message_loop_
;
244 class HostProxyTestHarness
: public ProxyTestHarnessBase
{
246 explicit HostProxyTestHarness(GlobalsConfiguration globals_config
);
247 virtual ~HostProxyTestHarness();
249 HostDispatcher
* host_dispatcher() { return host_dispatcher_
.get(); }
250 ResourceTracker
& resource_tracker() {
251 return *host_globals_
->GetResourceTracker();
253 VarTracker
& var_tracker() {
254 return *host_globals_
->GetVarTracker();
257 // ProxyTestBase implementation.
258 virtual PpapiGlobals
* GetGlobals();
259 virtual Dispatcher
* GetDispatcher();
260 virtual void SetUpHarness();
261 virtual void SetUpHarnessWithChannel(
262 const IPC::ChannelHandle
& channel_handle
,
263 base::SingleThreadTaskRunner
* ipc_task_runner
,
264 base::WaitableEvent
* shutdown_event
,
266 virtual void TearDownHarness();
268 class DelegateMock
: public ProxyChannel::Delegate
{
270 DelegateMock() : ipc_task_runner_(NULL
), shutdown_event_(NULL
) {}
271 ~DelegateMock() override
{}
273 void Init(base::SingleThreadTaskRunner
* ipc_task_runner
,
274 base::WaitableEvent
* shutdown_event
) {
275 ipc_task_runner_
= ipc_task_runner
;
276 shutdown_event_
= shutdown_event
;
279 // ProxyChannel::Delegate implementation.
280 base::SingleThreadTaskRunner
* GetIPCTaskRunner() override
;
281 base::WaitableEvent
* GetShutdownEvent() override
;
282 IPC::PlatformFileForTransit
ShareHandleWithRemote(
283 base::PlatformFile handle
,
284 base::ProcessId remote_pid
,
285 bool should_close_source
) override
;
286 base::SharedMemoryHandle
ShareSharedMemoryHandleWithRemote(
287 const base::SharedMemoryHandle
& handle
,
288 base::ProcessId remote_pid
) override
;
291 base::SingleThreadTaskRunner
* ipc_task_runner_
; // Weak
292 base::WaitableEvent
* shutdown_event_
; // Weak
294 DISALLOW_COPY_AND_ASSIGN(DelegateMock
);
298 void CreateHostGlobals();
300 GlobalsConfiguration globals_config_
;
301 scoped_ptr
<ppapi::TestGlobals
> host_globals_
;
302 scoped_ptr
<HostDispatcher
> host_dispatcher_
;
303 // The host side of the real proxy doesn't lock, so this disables locking for
304 // the thread the host side of the test runs on.
305 scoped_ptr
<ProxyLock::LockingDisablerForTest
> disable_locking_
;
306 DelegateMock delegate_mock_
;
309 class HostProxyTest
: public HostProxyTestHarness
, public testing::Test
{
312 virtual ~HostProxyTest();
314 // testing::Test implementation.
315 virtual void SetUp();
316 virtual void TearDown();
318 base::MessageLoop message_loop_
;
321 // Use this base class to test both sides of a proxy.
322 class TwoWayTest
: public testing::Test
{
324 enum TwoWayTestMode
{
328 TwoWayTest(TwoWayTestMode test_mode
);
329 virtual ~TwoWayTest();
331 HostProxyTestHarness
& host() { return host_
; }
332 PluginProxyTestHarness
& plugin() { return plugin_
; }
333 PP_Module
pp_module() const { return host_
.pp_module(); }
334 PP_Instance
pp_instance() const { return host_
.pp_instance(); }
335 TwoWayTestMode
test_mode() { return test_mode_
; }
337 // testing::Test implementation.
338 virtual void SetUp();
339 virtual void TearDown();
342 // Post a task to the thread where the remote harness lives. This
343 // is typically used to test the state of the var tracker on the plugin
344 // thread. This runs the task synchronously for convenience.
345 void PostTaskOnRemoteHarness(const base::Closure
& task
);
348 TwoWayTestMode test_mode_
;
349 HostProxyTestHarness host_
;
350 PluginProxyTestHarness plugin_
;
351 // In order to use sync IPC, we need to have an IO thread.
352 base::Thread io_thread_
;
353 // The plugin side of the proxy runs on its own thread.
354 base::Thread plugin_thread_
;
355 // The message loop for the main (host) thread.
356 base::MessageLoop message_loop_
;
358 // Aliases for the host and plugin harnesses; if we're testing a PPP
359 // interface, remote_harness will point to plugin_, and local_harness
360 // will point to host_. This makes it convenient when we're starting and
361 // stopping the harnesses.
362 ProxyTestHarnessBase
* remote_harness_
;
363 ProxyTestHarnessBase
* local_harness_
;
365 base::WaitableEvent channel_created_
;
366 base::WaitableEvent shutdown_event_
;
369 // Used during Gtests when you have a PP_Var that you want to EXPECT is equal
370 // to a certain constant string value:
372 // EXPECT_VAR_IS_STRING("foo", my_var);
373 #define EXPECT_VAR_IS_STRING(str, var) { \
374 StringVar* sv = StringVar::FromPPVar(var); \
377 EXPECT_EQ(str, sv->value()); \