OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <map> | 5 #include <map> |
6 #include <string> | 6 #include <string> |
7 | 7 |
| 8 #include "base/message_loop.h" |
8 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/synchronization/waitable_event.h" |
| 11 #include "base/threading/thread.h" |
9 #include "ipc/ipc_test_sink.h" | 12 #include "ipc/ipc_test_sink.h" |
10 #include "ppapi/c/pp_instance.h" | 13 #include "ppapi/c/pp_instance.h" |
11 #include "ppapi/proxy/host_dispatcher.h" | 14 #include "ppapi/proxy/host_dispatcher.h" |
12 #include "ppapi/proxy/plugin_dispatcher.h" | 15 #include "ppapi/proxy/plugin_dispatcher.h" |
13 #include "ppapi/proxy/plugin_resource_tracker.h" | 16 #include "ppapi/proxy/plugin_resource_tracker.h" |
14 #include "ppapi/proxy/plugin_var_tracker.h" | 17 #include "ppapi/proxy/plugin_var_tracker.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
16 | 19 |
17 namespace pp { | 20 namespace pp { |
18 namespace proxy { | 21 namespace proxy { |
19 | 22 |
20 // Base class for plugin and host tests. Tests will not use this directly. | 23 // Base class for plugin and host test harnesses. Tests will not use this |
21 // Instead, use the Plugin or HostProxyTest. | 24 // directly. Instead, use the PluginProxyTest, HostProxyTest, or TwoWayTest. |
22 class ProxyTestBase : public testing::Test { | 25 class ProxyTestHarnessBase { |
23 public: | 26 public: |
24 ProxyTestBase(); | 27 ProxyTestHarnessBase(); |
25 virtual ~ProxyTestBase(); | 28 virtual ~ProxyTestHarnessBase(); |
26 | 29 |
27 PP_Module pp_module() const { return pp_module_; } | 30 PP_Module pp_module() const { return pp_module_; } |
28 PP_Instance pp_instance() const { return pp_instance_; } | 31 PP_Instance pp_instance() const { return pp_instance_; } |
29 IPC::TestSink& sink() { return sink_; } | 32 IPC::TestSink& sink() { return sink_; } |
30 | 33 |
31 // Returns either the plugin or host dispatcher, depending on the test. | 34 // Returns either the plugin or host dispatcher, depending on the test. |
32 virtual Dispatcher* GetDispatcher() = 0; | 35 virtual Dispatcher* GetDispatcher() = 0; |
33 | 36 |
| 37 // Set up the harness using an IPC::TestSink to capture messages. |
| 38 virtual void SetUpHarness() = 0; |
| 39 |
| 40 // Set up the harness using a real IPC channel. |
| 41 virtual void SetUpHarnessWithChannel(const IPC::ChannelHandle& channel_handle, |
| 42 base::MessageLoopProxy* ipc_message_loop, |
| 43 base::WaitableEvent* shutdown_event, |
| 44 bool is_client) = 0; |
| 45 |
| 46 virtual void TearDownHarness() = 0; |
| 47 |
34 // Implementation of GetInterface for the dispatcher. This will | 48 // Implementation of GetInterface for the dispatcher. This will |
35 // return NULL for all interfaces unless one is registered by calling | 49 // return NULL for all interfaces unless one is registered by calling |
36 // RegisterTestInterface(); | 50 // RegisterTestInterface(); |
37 const void* GetInterface(const char* name); | 51 const void* GetInterface(const char* name); |
38 | 52 |
39 // Allows the test to specify an interface implementation for a given | 53 // Allows the test to specify an interface implementation for a given |
40 // interface name. This will be returned when any of the proxy logic | 54 // interface name. This will be returned when any of the proxy logic |
41 // requests a local interface. | 55 // requests a local interface. |
42 void RegisterTestInterface(const char* name, const void* interface); | 56 void RegisterTestInterface(const char* name, const void* interface); |
43 | 57 |
44 // Sends a "supports interface" message to the current dispatcher and returns | 58 // Sends a "supports interface" message to the current dispatcher and returns |
45 // true if it's supported. This is just for the convenience of tests. | 59 // true if it's supported. This is just for the convenience of tests. |
46 bool SupportsInterface(const char* name); | 60 bool SupportsInterface(const char* name); |
47 | 61 |
48 private: | 62 private: |
49 // Destination for IPC messages sent by the test. | 63 // Destination for IPC messages sent by the test. |
50 IPC::TestSink sink_; | 64 IPC::TestSink sink_; |
51 | 65 |
52 // The module and instance ID associated with the plugin dispatcher. | 66 // The module and instance ID associated with the plugin dispatcher. |
53 PP_Module pp_module_; | 67 PP_Module pp_module_; |
54 PP_Instance pp_instance_; | 68 PP_Instance pp_instance_; |
55 | 69 |
56 // Stores the data for GetInterface/RegisterTestInterface. | 70 // Stores the data for GetInterface/RegisterTestInterface. |
57 std::map<std::string, const void*> registered_interfaces_; | 71 std::map<std::string, const void*> registered_interfaces_; |
58 }; | 72 }; |
59 | 73 |
60 // Test harness for the plugin side of the proxy. | 74 // Test harness for the plugin side of the proxy. |
61 class PluginProxyTest : public ProxyTestBase { | 75 class PluginProxyTestHarness : public ProxyTestHarnessBase { |
62 public: | 76 public: |
63 PluginProxyTest(); | 77 PluginProxyTestHarness(); |
64 virtual ~PluginProxyTest(); | 78 virtual ~PluginProxyTestHarness(); |
65 | 79 |
66 PluginDispatcher* plugin_dispatcher() { return plugin_dispatcher_.get(); } | 80 PluginDispatcher* plugin_dispatcher() { return plugin_dispatcher_.get(); } |
67 PluginResourceTracker& resource_tracker() { return resource_tracker_; } | 81 PluginResourceTracker& resource_tracker() { return resource_tracker_; } |
68 PluginVarTracker& var_tracker() { return var_tracker_; } | 82 PluginVarTracker& var_tracker() { return var_tracker_; } |
69 | 83 |
| 84 // ProxyTestHarnessBase implementation. |
| 85 virtual Dispatcher* GetDispatcher(); |
| 86 virtual void SetUpHarness(); |
| 87 virtual void SetUpHarnessWithChannel(const IPC::ChannelHandle& channel_handle, |
| 88 base::MessageLoopProxy* ipc_message_loop, |
| 89 base::WaitableEvent* shutdown_event, |
| 90 bool is_client); |
| 91 virtual void TearDownHarness(); |
| 92 |
| 93 class PluginDelegateMock : public PluginDispatcher::PluginDelegate { |
| 94 public: |
| 95 PluginDelegateMock() : ipc_message_loop_(NULL), shutdown_event_() {} |
| 96 virtual ~PluginDelegateMock() {} |
| 97 |
| 98 void Init(base::MessageLoopProxy* ipc_message_loop, |
| 99 base::WaitableEvent* shutdown_event) { |
| 100 ipc_message_loop_ = ipc_message_loop; |
| 101 shutdown_event_ = shutdown_event; |
| 102 } |
| 103 |
| 104 // ProxyChannel::Delegate implementation. |
| 105 virtual base::MessageLoopProxy* GetIPCMessageLoop(); |
| 106 virtual base::WaitableEvent* GetShutdownEvent(); |
| 107 |
| 108 // PluginDispatcher::PluginDelegate implementation. |
| 109 virtual std::set<PP_Instance>* GetGloballySeenInstanceIDSet(); |
| 110 virtual ppapi::WebKitForwarding* GetWebKitForwarding(); |
| 111 virtual void PostToWebKitThread(const tracked_objects::Location& from_here, |
| 112 const base::Closure& task); |
| 113 virtual bool SendToBrowser(IPC::Message* msg); |
| 114 |
| 115 private: |
| 116 base::MessageLoopProxy* ipc_message_loop_; // Weak |
| 117 base::WaitableEvent* shutdown_event_; // Weak |
| 118 std::set<PP_Instance> instance_id_set_; |
| 119 |
| 120 DISALLOW_COPY_AND_ASSIGN(PluginDelegateMock); |
| 121 }; |
| 122 |
| 123 private: |
| 124 PluginResourceTracker resource_tracker_; |
| 125 PluginVarTracker var_tracker_; |
| 126 scoped_ptr<PluginDispatcher> plugin_dispatcher_; |
| 127 PluginDelegateMock plugin_delegate_mock_; |
| 128 }; |
| 129 |
| 130 class PluginProxyTest : public PluginProxyTestHarness, public testing::Test { |
| 131 public: |
| 132 PluginProxyTest(); |
| 133 virtual ~PluginProxyTest(); |
| 134 |
| 135 // testing::Test implementation. |
| 136 virtual void SetUp(); |
| 137 virtual void TearDown(); |
| 138 }; |
| 139 |
| 140 class HostProxyTestHarness : public ProxyTestHarnessBase { |
| 141 public: |
| 142 HostProxyTestHarness(); |
| 143 virtual ~HostProxyTestHarness(); |
| 144 |
| 145 HostDispatcher* host_dispatcher() { return host_dispatcher_.get(); } |
| 146 |
70 // ProxyTestBase implementation. | 147 // ProxyTestBase implementation. |
71 virtual Dispatcher* GetDispatcher(); | 148 virtual Dispatcher* GetDispatcher(); |
| 149 virtual void SetUpHarness(); |
| 150 virtual void SetUpHarnessWithChannel(const IPC::ChannelHandle& channel_handle, |
| 151 base::MessageLoopProxy* ipc_message_loop, |
| 152 base::WaitableEvent* shutdown_event, |
| 153 bool is_client); |
| 154 virtual void TearDownHarness(); |
| 155 |
| 156 class DelegateMock : public ProxyChannel::Delegate { |
| 157 public: |
| 158 DelegateMock() : ipc_message_loop_(NULL), shutdown_event_(NULL) { |
| 159 } |
| 160 virtual ~DelegateMock() {} |
| 161 |
| 162 void Init(base::MessageLoopProxy* ipc_message_loop, |
| 163 base::WaitableEvent* shutdown_event) { |
| 164 ipc_message_loop_ = ipc_message_loop; |
| 165 shutdown_event_ = shutdown_event; |
| 166 } |
| 167 |
| 168 // ProxyChannel::Delegate implementation. |
| 169 virtual base::MessageLoopProxy* GetIPCMessageLoop(); |
| 170 virtual base::WaitableEvent* GetShutdownEvent(); |
| 171 |
| 172 private: |
| 173 base::MessageLoopProxy* ipc_message_loop_; // Weak |
| 174 base::WaitableEvent* shutdown_event_; // Weak |
| 175 |
| 176 DISALLOW_COPY_AND_ASSIGN(DelegateMock); |
| 177 }; |
| 178 |
| 179 private: |
| 180 scoped_ptr<HostDispatcher> host_dispatcher_; |
| 181 DelegateMock delegate_mock_; |
| 182 }; |
| 183 |
| 184 class HostProxyTest : public HostProxyTestHarness, public testing::Test { |
| 185 public: |
| 186 HostProxyTest(); |
| 187 virtual ~HostProxyTest(); |
| 188 |
| 189 // testing::Test implementation. |
| 190 virtual void SetUp(); |
| 191 virtual void TearDown(); |
| 192 }; |
| 193 |
| 194 // Use this base class to test both sides of a proxy. |
| 195 class TwoWayTest : public testing::Test { |
| 196 public: |
| 197 enum TwoWayTestMode { |
| 198 TEST_PPP_INTERFACE, |
| 199 TEST_PPB_INTERFACE |
| 200 }; |
| 201 TwoWayTest(TwoWayTestMode test_mode); |
| 202 virtual ~TwoWayTest(); |
| 203 |
| 204 HostProxyTestHarness& host() { return host_; } |
| 205 PluginProxyTestHarness& plugin() { return plugin_; } |
| 206 PP_Module pp_module() const { return host_.pp_module(); } |
| 207 PP_Instance pp_instance() const { return host_.pp_instance(); } |
| 208 TwoWayTestMode test_mode() { return test_mode_; } |
72 | 209 |
73 // testing::Test implementation. | 210 // testing::Test implementation. |
74 virtual void SetUp(); | 211 virtual void SetUp(); |
75 virtual void TearDown(); | 212 virtual void TearDown(); |
76 | 213 |
77 private: | 214 private: |
78 PluginResourceTracker resource_tracker_; | 215 TwoWayTestMode test_mode_; |
79 PluginVarTracker var_tracker_; | 216 HostProxyTestHarness host_; |
80 scoped_ptr<PluginDispatcher> plugin_dispatcher_; | 217 PluginProxyTestHarness plugin_; |
81 }; | 218 // In order to use sync IPC, we need to have an IO thread. |
| 219 base::Thread io_thread_; |
| 220 // The plugin side of the proxy runs on its own thread. |
| 221 base::Thread plugin_thread_; |
| 222 // The message loop for the main (host) thread. |
| 223 MessageLoop message_loop_; |
82 | 224 |
83 class HostProxyTest : public ProxyTestBase { | 225 // Aliases for the host and plugin harnesses; if we're testing a PPP |
84 public: | 226 // interface, remote_harness will point to plugin_, and local_harness |
85 HostProxyTest(); | 227 // will point to host_. This makes it convenient when we're starting and |
86 virtual ~HostProxyTest(); | 228 // stopping the harnesses. |
| 229 ProxyTestHarnessBase* remote_harness_; |
| 230 ProxyTestHarnessBase* local_harness_; |
87 | 231 |
88 HostDispatcher* host_dispatcher() { return host_dispatcher_.get(); } | 232 base::WaitableEvent channel_created_; |
89 | 233 base::WaitableEvent shutdown_event_; |
90 // ProxyTestBase implementation. | |
91 virtual Dispatcher* GetDispatcher(); | |
92 | |
93 // testing::Test implementation. | |
94 virtual void SetUp(); | |
95 virtual void TearDown(); | |
96 | |
97 private: | |
98 scoped_ptr<HostDispatcher> host_dispatcher_; | |
99 }; | 234 }; |
100 | 235 |
101 } // namespace proxy | 236 } // namespace proxy |
102 } // namespace pp | 237 } // namespace pp |
OLD | NEW |