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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
|
// 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/bind.h"
#include "base/bind_helpers.h"
#include "base/command_line.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/path_service.h"
#include "base/prefs/public/pref_member.h"
#include "base/test/test_file_util.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/download/chrome_download_manager_delegate.h"
#include "chrome/browser/download/download_history.h"
#include "chrome/browser/download/download_prefs.h"
#include "chrome/browser/download/download_service.h"
#include "chrome/browser/download/download_service_factory.h"
#include "chrome/browser/history/download_row.h"
#include "chrome/browser/net/url_request_mock_util.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/download_item.h"
#include "content/public/browser/download_manager.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/test_utils.h"
#include "content/test/net/url_request_mock_http_job.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_CHROMEOS)
#include "chrome/browser/download/save_package_file_picker_chromeos.h"
#else
#include "chrome/browser/download/save_package_file_picker.h"
#endif
using content::BrowserContext;
using content::BrowserThread;
using content::DownloadItem;
using content::DownloadManager;
using content::URLRequestMockHTTPJob;
using content::WebContents;
namespace {
// Waits for an item record in the downloads database to match |filter|. See
// DownloadStoredProperly() below for an example filter.
class DownloadPersistedObserver : public DownloadHistory::Observer {
public:
typedef base::Callback<bool(
DownloadItem* item,
const history::DownloadRow&)> PersistedFilter;
DownloadPersistedObserver(Profile* profile, const PersistedFilter& filter)
: profile_(profile),
filter_(filter) {
DownloadServiceFactory::GetForProfile(profile_)->
GetDownloadHistory()->AddObserver(this);
}
virtual ~DownloadPersistedObserver() {
DownloadService* service = DownloadServiceFactory::GetForProfile(profile_);
if (service && service->GetDownloadHistory())
service->GetDownloadHistory()->RemoveObserver(this);
}
bool WaitForPersisted() {
if (persisted_)
return true;
waiting_ = true;
content::RunMessageLoop();
waiting_ = false;
return persisted_;
}
virtual void OnDownloadStored(DownloadItem* item,
const history::DownloadRow& info) {
persisted_ = filter_.Run(item, info);
if (persisted_ && waiting_)
MessageLoopForUI::current()->Quit();
}
private:
Profile* profile_;
DownloadItem* item_;
PersistedFilter filter_;
bool waiting_;
bool persisted_;
DISALLOW_COPY_AND_ASSIGN(DownloadPersistedObserver);
};
// Waits for an item record to be removed from the downloads database.
class DownloadRemovedObserver : public DownloadPersistedObserver {
public:
DownloadRemovedObserver(Profile* profile, int32 download_id)
: DownloadPersistedObserver(profile, PersistedFilter()),
removed_(false),
waiting_(false),
download_id_(download_id) {
}
virtual ~DownloadRemovedObserver() {}
bool WaitForRemoved() {
if (removed_)
return true;
waiting_ = true;
content::RunMessageLoop();
waiting_ = false;
return removed_;
}
virtual void OnDownloadStored(DownloadItem* item,
const history::DownloadRow& info) {
}
virtual void OnDownloadsRemoved(const DownloadHistory::IdSet& ids) {
removed_ = ids.find(download_id_) != ids.end();
if (removed_ && waiting_)
MessageLoopForUI::current()->Quit();
}
private:
bool removed_;
bool waiting_;
int32 download_id_;
DISALLOW_COPY_AND_ASSIGN(DownloadRemovedObserver);
};
bool DownloadStoredProperly(
const GURL& expected_url,
const FilePath& expected_path,
int64 num_files,
DownloadItem::DownloadState expected_state,
DownloadItem* item,
const history::DownloadRow& info) {
// This function may be called multiple times for a given test. Returning
// false doesn't necessarily mean that the test has failed or will fail, it
// might just mean that the test hasn't passed yet.
if (info.path != expected_path) {
VLOG(20) << __FUNCTION__ << " " << info.path.value()
<< " != " << expected_path.value();
return false;
}
if (info.url != expected_url) {
VLOG(20) << __FUNCTION__ << " " << info.url.spec()
<< " != " << expected_url.spec();
return false;
}
if ((num_files >= 0) && (info.received_bytes != num_files)) {
VLOG(20) << __FUNCTION__ << " " << num_files
<< " != " << info.received_bytes;
return false;
}
if (info.state != expected_state) {
VLOG(20) << __FUNCTION__ << " " << info.state
<< " != " << expected_state;
return false;
}
return true;
}
const FilePath::CharType kTestDir[] = FILE_PATH_LITERAL("save_page");
static const char kAppendedExtension[] =
#if defined(OS_WIN)
".htm";
#else
".html";
#endif
// Loosely based on logic in DownloadTestObserver.
class DownloadItemCreatedObserver : public DownloadManager::Observer {
public:
explicit DownloadItemCreatedObserver(DownloadManager* manager)
: waiting_(false), manager_(manager) {
manager->AddObserver(this);
}
~DownloadItemCreatedObserver() {
if (manager_)
manager_->RemoveObserver(this);
}
// Wait for the first download item created after object creation.
// Note that this class provides no protection against the download
// being destroyed between creation and return of WaitForNewDownloadItem();
// the caller must guarantee that in some other fashion.
void WaitForDownloadItem(std::vector<DownloadItem*>* items_seen) {
if (!manager_) {
// The manager went away before we were asked to wait; return
// what we have, even if it's null.
*items_seen = items_seen_;
return;
}
if (items_seen_.empty()) {
waiting_ = true;
content::RunMessageLoop();
waiting_ = false;
}
*items_seen = items_seen_;
return;
}
private:
// DownloadManager::Observer
virtual void OnDownloadCreated(
DownloadManager* manager, DownloadItem* item) OVERRIDE {
DCHECK_EQ(manager, manager_);
items_seen_.push_back(item);
if (waiting_)
MessageLoopForUI::current()->Quit();
}
virtual void ManagerGoingDown(DownloadManager* manager) OVERRIDE {
manager_->RemoveObserver(this);
manager_ = NULL;
if (waiting_)
MessageLoopForUI::current()->Quit();
}
bool waiting_;
DownloadManager* manager_;
std::vector<DownloadItem*> items_seen_;
DISALLOW_COPY_AND_ASSIGN(DownloadItemCreatedObserver);
};
class SavePackageFinishedObserver : public content::DownloadManager::Observer {
public:
SavePackageFinishedObserver(content::DownloadManager* manager,
const base::Closure& callback)
: download_manager_(manager),
callback_(callback) {
download_manager_->AddObserver(this);
}
virtual ~SavePackageFinishedObserver() {
if (download_manager_)
download_manager_->RemoveObserver(this);
}
// DownloadManager::Observer:
virtual void OnSavePackageSuccessfullyFinished(
content::DownloadManager* manager, content::DownloadItem* item) OVERRIDE {
callback_.Run();
}
virtual void ManagerGoingDown(content::DownloadManager* manager) OVERRIDE {
download_manager_->RemoveObserver(this);
download_manager_ = NULL;
}
private:
content::DownloadManager* download_manager_;
base::Closure callback_;
DISALLOW_COPY_AND_ASSIGN(SavePackageFinishedObserver);
};
class SavePageBrowserTest : public InProcessBrowserTest {
public:
SavePageBrowserTest() {}
virtual ~SavePageBrowserTest();
protected:
void SetUp() OVERRIDE {
ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_));
ASSERT_TRUE(save_dir_.CreateUniqueTempDir());
InProcessBrowserTest::SetUp();
}
void SetUpOnMainThread() OVERRIDE {
browser()->profile()->GetPrefs()->SetFilePath(
prefs::kDownloadDefaultDirectory, save_dir_.path());
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true));
}
GURL NavigateToMockURL(const std::string& prefix) {
GURL url = URLRequestMockHTTPJob::GetMockUrl(
FilePath(kTestDir).AppendASCII(prefix + ".htm"));
ui_test_utils::NavigateToURL(browser(), url);
return url;
}
// Returns full paths of destination file and directory.
void GetDestinationPaths(const std::string& prefix,
FilePath* full_file_name,
FilePath* dir) {
*full_file_name = save_dir_.path().AppendASCII(prefix + ".htm");
*dir = save_dir_.path().AppendASCII(prefix + "_files");
}
WebContents* GetCurrentTab(Browser* browser) const {
WebContents* current_tab = chrome::GetActiveWebContents(browser);
EXPECT_TRUE(current_tab);
return current_tab;
}
// Returns true if and when there was a single download created, and its url
// is |expected_url|.
bool VerifySavePackageExpectations(
Browser* browser,
const GURL& expected_url) const {
// Generally, there should only be one download item created
// in all of these tests. If it's already here, grab it; if not,
// wait for it to show up.
std::vector<DownloadItem*> items;
DownloadManager* manager(
BrowserContext::GetDownloadManager(browser->profile()));
manager->GetAllDownloads(&items);
if (items.size() == 0u) {
DownloadItemCreatedObserver(manager).WaitForDownloadItem(&items);
}
EXPECT_EQ(1u, items.size());
if (1u != items.size())
return false;
DownloadItem* download_item(items[0]);
return (expected_url == download_item->GetOriginalUrl());
}
// Note on synchronization:
//
// For each Save Page As operation, we create a corresponding shell
// DownloadItem to display progress to the user. That DownloadItem goes
// through its own state transitions, including being persisted out to the
// history database, and the download shelf is not shown until after the
// persistence occurs. Save Package completion (and marking the DownloadItem
// as completed) occurs asynchronously from persistence. Thus if we want to
// examine either UI state or DB state, we need to wait until both the save
// package operation is complete and the relevant download item has been
// persisted.
DownloadManager* GetDownloadManager() const {
DownloadManager* download_manager =
BrowserContext::GetDownloadManager(browser()->profile());
EXPECT_TRUE(download_manager);
return download_manager;
}
// Path to directory containing test data.
FilePath test_dir_;
// Temporary directory we will save pages to.
base::ScopedTempDir save_dir_;
private:
DISALLOW_COPY_AND_ASSIGN(SavePageBrowserTest);
};
SavePageBrowserTest::~SavePageBrowserTest() {
}
// Disabled on Windows due to flakiness. http://crbug.com/162323
#if defined(OS_WIN)
#define MAYBE_SaveHTMLOnly DISABLED_SaveHTMLOnly
#else
#define MAYBE_SaveHTMLOnly SaveHTMLOnly
#endif
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, MAYBE_SaveHTMLOnly) {
GURL url = NavigateToMockURL("a");
FilePath full_file_name, dir;
GetDestinationPaths("a", &full_file_name, &dir);
DownloadPersistedObserver persisted(browser()->profile(), base::Bind(
&DownloadStoredProperly, url, full_file_name, 1,
DownloadItem::COMPLETE));
scoped_refptr<content::MessageLoopRunner> loop_runner(
new content::MessageLoopRunner);
SavePackageFinishedObserver observer(
content::BrowserContext::GetDownloadManager(browser()->profile()),
loop_runner->QuitClosure());
ASSERT_TRUE(GetCurrentTab(browser())->SavePage(full_file_name, dir,
content::SAVE_PAGE_TYPE_AS_ONLY_HTML));
loop_runner->Run();
ASSERT_TRUE(VerifySavePackageExpectations(browser(), url));
persisted.WaitForPersisted();
EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
EXPECT_TRUE(file_util::PathExists(full_file_name));
EXPECT_FALSE(file_util::PathExists(dir));
EXPECT_TRUE(file_util::ContentsEqual(
test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")),
full_file_name));
}
// Disabled on Windows due to flakiness. http://crbug.com/162323
#if defined(OS_WIN)
#define MAYBE_SaveHTMLOnlyCancel DISABLED_SaveHTMLOnlyCancel
#else
#define MAYBE_SaveHTMLOnlyCancel SaveHTMLOnlyCancel
#endif
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, MAYBE_SaveHTMLOnlyCancel) {
GURL url = NavigateToMockURL("a");
DownloadManager* manager(GetDownloadManager());
std::vector<DownloadItem*> downloads;
manager->GetAllDownloads(&downloads);
ASSERT_EQ(0u, downloads.size());
FilePath full_file_name, dir;
GetDestinationPaths("a", &full_file_name, &dir);
DownloadItemCreatedObserver creation_observer(manager);
DownloadPersistedObserver persisted(browser()->profile(), base::Bind(
&DownloadStoredProperly, url, full_file_name, -1,
DownloadItem::CANCELLED));
// -1 to disable number of files check; we don't update after cancel, and
// we don't know when the single file completed in relationship to
// the cancel.
ASSERT_TRUE(GetCurrentTab(browser())->SavePage(full_file_name, dir,
content::SAVE_PAGE_TYPE_AS_ONLY_HTML));
std::vector<DownloadItem*> items;
creation_observer.WaitForDownloadItem(&items);
ASSERT_EQ(1UL, items.size());
ASSERT_EQ(url.spec(), items[0]->GetOriginalUrl().spec());
items[0]->Cancel(true);
// TODO(rdsmith): Fix DII::Cancel() to actually cancel the save package.
// Currently it's ignored.
persisted.WaitForPersisted();
EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
// TODO(benjhayden): Figure out how to safely wait for SavePackage's finished
// notification, then expect the contents of the downloaded file.
}
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnlyTabDestroy) {
GURL url = NavigateToMockURL("a");
DownloadManager* manager(GetDownloadManager());
std::vector<DownloadItem*> downloads;
manager->GetAllDownloads(&downloads);
ASSERT_EQ(0u, downloads.size());
FilePath full_file_name, dir;
GetDestinationPaths("a", &full_file_name, &dir);
DownloadItemCreatedObserver creation_observer(manager);
ASSERT_TRUE(GetCurrentTab(browser())->SavePage(full_file_name, dir,
content::SAVE_PAGE_TYPE_AS_ONLY_HTML));
std::vector<DownloadItem*> items;
creation_observer.WaitForDownloadItem(&items);
ASSERT_TRUE(items.size() == 1);
// Close the tab; does this cancel the download?
GetCurrentTab(browser())->Close();
EXPECT_TRUE(items[0]->IsCancelled());
EXPECT_FALSE(file_util::PathExists(full_file_name));
EXPECT_FALSE(file_util::PathExists(dir));
}
// Disabled on Windows due to flakiness. http://crbug.com/162323
#if defined(OS_WIN)
#define MAYBE_SaveViewSourceHTMLOnly DISABLED_SaveViewSourceHTMLOnly
#else
#define MAYBE_SaveViewSourceHTMLOnly SaveViewSourceHTMLOnly
#endif
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, MAYBE_SaveViewSourceHTMLOnly) {
FilePath file_name(FILE_PATH_LITERAL("a.htm"));
GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl(
FilePath(kTestDir).Append(file_name));
GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl(
FilePath(kTestDir).Append(file_name));
ui_test_utils::NavigateToURL(browser(), view_source_url);
FilePath full_file_name, dir;
GetDestinationPaths("a", &full_file_name, &dir);
DownloadPersistedObserver persisted(browser()->profile(), base::Bind(
&DownloadStoredProperly, actual_page_url, full_file_name, 1,
DownloadItem::COMPLETE));
scoped_refptr<content::MessageLoopRunner> loop_runner(
new content::MessageLoopRunner);
SavePackageFinishedObserver observer(
content::BrowserContext::GetDownloadManager(browser()->profile()),
loop_runner->QuitClosure());
ASSERT_TRUE(GetCurrentTab(browser())->SavePage(full_file_name, dir,
content::SAVE_PAGE_TYPE_AS_ONLY_HTML));
loop_runner->Run();
ASSERT_TRUE(VerifySavePackageExpectations(browser(), actual_page_url));
persisted.WaitForPersisted();
EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
EXPECT_TRUE(file_util::PathExists(full_file_name));
EXPECT_FALSE(file_util::PathExists(dir));
EXPECT_TRUE(file_util::ContentsEqual(
test_dir_.Append(FilePath(kTestDir)).Append(file_name),
full_file_name));
}
// Disabled on Windows due to flakiness. http://crbug.com/162323
#if defined(OS_WIN)
#define MAYBE_SaveCompleteHTML DISABLED_SaveCompleteHTML
#else
#define MAYBE_SaveCompleteHTML SaveCompleteHTML
#endif
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, MAYBE_SaveCompleteHTML) {
GURL url = NavigateToMockURL("b");
FilePath full_file_name, dir;
GetDestinationPaths("b", &full_file_name, &dir);
DownloadPersistedObserver persisted(browser()->profile(), base::Bind(
&DownloadStoredProperly, url, full_file_name, 3,
DownloadItem::COMPLETE));
scoped_refptr<content::MessageLoopRunner> loop_runner(
new content::MessageLoopRunner);
SavePackageFinishedObserver observer(
content::BrowserContext::GetDownloadManager(browser()->profile()),
loop_runner->QuitClosure());
ASSERT_TRUE(GetCurrentTab(browser())->SavePage(
full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML));
loop_runner->Run();
ASSERT_TRUE(VerifySavePackageExpectations(browser(), url));
persisted.WaitForPersisted();
EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
EXPECT_TRUE(file_util::PathExists(full_file_name));
EXPECT_TRUE(file_util::PathExists(dir));
EXPECT_TRUE(file_util::TextContentsEqual(
test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"),
full_file_name));
EXPECT_TRUE(file_util::ContentsEqual(
test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"),
dir.AppendASCII("1.png")));
EXPECT_TRUE(file_util::ContentsEqual(
test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"),
dir.AppendASCII("1.css")));
}
// Invoke a save page during the initial navigation.
// (Regression test for http://crbug.com/156538).
// Disabled on Windows due to flakiness. http://crbug.com/162323
// TODO(linux_aura) http://crbug.com/163931
#if defined(OS_WIN) || (defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA))
#define MAYBE_SaveDuringInitialNavigationIncognito DISABLED_SaveDuringInitialNavigationIncognito
#else
#define MAYBE_SaveDuringInitialNavigationIncognito SaveDuringInitialNavigationIncognito
#endif
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest,
MAYBE_SaveDuringInitialNavigationIncognito) {
// Open an Incognito window.
Browser* incognito = CreateIncognitoBrowser(); // Waits.
ASSERT_TRUE(incognito);
// Create a download item creation waiter on that window.
DownloadItemCreatedObserver creation_observer(
BrowserContext::GetDownloadManager(incognito->profile()));
// Navigate, unblocking with new tab.
GURL url = URLRequestMockHTTPJob::GetMockUrl(
FilePath(kTestDir).AppendASCII("b.htm"));
NavigateToURLWithDisposition(incognito, url, NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
// Save the page before completion.
FilePath full_file_name, dir;
GetDestinationPaths("b", &full_file_name, &dir);
scoped_refptr<content::MessageLoopRunner> loop_runner(
new content::MessageLoopRunner);
SavePackageFinishedObserver observer(
content::BrowserContext::GetDownloadManager(incognito->profile()),
loop_runner->QuitClosure());
ASSERT_TRUE(GetCurrentTab(incognito)->SavePage(
full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML));
loop_runner->Run();
ASSERT_TRUE(VerifySavePackageExpectations(incognito, url));
// Confirm download shelf is visible.
EXPECT_TRUE(incognito->window()->IsDownloadShelfVisible());
// We can't check more than this because SavePackage is racing with
// the page load. If the page load won the race, then SavePackage
// might have completed. If the page load lost the race, then
// SavePackage will cancel because there aren't any resources to
// save.
}
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) {
ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL));
EXPECT_FALSE(chrome::CanSavePage(browser()));
}
// Disabled on Windows due to flakiness. http://crbug.com/162323
#if defined(OS_WIN)
#define MAYBE_FileNameFromPageTitle DISABLED_FileNameFromPageTitle
#else
#define MAYBE_FileNameFromPageTitle FileNameFromPageTitle
#endif
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, MAYBE_FileNameFromPageTitle) {
GURL url = NavigateToMockURL("b");
FilePath full_file_name = save_dir_.path().AppendASCII(
std::string("Test page for saving page feature") + kAppendedExtension);
FilePath dir = save_dir_.path().AppendASCII(
"Test page for saving page feature_files");
DownloadPersistedObserver persisted(browser()->profile(), base::Bind(
&DownloadStoredProperly, url, full_file_name, 3,
DownloadItem::COMPLETE));
scoped_refptr<content::MessageLoopRunner> loop_runner(
new content::MessageLoopRunner);
SavePackageFinishedObserver observer(
content::BrowserContext::GetDownloadManager(browser()->profile()),
loop_runner->QuitClosure());
ASSERT_TRUE(GetCurrentTab(browser())->SavePage(
full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML));
loop_runner->Run();
ASSERT_TRUE(VerifySavePackageExpectations(browser(), url));
persisted.WaitForPersisted();
EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
EXPECT_TRUE(file_util::PathExists(full_file_name));
EXPECT_TRUE(file_util::PathExists(dir));
EXPECT_TRUE(file_util::TextContentsEqual(
test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"),
full_file_name));
EXPECT_TRUE(file_util::ContentsEqual(
test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"),
dir.AppendASCII("1.png")));
EXPECT_TRUE(file_util::ContentsEqual(
test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"),
dir.AppendASCII("1.css")));
}
// Disabled on Windows due to flakiness. http://crbug.com/162323
#if defined(OS_WIN)
#define MAYBE_RemoveFromList DISABLED_RemoveFromList
#else
#define MAYBE_RemoveFromList RemoveFromList
#endif
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, MAYBE_RemoveFromList) {
GURL url = NavigateToMockURL("a");
FilePath full_file_name, dir;
GetDestinationPaths("a", &full_file_name, &dir);
DownloadPersistedObserver persisted(browser()->profile(), base::Bind(
&DownloadStoredProperly, url, full_file_name, 1,
DownloadItem::COMPLETE));
scoped_refptr<content::MessageLoopRunner> loop_runner(
new content::MessageLoopRunner);
SavePackageFinishedObserver observer(
content::BrowserContext::GetDownloadManager(browser()->profile()),
loop_runner->QuitClosure());
ASSERT_TRUE(GetCurrentTab(browser())->SavePage(full_file_name, dir,
content::SAVE_PAGE_TYPE_AS_ONLY_HTML));
loop_runner->Run();
ASSERT_TRUE(VerifySavePackageExpectations(browser(), url));
persisted.WaitForPersisted();
EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
DownloadManager* manager(GetDownloadManager());
std::vector<DownloadItem*> downloads;
manager->GetAllDownloads(&downloads);
ASSERT_EQ(1UL, downloads.size());
DownloadRemovedObserver removed(browser()->profile(), downloads[0]->GetId());
EXPECT_EQ(manager->RemoveAllDownloads(), 1);
removed.WaitForRemoved();
EXPECT_TRUE(file_util::PathExists(full_file_name));
EXPECT_FALSE(file_util::PathExists(dir));
EXPECT_TRUE(file_util::ContentsEqual(
test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")),
full_file_name));
}
// This tests that a webpage with the title "test.exe" is saved as
// "test.exe.htm".
// We probably don't care to handle this on Linux or Mac.
#if defined(OS_WIN)
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, CleanFilenameFromPageTitle) {
const FilePath file_name(FILE_PATH_LITERAL("c.htm"));
FilePath download_dir =
DownloadPrefs::FromDownloadManager(GetDownloadManager())->
DownloadPath();
FilePath full_file_name =
download_dir.AppendASCII(std::string("test.exe") + kAppendedExtension);
FilePath dir = download_dir.AppendASCII("test.exe_files");
EXPECT_FALSE(file_util::PathExists(full_file_name));
GURL url = URLRequestMockHTTPJob::GetMockUrl(
FilePath(kTestDir).Append(file_name));
ui_test_utils::NavigateToURL(browser(), url);
SavePackageFilePicker::SetShouldPromptUser(false);
scoped_refptr<content::MessageLoopRunner> loop_runner(
new content::MessageLoopRunner);
SavePackageFinishedObserver observer(
content::BrowserContext::GetDownloadManager(browser()->profile()),
loop_runner->QuitClosure());
chrome::SavePage(browser());
loop_runner->Run();
EXPECT_TRUE(file_util::PathExists(full_file_name));
EXPECT_TRUE(file_util::DieFileDie(full_file_name, false));
EXPECT_TRUE(file_util::DieFileDie(dir, true));
}
#endif
class SavePageAsMHTMLBrowserTest : public SavePageBrowserTest {
public:
SavePageAsMHTMLBrowserTest() {}
virtual ~SavePageAsMHTMLBrowserTest();
virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
command_line->AppendSwitch(switches::kSavePageAsMHTML);
}
private:
DISALLOW_COPY_AND_ASSIGN(SavePageAsMHTMLBrowserTest);
};
SavePageAsMHTMLBrowserTest::~SavePageAsMHTMLBrowserTest() {
}
IN_PROC_BROWSER_TEST_F(SavePageAsMHTMLBrowserTest, SavePageAsMHTML) {
static const int64 kFileSizeMin = 2758;
GURL url = NavigateToMockURL("b");
FilePath download_dir = DownloadPrefs::FromDownloadManager(
GetDownloadManager())->DownloadPath();
FilePath full_file_name = download_dir.AppendASCII(std::string(
"Test page for saving page feature.mhtml"));
#if defined(OS_CHROMEOS)
SavePackageFilePickerChromeOS::SetShouldPromptUser(false);
#else
SavePackageFilePicker::SetShouldPromptUser(false);
#endif
DownloadPersistedObserver persisted(browser()->profile(), base::Bind(
&DownloadStoredProperly, url, full_file_name, -1,
DownloadItem::COMPLETE));
scoped_refptr<content::MessageLoopRunner> loop_runner(
new content::MessageLoopRunner);
SavePackageFinishedObserver observer(
content::BrowserContext::GetDownloadManager(browser()->profile()),
loop_runner->QuitClosure());
chrome::SavePage(browser());
loop_runner->Run();
ASSERT_TRUE(VerifySavePackageExpectations(browser(), url));
persisted.WaitForPersisted();
EXPECT_TRUE(file_util::PathExists(full_file_name));
int64 actual_file_size = -1;
EXPECT_TRUE(file_util::GetFileSize(full_file_name, &actual_file_size));
EXPECT_LE(kFileSizeMin, actual_file_size);
}
} // namespace
|