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

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

Issue 7606024: Add Cancel test for SavePackage. (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 | « no previous file | no next file » | 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_history.h"
(...skipping 26 matching lines...) Expand all
37 #endif 37 #endif
38 38
39 class SavePageBrowserTest : public InProcessBrowserTest { 39 class SavePageBrowserTest : public InProcessBrowserTest {
40 protected: 40 protected:
41 void SetUp() { 41 void SetUp() {
42 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_)); 42 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_));
43 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); 43 ASSERT_TRUE(save_dir_.CreateUniqueTempDir());
44 InProcessBrowserTest::SetUp(); 44 InProcessBrowserTest::SetUp();
45 } 45 }
46 46
47 GURL NavigateToMockURL(const std::string& prefix) {
48 GURL url = URLRequestMockHTTPJob::GetMockUrl(
49 FilePath(kTestDir).AppendASCII(prefix + ".htm"));
50 ui_test_utils::NavigateToURL(browser(), url);
51 return url;
52 }
53
54 // Returns full paths of destination file and directory.
55 void GetDestinationPaths(const std::string& prefix,
56 FilePath* full_file_name,
57 FilePath* dir) {
58 *full_file_name = save_dir_.path().AppendASCII(prefix + ".htm");
59 *dir = save_dir_.path().AppendASCII(prefix + "_files");
60 }
61
62 TabContents* GetCurrentTab() const {
63 TabContents* current_tab = browser()->GetSelectedTabContents();
64 EXPECT_TRUE(current_tab);
65 return current_tab;
66 }
67
68
47 GURL WaitForSavePackageToFinish() const { 69 GURL WaitForSavePackageToFinish() const {
48 ui_test_utils::TestNotificationObserver observer; 70 ui_test_utils::TestNotificationObserver observer;
49 ui_test_utils::RegisterAndWait(&observer, 71 ui_test_utils::RegisterAndWait(&observer,
50 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, 72 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED,
51 NotificationService::AllSources()); 73 NotificationService::AllSources());
52 return Details<DownloadItem>(observer.details()).ptr()->original_url(); 74 return Details<DownloadItem>(observer.details()).ptr()->original_url();
53 } 75 }
54 76
55 #if defined(OS_CHROMEOS) 77 #if defined(OS_CHROMEOS)
56 const ActiveDownloadsUI::DownloadList& GetDownloads() const { 78 const ActiveDownloadsUI::DownloadList& GetDownloads() const {
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 std::vector<DownloadHistoryInfo> history_entries_; 167 std::vector<DownloadHistoryInfo> history_entries_;
146 168
147 // Path to directory containing test data. 169 // Path to directory containing test data.
148 FilePath test_dir_; 170 FilePath test_dir_;
149 171
150 // Temporary directory we will save pages to. 172 // Temporary directory we will save pages to.
151 ScopedTempDir save_dir_; 173 ScopedTempDir save_dir_;
152 }; 174 };
153 175
154 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { 176 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) {
155 FilePath file_name(FILE_PATH_LITERAL("a.htm")); 177 GURL url = NavigateToMockURL("a");
156 GURL url = URLRequestMockHTTPJob::GetMockUrl(
157 FilePath(kTestDir).Append(file_name));
158 ui_test_utils::NavigateToURL(browser(), url);
159 178
160 TabContents* current_tab = browser()->GetSelectedTabContents(); 179 FilePath full_file_name, dir;
161 ASSERT_TRUE(current_tab); 180 GetDestinationPaths("a", &full_file_name, &dir);
162 181 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir,
163 FilePath full_file_name = save_dir_.path().Append(file_name); 182 SavePackage::SAVE_AS_ONLY_HTML));
164 FilePath dir = save_dir_.path().AppendASCII("a_files");
165 ASSERT_TRUE(current_tab->SavePage(full_file_name, dir,
166 SavePackage::SAVE_AS_ONLY_HTML));
167 183
168 EXPECT_EQ(url, WaitForSavePackageToFinish()); 184 EXPECT_EQ(url, WaitForSavePackageToFinish());
169 185
170 CheckDownloadUI(full_file_name); 186 CheckDownloadUI(full_file_name);
171 CheckDownloadHistory(url, full_file_name, 1); // a.htm is 1 file. 187 CheckDownloadHistory(url, full_file_name, 1); // a.htm is 1 file.
172 188
173 EXPECT_TRUE(file_util::PathExists(full_file_name)); 189 EXPECT_TRUE(file_util::PathExists(full_file_name));
174 EXPECT_FALSE(file_util::PathExists(dir)); 190 EXPECT_FALSE(file_util::PathExists(dir));
175 EXPECT_TRUE(file_util::ContentsEqual( 191 EXPECT_TRUE(file_util::ContentsEqual(
176 test_dir_.Append(FilePath(kTestDir)).Append(file_name), 192 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")),
177 full_file_name)); 193 full_file_name));
178 } 194 }
179 195
180 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveViewSourceHTMLOnly) { 196 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveViewSourceHTMLOnly) {
181 FilePath file_name(FILE_PATH_LITERAL("a.htm")); 197 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
182 GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl( 198 GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl(
183 FilePath(kTestDir).Append(file_name)); 199 FilePath(kTestDir).Append(file_name));
184 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl( 200 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl(
185 FilePath(kTestDir).Append(file_name)); 201 FilePath(kTestDir).Append(file_name));
186 ui_test_utils::NavigateToURL(browser(), view_source_url); 202 ui_test_utils::NavigateToURL(browser(), view_source_url);
187 203
188 TabContents* current_tab = browser()->GetSelectedTabContents(); 204 FilePath full_file_name, dir;
189 ASSERT_TRUE(current_tab); 205 GetDestinationPaths("a", &full_file_name, &dir);
190 206 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir,
191 FilePath full_file_name = save_dir_.path().Append(file_name); 207 SavePackage::SAVE_AS_ONLY_HTML));
192 FilePath dir = save_dir_.path().AppendASCII("a_files");
193
194 ASSERT_TRUE(current_tab->SavePage(full_file_name, dir,
195 SavePackage::SAVE_AS_ONLY_HTML));
196 208
197 EXPECT_EQ(actual_page_url, WaitForSavePackageToFinish()); 209 EXPECT_EQ(actual_page_url, WaitForSavePackageToFinish());
198 210
199 CheckDownloadUI(full_file_name); 211 CheckDownloadUI(full_file_name);
200 CheckDownloadHistory(actual_page_url, full_file_name, 1); // a.htm is 1 file. 212 CheckDownloadHistory(actual_page_url, full_file_name, 1); // a.htm is 1 file.
201 213
202 EXPECT_TRUE(file_util::PathExists(full_file_name)); 214 EXPECT_TRUE(file_util::PathExists(full_file_name));
203 EXPECT_FALSE(file_util::PathExists(dir)); 215 EXPECT_FALSE(file_util::PathExists(dir));
204 EXPECT_TRUE(file_util::ContentsEqual( 216 EXPECT_TRUE(file_util::ContentsEqual(
205 test_dir_.Append(FilePath(kTestDir)).Append(file_name), 217 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
206 full_file_name)); 218 full_file_name));
207 } 219 }
208 220
209 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) { 221 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) {
210 FilePath file_name(FILE_PATH_LITERAL("b.htm")); 222 GURL url = NavigateToMockURL("b");
211 GURL url = URLRequestMockHTTPJob::GetMockUrl(
212 FilePath(kTestDir).Append(file_name));
213 ui_test_utils::NavigateToURL(browser(), url);
214 223
215 TabContents* current_tab = browser()->GetSelectedTabContents(); 224 FilePath full_file_name, dir;
216 ASSERT_TRUE(current_tab); 225 GetDestinationPaths("b", &full_file_name, &dir);
217 226 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir,
218 FilePath full_file_name = save_dir_.path().Append(file_name); 227 SavePackage::SAVE_AS_COMPLETE_HTML));
219 FilePath dir = save_dir_.path().AppendASCII("b_files");
220 ASSERT_TRUE(current_tab->SavePage(full_file_name, dir,
221 SavePackage::SAVE_AS_COMPLETE_HTML));
222 228
223 EXPECT_EQ(url, WaitForSavePackageToFinish()); 229 EXPECT_EQ(url, WaitForSavePackageToFinish());
224 230
225 CheckDownloadUI(full_file_name); 231 CheckDownloadUI(full_file_name);
226 CheckDownloadHistory(url, full_file_name, 3); // b.htm is 3 files. 232 CheckDownloadHistory(url, full_file_name, 3); // b.htm is 3 files.
227 233
228 EXPECT_TRUE(file_util::PathExists(full_file_name)); 234 EXPECT_TRUE(file_util::PathExists(full_file_name));
229 EXPECT_TRUE(file_util::PathExists(dir)); 235 EXPECT_TRUE(file_util::PathExists(dir));
230 EXPECT_TRUE(file_util::TextContentsEqual( 236 EXPECT_TRUE(file_util::TextContentsEqual(
231 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"), 237 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"),
232 full_file_name)); 238 full_file_name));
233 EXPECT_TRUE(file_util::ContentsEqual( 239 EXPECT_TRUE(file_util::ContentsEqual(
234 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), 240 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"),
235 dir.AppendASCII("1.png"))); 241 dir.AppendASCII("1.png")));
236 EXPECT_TRUE(file_util::ContentsEqual( 242 EXPECT_TRUE(file_util::ContentsEqual(
237 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), 243 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"),
238 dir.AppendASCII("1.css"))); 244 dir.AppendASCII("1.css")));
239 } 245 }
240 246
241 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) { 247 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) {
242 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL)); 248 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL));
243 ASSERT_TRUE(browser()->command_updater()->SupportsCommand(IDC_SAVE_PAGE)); 249 ASSERT_TRUE(browser()->command_updater()->SupportsCommand(IDC_SAVE_PAGE));
244 EXPECT_FALSE(browser()->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)); 250 EXPECT_FALSE(browser()->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE));
245 } 251 }
246 252
247 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) { 253 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) {
248 FilePath file_name(FILE_PATH_LITERAL("b.htm")); 254 GURL url = NavigateToMockURL("b");
249
250 GURL url = URLRequestMockHTTPJob::GetMockUrl(
251 FilePath(kTestDir).Append(file_name));
252 ui_test_utils::NavigateToURL(browser(), url);
253 255
254 FilePath full_file_name = save_dir_.path().AppendASCII( 256 FilePath full_file_name = save_dir_.path().AppendASCII(
255 std::string("Test page for saving page feature") + kAppendedExtension); 257 std::string("Test page for saving page feature") + kAppendedExtension);
256 FilePath dir = save_dir_.path().AppendASCII( 258 FilePath dir = save_dir_.path().AppendASCII(
257 "Test page for saving page feature_files"); 259 "Test page for saving page feature_files");
258 260 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir,
259 TabContents* current_tab = browser()->GetSelectedTabContents(); 261 SavePackage::SAVE_AS_COMPLETE_HTML));
260 ASSERT_TRUE(current_tab);
261
262 ASSERT_TRUE(current_tab->SavePage(full_file_name, dir,
263 SavePackage::SAVE_AS_COMPLETE_HTML));
264 262
265 EXPECT_EQ(url, WaitForSavePackageToFinish()); 263 EXPECT_EQ(url, WaitForSavePackageToFinish());
266 264
267 CheckDownloadUI(full_file_name); 265 CheckDownloadUI(full_file_name);
268 CheckDownloadHistory(url, full_file_name, 3); // b.htm is 3 files. 266 CheckDownloadHistory(url, full_file_name, 3); // b.htm is 3 files.
269 267
270 EXPECT_TRUE(file_util::PathExists(full_file_name)); 268 EXPECT_TRUE(file_util::PathExists(full_file_name));
271 EXPECT_TRUE(file_util::PathExists(dir)); 269 EXPECT_TRUE(file_util::PathExists(dir));
272 EXPECT_TRUE(file_util::TextContentsEqual( 270 EXPECT_TRUE(file_util::TextContentsEqual(
273 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"), 271 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"),
274 full_file_name)); 272 full_file_name));
275 EXPECT_TRUE(file_util::ContentsEqual( 273 EXPECT_TRUE(file_util::ContentsEqual(
276 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), 274 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"),
277 dir.AppendASCII("1.png"))); 275 dir.AppendASCII("1.png")));
278 EXPECT_TRUE(file_util::ContentsEqual( 276 EXPECT_TRUE(file_util::ContentsEqual(
279 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), 277 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"),
280 dir.AppendASCII("1.css"))); 278 dir.AppendASCII("1.css")));
281 } 279 }
282 280
283 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, RemoveFromList) { 281 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, RemoveFromList) {
284 FilePath file_name(FILE_PATH_LITERAL("a.htm")); 282 GURL url = NavigateToMockURL("a");
285 GURL url = URLRequestMockHTTPJob::GetMockUrl(
286 FilePath(kTestDir).Append(file_name));
287 ui_test_utils::NavigateToURL(browser(), url);
288 283
289 TabContents* current_tab = browser()->GetSelectedTabContents(); 284 FilePath full_file_name, dir;
290 ASSERT_TRUE(current_tab); 285 GetDestinationPaths("a", &full_file_name, &dir);
291 286 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir,
292 FilePath full_file_name = save_dir_.path().Append(file_name); 287 SavePackage::SAVE_AS_ONLY_HTML));
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 288
297 EXPECT_EQ(url, WaitForSavePackageToFinish()); 289 EXPECT_EQ(url, WaitForSavePackageToFinish());
298 290
299 CheckDownloadUI(full_file_name); 291 CheckDownloadUI(full_file_name);
300 CheckDownloadHistory(url, full_file_name, 1); // a.htm is 1 file. 292 CheckDownloadHistory(url, full_file_name, 1); // a.htm is 1 file.
301 293
302 EXPECT_EQ(GetDownloadManager()->RemoveAllDownloads(), 1); 294 EXPECT_EQ(GetDownloadManager()->RemoveAllDownloads(), 1);
303 295
304 #if defined(OS_CHROMEOS) 296 #if defined(OS_CHROMEOS)
305 EXPECT_EQ(GetDownloads().size(), 0U); 297 EXPECT_EQ(GetDownloads().size(), 0U);
306 #endif 298 #endif
307 299
308 // Should not be in history. 300 // Should not be in history.
309 QueryDownloadHistory(); 301 QueryDownloadHistory();
310 EXPECT_EQ(std::find_if(history_entries_.begin(), history_entries_.end(), 302 EXPECT_EQ(std::find_if(history_entries_.begin(), history_entries_.end(),
311 DownloadHistoryInfoMatch(url, full_file_name, 1)), 303 DownloadHistoryInfoMatch(url, full_file_name, 1)),
312 history_entries_.end()); 304 history_entries_.end());
313 305
314 EXPECT_TRUE(file_util::PathExists(full_file_name)); 306 EXPECT_TRUE(file_util::PathExists(full_file_name));
315 EXPECT_FALSE(file_util::PathExists(dir)); 307 EXPECT_FALSE(file_util::PathExists(dir));
316 EXPECT_TRUE(file_util::ContentsEqual( 308 EXPECT_TRUE(file_util::ContentsEqual(
317 test_dir_.Append(FilePath(kTestDir)).Append(file_name), 309 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")),
318 full_file_name)); 310 full_file_name));
319 } 311 }
320 312
313 // Create a SavePackage and delete it without calling Init.
314 // SavePackage dtor has various asserts/checks that should not fire.
315 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, ImplicitCancel) {
316 GURL url = NavigateToMockURL("a");
317 FilePath full_file_name, dir;
318 GetDestinationPaths("a", &full_file_name, &dir);
319 scoped_refptr<SavePackage> save_package(new SavePackage(GetCurrentTab(),
320 SavePackage::SAVE_AS_ONLY_HTML, full_file_name, dir));
321 } 321 }
322
323 // Create a SavePackage, call Cancel, then delete it.
324 // SavePackage dtor has various asserts/checks that should not fire.
325 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, ExplicitCancel) {
326 GURL url = NavigateToMockURL("a");
327 FilePath full_file_name, dir;
328 GetDestinationPaths("a", &full_file_name, &dir);
329 scoped_refptr<SavePackage> save_package(new SavePackage(GetCurrentTab(),
330 SavePackage::SAVE_AS_ONLY_HTML, full_file_name, dir));
331 save_package->Cancel(true);
332 }
333
334 } // namespace
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698