OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/bind_helpers.h" | 6 #include "base/bind_helpers.h" |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
12 #include "base/prefs/pref_member.h" | 12 #include "base/prefs/pref_member.h" |
13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
14 #include "base/test/test_file_util.h" | 14 #include "base/test/test_file_util.h" |
15 #include "chrome/app/chrome_command_ids.h" | 15 #include "chrome/app/chrome_command_ids.h" |
16 #include "chrome/browser/download/chrome_download_manager_delegate.h" | 16 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
17 #include "chrome/browser/download/download_history.h" | 17 #include "chrome/browser/download/download_history.h" |
18 #include "chrome/browser/download/download_prefs.h" | 18 #include "chrome/browser/download/download_prefs.h" |
19 #include "chrome/browser/download/download_service.h" | 19 #include "chrome/browser/download/download_service.h" |
20 #include "chrome/browser/download/download_service_factory.h" | 20 #include "chrome/browser/download/download_service_factory.h" |
| 21 #include "chrome/browser/download/save_package_file_picker.h" |
21 #include "chrome/browser/history/download_row.h" | 22 #include "chrome/browser/history/download_row.h" |
22 #include "chrome/browser/net/url_request_mock_util.h" | 23 #include "chrome/browser/net/url_request_mock_util.h" |
23 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
24 #include "chrome/browser/ui/browser.h" | 25 #include "chrome/browser/ui/browser.h" |
25 #include "chrome/browser/ui/browser_commands.h" | 26 #include "chrome/browser/ui/browser_commands.h" |
26 #include "chrome/browser/ui/browser_window.h" | 27 #include "chrome/browser/ui/browser_window.h" |
27 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 28 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
28 #include "chrome/common/chrome_paths.h" | 29 #include "chrome/common/chrome_paths.h" |
29 #include "chrome/common/chrome_switches.h" | 30 #include "chrome/common/chrome_switches.h" |
30 #include "chrome/common/pref_names.h" | 31 #include "chrome/common/pref_names.h" |
31 #include "chrome/common/url_constants.h" | 32 #include "chrome/common/url_constants.h" |
32 #include "chrome/test/base/in_process_browser_test.h" | 33 #include "chrome/test/base/in_process_browser_test.h" |
33 #include "chrome/test/base/ui_test_utils.h" | 34 #include "chrome/test/base/ui_test_utils.h" |
34 #include "content/public/browser/download_item.h" | 35 #include "content/public/browser/download_item.h" |
35 #include "content/public/browser/download_manager.h" | 36 #include "content/public/browser/download_manager.h" |
36 #include "content/public/browser/notification_service.h" | 37 #include "content/public/browser/notification_service.h" |
37 #include "content/public/browser/notification_types.h" | 38 #include "content/public/browser/notification_types.h" |
38 #include "content/public/browser/web_contents.h" | 39 #include "content/public/browser/web_contents.h" |
39 #include "content/public/test/test_utils.h" | 40 #include "content/public/test/test_utils.h" |
40 #include "content/test/net/url_request_mock_http_job.h" | 41 #include "content/test/net/url_request_mock_http_job.h" |
41 #include "testing/gtest/include/gtest/gtest.h" | 42 #include "testing/gtest/include/gtest/gtest.h" |
42 | 43 |
43 #if defined(OS_CHROMEOS) | |
44 #include "chrome/browser/download/save_package_file_picker_chromeos.h" | |
45 #else | |
46 #include "chrome/browser/download/save_package_file_picker.h" | |
47 #endif | |
48 | |
49 using content::BrowserContext; | 44 using content::BrowserContext; |
50 using content::BrowserThread; | 45 using content::BrowserThread; |
51 using content::DownloadItem; | 46 using content::DownloadItem; |
52 using content::DownloadManager; | 47 using content::DownloadManager; |
53 using content::URLRequestMockHTTPJob; | 48 using content::URLRequestMockHTTPJob; |
54 using content::WebContents; | 49 using content::WebContents; |
55 | 50 |
56 namespace { | 51 namespace { |
57 | 52 |
58 // Waits for an item record in the downloads database to match |filter|. See | 53 // Waits for an item record in the downloads database to match |filter|. See |
59 // DownloadStoredProperly() below for an example filter. | 54 // DownloadStoredProperly() below for an example filter. |
60 class DownloadPersistedObserver : public DownloadHistory::Observer { | 55 class DownloadPersistedObserver : public DownloadHistory::Observer { |
61 public: | 56 public: |
62 typedef base::Callback<bool( | 57 typedef base::Callback<bool( |
63 DownloadItem* item, | 58 DownloadItem* item, |
64 const history::DownloadRow&)> PersistedFilter; | 59 const history::DownloadRow&)> PersistedFilter; |
65 | 60 |
66 DownloadPersistedObserver(Profile* profile, const PersistedFilter& filter) | 61 DownloadPersistedObserver(Profile* profile, const PersistedFilter& filter) |
67 : profile_(profile), | 62 : profile_(profile), |
68 filter_(filter) { | 63 filter_(filter), |
| 64 waiting_(false), |
| 65 persisted_(false) { |
69 DownloadServiceFactory::GetForProfile(profile_)-> | 66 DownloadServiceFactory::GetForProfile(profile_)-> |
70 GetDownloadHistory()->AddObserver(this); | 67 GetDownloadHistory()->AddObserver(this); |
71 } | 68 } |
72 | 69 |
73 virtual ~DownloadPersistedObserver() { | 70 virtual ~DownloadPersistedObserver() { |
74 DownloadService* service = DownloadServiceFactory::GetForProfile(profile_); | 71 DownloadService* service = DownloadServiceFactory::GetForProfile(profile_); |
75 if (service && service->GetDownloadHistory()) | 72 if (service && service->GetDownloadHistory()) |
76 service->GetDownloadHistory()->RemoveObserver(this); | 73 service->GetDownloadHistory()->RemoveObserver(this); |
77 } | 74 } |
78 | 75 |
79 bool WaitForPersisted() { | 76 bool WaitForPersisted() { |
80 if (persisted_) | 77 if (persisted_) |
81 return true; | 78 return true; |
82 waiting_ = true; | 79 waiting_ = true; |
83 content::RunMessageLoop(); | 80 content::RunMessageLoop(); |
84 waiting_ = false; | 81 waiting_ = false; |
85 return persisted_; | 82 return persisted_; |
86 } | 83 } |
87 | 84 |
88 virtual void OnDownloadStored(DownloadItem* item, | 85 virtual void OnDownloadStored(DownloadItem* item, |
89 const history::DownloadRow& info) OVERRIDE { | 86 const history::DownloadRow& info) OVERRIDE { |
90 persisted_ = filter_.Run(item, info); | 87 persisted_ = persisted_ || filter_.Run(item, info); |
91 if (persisted_ && waiting_) | 88 if (persisted_ && waiting_) |
92 base::MessageLoopForUI::current()->Quit(); | 89 base::MessageLoopForUI::current()->Quit(); |
93 } | 90 } |
94 | 91 |
95 private: | 92 private: |
96 Profile* profile_; | 93 Profile* profile_; |
97 DownloadItem* item_; | 94 DownloadItem* item_; |
98 PersistedFilter filter_; | 95 PersistedFilter filter_; |
99 bool waiting_; | 96 bool waiting_; |
100 bool persisted_; | 97 bool persisted_; |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 protected: | 283 protected: |
287 virtual void SetUp() OVERRIDE { | 284 virtual void SetUp() OVERRIDE { |
288 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_)); | 285 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_)); |
289 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); | 286 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); |
290 InProcessBrowserTest::SetUp(); | 287 InProcessBrowserTest::SetUp(); |
291 } | 288 } |
292 | 289 |
293 virtual void SetUpOnMainThread() OVERRIDE { | 290 virtual void SetUpOnMainThread() OVERRIDE { |
294 browser()->profile()->GetPrefs()->SetFilePath( | 291 browser()->profile()->GetPrefs()->SetFilePath( |
295 prefs::kDownloadDefaultDirectory, save_dir_.path()); | 292 prefs::kDownloadDefaultDirectory, save_dir_.path()); |
| 293 browser()->profile()->GetPrefs()->SetFilePath( |
| 294 prefs::kSaveFileDefaultDirectory, save_dir_.path()); |
296 BrowserThread::PostTask( | 295 BrowserThread::PostTask( |
297 BrowserThread::IO, FROM_HERE, | 296 BrowserThread::IO, FROM_HERE, |
298 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); | 297 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); |
299 } | 298 } |
300 | 299 |
301 GURL NavigateToMockURL(const std::string& prefix) { | 300 GURL NavigateToMockURL(const std::string& prefix) { |
302 GURL url = URLRequestMockHTTPJob::GetMockUrl( | 301 GURL url = URLRequestMockHTTPJob::GetMockUrl( |
303 base::FilePath(kTestDir).AppendASCII(prefix + ".htm")); | 302 base::FilePath(kTestDir).AppendASCII(prefix + ".htm")); |
304 ui_test_utils::NavigateToURL(browser(), url); | 303 ui_test_utils::NavigateToURL(browser(), url); |
305 return url; | 304 return url; |
(...skipping 441 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
747 SavePageAsMHTMLBrowserTest::~SavePageAsMHTMLBrowserTest() { | 746 SavePageAsMHTMLBrowserTest::~SavePageAsMHTMLBrowserTest() { |
748 } | 747 } |
749 | 748 |
750 IN_PROC_BROWSER_TEST_F(SavePageAsMHTMLBrowserTest, SavePageAsMHTML) { | 749 IN_PROC_BROWSER_TEST_F(SavePageAsMHTMLBrowserTest, SavePageAsMHTML) { |
751 static const int64 kFileSizeMin = 2758; | 750 static const int64 kFileSizeMin = 2758; |
752 GURL url = NavigateToMockURL("b"); | 751 GURL url = NavigateToMockURL("b"); |
753 base::FilePath download_dir = DownloadPrefs::FromDownloadManager( | 752 base::FilePath download_dir = DownloadPrefs::FromDownloadManager( |
754 GetDownloadManager())->DownloadPath(); | 753 GetDownloadManager())->DownloadPath(); |
755 base::FilePath full_file_name = download_dir.AppendASCII(std::string( | 754 base::FilePath full_file_name = download_dir.AppendASCII(std::string( |
756 "Test page for saving page feature.mhtml")); | 755 "Test page for saving page feature.mhtml")); |
757 #if defined(OS_CHROMEOS) | |
758 SavePackageFilePickerChromeOS::SetShouldPromptUser(false); | |
759 #else | |
760 SavePackageFilePicker::SetShouldPromptUser(false); | 756 SavePackageFilePicker::SetShouldPromptUser(false); |
761 #endif | |
762 DownloadPersistedObserver persisted(browser()->profile(), base::Bind( | 757 DownloadPersistedObserver persisted(browser()->profile(), base::Bind( |
763 &DownloadStoredProperly, url, full_file_name, -1, | 758 &DownloadStoredProperly, url, full_file_name, -1, |
764 DownloadItem::COMPLETE)); | 759 DownloadItem::COMPLETE)); |
765 scoped_refptr<content::MessageLoopRunner> loop_runner( | 760 scoped_refptr<content::MessageLoopRunner> loop_runner( |
766 new content::MessageLoopRunner); | 761 new content::MessageLoopRunner); |
767 SavePackageFinishedObserver observer( | 762 SavePackageFinishedObserver observer( |
768 content::BrowserContext::GetDownloadManager(browser()->profile()), | 763 content::BrowserContext::GetDownloadManager(browser()->profile()), |
769 loop_runner->QuitClosure()); | 764 loop_runner->QuitClosure()); |
770 chrome::SavePage(browser()); | 765 chrome::SavePage(browser()); |
771 loop_runner->Run(); | 766 loop_runner->Run(); |
772 ASSERT_TRUE(VerifySavePackageExpectations(browser(), url)); | 767 ASSERT_TRUE(VerifySavePackageExpectations(browser(), url)); |
773 persisted.WaitForPersisted(); | 768 persisted.WaitForPersisted(); |
774 | 769 |
775 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 770 ASSERT_TRUE(file_util::PathExists(full_file_name)); |
776 int64 actual_file_size = -1; | 771 int64 actual_file_size = -1; |
777 EXPECT_TRUE(file_util::GetFileSize(full_file_name, &actual_file_size)); | 772 EXPECT_TRUE(file_util::GetFileSize(full_file_name, &actual_file_size)); |
778 EXPECT_LE(kFileSizeMin, actual_file_size); | 773 EXPECT_LE(kFileSizeMin, actual_file_size); |
779 } | 774 } |
780 | 775 |
781 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SavePageBrowserTest_NonMHTML) { | 776 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SavePageBrowserTest_NonMHTML) { |
782 #if defined(OS_CHROMEOS) | |
783 SavePackageFilePickerChromeOS::SetShouldPromptUser(false); | |
784 #else | |
785 SavePackageFilePicker::SetShouldPromptUser(false); | 777 SavePackageFilePicker::SetShouldPromptUser(false); |
786 #endif | |
787 GURL url("data:text/plain,foo"); | 778 GURL url("data:text/plain,foo"); |
788 ui_test_utils::NavigateToURL(browser(), url); | 779 ui_test_utils::NavigateToURL(browser(), url); |
789 scoped_refptr<content::MessageLoopRunner> loop_runner( | 780 scoped_refptr<content::MessageLoopRunner> loop_runner( |
790 new content::MessageLoopRunner); | 781 new content::MessageLoopRunner); |
791 SavePackageFinishedObserver observer( | 782 SavePackageFinishedObserver observer( |
792 content::BrowserContext::GetDownloadManager(browser()->profile()), | 783 content::BrowserContext::GetDownloadManager(browser()->profile()), |
793 loop_runner->QuitClosure()); | 784 loop_runner->QuitClosure()); |
794 chrome::SavePage(browser()); | 785 chrome::SavePage(browser()); |
795 loop_runner->Run(); | 786 loop_runner->Run(); |
796 base::FilePath download_dir = DownloadPrefs::FromDownloadManager( | 787 base::FilePath download_dir = DownloadPrefs::FromDownloadManager( |
797 GetDownloadManager())->DownloadPath(); | 788 GetDownloadManager())->DownloadPath(); |
798 base::FilePath filename = download_dir.AppendASCII("dataurl.txt"); | 789 base::FilePath filename = download_dir.AppendASCII("dataurl.txt"); |
799 EXPECT_TRUE(file_util::PathExists(filename)); | 790 ASSERT_TRUE(file_util::PathExists(filename)); |
800 std::string contents; | 791 std::string contents; |
801 EXPECT_TRUE(file_util::ReadFileToString(filename, &contents)); | 792 EXPECT_TRUE(file_util::ReadFileToString(filename, &contents)); |
802 EXPECT_EQ("foo", contents); | 793 EXPECT_EQ("foo", contents); |
803 } | 794 } |
804 | 795 |
805 } // namespace | 796 } // namespace |
806 | 797 |
OLD | NEW |