1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome_frame/test/automation_client_mock.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "chrome/common/automation_messages.h"
#include "chrome_frame/custom_sync_call_context.h"
#include "chrome_frame/navigation_constraints.h"
#include "chrome_frame/test/chrome_frame_test_utils.h"
#include "chrome_frame/test/test_scrubber.h"
#include "net/base/net_errors.h"
#define GMOCK_MUTANT_INCLUDE_LATE_OBJECT_BINDING
#include "testing/gmock_mutant.h"
using testing::_;
using testing::CreateFunctor;
using testing::Return;
namespace {
#ifndef NDEBUG
const base::TimeDelta kChromeLaunchTimeout = base::TimeDelta::FromSeconds(15);
#else
const base::TimeDelta kChromeLaunchTimeout = base::TimeDelta::FromSeconds(10);
#endif
const int kSaneAutomationTimeoutMs = 10 * 1000;
} // namespace
MATCHER_P(LaunchParamProfileEq, profile_name, "Check for profile name") {
return arg->profile_name().compare(profile_name) == 0;
}
void MockProxyFactory::GetServerImpl(ChromeFrameAutomationProxy* pxy,
void* proxy_id,
AutomationLaunchResult result,
LaunchDelegate* d,
ChromeFrameLaunchParams* params,
void** automation_server_id) {
*automation_server_id = proxy_id;
loop_->PostDelayedTask(FROM_HERE,
base::Bind(&LaunchDelegate::LaunchComplete,
base::Unretained(d), pxy, result),
base::TimeDelta::FromMilliseconds(params->launch_timeout()) / 2);
}
void CFACMockTest::SetAutomationServerOk(int times) {
EXPECT_CALL(factory_, GetAutomationServer(testing::NotNull(),
LaunchParamProfileEq(profile_path_.BaseName().value()),
testing::NotNull()))
.Times(times)
.WillRepeatedly(testing::Invoke(CreateFunctor(&factory_,
&MockProxyFactory::GetServerImpl, get_proxy(), id_,
AUTOMATION_SUCCESS)));
EXPECT_CALL(factory_,
ReleaseAutomationServer(testing::Eq(id_), testing::NotNull()))
.Times(times);
}
void CFACMockTest::Set_CFD_LaunchFailed(AutomationLaunchResult result) {
EXPECT_CALL(cfd_, OnAutomationServerLaunchFailed(testing::Eq(result),
testing::_))
.Times(1)
.WillOnce(QUIT_LOOP(loop_));
}
MATCHER_P(MsgType, msg_type, "IPC::Message::type()") {
const IPC::Message& m = arg;
return (m.type() == msg_type);
}
MATCHER_P(EqNavigationInfoUrl, url, "IPC::NavigationInfo matcher") {
if (url.is_valid() && url != arg.url)
return false;
// TODO(stevet): other members
return true;
}
// Could be implemented as MockAutomationProxy member (we have WithArgs<>!)
ACTION_P4(HandleCreateTab, tab_handle, external_tab_container, tab_wnd,
session_id) {
// arg0 - message
// arg1 - callback
// arg2 - key
CreateExternalTabContext::output_type input_args(tab_wnd,
external_tab_container,
tab_handle,
session_id);
CreateExternalTabContext* context =
reinterpret_cast<CreateExternalTabContext*>(arg1);
DispatchToMethod(context, &CreateExternalTabContext::Completed, input_args);
delete context;
}
ACTION_P4(InitiateNavigation, client, url, referrer, constraints) {
client->InitiateNavigation(url, referrer, constraints);
}
// ChromeFrameAutomationClient tests that launch Chrome.
class CFACWithChrome : public testing::Test {
protected:
static void SetUpTestCase();
static void TearDownTestCase();
virtual void SetUp() OVERRIDE;
virtual void TearDown() OVERRIDE;
static base::FilePath profile_path_;
MockCFDelegate cfd_;
scoped_refptr<ChromeFrameAutomationClient> client_;
scoped_refptr<ChromeFrameLaunchParams> launch_params_;
chrome_frame_test::TimedMsgLoop loop_;
};
// static
base::FilePath CFACWithChrome::profile_path_;
// static
void CFACWithChrome::SetUpTestCase() {
GetChromeFrameProfilePath(L"Adam.N.Epilinter", &profile_path_);
}
// static
void CFACWithChrome::TearDownTestCase() {
profile_path_.clear();
}
void CFACWithChrome::SetUp() {
chrome_frame_test::OverrideDataDirectoryForThisTest(profile_path_.value());
client_ = new ChromeFrameAutomationClient();
GURL empty;
launch_params_ = new ChromeFrameLaunchParams(
empty, empty, profile_path_, profile_path_.BaseName().value(), L"",
false, false, false);
launch_params_->set_version_check(false);
launch_params_->set_launch_timeout(kSaneAutomationTimeoutMs);
}
void CFACWithChrome::TearDown() {
client_->Uninitialize();
}
// We mock ChromeFrameDelegate only. The rest is with real AutomationProxy
TEST_F(CFACWithChrome, CreateTooFast) {
int timeout = 0; // Chrome cannot send Hello message so fast.
EXPECT_CALL(cfd_, OnAutomationServerLaunchFailed(AUTOMATION_TIMEOUT, _))
.WillOnce(QUIT_LOOP(loop_));
launch_params_->set_launch_timeout(timeout);
EXPECT_TRUE(client_->Initialize(&cfd_, launch_params_));
loop_.RunFor(kChromeLaunchTimeout);
}
// This test may fail if Chrome take more that 10 seconds (timeout var) to
// launch. In this case GMock shall print something like "unexpected call to
// OnAutomationServerLaunchFailed". I'm yet to find out how to specify
// that this is an unexpected call, and still to execute an action.
TEST_F(CFACWithChrome, CreateNotSoFast) {
EXPECT_CALL(cfd_, OnAutomationServerReady())
.WillOnce(QUIT_LOOP(loop_));
EXPECT_CALL(cfd_, OnAutomationServerLaunchFailed(_, _))
.Times(0);
EXPECT_TRUE(client_->Initialize(&cfd_, launch_params_));
loop_.RunFor(kChromeLaunchTimeout);
}
TEST_F(CFACWithChrome, NavigateOk) {
NavigationConstraintsImpl navigation_constraints;
const std::string url = "about:version";
EXPECT_CALL(cfd_, OnAutomationServerReady())
.WillOnce(InitiateNavigation(client_.get(), url, std::string(),
&navigation_constraints));
EXPECT_CALL(cfd_, GetBounds(_)).Times(testing::AnyNumber());
EXPECT_CALL(cfd_, OnNavigationStateChanged(_))
.Times(testing::AnyNumber());
{
testing::InSequence s;
EXPECT_CALL(cfd_, OnDidNavigate(EqNavigationInfoUrl(GURL())))
.Times(1);
EXPECT_CALL(cfd_, OnUpdateTargetUrl(_)).Times(testing::AtMost(1));
EXPECT_CALL(cfd_, OnLoad(_))
.WillOnce(QUIT_LOOP(loop_));
}
EXPECT_TRUE(client_->Initialize(&cfd_, launch_params_));
loop_.RunFor(kChromeLaunchTimeout);
}
TEST_F(CFACWithChrome, NavigateFailed) {
NavigationConstraintsImpl navigation_constraints;
const std::string url = "http://127.0.0.3:65412/";
const net::URLRequestStatus connection_failed(net::URLRequestStatus::FAILED,
net::ERR_INVALID_URL);
cfd_.SetRequestDelegate(client_);
EXPECT_CALL(cfd_, OnAutomationServerReady())
.WillOnce(testing::IgnoreResult(testing::InvokeWithoutArgs(CreateFunctor(
client_.get(), &ChromeFrameAutomationClient::InitiateNavigation,
url, std::string(), &navigation_constraints))));
EXPECT_CALL(cfd_, GetBounds(_)).Times(testing::AnyNumber());
EXPECT_CALL(cfd_, OnNavigationStateChanged(_)).Times(testing::AnyNumber());
EXPECT_CALL(cfd_, OnRequestStart(_, _))
// Often there's another request for the error page
.Times(testing::Between(1, 2))
.WillRepeatedly(testing::WithArgs<0>(testing::Invoke(CreateFunctor(&cfd_,
&MockCFDelegate::Reply, connection_failed))));
EXPECT_CALL(cfd_, OnUpdateTargetUrl(_)).Times(testing::AnyNumber());
EXPECT_CALL(cfd_, OnLoad(_)).Times(testing::AtMost(1));
EXPECT_CALL(cfd_, OnNavigationFailed(_, GURL(url)))
.Times(1)
.WillOnce(QUIT_LOOP_SOON(loop_, base::TimeDelta::FromSeconds(2)));
EXPECT_TRUE(client_->Initialize(&cfd_, launch_params_));
loop_.RunFor(kChromeLaunchTimeout);
}
TEST_F(CFACMockTest, MockedCreateTabOk) {
int timeout = 500;
CreateTab();
SetAutomationServerOk(1);
EXPECT_CALL(mock_proxy_, server_version()).Times(testing::AnyNumber())
.WillRepeatedly(Return(""));
// We need some valid HWNDs, when responding to CreateExternalTab
HWND h1 = ::GetDesktopWindow();
HWND h2 = ::GetDesktopWindow();
EXPECT_CALL(mock_proxy_, SendAsAsync(testing::Property(
&IPC::SyncMessage::type, AutomationMsg_CreateExternalTab::ID),
testing::NotNull(), _))
.Times(1).WillOnce(HandleCreateTab(tab_handle_, h1, h2, 99));
EXPECT_CALL(mock_proxy_, CreateTabProxy(testing::Eq(tab_handle_)))
.WillOnce(Return(tab_));
EXPECT_CALL(cfd_, OnAutomationServerReady())
.WillOnce(QUIT_LOOP(loop_));
EXPECT_CALL(mock_proxy_, CancelAsync(_)).Times(testing::AnyNumber());
// Here we go!
GURL empty;
scoped_refptr<ChromeFrameLaunchParams> clp(new ChromeFrameLaunchParams(
empty, empty, profile_path_, profile_path_.BaseName().value(), L"",
false, false, false));
clp->set_launch_timeout(timeout);
clp->set_version_check(false);
EXPECT_TRUE(client_->Initialize(&cfd_, clp));
loop_.RunFor(base::TimeDelta::FromSeconds(10));
EXPECT_CALL(mock_proxy_, ReleaseTabProxy(testing::Eq(tab_handle_))).Times(1);
client_->Uninitialize();
}
TEST_F(CFACMockTest, MockedCreateTabFailed) {
HWND null_wnd = NULL;
SetAutomationServerOk(1);
EXPECT_CALL(mock_proxy_, server_version()).Times(testing::AnyNumber())
.WillRepeatedly(Return(""));
EXPECT_CALL(mock_proxy_, SendAsAsync(testing::Property(
&IPC::SyncMessage::type, AutomationMsg_CreateExternalTab::ID),
testing::NotNull(), _))
.Times(1).WillOnce(HandleCreateTab(tab_handle_, null_wnd, null_wnd,
99));
EXPECT_CALL(mock_proxy_, CreateTabProxy(_)).Times(0);
EXPECT_CALL(mock_proxy_, CancelAsync(_)).Times(testing::AnyNumber());
Set_CFD_LaunchFailed(AUTOMATION_CREATE_TAB_FAILED);
// Here we go!
GURL empty;
scoped_refptr<ChromeFrameLaunchParams> clp(new ChromeFrameLaunchParams(
empty, empty, profile_path_, profile_path_.BaseName().value(), L"",
false, false, false));
clp->set_launch_timeout(timeout_);
clp->set_version_check(false);
EXPECT_TRUE(client_->Initialize(&cfd_, clp));
loop_.RunFor(base::TimeDelta::FromSeconds(4));
client_->Uninitialize();
}
class TestChromeFrameAutomationProxyImpl
: public ChromeFrameAutomationProxyImpl {
public:
TestChromeFrameAutomationProxyImpl()
// 1 is an unneeded timeout.
: ChromeFrameAutomationProxyImpl(
NULL,
AutomationProxy::GenerateChannelID(),
base::TimeDelta::FromMilliseconds(1)) {
}
MOCK_METHOD3(
SendAsAsync,
void(IPC::SyncMessage* msg,
SyncMessageReplyDispatcher::SyncMessageCallContext* context,
void* key));
void FakeChannelError() {
reinterpret_cast<IPC::ChannelProxy::MessageFilter*>(message_filter_.get())->
OnChannelError();
}
};
TEST_F(CFACMockTest, OnChannelErrorEmpty) {
TestChromeFrameAutomationProxyImpl proxy;
// No tabs should do nothing yet still not fail either.
proxy.FakeChannelError();
}
TEST_F(CFACMockTest, OnChannelError) {
const base::TimeDelta loop_duration = base::TimeDelta::FromSeconds(11);
TestChromeFrameAutomationProxyImpl proxy;
returned_proxy_ = &proxy;
GURL empty;
scoped_refptr<ChromeFrameLaunchParams> clp(new ChromeFrameLaunchParams(
empty, empty, profile_path_, profile_path_.BaseName().value(), L"",
false, false, false));
clp->set_launch_timeout(1); // Unneeded timeout, but can't be 0.
clp->set_version_check(false);
HWND h1 = ::GetDesktopWindow();
HWND h2 = ::GetDesktopWindow();
EXPECT_CALL(proxy, SendAsAsync(testing::Property(
&IPC::SyncMessage::type, AutomationMsg_CreateExternalTab::ID),
testing::NotNull(), _)).Times(3)
.WillOnce(HandleCreateTab(tab_handle_, h1, h2, 99))
.WillOnce(HandleCreateTab(tab_handle_ * 2, h1, h2, 100))
.WillOnce(HandleCreateTab(tab_handle_ * 3, h1, h2, 101));
SetAutomationServerOk(3);
// First, try a single tab and make sure the notification find its way to the
// Chrome Frame Delegate.
StrictMock<MockCFDelegate> cfd1;
scoped_refptr<ChromeFrameAutomationClient> client1;
client1 = new ChromeFrameAutomationClient;
client1->set_proxy_factory(&factory_);
EXPECT_CALL(cfd1, OnAutomationServerReady()).WillOnce(QUIT_LOOP(loop_));
EXPECT_TRUE(client1->Initialize(&cfd1, clp));
// Wait for OnAutomationServerReady to be called in the UI thread.
loop_.RunFor(loop_duration);
proxy.FakeChannelError();
EXPECT_CALL(cfd1, OnChannelError()).WillOnce(QUIT_LOOP(loop_));
// Wait for OnChannelError to be propagated to delegate from the UI thread.
loop_.RunFor(loop_duration);
// Add a second tab using a different delegate.
StrictMock<MockCFDelegate> cfd2;
scoped_refptr<ChromeFrameAutomationClient> client2;
client2 = new ChromeFrameAutomationClient;
client2->set_proxy_factory(&factory_);
EXPECT_CALL(cfd2, OnAutomationServerReady()).WillOnce(QUIT_LOOP(loop_));
EXPECT_TRUE(client2->Initialize(&cfd2, clp));
// Wait for OnAutomationServerReady to be called in the UI thread.
loop_.RunFor(loop_duration);
EXPECT_CALL(cfd1, OnChannelError()).Times(1);
EXPECT_CALL(cfd2, OnChannelError()).WillOnce(QUIT_LOOP(loop_));
proxy.FakeChannelError();
// Wait for OnChannelError to be propagated to delegate from the UI thread.
loop_.RunFor(loop_duration);
// And now a 3rd tab using the first delegate.
scoped_refptr<ChromeFrameAutomationClient> client3;
client3 = new ChromeFrameAutomationClient;
client3->set_proxy_factory(&factory_);
EXPECT_CALL(cfd1, OnAutomationServerReady()).WillOnce(QUIT_LOOP(loop_));
EXPECT_TRUE(client3->Initialize(&cfd1, clp));
// Wait for OnAutomationServerReady to be called in the UI thread.
loop_.RunFor(loop_duration);
EXPECT_CALL(cfd2, OnChannelError()).Times(1);
EXPECT_CALL(cfd1, OnChannelError()).Times(2).WillOnce(Return())
.WillOnce(QUIT_LOOP(loop_));
proxy.FakeChannelError();
// Wait for OnChannelError to be propagated to delegate from the UI thread.
loop_.RunFor(loop_duration);
// Cleanup.
client1->Uninitialize();
client2->Uninitialize();
client3->Uninitialize();
client1 = NULL;
client2 = NULL;
client3 = NULL;
}
TEST_F(CFACMockTest, NavigateTwiceAfterInitToSameUrl) {
int timeout = 500;
NavigationConstraintsImpl navigation_constraints;
CreateTab();
SetAutomationServerOk(1);
EXPECT_CALL(mock_proxy_, server_version()).Times(testing::AnyNumber())
.WillRepeatedly(Return(""));
// We need some valid HWNDs, when responding to CreateExternalTab
HWND h1 = ::GetDesktopWindow();
HWND h2 = ::GetDesktopWindow();
EXPECT_CALL(mock_proxy_, SendAsAsync(testing::Property(
&IPC::SyncMessage::type, AutomationMsg_CreateExternalTab::ID),
testing::NotNull(), _))
.Times(1).WillOnce(HandleCreateTab(tab_handle_, h1, h2, 99));
EXPECT_CALL(mock_proxy_, CreateTabProxy(testing::Eq(tab_handle_)))
.WillOnce(Return(tab_));
EXPECT_CALL(cfd_, OnAutomationServerReady())
.WillOnce(InitiateNavigation(client_.get(),
std::string("http://www.nonexistent.com"),
std::string(), &navigation_constraints));
EXPECT_CALL(mock_proxy_, SendAsAsync(testing::Property(
&IPC::SyncMessage::type, AutomationMsg_NavigateInExternalTab::ID),
testing::NotNull(), _))
.Times(1).WillOnce(QUIT_LOOP(loop_));
EXPECT_CALL(mock_proxy_, CancelAsync(_)).Times(testing::AnyNumber());
EXPECT_CALL(mock_proxy_, Send(
testing::Property(&IPC::Message::type, AutomationMsg_TabReposition::ID)))
.Times(1)
.WillOnce(Return(true));
EXPECT_CALL(cfd_, GetBounds(_)).Times(1);
// Here we go!
GURL empty;
scoped_refptr<ChromeFrameLaunchParams> launch_params(
new ChromeFrameLaunchParams(
GURL("http://www.nonexistent.com"), empty, profile_path_,
profile_path_.BaseName().value(), L"", false, false, false));
launch_params->set_launch_timeout(timeout);
launch_params->set_version_check(false);
EXPECT_TRUE(client_->Initialize(&cfd_, launch_params));
loop_.RunFor(base::TimeDelta::FromSeconds(10));
EXPECT_CALL(mock_proxy_, ReleaseTabProxy(testing::Eq(tab_handle_))).Times(1);
client_->Uninitialize();
}
|