Include all dupe types (event when value is zero) in scan stats.
[chromium-blink-merge.git] / ppapi / proxy / ppapi_proxy_test.h
blobe9618743c75deca7546f8f167cbe039c6aa9a79e
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/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"
28 namespace base {
29 class MessageLoopProxy;
30 class RunLoop;
33 namespace ppapi {
34 namespace proxy {
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 {
41 public:
42 enum GlobalsConfiguration {
43 PER_THREAD_GLOBALS,
44 SINGLETON_GLOBALS
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,
65 bool is_client) = 0;
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);
83 private:
84 // Destination for IPC messages sent by the test.
85 ResourceMessageTestSink sink_;
87 // The module and instance ID associated with the plugin dispatcher.
88 PP_Module pp_module_;
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 {
97 public:
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,
116 bool is_client);
117 virtual void TearDownHarness();
119 class PluginDelegateMock : public PluginDispatcher::PluginDelegate,
120 public PluginProxyDelegate {
121 public:
122 PluginDelegateMock() : ipc_message_loop_(NULL), shutdown_event_() {}
123 ~PluginDelegateMock() override {}
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 base::SingleThreadTaskRunner* GetIPCTaskRunner() override;
137 base::WaitableEvent* GetShutdownEvent() override;
138 IPC::PlatformFileForTransit ShareHandleWithRemote(
139 base::PlatformFile handle,
140 base::ProcessId remote_pid,
141 bool should_close_source) override;
142 base::SharedMemoryHandle ShareSharedMemoryHandleWithRemote(
143 const base::SharedMemoryHandle& handle,
144 base::ProcessId remote_pid) override;
146 // PluginDispatcher::PluginDelegate implementation.
147 std::set<PP_Instance>* GetGloballySeenInstanceIDSet() override;
148 uint32 Register(PluginDispatcher* plugin_dispatcher) override;
149 void Unregister(uint32 plugin_dispatcher_id) override;
151 // PluginProxyDelegate implementation.
152 IPC::Sender* GetBrowserSender() override;
153 std::string GetUILanguage() override;
154 void PreCacheFont(const void* logfontw) override;
155 void SetActiveURL(const std::string& url) override;
156 PP_Resource CreateBrowserFont(
157 Connection connection,
158 PP_Instance instance,
159 const PP_BrowserFont_Trusted_Description& desc,
160 const Preferences& prefs) override;
162 private:
163 base::MessageLoopProxy* ipc_message_loop_; // Weak
164 base::WaitableEvent* shutdown_event_; // Weak
165 std::set<PP_Instance> instance_id_set_;
166 IPC::Sender* browser_sender_;
168 DISALLOW_COPY_AND_ASSIGN(PluginDelegateMock);
171 private:
172 void CreatePluginGlobals(
173 const scoped_refptr<base::TaskRunner>& ipc_task_runner);
175 GlobalsConfiguration globals_config_;
176 scoped_ptr<PluginGlobals> plugin_globals_;
178 scoped_ptr<PluginDispatcher> plugin_dispatcher_;
179 PluginDelegateMock plugin_delegate_mock_;
182 class PluginProxyTest : public PluginProxyTestHarness, public testing::Test {
183 public:
184 PluginProxyTest();
185 virtual ~PluginProxyTest();
187 // testing::Test implementation.
188 virtual void SetUp();
189 virtual void TearDown();
190 private:
191 base::MessageLoop message_loop_;
194 // This class provides support for multi-thread testing. A secondary thread is
195 // created with a Pepper message loop.
196 // Subclasses need to implement the two SetUpTestOn*Thread() methods to do the
197 // actual testing work; and call both PostQuitFor*Thread() when testing is
198 // done.
199 class PluginProxyMultiThreadTest
200 : public PluginProxyTest,
201 public base::DelegateSimpleThread::Delegate {
202 public:
203 PluginProxyMultiThreadTest();
204 ~PluginProxyMultiThreadTest() override;
206 // Called before the secondary thread is started, but after all the member
207 // variables, including |secondary_thread_| and
208 // |secondary_thread_message_loop_|, are initialized.
209 virtual void SetUpTestOnMainThread() = 0;
211 virtual void SetUpTestOnSecondaryThread() = 0;
213 // TEST_F() should call this method.
214 void RunTest();
216 enum ThreadType {
217 MAIN_THREAD,
218 SECONDARY_THREAD
220 void CheckOnThread(ThreadType thread_type);
222 // These can be called on any thread.
223 void PostQuitForMainThread();
224 void PostQuitForSecondaryThread();
226 protected:
227 scoped_refptr<MessageLoopResource> secondary_thread_message_loop_;
228 scoped_refptr<base::MessageLoopProxy> main_thread_message_loop_proxy_;
230 private:
231 // base::DelegateSimpleThread::Delegate implementation.
232 void Run() override;
234 void QuitNestedLoop();
236 static void InternalSetUpTestOnSecondaryThread(void* user_data,
237 int32_t result);
239 scoped_ptr<base::DelegateSimpleThread> secondary_thread_;
240 scoped_ptr<base::RunLoop> nested_main_thread_message_loop_;
243 class HostProxyTestHarness : public ProxyTestHarnessBase {
244 public:
245 explicit HostProxyTestHarness(GlobalsConfiguration globals_config);
246 virtual ~HostProxyTestHarness();
248 HostDispatcher* host_dispatcher() { return host_dispatcher_.get(); }
249 ResourceTracker& resource_tracker() {
250 return *host_globals_->GetResourceTracker();
252 VarTracker& var_tracker() {
253 return *host_globals_->GetVarTracker();
256 // ProxyTestBase implementation.
257 virtual PpapiGlobals* GetGlobals();
258 virtual Dispatcher* GetDispatcher();
259 virtual void SetUpHarness();
260 virtual void SetUpHarnessWithChannel(const IPC::ChannelHandle& channel_handle,
261 base::MessageLoopProxy* ipc_message_loop,
262 base::WaitableEvent* shutdown_event,
263 bool is_client);
264 virtual void TearDownHarness();
266 class DelegateMock : public ProxyChannel::Delegate {
267 public:
268 DelegateMock() : ipc_message_loop_(NULL), shutdown_event_(NULL) {
270 ~DelegateMock() override {}
272 void Init(base::MessageLoopProxy* ipc_message_loop,
273 base::WaitableEvent* shutdown_event) {
274 ipc_message_loop_ = ipc_message_loop;
275 shutdown_event_ = shutdown_event;
278 // ProxyChannel::Delegate implementation.
279 base::MessageLoopProxy* GetIPCTaskRunner() override;
280 base::WaitableEvent* GetShutdownEvent() override;
281 IPC::PlatformFileForTransit ShareHandleWithRemote(
282 base::PlatformFile handle,
283 base::ProcessId remote_pid,
284 bool should_close_source) override;
285 base::SharedMemoryHandle ShareSharedMemoryHandleWithRemote(
286 const base::SharedMemoryHandle& handle,
287 base::ProcessId remote_pid) override;
289 private:
290 base::MessageLoopProxy* ipc_message_loop_; // Weak
291 base::WaitableEvent* shutdown_event_; // Weak
293 DISALLOW_COPY_AND_ASSIGN(DelegateMock);
296 private:
297 void CreateHostGlobals();
299 GlobalsConfiguration globals_config_;
300 scoped_ptr<ppapi::TestGlobals> host_globals_;
301 scoped_ptr<HostDispatcher> host_dispatcher_;
302 // The host side of the real proxy doesn't lock, so this disables locking for
303 // the thread the host side of the test runs on.
304 scoped_ptr<ProxyLock::LockingDisablerForTest> disable_locking_;
305 DelegateMock delegate_mock_;
308 class HostProxyTest : public HostProxyTestHarness, public testing::Test {
309 public:
310 HostProxyTest();
311 virtual ~HostProxyTest();
313 // testing::Test implementation.
314 virtual void SetUp();
315 virtual void TearDown();
316 private:
317 base::MessageLoop message_loop_;
320 // Use this base class to test both sides of a proxy.
321 class TwoWayTest : public testing::Test {
322 public:
323 enum TwoWayTestMode {
324 TEST_PPP_INTERFACE,
325 TEST_PPB_INTERFACE
327 TwoWayTest(TwoWayTestMode test_mode);
328 virtual ~TwoWayTest();
330 HostProxyTestHarness& host() { return host_; }
331 PluginProxyTestHarness& plugin() { return plugin_; }
332 PP_Module pp_module() const { return host_.pp_module(); }
333 PP_Instance pp_instance() const { return host_.pp_instance(); }
334 TwoWayTestMode test_mode() { return test_mode_; }
336 // testing::Test implementation.
337 virtual void SetUp();
338 virtual void TearDown();
340 protected:
341 // Post a task to the thread where the remote harness lives. This
342 // is typically used to test the state of the var tracker on the plugin
343 // thread. This runs the task synchronously for convenience.
344 void PostTaskOnRemoteHarness(const base::Closure& task);
346 private:
347 TwoWayTestMode test_mode_;
348 HostProxyTestHarness host_;
349 PluginProxyTestHarness plugin_;
350 // In order to use sync IPC, we need to have an IO thread.
351 base::Thread io_thread_;
352 // The plugin side of the proxy runs on its own thread.
353 base::Thread plugin_thread_;
354 // The message loop for the main (host) thread.
355 base::MessageLoop message_loop_;
357 // Aliases for the host and plugin harnesses; if we're testing a PPP
358 // interface, remote_harness will point to plugin_, and local_harness
359 // will point to host_. This makes it convenient when we're starting and
360 // stopping the harnesses.
361 ProxyTestHarnessBase* remote_harness_;
362 ProxyTestHarnessBase* local_harness_;
364 base::WaitableEvent channel_created_;
365 base::WaitableEvent shutdown_event_;
368 // Used during Gtests when you have a PP_Var that you want to EXPECT is equal
369 // to a certain constant string value:
371 // EXPECT_VAR_IS_STRING("foo", my_var);
372 #define EXPECT_VAR_IS_STRING(str, var) { \
373 StringVar* sv = StringVar::FromPPVar(var); \
374 EXPECT_TRUE(sv); \
375 if (sv) \
376 EXPECT_EQ(str, sv->value()); \
379 } // namespace proxy
380 } // namespace ppapi