Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(93)

Side by Side Diff: chrome/browser/download/save_page_browsertest.cc

Issue 10069014: Save Page As MHTML (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: nonconst nonref params Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/file_path.h" 8 #include "base/file_path.h"
8 #include "base/file_util.h" 9 #include "base/file_util.h"
9 #include "base/path_service.h" 10 #include "base/path_service.h"
10 #include "base/scoped_temp_dir.h" 11 #include "base/scoped_temp_dir.h"
11 #include "base/test/test_file_util.h" 12 #include "base/test/test_file_util.h"
12 #include "chrome/app/chrome_command_ids.h" 13 #include "chrome/app/chrome_command_ids.h"
13 #include "chrome/browser/download/chrome_download_manager_delegate.h" 14 #include "chrome/browser/download/chrome_download_manager_delegate.h"
14 #include "chrome/browser/download/download_history.h" 15 #include "chrome/browser/download/download_history.h"
15 #include "chrome/browser/download/download_prefs.h" 16 #include "chrome/browser/download/download_prefs.h"
16 #include "chrome/browser/download/download_service.h" 17 #include "chrome/browser/download/download_service.h"
17 #include "chrome/browser/download/download_service_factory.h" 18 #include "chrome/browser/download/download_service_factory.h"
18 #include "chrome/browser/download/save_package_file_picker.h" 19 #include "chrome/browser/download/save_package_file_picker.h"
19 #include "chrome/browser/net/url_request_mock_util.h" 20 #include "chrome/browser/net/url_request_mock_util.h"
21 #include "chrome/browser/prefs/pref_member.h"
22 #include "chrome/browser/prefs/pref_service.h"
20 #include "chrome/browser/profiles/profile.h" 23 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/ui/browser.h" 24 #include "chrome/browser/ui/browser.h"
22 #include "chrome/browser/ui/browser_window.h" 25 #include "chrome/browser/ui/browser_window.h"
23 #include "chrome/common/chrome_paths.h" 26 #include "chrome/common/chrome_paths.h"
27 #include "chrome/common/chrome_switches.h"
28 #include "chrome/common/pref_names.h"
24 #include "chrome/common/url_constants.h" 29 #include "chrome/common/url_constants.h"
25 #include "chrome/test/base/in_process_browser_test.h" 30 #include "chrome/test/base/in_process_browser_test.h"
26 #include "chrome/test/base/ui_test_utils.h" 31 #include "chrome/test/base/ui_test_utils.h"
27 #include "content/public/browser/download_item.h" 32 #include "content/public/browser/download_item.h"
28 #include "content/public/browser/download_manager.h" 33 #include "content/public/browser/download_manager.h"
29 #include "content/public/browser/download_persistent_store_info.h" 34 #include "content/public/browser/download_persistent_store_info.h"
30 #include "content/public/browser/notification_service.h" 35 #include "content/public/browser/notification_service.h"
31 #include "content/public/browser/notification_types.h" 36 #include "content/public/browser/notification_types.h"
32 #include "content/public/browser/web_contents.h" 37 #include "content/public/browser/web_contents.h"
33 #include "content/test/net/url_request_mock_http_job.h" 38 #include "content/test/net/url_request_mock_http_job.h"
(...skipping 12 matching lines...) Expand all
46 static const char* kAppendedExtension = 51 static const char* kAppendedExtension =
47 #if defined(OS_WIN) 52 #if defined(OS_WIN)
48 ".htm"; 53 ".htm";
49 #else 54 #else
50 ".html"; 55 ".html";
51 #endif 56 #endif
52 57
53 } // namespace 58 } // namespace
54 59
55 class SavePageBrowserTest : public InProcessBrowserTest { 60 class SavePageBrowserTest : public InProcessBrowserTest {
61 public:
62 SavePageBrowserTest() {}
63 virtual ~SavePageBrowserTest();
64
56 protected: 65 protected:
57 void SetUp() OVERRIDE { 66 void SetUp() OVERRIDE {
58 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_)); 67 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_));
59 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); 68 ASSERT_TRUE(save_dir_.CreateUniqueTempDir());
60 InProcessBrowserTest::SetUp(); 69 InProcessBrowserTest::SetUp();
61 } 70 }
62 71
63 void SetUpOnMainThread() OVERRIDE { 72 void SetUpOnMainThread() OVERRIDE {
73 browser()->profile()->GetPrefs()->SetFilePath(
74 prefs::kDownloadDefaultDirectory, save_dir_.path());
64 BrowserThread::PostTask( 75 BrowserThread::PostTask(
65 BrowserThread::IO, FROM_HERE, 76 BrowserThread::IO, FROM_HERE,
66 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); 77 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true));
67 } 78 }
68 79
69 GURL NavigateToMockURL(const std::string& prefix) { 80 GURL NavigateToMockURL(const std::string& prefix) {
70 GURL url = URLRequestMockHTTPJob::GetMockUrl( 81 GURL url = URLRequestMockHTTPJob::GetMockUrl(
71 FilePath(kTestDir).AppendASCII(prefix + ".htm")); 82 FilePath(kTestDir).AppendASCII(prefix + ".htm"));
72 ui_test_utils::NavigateToURL(browser(), url); 83 ui_test_utils::NavigateToURL(browser(), url);
73 return url; 84 return url;
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 info.state == DownloadItem::COMPLETE; 158 info.state == DownloadItem::COMPLETE;
148 } 159 }
149 160
150 GURL url_; 161 GURL url_;
151 FilePath path_; 162 FilePath path_;
152 int64 num_files_; 163 int64 num_files_;
153 }; 164 };
154 165
155 void CheckDownloadHistory(const GURL& url, 166 void CheckDownloadHistory(const GURL& url,
156 const FilePath& path, 167 const FilePath& path,
157 int64 num_files_) { 168 int64 num_files) {
158 QueryDownloadHistory(); 169 QueryDownloadHistory();
159 170
160 EXPECT_NE(std::find_if(history_entries_.begin(), history_entries_.end(), 171 std::vector<DownloadPersistentStoreInfo>::iterator found =
161 DownloadPersistentStoreInfoMatch(url, path, num_files_)), 172 std::find_if(history_entries_.begin(), history_entries_.end(),
162 history_entries_.end()); 173 DownloadPersistentStoreInfoMatch(url, path, num_files));
174
175 if (found == history_entries_.end()) {
176 LOG(ERROR) << "Missing url=" << url.spec()
177 << " path=" << path.value()
178 << " received=" << num_files;
179 for (size_t index = 0; index < history_entries_.size(); ++index) {
180 LOG(ERROR) << "History@" << index << ": url="
181 << history_entries_[index].url.spec()
182 << " path=" << history_entries_[index].path.value()
183 << " received=" << history_entries_[index].received_bytes
184 << " total=" << history_entries_[index].total_bytes
185 << " state=" << history_entries_[index].state;
186 }
187 EXPECT_TRUE(false);
188 }
163 } 189 }
164 190
165 std::vector<DownloadPersistentStoreInfo> history_entries_; 191 std::vector<DownloadPersistentStoreInfo> history_entries_;
166 192
167 // Path to directory containing test data. 193 // Path to directory containing test data.
168 FilePath test_dir_; 194 FilePath test_dir_;
169 195
170 // Temporary directory we will save pages to. 196 // Temporary directory we will save pages to.
171 ScopedTempDir save_dir_; 197 ScopedTempDir save_dir_;
198
199 private:
200 DISALLOW_COPY_AND_ASSIGN(SavePageBrowserTest);
172 }; 201 };
173 202
203 SavePageBrowserTest::~SavePageBrowserTest() {
204 }
205
174 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { 206 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) {
175 GURL url = NavigateToMockURL("a"); 207 GURL url = NavigateToMockURL("a");
176 208
177 FilePath full_file_name, dir; 209 FilePath full_file_name, dir;
178 GetDestinationPaths("a", &full_file_name, &dir); 210 GetDestinationPaths("a", &full_file_name, &dir);
179 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, 211 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir,
180 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); 212 content::SAVE_PAGE_TYPE_AS_ONLY_HTML));
181 213
182 EXPECT_EQ(url, WaitForSavePackageToFinish()); 214 EXPECT_EQ(url, WaitForSavePackageToFinish());
183 215
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 content::NotificationService::AllSources()); 360 content::NotificationService::AllSources());
329 browser()->SavePage(); 361 browser()->SavePage();
330 observer.Wait(); 362 observer.Wait();
331 363
332 EXPECT_TRUE(file_util::PathExists(full_file_name)); 364 EXPECT_TRUE(file_util::PathExists(full_file_name));
333 365
334 EXPECT_TRUE(file_util::DieFileDie(full_file_name, false)); 366 EXPECT_TRUE(file_util::DieFileDie(full_file_name, false));
335 EXPECT_TRUE(file_util::DieFileDie(dir, true)); 367 EXPECT_TRUE(file_util::DieFileDie(dir, true));
336 } 368 }
337 #endif 369 #endif
370
371 #if !defined(OS_CHROMEOS)
achuithb 2012/04/24 22:06:09 Why does this not work on chromeos?
benjhayden 2012/04/26 15:05:57 Done.
372 class SavePageAsMHTMLBrowserTest : public SavePageBrowserTest {
373 public:
374 SavePageAsMHTMLBrowserTest() {}
375 virtual ~SavePageAsMHTMLBrowserTest();
376 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
377 command_line->AppendSwitch(switches::kSavePageAsMHTML);
378 }
379
380 private:
381 DISALLOW_COPY_AND_ASSIGN(SavePageAsMHTMLBrowserTest);
382 };
383
384 SavePageAsMHTMLBrowserTest::~SavePageAsMHTMLBrowserTest() {
385 }
386
387 IN_PROC_BROWSER_TEST_F(SavePageAsMHTMLBrowserTest, SavePageAsMHTML) {
388 GURL url = NavigateToMockURL("b");
389 FilePath download_dir = DownloadPrefs::FromDownloadManager(
390 GetDownloadManager())->download_path();
391 FilePath full_file_name = download_dir.AppendASCII(std::string(
392 "Test page for saving page feature.mhtml"));
393 SavePackageFilePicker::SetShouldPromptUser(false);
394 ui_test_utils::WindowedNotificationObserver observer(
395 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED,
396 content::NotificationService::AllSources());
397 browser()->SavePage();
398 observer.Wait();
399 CheckDownloadHistory(url, full_file_name, 1);
400
401 EXPECT_TRUE(file_util::PathExists(full_file_name));
402 int64 actual_file_size = -1;
403 EXPECT_TRUE(file_util::GetFileSize(full_file_name, &actual_file_size));
404 EXPECT_EQ(2760, actual_file_size);
405 }
406 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698