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/message_loop/message_loop.h"
12 #include "base/synchronization/waitable_event.h"
13 #include "base/task_runner.h"
14 #include "base/threading/simple_thread.h"
15 #include "base/threading/thread.h"
16 #include "ppapi/c/pp_instance.h"
17 #include "ppapi/proxy/host_dispatcher.h"
18 #include "ppapi/proxy/plugin_dispatcher.h"
19 #include "ppapi/proxy/plugin_globals.h"
20 #include "ppapi/proxy/plugin_proxy_delegate.h"
21 #include "ppapi/proxy/plugin_resource_tracker.h"
22 #include "ppapi/proxy/plugin_var_tracker.h"
23 #include "ppapi/proxy/resource_message_test_sink.h"
24 #include "ppapi/shared_impl/proxy_lock.h"
25 #include "ppapi/shared_impl/test_globals.h"
26 #include "testing/gtest/include/gtest/gtest.h"
29 class MessageLoopProxy
;
36 class MessageLoopResource
;
38 // Base class for plugin and host test harnesses. Tests will not use this
39 // directly. Instead, use the PluginProxyTest, HostProxyTest, or TwoWayTest.
40 class ProxyTestHarnessBase
{
42 enum GlobalsConfiguration
{
47 ProxyTestHarnessBase();
48 virtual ~ProxyTestHarnessBase();
50 PP_Module
pp_module() const { return pp_module_
; }
51 PP_Instance
pp_instance() const { return pp_instance_
; }
52 ResourceMessageTestSink
& sink() { return sink_
; }
54 virtual PpapiGlobals
* GetGlobals() = 0;
55 // Returns either the plugin or host dispatcher, depending on the test.
56 virtual Dispatcher
* GetDispatcher() = 0;
58 // Set up the harness using an IPC::TestSink to capture messages.
59 virtual void SetUpHarness() = 0;
61 // Set up the harness using a real IPC channel.
62 virtual void SetUpHarnessWithChannel(const IPC::ChannelHandle
& channel_handle
,
63 base::MessageLoopProxy
* ipc_message_loop
,
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(const IPC::ChannelHandle
& channel_handle
,
114 base::MessageLoopProxy
* ipc_message_loop
,
115 base::WaitableEvent
* shutdown_event
,
117 virtual void TearDownHarness();
119 class PluginDelegateMock
: public PluginDispatcher::PluginDelegate
,
120 public PluginProxyDelegate
{
122 PluginDelegateMock() : ipc_message_loop_(NULL
), shutdown_event_() {}
123 virtual ~PluginDelegateMock() {}
125 void Init(base::MessageLoopProxy
* ipc_message_loop
,
126 base::WaitableEvent
* shutdown_event
) {
127 ipc_message_loop_
= ipc_message_loop
;
128 shutdown_event_
= shutdown_event
;
131 void set_browser_sender(IPC::Sender
* browser_sender
) {
132 browser_sender_
= browser_sender
;
135 // ProxyChannel::Delegate implementation.
136 virtual base::MessageLoopProxy
* GetIPCMessageLoop() override
;
137 virtual base::WaitableEvent
* GetShutdownEvent() override
;
138 virtual IPC::PlatformFileForTransit
ShareHandleWithRemote(
139 base::PlatformFile handle
,
140 base::ProcessId remote_pid
,
141 bool should_close_source
) override
;
143 // PluginDispatcher::PluginDelegate implementation.
144 virtual std::set
<PP_Instance
>* GetGloballySeenInstanceIDSet() override
;
145 virtual uint32
Register(PluginDispatcher
* plugin_dispatcher
) override
;
146 virtual void Unregister(uint32 plugin_dispatcher_id
) override
;
148 // PluginProxyDelegate implementation.
149 virtual IPC::Sender
* GetBrowserSender() override
;
150 virtual std::string
GetUILanguage() override
;
151 virtual void PreCacheFont(const void* logfontw
) override
;
152 virtual void SetActiveURL(const std::string
& url
) override
;
153 virtual PP_Resource
CreateBrowserFont(
154 Connection connection
,
155 PP_Instance instance
,
156 const PP_BrowserFont_Trusted_Description
& desc
,
157 const Preferences
& prefs
) override
;
160 base::MessageLoopProxy
* ipc_message_loop_
; // Weak
161 base::WaitableEvent
* shutdown_event_
; // Weak
162 std::set
<PP_Instance
> instance_id_set_
;
163 IPC::Sender
* browser_sender_
;
165 DISALLOW_COPY_AND_ASSIGN(PluginDelegateMock
);
169 void CreatePluginGlobals(
170 const scoped_refptr
<base::TaskRunner
>& ipc_task_runner
);
172 GlobalsConfiguration globals_config_
;
173 scoped_ptr
<PluginGlobals
> plugin_globals_
;
175 scoped_ptr
<PluginDispatcher
> plugin_dispatcher_
;
176 PluginDelegateMock plugin_delegate_mock_
;
179 class PluginProxyTest
: public PluginProxyTestHarness
, public testing::Test
{
182 virtual ~PluginProxyTest();
184 // testing::Test implementation.
185 virtual void SetUp();
186 virtual void TearDown();
188 base::MessageLoop message_loop_
;
191 // This class provides support for multi-thread testing. A secondary thread is
192 // created with a Pepper message loop.
193 // Subclasses need to implement the two SetUpTestOn*Thread() methods to do the
194 // actual testing work; and call both PostQuitFor*Thread() when testing is
196 class PluginProxyMultiThreadTest
197 : public PluginProxyTest
,
198 public base::DelegateSimpleThread::Delegate
{
200 PluginProxyMultiThreadTest();
201 virtual ~PluginProxyMultiThreadTest();
203 // Called before the secondary thread is started, but after all the member
204 // variables, including |secondary_thread_| and
205 // |secondary_thread_message_loop_|, are initialized.
206 virtual void SetUpTestOnMainThread() = 0;
208 virtual void SetUpTestOnSecondaryThread() = 0;
210 // TEST_F() should call this method.
217 void CheckOnThread(ThreadType thread_type
);
219 // These can be called on any thread.
220 void PostQuitForMainThread();
221 void PostQuitForSecondaryThread();
224 scoped_refptr
<MessageLoopResource
> secondary_thread_message_loop_
;
225 scoped_refptr
<base::MessageLoopProxy
> main_thread_message_loop_proxy_
;
228 // base::DelegateSimpleThread::Delegate implementation.
229 virtual void Run() override
;
231 void QuitNestedLoop();
233 static void InternalSetUpTestOnSecondaryThread(void* user_data
,
236 scoped_ptr
<base::DelegateSimpleThread
> secondary_thread_
;
237 scoped_ptr
<base::RunLoop
> nested_main_thread_message_loop_
;
240 class HostProxyTestHarness
: public ProxyTestHarnessBase
{
242 explicit HostProxyTestHarness(GlobalsConfiguration globals_config
);
243 virtual ~HostProxyTestHarness();
245 HostDispatcher
* host_dispatcher() { return host_dispatcher_
.get(); }
246 ResourceTracker
& resource_tracker() {
247 return *host_globals_
->GetResourceTracker();
249 VarTracker
& var_tracker() {
250 return *host_globals_
->GetVarTracker();
253 // ProxyTestBase implementation.
254 virtual PpapiGlobals
* GetGlobals();
255 virtual Dispatcher
* GetDispatcher();
256 virtual void SetUpHarness();
257 virtual void SetUpHarnessWithChannel(const IPC::ChannelHandle
& channel_handle
,
258 base::MessageLoopProxy
* ipc_message_loop
,
259 base::WaitableEvent
* shutdown_event
,
261 virtual void TearDownHarness();
263 class DelegateMock
: public ProxyChannel::Delegate
{
265 DelegateMock() : ipc_message_loop_(NULL
), shutdown_event_(NULL
) {
267 virtual ~DelegateMock() {}
269 void Init(base::MessageLoopProxy
* ipc_message_loop
,
270 base::WaitableEvent
* shutdown_event
) {
271 ipc_message_loop_
= ipc_message_loop
;
272 shutdown_event_
= shutdown_event
;
275 // ProxyChannel::Delegate implementation.
276 virtual base::MessageLoopProxy
* GetIPCMessageLoop();
277 virtual base::WaitableEvent
* GetShutdownEvent();
278 virtual IPC::PlatformFileForTransit
ShareHandleWithRemote(
279 base::PlatformFile handle
,
280 base::ProcessId remote_pid
,
281 bool should_close_source
) override
;
284 base::MessageLoopProxy
* ipc_message_loop_
; // Weak
285 base::WaitableEvent
* shutdown_event_
; // Weak
287 DISALLOW_COPY_AND_ASSIGN(DelegateMock
);
291 void CreateHostGlobals();
293 GlobalsConfiguration globals_config_
;
294 scoped_ptr
<ppapi::TestGlobals
> host_globals_
;
295 scoped_ptr
<HostDispatcher
> host_dispatcher_
;
296 // The host side of the real proxy doesn't lock, so this disables locking for
297 // the thread the host side of the test runs on.
298 scoped_ptr
<ProxyLock::LockingDisablerForTest
> disable_locking_
;
299 DelegateMock delegate_mock_
;
302 class HostProxyTest
: public HostProxyTestHarness
, public testing::Test
{
305 virtual ~HostProxyTest();
307 // testing::Test implementation.
308 virtual void SetUp();
309 virtual void TearDown();
311 base::MessageLoop message_loop_
;
314 // Use this base class to test both sides of a proxy.
315 class TwoWayTest
: public testing::Test
{
317 enum TwoWayTestMode
{
321 TwoWayTest(TwoWayTestMode test_mode
);
322 virtual ~TwoWayTest();
324 HostProxyTestHarness
& host() { return host_
; }
325 PluginProxyTestHarness
& plugin() { return plugin_
; }
326 PP_Module
pp_module() const { return host_
.pp_module(); }
327 PP_Instance
pp_instance() const { return host_
.pp_instance(); }
328 TwoWayTestMode
test_mode() { return test_mode_
; }
330 // testing::Test implementation.
331 virtual void SetUp();
332 virtual void TearDown();
335 // Post a task to the thread where the remote harness lives. This
336 // is typically used to test the state of the var tracker on the plugin
337 // thread. This runs the task synchronously for convenience.
338 void PostTaskOnRemoteHarness(const base::Closure
& task
);
341 TwoWayTestMode test_mode_
;
342 HostProxyTestHarness host_
;
343 PluginProxyTestHarness plugin_
;
344 // In order to use sync IPC, we need to have an IO thread.
345 base::Thread io_thread_
;
346 // The plugin side of the proxy runs on its own thread.
347 base::Thread plugin_thread_
;
348 // The message loop for the main (host) thread.
349 base::MessageLoop message_loop_
;
351 // Aliases for the host and plugin harnesses; if we're testing a PPP
352 // interface, remote_harness will point to plugin_, and local_harness
353 // will point to host_. This makes it convenient when we're starting and
354 // stopping the harnesses.
355 ProxyTestHarnessBase
* remote_harness_
;
356 ProxyTestHarnessBase
* local_harness_
;
358 base::WaitableEvent channel_created_
;
359 base::WaitableEvent shutdown_event_
;
362 // Used during Gtests when you have a PP_Var that you want to EXPECT is equal
363 // to a certain constant string value:
365 // EXPECT_VAR_IS_STRING("foo", my_var);
366 #define EXPECT_VAR_IS_STRING(str, var) { \
367 StringVar* sv = StringVar::FromPPVar(var); \
370 EXPECT_EQ(str, sv->value()); \