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
|
// 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 "base/file_util.h"
#include "base/path_service.h"
#include "base/prefs/testing_pref_service.h"
#include "base/string_util.h"
#include "base/strings/string_split.h"
#include "base/threading/sequenced_worker_pool.h"
#include "base/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/page_cycler/page_cycler.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/test/test_browser_thread.h"
#include "net/base/net_errors.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::_;
using ::testing::Invoke;
using content::RenderViewHost;
using content::TestBrowserThread;
using content::WebContentsObserver;
using file_util::ContentsEqual;
using file_util::PathExists;
namespace {
const int kFrameID = 1;
const bool kIsMainFrame = true;
const GURL kAboutURL = GURL(content::kAboutBlankURL);
} // namespace
class MockPageCycler : public PageCycler {
public:
MockPageCycler(Browser* browser, base::FilePath urls_file,
base::FilePath errors_file)
: PageCycler(browser, urls_file) {
set_errors_file(errors_file);
}
MockPageCycler(Browser* browser,
base::FilePath urls_file,
base::FilePath errors_file,
base::FilePath stats_file)
: PageCycler(browser, urls_file) {
set_stats_file(stats_file);
set_errors_file(errors_file);
}
MOCK_METHOD4(DidFinishLoad, void(int64 frame_id,
const GURL& validated_url,
bool is_main_frame,
RenderViewHost* render_view_host));
MOCK_METHOD6(DidFailProvisionalLoad, void(int64 frame_id,
bool is_main_frame,
const GURL& validated_url,
int error_code,
const string16& error_description,
RenderViewHost* render_view_host));
MOCK_METHOD1(RenderViewGone, void(base::TerminationStatus status));
void PageCyclerDidFailProvisionalLoad(
int64 frame_id,
bool is_main_frame,
const GURL& validated_url,
int error_code,
const string16& error_description,
RenderViewHost* render_view_host) {
PageCycler::DidFailProvisionalLoad(frame_id, is_main_frame,
validated_url,
error_code, error_description,
render_view_host);
}
void PageCyclerDidFinishLoad(int64 frame_id,
const GURL& validated_url,
bool is_main_frame,
RenderViewHost* render_view_host) {
PageCycler::DidFinishLoad(
frame_id, validated_url, is_main_frame, render_view_host);
}
private:
// We need to override Finish() because the calls to exit the browser in a
// real PageCycler do not work in unittests (they interfere with later tests).
virtual void Finish() OVERRIDE {
BrowserList::RemoveObserver(this);
Release();
}
virtual ~MockPageCycler() {}\
DISALLOW_COPY_AND_ASSIGN(MockPageCycler);
};
class PageCyclerTest : public BrowserWithTestWindowTest {
public:
PageCyclerTest() {
}
virtual ~PageCyclerTest() {
}
virtual void SetUp() OVERRIDE {
PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_);
test_data_dir_ = test_data_dir_.AppendASCII("page_cycler");
BrowserWithTestWindowTest::SetUp();
AddTab(browser(), kAboutURL);
ASSERT_FALSE(browser()->tab_strip_model()->GetActiveWebContents() == NULL);
}
void InitFilePaths(const base::FilePath& temp_path) {
errors_file_ = temp_path.AppendASCII("errors_file");
stats_file_ = temp_path.AppendASCII("stats_file");
CHECK(!file_util::PathExists(errors_file_));
CHECK(!file_util::PathExists(stats_file_));
}
void FailProvisionalLoad(int error_code, string16& error_description) {
FOR_EACH_OBSERVER(
WebContentsObserver,
observers_,
DidFailProvisionalLoad(kFrameID, kIsMainFrame, kAboutURL, error_code,
error_description, NULL));
PumpLoop();
}
void FinishLoad() {
FOR_EACH_OBSERVER(
WebContentsObserver,
observers_,
DidFinishLoad(kFrameID, kAboutURL, kIsMainFrame, NULL));
PumpLoop();
}
void RunPageCycler() {
page_cycler_->Run();
PumpLoop();
}
void PumpLoop() {
content::BrowserThread::GetBlockingPool()->FlushForTesting();
message_loop()->RunUntilIdle();
}
void CloseBrowser() {
DestroyBrowserAndProfile();
PumpLoop();
}
MockPageCycler* page_cycler() {
return page_cycler_.get();
}
void set_page_cycler(MockPageCycler* page_cycler) {
page_cycler_ = page_cycler;
observers_.AddObserver(page_cycler);
}
const std::vector<GURL>* urls_for_test() {
return page_cycler_->urls_for_test();
}
base::FilePath stats_file() {
return stats_file_;
}
base::FilePath errors_file() {
return errors_file_;
}
base::FilePath urls_file() {
return test_data_dir_.AppendASCII("about_url");
}
base::FilePath test_data_dir() {
return test_data_dir_;
}
private:
ObserverList<WebContentsObserver> observers_;
scoped_refptr<MockPageCycler> page_cycler_;
base::FilePath test_data_dir_;
base::FilePath stats_file_;
base::FilePath errors_file_;
base::FilePath urls_file_;
};
TEST_F(PageCyclerTest, FailProvisionalLoads) {
const base::FilePath errors_expected_file =
test_data_dir().AppendASCII("errors_expected");
base::ScopedTempDir temp;
ASSERT_TRUE(temp.CreateUniqueTempDir());
InitFilePaths(temp.path());
ASSERT_TRUE(PathExists(errors_expected_file));
ASSERT_TRUE(PathExists(urls_file()));
set_page_cycler(new MockPageCycler(browser(),
urls_file(),
errors_file()));
RunPageCycler();
// Page cycler expects browser to automatically start loading the first page.
EXPECT_CALL(*page_cycler(),
DidFinishLoad(kFrameID, kAboutURL, kIsMainFrame, _))
.WillOnce(Invoke(page_cycler(),
&MockPageCycler::PageCyclerDidFinishLoad));
FinishLoad();
// DNS server fail error message.
string16 error_string =
string16(ASCIIToUTF16(net::ErrorToString(net::ERR_DNS_SERVER_FAILED)));
EXPECT_CALL(*page_cycler(),
DidFailProvisionalLoad(kFrameID, kIsMainFrame, _,
net::ERR_DNS_SERVER_FAILED, error_string,
_))
.WillOnce(Invoke(page_cycler(),
&MockPageCycler::PageCyclerDidFailProvisionalLoad));
FailProvisionalLoad(net::ERR_DNS_SERVER_FAILED, error_string);
// DNS time-out error message.
error_string = string16(
ASCIIToUTF16(net::ErrorToString(net::ERR_DNS_TIMED_OUT)));
EXPECT_CALL(*page_cycler(),
DidFailProvisionalLoad(kFrameID,
kIsMainFrame, _, net::ERR_DNS_TIMED_OUT,
error_string, _))
.WillOnce(Invoke(page_cycler(),
&MockPageCycler::PageCyclerDidFailProvisionalLoad));
FailProvisionalLoad(net::ERR_DNS_TIMED_OUT, error_string);
// DNS time-out error message.
error_string = string16(
ASCIIToUTF16(net::ErrorToString(net::ERR_INVALID_URL)));
EXPECT_CALL(*page_cycler(),
DidFailProvisionalLoad(kFrameID, kIsMainFrame, _,
net::ERR_INVALID_URL, error_string, _))
.WillOnce(Invoke(page_cycler(),
&MockPageCycler::PageCyclerDidFailProvisionalLoad));
FailProvisionalLoad(net::ERR_INVALID_URL, error_string);
PumpLoop();
std::string errors_output;
std::string errors_expected;
ASSERT_TRUE(file_util::ReadFileToString(errors_file(),
&errors_output));
ASSERT_TRUE(file_util::ReadFileToString(errors_expected_file,
&errors_expected));
ASSERT_EQ(errors_output, errors_expected);
}
TEST_F(PageCyclerTest, StatsFile) {
const int kNumLoads = 4;
base::ScopedTempDir temp;
ASSERT_TRUE(temp.CreateUniqueTempDir());
InitFilePaths(temp.path());
ASSERT_TRUE(PathExists(urls_file()));
set_page_cycler(new MockPageCycler(browser(), urls_file(),
errors_file()));
page_cycler()->set_stats_file(stats_file());
RunPageCycler();
for (int i = 0; i < kNumLoads; ++i) {
EXPECT_CALL(*page_cycler(), DidFinishLoad(
kFrameID, kAboutURL, kIsMainFrame, _))
.WillOnce(Invoke(page_cycler(),
&MockPageCycler::PageCyclerDidFinishLoad));
FinishLoad();
}
PumpLoop();
EXPECT_FALSE(PathExists(errors_file()));
ASSERT_TRUE(PathExists(stats_file()));
}
TEST_F(PageCyclerTest, KillBrowserAndAbort) {
const base::FilePath errors_expected_file =
test_data_dir().AppendASCII("abort_expected");
base::ScopedTempDir temp;
ASSERT_TRUE(temp.CreateUniqueTempDir());
InitFilePaths(temp.path());
ASSERT_TRUE(PathExists(errors_expected_file));
ASSERT_TRUE(PathExists(urls_file()));
set_page_cycler(new MockPageCycler(browser(),
urls_file(),
errors_file()));
RunPageCycler();
EXPECT_CALL(*page_cycler(),
DidFinishLoad(kFrameID, kAboutURL, kIsMainFrame, _))
.WillOnce(Invoke(page_cycler(),
&MockPageCycler::PageCyclerDidFinishLoad));
message_loop()->RunUntilIdle();
FinishLoad();
CloseBrowser();
PumpLoop();
std::string errors_output;
std::string errors_expected;
ASSERT_TRUE(file_util::ReadFileToString(errors_file(),
&errors_output));
ASSERT_TRUE(file_util::ReadFileToString(errors_expected_file,
&errors_expected));
ASSERT_EQ(errors_output, errors_expected);
}
TEST_F(PageCyclerTest, MultipleIterations) {
const int kNumLoads = 4;
base::ScopedTempDir temp;
ASSERT_TRUE(temp.CreateUniqueTempDir());
InitFilePaths(temp.path());
ASSERT_TRUE(PathExists(urls_file()));
set_page_cycler(new MockPageCycler(browser(),
urls_file(),
errors_file()));
page_cycler()->set_stats_file(stats_file());
RunPageCycler();
EXPECT_CALL(*page_cycler(),
DidFinishLoad(kFrameID, kAboutURL, kIsMainFrame, _))
.WillRepeatedly(Invoke(page_cycler(),
&MockPageCycler::PageCyclerDidFinishLoad));
for (int i = 0; i < kNumLoads; ++i)
FinishLoad();
PumpLoop();
EXPECT_FALSE(PathExists(errors_file()));
ASSERT_TRUE(PathExists(stats_file()));
}
|