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

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

Issue 7277073: Support for adding save page download items into downloads history. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 4 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
« no previous file with comments | « chrome/browser/download/download_manager.cc ('k') | content/browser/download/save_package.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/file_path.h" 5 #include "base/file_path.h"
6 #include "base/file_util.h" 6 #include "base/file_util.h"
7 #include "base/path_service.h" 7 #include "base/path_service.h"
8 #include "base/scoped_temp_dir.h" 8 #include "base/scoped_temp_dir.h"
9 #include "chrome/app/chrome_command_ids.h" 9 #include "chrome/app/chrome_command_ids.h"
10 #include "chrome/browser/download/download_history.h"
10 #include "chrome/browser/download/download_item.h" 11 #include "chrome/browser/download/download_item.h"
12 #include "chrome/browser/download/download_manager.h"
13 #include "chrome/browser/history/download_history_info.h"
14 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/browser.h" 15 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_window.h" 16 #include "chrome/browser/ui/browser_window.h"
13 #include "chrome/browser/ui/webui/active_downloads_ui.h" 17 #include "chrome/browser/ui/webui/active_downloads_ui.h"
14 #include "chrome/common/chrome_paths.h" 18 #include "chrome/common/chrome_paths.h"
15 #include "chrome/common/url_constants.h" 19 #include "chrome/common/url_constants.h"
16 #include "chrome/test/base/in_process_browser_test.h" 20 #include "chrome/test/base/in_process_browser_test.h"
17 #include "chrome/test/base/ui_test_utils.h" 21 #include "chrome/test/base/ui_test_utils.h"
18 #include "content/browser/net/url_request_mock_http_job.h" 22 #include "content/browser/net/url_request_mock_http_job.h"
19 #include "content/browser/tab_contents/tab_contents.h" 23 #include "content/browser/tab_contents/tab_contents.h"
20 #include "content/common/content_notification_types.h" 24 #include "content/common/content_notification_types.h"
(...skipping 12 matching lines...) Expand all
33 #endif 37 #endif
34 38
35 class SavePageBrowserTest : public InProcessBrowserTest { 39 class SavePageBrowserTest : public InProcessBrowserTest {
36 protected: 40 protected:
37 void SetUp() { 41 void SetUp() {
38 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_)); 42 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_));
39 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); 43 ASSERT_TRUE(save_dir_.CreateUniqueTempDir());
40 InProcessBrowserTest::SetUp(); 44 InProcessBrowserTest::SetUp();
41 } 45 }
42 46
43 GURL WaitForSavePackageToFinish() { 47 GURL WaitForSavePackageToFinish() const {
44 ui_test_utils::TestNotificationObserver observer; 48 ui_test_utils::TestNotificationObserver observer;
45 ui_test_utils::RegisterAndWait(&observer, 49 ui_test_utils::RegisterAndWait(&observer,
46 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, 50 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED,
47 NotificationService::AllSources()); 51 NotificationService::AllSources());
48 return *Details<GURL>(observer.details()).ptr(); 52 return Details<DownloadItem>(observer.details()).ptr()->original_url();
49 } 53 }
50 54
51 void CheckDownloadUI(const FilePath& download_path) {
52 #if defined(OS_CHROMEOS) 55 #if defined(OS_CHROMEOS)
56 const ActiveDownloadsUI::DownloadList& GetDownloads() const {
53 Browser* popup = ActiveDownloadsUI::GetPopup(); 57 Browser* popup = ActiveDownloadsUI::GetPopup();
54 EXPECT_TRUE(popup); 58 EXPECT_TRUE(popup);
55 ActiveDownloadsUI* downloads_ui = static_cast<ActiveDownloadsUI*>( 59 ActiveDownloadsUI* downloads_ui = static_cast<ActiveDownloadsUI*>(
56 popup->GetSelectedTabContents()->web_ui()); 60 popup->GetSelectedTabContents()->web_ui());
57 ASSERT_TRUE(downloads_ui); 61 EXPECT_TRUE(downloads_ui);
58 const ActiveDownloadsUI::DownloadList& downloads = 62 return downloads_ui->GetDownloads();
59 downloads_ui->GetDownloads(); 63 }
64 #endif
65
66 void CheckDownloadUI(const FilePath& download_path) const {
67 #if defined(OS_CHROMEOS)
68 const ActiveDownloadsUI::DownloadList& downloads = GetDownloads();
60 EXPECT_EQ(downloads.size(), 1U); 69 EXPECT_EQ(downloads.size(), 1U);
61 70
62 bool found = false; 71 bool found = false;
63 for (size_t i = 0; i < downloads.size(); ++i) { 72 for (size_t i = 0; i < downloads.size(); ++i) {
64 if (downloads[i]->full_path() == download_path) { 73 if (downloads[i]->full_path() == download_path) {
65 found = true; 74 found = true;
66 break; 75 break;
67 } 76 }
68 } 77 }
69 EXPECT_TRUE(found); 78 EXPECT_TRUE(found);
70 #else 79 #else
71 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); 80 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
72 #endif 81 #endif
73 } 82 }
74 83
84 DownloadManager* GetDownloadManager() const {
85 DownloadManager* download_manager =
86 browser()->profile()->GetDownloadManager();
87 EXPECT_TRUE(download_manager);
88 return download_manager;
89 }
90
91 void QueryDownloadHistory() {
92 // Query the history system.
93 GetDownloadManager()->download_history()->Load(
94 NewCallback(this,
95 &SavePageBrowserTest::OnQueryDownloadEntriesComplete));
96
97 // Run message loop until a quit message is sent from
98 // OnQueryDownloadEntriesComplete().
99 ui_test_utils::RunMessageLoop();
100 }
101
102 void OnQueryDownloadEntriesComplete(
103 std::vector<DownloadHistoryInfo>* entries) {
104 history_entries_ = *entries;
105
106 // Indicate thet we have received the history and can continue.
107 MessageLoopForUI::current()->Quit();
108 }
109
110 struct DownloadHistoryInfoMatch
111 : public std::unary_function<DownloadHistoryInfo, bool> {
112
113 DownloadHistoryInfoMatch(const GURL& url,
114 const FilePath& path,
115 int64 num_files)
116 : url_(url),
117 path_(path),
118 num_files_(num_files) {
119 }
120
121 bool operator() (const DownloadHistoryInfo& info) const {
122 return info.url == url_ &&
123 info.path == path_ &&
124 // For save packages, received bytes is actually the number of files.
125 info.received_bytes == num_files_ &&
126 info.total_bytes == 0 &&
127 info.state == DownloadItem::COMPLETE;
128 }
129
130 GURL url_;
131 FilePath path_;
132 int64 num_files_;
133 };
134
135 void CheckDownloadHistory(const GURL& url,
136 const FilePath& path,
137 int64 num_files_) {
138 QueryDownloadHistory();
139
140 EXPECT_NE(std::find_if(history_entries_.begin(), history_entries_.end(),
141 DownloadHistoryInfoMatch(url, path, num_files_)),
142 history_entries_.end());
143 }
144
145 std::vector<DownloadHistoryInfo> history_entries_;
146
75 // Path to directory containing test data. 147 // Path to directory containing test data.
76 FilePath test_dir_; 148 FilePath test_dir_;
77 149
78 // Temporary directory we will save pages to. 150 // Temporary directory we will save pages to.
79 ScopedTempDir save_dir_; 151 ScopedTempDir save_dir_;
80 }; 152 };
81 153
82 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { 154 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) {
83 FilePath file_name(FILE_PATH_LITERAL("a.htm")); 155 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
84 GURL url = URLRequestMockHTTPJob::GetMockUrl( 156 GURL url = URLRequestMockHTTPJob::GetMockUrl(
85 FilePath(kTestDir).Append(file_name)); 157 FilePath(kTestDir).Append(file_name));
86 ui_test_utils::NavigateToURL(browser(), url); 158 ui_test_utils::NavigateToURL(browser(), url);
87 159
88 TabContents* current_tab = browser()->GetSelectedTabContents(); 160 TabContents* current_tab = browser()->GetSelectedTabContents();
89 ASSERT_TRUE(current_tab); 161 ASSERT_TRUE(current_tab);
90 162
91 FilePath full_file_name = save_dir_.path().Append(file_name); 163 FilePath full_file_name = save_dir_.path().Append(file_name);
92 FilePath dir = save_dir_.path().AppendASCII("a_files"); 164 FilePath dir = save_dir_.path().AppendASCII("a_files");
93 ASSERT_TRUE(current_tab->SavePage(full_file_name, dir, 165 ASSERT_TRUE(current_tab->SavePage(full_file_name, dir,
94 SavePackage::SAVE_AS_ONLY_HTML)); 166 SavePackage::SAVE_AS_ONLY_HTML));
95 167
96 EXPECT_EQ(url, WaitForSavePackageToFinish()); 168 EXPECT_EQ(url, WaitForSavePackageToFinish());
97 169
98 CheckDownloadUI(full_file_name); 170 CheckDownloadUI(full_file_name);
171 CheckDownloadHistory(url, full_file_name, 1); // a.htm is 1 file.
99 172
100 EXPECT_TRUE(file_util::PathExists(full_file_name)); 173 EXPECT_TRUE(file_util::PathExists(full_file_name));
101 EXPECT_FALSE(file_util::PathExists(dir)); 174 EXPECT_FALSE(file_util::PathExists(dir));
102 EXPECT_TRUE(file_util::ContentsEqual( 175 EXPECT_TRUE(file_util::ContentsEqual(
103 test_dir_.Append(FilePath(kTestDir)).Append(file_name), 176 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
104 full_file_name)); 177 full_file_name));
105 } 178 }
106 179
107 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveViewSourceHTMLOnly) { 180 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveViewSourceHTMLOnly) {
108 FilePath file_name(FILE_PATH_LITERAL("a.htm")); 181 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
109 GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl( 182 GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl(
110 FilePath(kTestDir).Append(file_name)); 183 FilePath(kTestDir).Append(file_name));
111 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl( 184 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl(
112 FilePath(kTestDir).Append(file_name)); 185 FilePath(kTestDir).Append(file_name));
113 ui_test_utils::NavigateToURL(browser(), view_source_url); 186 ui_test_utils::NavigateToURL(browser(), view_source_url);
114 187
115 TabContents* current_tab = browser()->GetSelectedTabContents(); 188 TabContents* current_tab = browser()->GetSelectedTabContents();
116 ASSERT_TRUE(current_tab); 189 ASSERT_TRUE(current_tab);
117 190
118 FilePath full_file_name = save_dir_.path().Append(file_name); 191 FilePath full_file_name = save_dir_.path().Append(file_name);
119 FilePath dir = save_dir_.path().AppendASCII("a_files"); 192 FilePath dir = save_dir_.path().AppendASCII("a_files");
120 193
121 ASSERT_TRUE(current_tab->SavePage(full_file_name, dir, 194 ASSERT_TRUE(current_tab->SavePage(full_file_name, dir,
122 SavePackage::SAVE_AS_ONLY_HTML)); 195 SavePackage::SAVE_AS_ONLY_HTML));
123 196
124 EXPECT_EQ(actual_page_url, WaitForSavePackageToFinish()); 197 EXPECT_EQ(actual_page_url, WaitForSavePackageToFinish());
125 198
126 CheckDownloadUI(full_file_name); 199 CheckDownloadUI(full_file_name);
200 CheckDownloadHistory(actual_page_url, full_file_name, 1); // a.htm is 1 file.
127 201
128 EXPECT_TRUE(file_util::PathExists(full_file_name)); 202 EXPECT_TRUE(file_util::PathExists(full_file_name));
129 EXPECT_FALSE(file_util::PathExists(dir)); 203 EXPECT_FALSE(file_util::PathExists(dir));
130 EXPECT_TRUE(file_util::ContentsEqual( 204 EXPECT_TRUE(file_util::ContentsEqual(
131 test_dir_.Append(FilePath(kTestDir)).Append(file_name), 205 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
132 full_file_name)); 206 full_file_name));
133 } 207 }
134 208
135 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) { 209 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) {
136 FilePath file_name(FILE_PATH_LITERAL("b.htm")); 210 FilePath file_name(FILE_PATH_LITERAL("b.htm"));
137 GURL url = URLRequestMockHTTPJob::GetMockUrl( 211 GURL url = URLRequestMockHTTPJob::GetMockUrl(
138 FilePath(kTestDir).Append(file_name)); 212 FilePath(kTestDir).Append(file_name));
139 ui_test_utils::NavigateToURL(browser(), url); 213 ui_test_utils::NavigateToURL(browser(), url);
140 214
141 TabContents* current_tab = browser()->GetSelectedTabContents(); 215 TabContents* current_tab = browser()->GetSelectedTabContents();
142 ASSERT_TRUE(current_tab); 216 ASSERT_TRUE(current_tab);
143 217
144 FilePath full_file_name = save_dir_.path().Append(file_name); 218 FilePath full_file_name = save_dir_.path().Append(file_name);
145 FilePath dir = save_dir_.path().AppendASCII("b_files"); 219 FilePath dir = save_dir_.path().AppendASCII("b_files");
146 ASSERT_TRUE(current_tab->SavePage(full_file_name, dir, 220 ASSERT_TRUE(current_tab->SavePage(full_file_name, dir,
147 SavePackage::SAVE_AS_COMPLETE_HTML)); 221 SavePackage::SAVE_AS_COMPLETE_HTML));
148 222
149 EXPECT_EQ(url, WaitForSavePackageToFinish()); 223 EXPECT_EQ(url, WaitForSavePackageToFinish());
150 224
151 CheckDownloadUI(full_file_name); 225 CheckDownloadUI(full_file_name);
226 CheckDownloadHistory(url, full_file_name, 3); // b.htm is 3 files.
152 227
153 EXPECT_TRUE(file_util::PathExists(full_file_name)); 228 EXPECT_TRUE(file_util::PathExists(full_file_name));
154 EXPECT_TRUE(file_util::PathExists(dir)); 229 EXPECT_TRUE(file_util::PathExists(dir));
155 EXPECT_TRUE(file_util::TextContentsEqual( 230 EXPECT_TRUE(file_util::TextContentsEqual(
156 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"), 231 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"),
157 full_file_name)); 232 full_file_name));
158 EXPECT_TRUE(file_util::ContentsEqual( 233 EXPECT_TRUE(file_util::ContentsEqual(
159 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), 234 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"),
160 dir.AppendASCII("1.png"))); 235 dir.AppendASCII("1.png")));
161 EXPECT_TRUE(file_util::ContentsEqual( 236 EXPECT_TRUE(file_util::ContentsEqual(
(...skipping 21 matching lines...) Expand all
183 258
184 TabContents* current_tab = browser()->GetSelectedTabContents(); 259 TabContents* current_tab = browser()->GetSelectedTabContents();
185 ASSERT_TRUE(current_tab); 260 ASSERT_TRUE(current_tab);
186 261
187 ASSERT_TRUE(current_tab->SavePage(full_file_name, dir, 262 ASSERT_TRUE(current_tab->SavePage(full_file_name, dir,
188 SavePackage::SAVE_AS_COMPLETE_HTML)); 263 SavePackage::SAVE_AS_COMPLETE_HTML));
189 264
190 EXPECT_EQ(url, WaitForSavePackageToFinish()); 265 EXPECT_EQ(url, WaitForSavePackageToFinish());
191 266
192 CheckDownloadUI(full_file_name); 267 CheckDownloadUI(full_file_name);
268 CheckDownloadHistory(url, full_file_name, 3); // b.htm is 3 files.
193 269
194 EXPECT_TRUE(file_util::PathExists(full_file_name)); 270 EXPECT_TRUE(file_util::PathExists(full_file_name));
195 EXPECT_TRUE(file_util::PathExists(dir)); 271 EXPECT_TRUE(file_util::PathExists(dir));
196 EXPECT_TRUE(file_util::TextContentsEqual( 272 EXPECT_TRUE(file_util::TextContentsEqual(
197 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"), 273 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"),
198 full_file_name)); 274 full_file_name));
199 EXPECT_TRUE(file_util::ContentsEqual( 275 EXPECT_TRUE(file_util::ContentsEqual(
200 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), 276 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"),
201 dir.AppendASCII("1.png"))); 277 dir.AppendASCII("1.png")));
202 EXPECT_TRUE(file_util::ContentsEqual( 278 EXPECT_TRUE(file_util::ContentsEqual(
203 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), 279 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"),
204 dir.AppendASCII("1.css"))); 280 dir.AppendASCII("1.css")));
205 } 281 }
206 282
207 } // namespace 283 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, RemoveFromList) {
284 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
285 GURL url = URLRequestMockHTTPJob::GetMockUrl(
286 FilePath(kTestDir).Append(file_name));
287 ui_test_utils::NavigateToURL(browser(), url);
288
289 TabContents* current_tab = browser()->GetSelectedTabContents();
290 ASSERT_TRUE(current_tab);
291
292 FilePath full_file_name = save_dir_.path().Append(file_name);
293 FilePath dir = save_dir_.path().AppendASCII("a_files");
294 ASSERT_TRUE(current_tab->SavePage(full_file_name, dir,
295 SavePackage::SAVE_AS_ONLY_HTML));
296
297 EXPECT_EQ(url, WaitForSavePackageToFinish());
298
299 CheckDownloadUI(full_file_name);
300 CheckDownloadHistory(url, full_file_name, 1); // a.htm is 1 file.
301
302 EXPECT_EQ(GetDownloadManager()->RemoveAllDownloads(), 1);
303
304 #if defined(OS_CHROMEOS)
305 EXPECT_EQ(GetDownloads().size(), 0U);
306 #endif
307
308 // Should not be in history.
309 QueryDownloadHistory();
310 EXPECT_EQ(std::find_if(history_entries_.begin(), history_entries_.end(),
311 DownloadHistoryInfoMatch(url, full_file_name, 1)),
312 history_entries_.end());
313
314 EXPECT_TRUE(file_util::PathExists(full_file_name));
315 EXPECT_FALSE(file_util::PathExists(dir));
316 EXPECT_TRUE(file_util::ContentsEqual(
317 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
318 full_file_name));
319 }
320
321 }
OLDNEW
« no previous file with comments | « chrome/browser/download/download_manager.cc ('k') | content/browser/download/save_package.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698