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 <string> | 5 #include <string> |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
6 #include "base/file_path.h" | 9 #include "base/file_path.h" |
7 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
8 #include "base/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
9 #include "chrome/common/automation_messages.h" | 12 #include "chrome/common/automation_messages.h" |
10 #include "chrome_frame/cfproxy_private.h" | 13 #include "chrome_frame/cfproxy_private.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
12 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
13 #include "testing/gmock_mutant.h" | 16 #include "testing/gmock_mutant.h" |
14 | 17 |
15 using testing::_; | 18 using testing::_; |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 IPC::Channel::Listener* l) { | 97 IPC::Channel::Listener* l) { |
95 ipc_loop = MessageLoop::current(); | 98 ipc_loop = MessageLoop::current(); |
96 listener = l; | 99 listener = l; |
97 return this->DoCreateChannel(id, l); | 100 return this->DoCreateChannel(id, l); |
98 } | 101 } |
99 | 102 |
100 // Simulate some activity in the IPC thread. | 103 // Simulate some activity in the IPC thread. |
101 // You may find API_FIRE_XXXX macros (see below) handy instead. | 104 // You may find API_FIRE_XXXX macros (see below) handy instead. |
102 void FireConnect(base::TimeDelta t) { | 105 void FireConnect(base::TimeDelta t) { |
103 ASSERT_TRUE(ipc_loop != NULL); | 106 ASSERT_TRUE(ipc_loop != NULL); |
104 ipc_loop->PostDelayedTask(FROM_HERE, NewRunnableMethod(listener, | 107 ipc_loop->PostDelayedTask( |
105 &IPC::Channel::Listener::OnChannelConnected, 0), t.InMilliseconds()); | 108 FROM_HERE, base::Bind(&IPC::Channel::Listener::OnChannelConnected, |
| 109 base::Unretained(listener), 0), |
| 110 t.InMilliseconds()); |
106 } | 111 } |
107 | 112 |
108 void FireError(base::TimeDelta t) { | 113 void FireError(base::TimeDelta t) { |
109 ASSERT_TRUE(ipc_loop != NULL); | 114 ASSERT_TRUE(ipc_loop != NULL); |
110 ipc_loop->PostDelayedTask(FROM_HERE, NewRunnableMethod(listener, | 115 ipc_loop->PostDelayedTask( |
111 &IPC::Channel::Listener::OnChannelError), t.InMilliseconds()); | 116 FROM_HERE, base::Bind(&IPC::Channel::Listener::OnChannelError, |
| 117 base::Unretained(listener)), |
| 118 t.InMilliseconds()); |
112 } | 119 } |
113 | 120 |
114 void FireMessage(const IPC::Message& m, base::TimeDelta t) { | 121 void FireMessage(const IPC::Message& m, base::TimeDelta t) { |
115 ASSERT_TRUE(ipc_loop != NULL); | 122 ASSERT_TRUE(ipc_loop != NULL); |
116 ipc_loop->PostDelayedTask(FROM_HERE, NewRunnableMethod(listener, | 123 ipc_loop->PostDelayedTask( |
117 &IPC::Channel::Listener::OnMessageReceived, m), t.InMilliseconds()); | 124 FROM_HERE, |
| 125 base::IgnoreReturn<bool>( |
| 126 base::Bind(&IPC::Channel::Listener::OnMessageReceived, |
| 127 base::Unretained(listener), m)), |
| 128 t.InMilliseconds()); |
118 } | 129 } |
119 | 130 |
120 MockCFProxyTraits() : ipc_loop(NULL) {} | 131 MockCFProxyTraits() : ipc_loop(NULL) {} |
121 MockSender sender; | 132 MockSender sender; |
122 private: | 133 private: |
123 MessageLoop* ipc_loop; | 134 MessageLoop* ipc_loop; |
124 IPC::Channel::Listener* listener; | 135 IPC::Channel::Listener* listener; |
125 }; | 136 }; |
126 | 137 |
127 // Handy macros when we want so similate something on the IPC thread. | 138 // Handy macros when we want so simulate something on the IPC thread. |
128 #define API_FIRE_CONNECT(api, t) InvokeWithoutArgs(CreateFunctor(&api, \ | 139 #define API_FIRE_CONNECT(api, t) InvokeWithoutArgs(CreateFunctor(&api, \ |
129 &MockCFProxyTraits::FireConnect, t)) | 140 &MockCFProxyTraits::FireConnect, t)) |
130 #define API_FIRE_ERROR(api, t) InvokeWithoutArgs(CreateFunctor(&api, \ | 141 #define API_FIRE_ERROR(api, t) InvokeWithoutArgs(CreateFunctor(&api, \ |
131 &MockCFProxyTraits::FireError, t)) | 142 &MockCFProxyTraits::FireError, t)) |
132 #define API_FIRE_MESSAGE(api, t) InvokeWithoutArgs(CreateFunctor(&api, \ | 143 #define API_FIRE_MESSAGE(api, t) InvokeWithoutArgs(CreateFunctor(&api, \ |
133 &MockCFProxyTraits::FireMessage, t)) | 144 &MockCFProxyTraits::FireMessage, t)) |
134 DISABLE_RUNNABLE_METHOD_REFCOUNT(IPC::Channel::Listener); | |
135 | 145 |
136 TEST(ChromeProxy, DelegateAddRemove) { | 146 TEST(ChromeProxy, DelegateAddRemove) { |
137 StrictMock<MockCFProxyTraits> api; | 147 StrictMock<MockCFProxyTraits> api; |
138 StrictMock<MockChromeProxyDelegate> delegate; | 148 StrictMock<MockChromeProxyDelegate> delegate; |
139 StrictMock<MockFactory> factory; // to be destroyed before other mocks | 149 StrictMock<MockFactory> factory; // to be destroyed before other mocks |
140 CFProxy* proxy = new CFProxy(&api); | 150 CFProxy* proxy = new CFProxy(&api); |
141 | 151 |
142 EXPECT_CALL(factory, CreateProxy()).WillOnce(Return(proxy)); | 152 EXPECT_CALL(factory, CreateProxy()).WillOnce(Return(proxy)); |
143 EXPECT_CALL(api, DoCreateChannel(_, proxy)).WillOnce(Return(&api.sender)); | 153 EXPECT_CALL(api, DoCreateChannel(_, proxy)).WillOnce(Return(&api.sender)); |
144 EXPECT_CALL(api, LaunchApp(_)).WillOnce(Return(true)); | 154 EXPECT_CALL(api, LaunchApp(_)).WillOnce(Return(true)); |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 template <typename M, typename A, typename B, typename C, typename D> | 326 template <typename M, typename A, typename B, typename C, typename D> |
317 inline IPC::Message* CreateReply(M* m, const A& a, const B& b, const C& c, | 327 inline IPC::Message* CreateReply(M* m, const A& a, const B& b, const C& c, |
318 const D& d) { | 328 const D& d) { |
319 IPC::Message* r = IPC::SyncMessage::GenerateReply(m); | 329 IPC::Message* r = IPC::SyncMessage::GenerateReply(m); |
320 if (r) { | 330 if (r) { |
321 M::WriteReplyParams(r, a, b, c, d); | 331 M::WriteReplyParams(r, a, b, c, d); |
322 } | 332 } |
323 return r; | 333 return r; |
324 }} // namespace | 334 }} // namespace |
325 | 335 |
326 DISABLE_RUNNABLE_METHOD_REFCOUNT(SyncMsgSender); | |
327 TEST(SyncMsgSender, Deserialize) { | 336 TEST(SyncMsgSender, Deserialize) { |
328 // Note the ipc thread is not actually needed, but we try to be close | 337 // Note the ipc thread is not actually needed, but we try to be close |
329 // to real-world conditions - that SyncMsgSender works from multiple threads. | 338 // to real-world conditions - that SyncMsgSender works from multiple threads. |
330 base::Thread ipc("ipc"); | 339 base::Thread ipc("ipc"); |
331 ipc.StartWithOptions(base::Thread::Options(MessageLoop::TYPE_IO, 0)); | 340 ipc.StartWithOptions(base::Thread::Options(MessageLoop::TYPE_IO, 0)); |
332 | 341 |
333 StrictMock<MockChromeProxyDelegate> d1; | 342 StrictMock<MockChromeProxyDelegate> d1; |
334 TabsMap tab2delegate; | 343 TabsMap tab2delegate; |
335 SyncMsgSender queue(&tab2delegate); | 344 SyncMsgSender queue(&tab2delegate); |
336 | 345 |
337 const int kTabHandle = 6; | 346 const int kTabHandle = 6; |
338 const int kSessionId = 8; | 347 const int kSessionId = 8; |
339 | 348 |
340 // Create a sync message and its reply. | 349 // Create a sync message and its reply. |
341 AutomationMsg_CreateExternalTab m(ExternalTabSettings(), 0, 0, 0, 0); | 350 AutomationMsg_CreateExternalTab m(ExternalTabSettings(), 0, 0, 0, 0); |
342 scoped_ptr<IPC::Message> r(CreateReply(&m, (HWND)1, (HWND)2, kTabHandle, | 351 scoped_ptr<IPC::Message> r(CreateReply(&m, (HWND)1, (HWND)2, kTabHandle, |
343 kSessionId)); | 352 kSessionId)); |
344 | 353 |
345 queue.QueueSyncMessage(&m, &d1, NULL); | 354 queue.QueueSyncMessage(&m, &d1, NULL); |
346 | 355 |
347 testing::InSequence s; | 356 testing::InSequence s; |
348 EXPECT_CALL(d1, Completed_CreateTab(true, (HWND)1, (HWND)2, kTabHandle, | 357 EXPECT_CALL(d1, Completed_CreateTab(true, (HWND)1, (HWND)2, kTabHandle, |
349 kSessionId)); | 358 kSessionId)); |
350 | 359 |
351 // Execute replies in a worker thread. | 360 // Execute replies in a worker thread. |
352 ipc.message_loop()->PostTask(FROM_HERE, NewRunnableMethod(&queue, | 361 ipc.message_loop()->PostTask( |
353 &SyncMsgSender::OnReplyReceived, r.get())); | 362 FROM_HERE, |
| 363 base::IgnoreReturn<bool>(base::Bind(&SyncMsgSender::OnReplyReceived, |
| 364 base::Unretained(&queue), r.get()))); |
354 ipc.Stop(); | 365 ipc.Stop(); |
355 | 366 |
356 // Expect that tab 6 has been associated with the delegate. | 367 // Expect that tab 6 has been associated with the delegate. |
357 EXPECT_EQ(&d1, tab2delegate[6]); | 368 EXPECT_EQ(&d1, tab2delegate[6]); |
358 } | 369 } |
359 | 370 |
360 TEST(SyncMsgSender, OnChannelClosed) { | 371 TEST(SyncMsgSender, OnChannelClosed) { |
361 // TODO(stoyan): implement. | 372 // TODO(stoyan): implement. |
362 } | 373 } |
OLD | NEW |