Roll leveldb from r73 to r75.
[chromium-blink-merge.git] / ppapi / proxy / ppapi_proxy_test.h
blobd4e54bf396402216fb0d5496e097da4a19949106
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.
5 #include <map>
6 #include <string>
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.h"
12 #include "base/synchronization/waitable_event.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/test_globals.h"
24 #include "testing/gtest/include/gtest/gtest.h"
26 namespace base {
27 class MessageLoopProxy;
28 class RunLoop;
31 namespace ppapi {
32 namespace proxy {
34 class MessageLoopResource;
36 // Base class for plugin and host test harnesses. Tests will not use this
37 // directly. Instead, use the PluginProxyTest, HostProxyTest, or TwoWayTest.
38 class ProxyTestHarnessBase {
39 public:
40 enum GlobalsConfiguration {
41 PER_THREAD_GLOBALS,
42 SINGLETON_GLOBALS
45 ProxyTestHarnessBase();
46 virtual ~ProxyTestHarnessBase();
48 PP_Module pp_module() const { return pp_module_; }
49 PP_Instance pp_instance() const { return pp_instance_; }
50 ResourceMessageTestSink& sink() { return sink_; }
52 virtual PpapiGlobals* GetGlobals() = 0;
53 // Returns either the plugin or host dispatcher, depending on the test.
54 virtual Dispatcher* GetDispatcher() = 0;
56 // Set up the harness using an IPC::TestSink to capture messages.
57 virtual void SetUpHarness() = 0;
59 // Set up the harness using a real IPC channel.
60 virtual void SetUpHarnessWithChannel(const IPC::ChannelHandle& channel_handle,
61 base::MessageLoopProxy* ipc_message_loop,
62 base::WaitableEvent* shutdown_event,
63 bool is_client) = 0;
65 virtual void TearDownHarness() = 0;
67 // Implementation of GetInterface for the dispatcher. This will
68 // return NULL for all interfaces unless one is registered by calling
69 // RegisterTestInterface();
70 const void* GetInterface(const char* name);
72 // Allows the test to specify an interface implementation for a given
73 // interface name. This will be returned when any of the proxy logic
74 // requests a local interface.
75 void RegisterTestInterface(const char* name, const void* test_interface);
77 // Sends a "supports interface" message to the current dispatcher and returns
78 // true if it's supported. This is just for the convenience of tests.
79 bool SupportsInterface(const char* name);
81 private:
82 // Destination for IPC messages sent by the test.
83 ResourceMessageTestSink sink_;
85 // The module and instance ID associated with the plugin dispatcher.
86 PP_Module pp_module_;
87 PP_Instance pp_instance_;
89 // Stores the data for GetInterface/RegisterTestInterface.
90 std::map<std::string, const void*> registered_interfaces_;
93 // Test harness for the plugin side of the proxy.
94 class PluginProxyTestHarness : public ProxyTestHarnessBase {
95 public:
96 explicit PluginProxyTestHarness(GlobalsConfiguration globals_config);
97 virtual ~PluginProxyTestHarness();
99 PluginDispatcher* plugin_dispatcher() { return plugin_dispatcher_.get(); }
100 PluginResourceTracker& resource_tracker() {
101 return *plugin_globals_->plugin_resource_tracker();
103 PluginVarTracker& var_tracker() {
104 return *plugin_globals_->plugin_var_tracker();
107 // ProxyTestHarnessBase implementation.
108 virtual PpapiGlobals* GetGlobals();
109 virtual Dispatcher* GetDispatcher();
110 virtual void SetUpHarness();
111 virtual void SetUpHarnessWithChannel(const IPC::ChannelHandle& channel_handle,
112 base::MessageLoopProxy* ipc_message_loop,
113 base::WaitableEvent* shutdown_event,
114 bool is_client);
115 virtual void TearDownHarness();
117 class PluginDelegateMock : public PluginDispatcher::PluginDelegate,
118 public PluginProxyDelegate {
119 public:
120 PluginDelegateMock() : ipc_message_loop_(NULL), shutdown_event_() {}
121 virtual ~PluginDelegateMock() {}
123 void Init(base::MessageLoopProxy* ipc_message_loop,
124 base::WaitableEvent* shutdown_event) {
125 ipc_message_loop_ = ipc_message_loop;
126 shutdown_event_ = shutdown_event;
129 void set_browser_sender(IPC::Sender* browser_sender) {
130 browser_sender_ = browser_sender;
133 // ProxyChannel::Delegate implementation.
134 virtual base::MessageLoopProxy* GetIPCMessageLoop() OVERRIDE;
135 virtual base::WaitableEvent* GetShutdownEvent() OVERRIDE;
136 virtual IPC::PlatformFileForTransit ShareHandleWithRemote(
137 base::PlatformFile handle,
138 base::ProcessId remote_pid,
139 bool should_close_source) OVERRIDE;
141 // PluginDispatcher::PluginDelegate implementation.
142 virtual std::set<PP_Instance>* GetGloballySeenInstanceIDSet() OVERRIDE;
143 virtual uint32 Register(PluginDispatcher* plugin_dispatcher) OVERRIDE;
144 virtual void Unregister(uint32 plugin_dispatcher_id) OVERRIDE;
146 // PluginProxyDelegate implementation.
147 virtual IPC::Sender* GetBrowserSender() OVERRIDE;
148 virtual std::string GetUILanguage() OVERRIDE;
149 virtual void PreCacheFont(const void* logfontw) OVERRIDE;
150 virtual void SetActiveURL(const std::string& url) OVERRIDE;
152 private:
153 base::MessageLoopProxy* ipc_message_loop_; // Weak
154 base::WaitableEvent* shutdown_event_; // Weak
155 std::set<PP_Instance> instance_id_set_;
156 IPC::Sender* browser_sender_;
158 DISALLOW_COPY_AND_ASSIGN(PluginDelegateMock);
161 private:
162 void CreatePluginGlobals();
164 GlobalsConfiguration globals_config_;
165 scoped_ptr<PluginGlobals> plugin_globals_;
167 scoped_ptr<PluginDispatcher> plugin_dispatcher_;
168 PluginDelegateMock plugin_delegate_mock_;
171 class PluginProxyTest : public PluginProxyTestHarness, public testing::Test {
172 public:
173 PluginProxyTest();
174 virtual ~PluginProxyTest();
176 // testing::Test implementation.
177 virtual void SetUp();
178 virtual void TearDown();
179 private:
180 base::MessageLoop message_loop_;
183 // This class provides support for multi-thread testing. A secondary thread is
184 // created with a Pepper message loop.
185 // Subclasses need to implement the two SetUpTestOn*Thread() methods to do the
186 // actual testing work; and call both PostQuitFor*Thread() when testing is
187 // done.
188 class PluginProxyMultiThreadTest
189 : public PluginProxyTest,
190 public base::DelegateSimpleThread::Delegate {
191 public:
192 PluginProxyMultiThreadTest();
193 virtual ~PluginProxyMultiThreadTest();
195 // Called before the secondary thread is started, but after all the member
196 // variables, including |secondary_thread_| and
197 // |secondary_thread_message_loop_|, are initialized.
198 virtual void SetUpTestOnMainThread() = 0;
200 virtual void SetUpTestOnSecondaryThread() = 0;
202 // TEST_F() should call this method.
203 void RunTest();
205 enum ThreadType {
206 MAIN_THREAD,
207 SECONDARY_THREAD
209 void CheckOnThread(ThreadType thread_type);
211 // These can be called on any thread.
212 void PostQuitForMainThread();
213 void PostQuitForSecondaryThread();
215 protected:
216 scoped_refptr<MessageLoopResource> secondary_thread_message_loop_;
217 scoped_refptr<base::MessageLoopProxy> main_thread_message_loop_proxy_;
219 private:
220 // base::DelegateSimpleThread::Delegate implementation.
221 virtual void Run() OVERRIDE;
223 void QuitNestedLoop();
225 static void InternalSetUpTestOnSecondaryThread(void* user_data,
226 int32_t result);
228 scoped_ptr<base::DelegateSimpleThread> secondary_thread_;
229 scoped_ptr<base::RunLoop> nested_main_thread_message_loop_;
232 class HostProxyTestHarness : public ProxyTestHarnessBase {
233 public:
234 explicit HostProxyTestHarness(GlobalsConfiguration globals_config);
235 virtual ~HostProxyTestHarness();
237 HostDispatcher* host_dispatcher() { return host_dispatcher_.get(); }
238 ResourceTracker& resource_tracker() {
239 return *host_globals_->GetResourceTracker();
241 VarTracker& var_tracker() {
242 return *host_globals_->GetVarTracker();
245 // ProxyTestBase implementation.
246 virtual PpapiGlobals* GetGlobals();
247 virtual Dispatcher* GetDispatcher();
248 virtual void SetUpHarness();
249 virtual void SetUpHarnessWithChannel(const IPC::ChannelHandle& channel_handle,
250 base::MessageLoopProxy* ipc_message_loop,
251 base::WaitableEvent* shutdown_event,
252 bool is_client);
253 virtual void TearDownHarness();
255 class DelegateMock : public ProxyChannel::Delegate {
256 public:
257 DelegateMock() : ipc_message_loop_(NULL), shutdown_event_(NULL) {
259 virtual ~DelegateMock() {}
261 void Init(base::MessageLoopProxy* ipc_message_loop,
262 base::WaitableEvent* shutdown_event) {
263 ipc_message_loop_ = ipc_message_loop;
264 shutdown_event_ = shutdown_event;
267 // ProxyChannel::Delegate implementation.
268 virtual base::MessageLoopProxy* GetIPCMessageLoop();
269 virtual base::WaitableEvent* GetShutdownEvent();
270 virtual IPC::PlatformFileForTransit ShareHandleWithRemote(
271 base::PlatformFile handle,
272 base::ProcessId remote_pid,
273 bool should_close_source) OVERRIDE;
275 private:
276 base::MessageLoopProxy* ipc_message_loop_; // Weak
277 base::WaitableEvent* shutdown_event_; // Weak
279 DISALLOW_COPY_AND_ASSIGN(DelegateMock);
282 private:
283 class MockSyncMessageStatusReceiver;
285 void CreateHostGlobals();
287 GlobalsConfiguration globals_config_;
288 scoped_ptr<ppapi::TestGlobals> host_globals_;
289 scoped_ptr<HostDispatcher> host_dispatcher_;
290 DelegateMock delegate_mock_;
292 scoped_ptr<MockSyncMessageStatusReceiver> status_receiver_;
295 class HostProxyTest : public HostProxyTestHarness, public testing::Test {
296 public:
297 HostProxyTest();
298 virtual ~HostProxyTest();
300 // testing::Test implementation.
301 virtual void SetUp();
302 virtual void TearDown();
303 private:
304 base::MessageLoop message_loop_;
307 // Use this base class to test both sides of a proxy.
308 class TwoWayTest : public testing::Test {
309 public:
310 enum TwoWayTestMode {
311 TEST_PPP_INTERFACE,
312 TEST_PPB_INTERFACE
314 TwoWayTest(TwoWayTestMode test_mode);
315 virtual ~TwoWayTest();
317 HostProxyTestHarness& host() { return host_; }
318 PluginProxyTestHarness& plugin() { return plugin_; }
319 PP_Module pp_module() const { return host_.pp_module(); }
320 PP_Instance pp_instance() const { return host_.pp_instance(); }
321 TwoWayTestMode test_mode() { return test_mode_; }
323 // testing::Test implementation.
324 virtual void SetUp();
325 virtual void TearDown();
327 protected:
328 // Post a task to the thread where the remote harness lives. This
329 // is typically used to test the state of the var tracker on the plugin
330 // thread. This runs the task synchronously for convenience.
331 void PostTaskOnRemoteHarness(const base::Closure& task);
333 private:
334 TwoWayTestMode test_mode_;
335 HostProxyTestHarness host_;
336 PluginProxyTestHarness plugin_;
337 // In order to use sync IPC, we need to have an IO thread.
338 base::Thread io_thread_;
339 // The plugin side of the proxy runs on its own thread.
340 base::Thread plugin_thread_;
341 // The message loop for the main (host) thread.
342 base::MessageLoop message_loop_;
344 // Aliases for the host and plugin harnesses; if we're testing a PPP
345 // interface, remote_harness will point to plugin_, and local_harness
346 // will point to host_. This makes it convenient when we're starting and
347 // stopping the harnesses.
348 ProxyTestHarnessBase* remote_harness_;
349 ProxyTestHarnessBase* local_harness_;
351 base::WaitableEvent channel_created_;
352 base::WaitableEvent shutdown_event_;
355 // Used during Gtests when you have a PP_Var that you want to EXPECT is equal
356 // to a certain constant string value:
358 // EXPECT_VAR_IS_STRING("foo", my_var);
359 #define EXPECT_VAR_IS_STRING(str, var) { \
360 StringVar* sv = StringVar::FromPPVar(var); \
361 EXPECT_TRUE(sv); \
362 if (sv) \
363 EXPECT_EQ(str, sv->value()); \
366 } // namespace proxy
367 } // namespace ppapi