Chromium Code Reviews| 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_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 57 | 57 |
| 58 static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("save_page"); | 58 static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("save_page"); |
| 59 | 59 |
| 60 static const char* kAppendedExtension = | 60 static const char* kAppendedExtension = |
| 61 #if defined(OS_WIN) | 61 #if defined(OS_WIN) |
| 62 ".htm"; | 62 ".htm"; |
| 63 #else | 63 #else |
| 64 ".html"; | 64 ".html"; |
| 65 #endif | 65 #endif |
| 66 | 66 |
| 67 void NullFunction() { | |
| 68 } | |
| 69 | |
| 67 } // namespace | 70 } // namespace |
| 68 | 71 |
| 69 // Loosely based on logic in DownloadTestObserver. | 72 // Loosely based on logic in DownloadTestObserver. |
| 70 class DownloadItemCreatedObserver : public DownloadManager::Observer { | 73 class DownloadItemCreatedObserver : public DownloadManager::Observer { |
| 71 public: | 74 public: |
| 72 explicit DownloadItemCreatedObserver(DownloadManager* manager) | 75 explicit DownloadItemCreatedObserver(DownloadManager* manager) |
| 73 : waiting_(false), manager_(manager), created_item_(NULL) { | 76 : waiting_(false), manager_(manager) { |
| 74 manager->AddObserver(this); | 77 manager->AddObserver(this); |
| 75 } | 78 } |
| 76 | 79 |
| 77 ~DownloadItemCreatedObserver() { | 80 ~DownloadItemCreatedObserver() { |
| 78 if (manager_) | 81 if (manager_) |
| 79 manager_->RemoveObserver(this); | 82 manager_->RemoveObserver(this); |
| 80 } | 83 } |
| 81 | 84 |
| 82 // Wait for the first download item created after object creation. | 85 // Wait for the first download item created after object creation. |
| 83 // Note that this class provides no protection against the download | 86 // Note that this class provides no protection against the download |
| 84 // being destroyed between creation and return of WaitForNewDownloadItem(); | 87 // being destroyed between creation and return of WaitForNewDownloadItem(); |
| 85 // the caller must guarantee that in some other fashion. | 88 // the caller must guarantee that in some other fashion. |
| 86 DownloadItem* WaitForNewDownloadItem() { | 89 const std::vector<DownloadItem*>& WaitForDownloadItem() { |
|
benjhayden
2012/08/30 17:31:15
Could you return via pointer instead?
void WaitFor
Randy Smith (Not in Mondays)
2012/08/30 19:44:10
Yeah, that's probably more in line with existing c
| |
| 87 if (!manager_) { | 90 if (!manager_) { |
| 88 // The manager went away before we were asked to wait; return | 91 // The manager went away before we were asked to wait; return |
| 89 // what we have, even if it's null. | 92 // what we have, even if it's null. |
| 90 return created_item_; | 93 return items_seen_; |
| 91 } | 94 } |
| 92 | 95 |
| 93 if (!created_item_) { | 96 if (items_seen_.empty()) { |
| 94 waiting_ = true; | 97 waiting_ = true; |
| 95 content::RunMessageLoop(); | 98 content::RunMessageLoop(); |
| 96 waiting_ = false; | 99 waiting_ = false; |
| 97 } | 100 } |
| 98 return created_item_; | 101 |
| 102 return items_seen_; | |
| 99 } | 103 } |
| 100 | 104 |
| 101 private: | 105 private: |
| 102 | 106 |
| 103 // DownloadManager::Observer | 107 // DownloadManager::Observer |
| 104 void OnDownloadCreated(DownloadManager* manager, DownloadItem* item) { | 108 virtual void OnDownloadCreated( |
| 109 DownloadManager* manager, DownloadItem* item) OVERRIDE { | |
| 105 DCHECK_EQ(manager, manager_); | 110 DCHECK_EQ(manager, manager_); |
| 106 if (!created_item_) | 111 items_seen_.push_back(item); |
| 107 created_item_ = item; | |
| 108 | 112 |
| 109 if (waiting_) | 113 if (waiting_) |
| 110 MessageLoopForUI::current()->Quit(); | 114 MessageLoopForUI::current()->Quit(); |
| 111 } | 115 } |
| 112 | 116 |
| 113 void ManagerGoingDownload(DownloadManager* manager) { | 117 virtual void ManagerGoingDown(DownloadManager* manager) OVERRIDE { |
| 114 manager_->RemoveObserver(this); | 118 manager_->RemoveObserver(this); |
| 115 manager_ = NULL; | 119 manager_ = NULL; |
| 116 if (waiting_) | 120 if (waiting_) |
| 117 MessageLoopForUI::current()->Quit(); | 121 MessageLoopForUI::current()->Quit(); |
| 118 } | 122 } |
| 119 | 123 |
| 120 bool waiting_; | 124 bool waiting_; |
| 121 DownloadManager* manager_; | 125 DownloadManager* manager_; |
| 122 DownloadItem* created_item_; | 126 std::vector<DownloadItem*> items_seen_; |
| 123 | 127 |
| 124 DISALLOW_COPY_AND_ASSIGN(DownloadItemCreatedObserver); | 128 DISALLOW_COPY_AND_ASSIGN(DownloadItemCreatedObserver); |
| 125 }; | 129 }; |
| 126 | 130 |
| 131 class DownloadPersistedObserver : public DownloadItem::Observer { | |
| 132 public: | |
| 133 explicit DownloadPersistedObserver(DownloadItem* item) | |
| 134 : waiting_(false), item_(item) { | |
| 135 item->AddObserver(this); | |
| 136 } | |
| 137 | |
| 138 ~DownloadPersistedObserver() { | |
| 139 if (item_) | |
| 140 item_->RemoveObserver(this); | |
| 141 } | |
| 142 | |
| 143 // Wait for download item to get the persisted bit set. | |
| 144 // Note that this class provides no protection against the download | |
| 145 // being destroyed between creation and return of WaitForPersisted(); | |
| 146 // the caller must guarantee that in some other fashion. | |
| 147 void WaitForPersisted() { | |
| 148 // In combination with OnDownloadDestroyed() below, verify the | |
| 149 // above interface contract. | |
| 150 DCHECK(item_); | |
| 151 | |
| 152 if (item_->IsPersisted()) | |
| 153 return; | |
| 154 | |
| 155 waiting_ = true; | |
| 156 content::RunMessageLoop(); | |
| 157 waiting_ = false; | |
| 158 | |
| 159 return; | |
| 160 } | |
| 161 | |
| 162 private: | |
| 163 // DownloadItem::Observer | |
| 164 virtual void OnDownloadUpdated(DownloadItem* item) OVERRIDE { | |
| 165 DCHECK_EQ(item, item_); | |
| 166 | |
| 167 if (waiting_ && item->IsPersisted()) | |
| 168 MessageLoopForUI::current()->Quit(); | |
| 169 } | |
| 170 | |
| 171 virtual void OnDownloadDestroyed(DownloadItem* item) OVERRIDE { | |
| 172 if (item != item_) | |
| 173 return; | |
| 174 | |
| 175 item_->RemoveObserver(this); | |
| 176 item_ = NULL; | |
| 177 } | |
| 178 | |
| 179 bool waiting_; | |
| 180 DownloadItem* item_; | |
| 181 | |
| 182 DISALLOW_COPY_AND_ASSIGN(DownloadPersistedObserver); | |
| 183 }; | |
| 184 | |
| 127 class SavePageBrowserTest : public InProcessBrowserTest { | 185 class SavePageBrowserTest : public InProcessBrowserTest { |
| 128 public: | 186 public: |
| 129 SavePageBrowserTest() {} | 187 SavePageBrowserTest() {} |
| 130 virtual ~SavePageBrowserTest(); | 188 virtual ~SavePageBrowserTest(); |
| 131 | 189 |
| 132 protected: | 190 protected: |
| 133 void SetUp() OVERRIDE { | 191 void SetUp() OVERRIDE { |
| 134 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_)); | 192 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_)); |
| 135 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); | 193 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); |
| 136 InProcessBrowserTest::SetUp(); | 194 InProcessBrowserTest::SetUp(); |
| 137 } | 195 } |
| 138 | 196 |
| 139 void SetUpOnMainThread() OVERRIDE { | 197 void SetUpOnMainThread() OVERRIDE { |
| 140 browser()->profile()->GetPrefs()->SetFilePath( | 198 browser()->profile()->GetPrefs()->SetFilePath( |
| 141 prefs::kDownloadDefaultDirectory, save_dir_.path()); | 199 prefs::kDownloadDefaultDirectory, save_dir_.path()); |
| 142 BrowserThread::PostTask( | 200 BrowserThread::PostTask( |
| 143 BrowserThread::IO, FROM_HERE, | 201 BrowserThread::IO, FROM_HERE, |
| 144 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); | 202 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); |
| 203 item_creation_observer_.reset( | |
| 204 new DownloadItemCreatedObserver(GetDownloadManager())); | |
| 145 } | 205 } |
| 146 | 206 |
| 147 GURL NavigateToMockURL(const std::string& prefix) { | 207 GURL NavigateToMockURL(const std::string& prefix) { |
| 148 GURL url = URLRequestMockHTTPJob::GetMockUrl( | 208 GURL url = URLRequestMockHTTPJob::GetMockUrl( |
| 149 FilePath(kTestDir).AppendASCII(prefix + ".htm")); | 209 FilePath(kTestDir).AppendASCII(prefix + ".htm")); |
| 150 ui_test_utils::NavigateToURL(browser(), url); | 210 ui_test_utils::NavigateToURL(browser(), url); |
| 151 return url; | 211 return url; |
| 152 } | 212 } |
| 153 | 213 |
| 154 // Returns full paths of destination file and directory. | 214 // Returns full paths of destination file and directory. |
| 155 void GetDestinationPaths(const std::string& prefix, | 215 void GetDestinationPaths(const std::string& prefix, |
| 156 FilePath* full_file_name, | 216 FilePath* full_file_name, |
| 157 FilePath* dir) { | 217 FilePath* dir) { |
| 158 *full_file_name = save_dir_.path().AppendASCII(prefix + ".htm"); | 218 *full_file_name = save_dir_.path().AppendASCII(prefix + ".htm"); |
| 159 *dir = save_dir_.path().AppendASCII(prefix + "_files"); | 219 *dir = save_dir_.path().AppendASCII(prefix + "_files"); |
| 160 } | 220 } |
| 161 | 221 |
| 162 WebContents* GetCurrentTab() const { | 222 WebContents* GetCurrentTab() const { |
| 163 WebContents* current_tab = chrome::GetActiveWebContents(browser()); | 223 WebContents* current_tab = chrome::GetActiveWebContents(browser()); |
| 164 EXPECT_TRUE(current_tab); | 224 EXPECT_TRUE(current_tab); |
| 165 return current_tab; | 225 return current_tab; |
| 166 } | 226 } |
| 167 | 227 |
| 168 | |
| 169 GURL WaitForSavePackageToFinish() const { | 228 GURL WaitForSavePackageToFinish() const { |
| 170 content::WindowedNotificationObserver observer( | 229 content::WindowedNotificationObserver observer( |
| 171 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, | 230 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, |
| 172 content::NotificationService::AllSources()); | 231 content::NotificationService::AllSources()); |
| 173 observer.Wait(); | 232 observer.Wait(); |
| 233 | |
| 234 // Generally, there should only be one download item created | |
| 235 // in all of these tests. Wait for it, and wait for it to | |
| 236 // be persisted. | |
| 237 const std::vector<DownloadItem*>& items( | |
| 238 item_creation_observer_->WaitForDownloadItem()); | |
| 239 DCHECK_EQ(1u, items.size()); | |
| 240 DownloadItem* download_item(items[0]); | |
| 241 | |
| 242 // Note on synchronization: | |
| 243 // | |
| 244 // For each Save Page As operation, we create a corresponding shell | |
| 245 // DownloadItem to display progress to the user. That DownloadItem | |
| 246 // goes through its own state transitions, including being persisted | |
| 247 // out to the history database, and the download shelf is not shown | |
| 248 // until after the persistence occurs. Save Package completion (and | |
| 249 // marking the DownloadItem as completed) occurs asynchronously from | |
| 250 // persistence. Thus if we want to examine either UI state or DB | |
| 251 // state, we need to wait until both the save package operation is | |
|
benjhayden
2012/08/30 17:31:15
Do we really need to wait for the save package fin
Randy Smith (Not in Mondays)
2012/08/30 19:44:10
Huh. Good point. I think I'll say that I'd rathe
| |
| 252 // complete and the relevant download item has been persisted. | |
| 253 DownloadPersistedObserver(download_item).WaitForPersisted(); | |
| 254 | |
| 174 return content::Details<DownloadItem>(observer.details()).ptr()-> | 255 return content::Details<DownloadItem>(observer.details()).ptr()-> |
| 175 GetOriginalUrl(); | 256 GetOriginalUrl(); |
| 176 } | 257 } |
| 177 | 258 |
| 178 DownloadManager* GetDownloadManager() const { | 259 DownloadManager* GetDownloadManager() const { |
| 179 DownloadManager* download_manager = | 260 DownloadManager* download_manager = |
| 180 BrowserContext::GetDownloadManager(browser()->profile()); | 261 BrowserContext::GetDownloadManager(browser()->profile()); |
| 181 EXPECT_TRUE(download_manager); | 262 EXPECT_TRUE(download_manager); |
| 182 return download_manager; | 263 return download_manager; |
| 183 } | 264 } |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 202 | 283 |
| 203 // Indicate thet we have received the history and can continue. | 284 // Indicate thet we have received the history and can continue. |
| 204 MessageLoopForUI::current()->Quit(); | 285 MessageLoopForUI::current()->Quit(); |
| 205 } | 286 } |
| 206 | 287 |
| 207 struct DownloadPersistentStoreInfoMatch | 288 struct DownloadPersistentStoreInfoMatch |
| 208 : public std::unary_function<DownloadPersistentStoreInfo, bool> { | 289 : public std::unary_function<DownloadPersistentStoreInfo, bool> { |
| 209 | 290 |
| 210 DownloadPersistentStoreInfoMatch(const GURL& url, | 291 DownloadPersistentStoreInfoMatch(const GURL& url, |
| 211 const FilePath& path, | 292 const FilePath& path, |
| 212 int64 num_files) | 293 int64 num_files, |
| 294 DownloadItem::DownloadState state) | |
| 213 : url_(url), | 295 : url_(url), |
| 214 path_(path), | 296 path_(path), |
| 215 num_files_(num_files) { | 297 num_files_(num_files), |
| 216 } | 298 state_(state) {} |
| 217 | 299 |
| 218 bool operator() (const DownloadPersistentStoreInfo& info) const { | 300 bool operator() (const DownloadPersistentStoreInfo& info) const { |
| 219 return info.url == url_ && | 301 return info.url == url_ && |
| 220 info.path == path_ && | 302 info.path == path_ && |
| 221 // For non-MHTML save packages, received_bytes is actually the | 303 // For non-MHTML save packages, received_bytes is actually the |
| 222 // number of files. | 304 // number of files. |
| 223 ((num_files_ < 0) || | 305 ((num_files_ < 0) || |
| 224 (info.received_bytes == num_files_)) && | 306 (info.received_bytes == num_files_)) && |
| 225 info.total_bytes == 0 && | 307 info.total_bytes == 0 && |
| 226 info.state == DownloadItem::COMPLETE; | 308 info.state == state_; |
| 227 } | 309 } |
| 228 | 310 |
| 229 GURL url_; | 311 GURL url_; |
| 230 FilePath path_; | 312 FilePath path_; |
| 231 int64 num_files_; | 313 int64 num_files_; |
| 314 DownloadItem::DownloadState state_; | |
| 232 }; | 315 }; |
| 233 | 316 |
| 234 void CheckDownloadHistory(const GURL& url, | 317 void CheckDownloadHistory(const GURL& url, |
| 235 const FilePath& path, | 318 const FilePath& path, |
| 236 int64 num_files) { | 319 int64 num_files, |
| 320 DownloadItem::DownloadState state) { | |
| 321 // Make sure the relevant download item made it into the history. | |
| 322 std::vector<DownloadItem*> downloads; | |
| 323 GetDownloadManager()->SearchDownloads(string16(), &downloads); | |
| 324 ASSERT_EQ(1u, downloads.size()); | |
| 325 | |
| 237 QueryDownloadHistory(); | 326 QueryDownloadHistory(); |
| 238 | 327 |
| 239 std::vector<DownloadPersistentStoreInfo>::iterator found = | 328 std::vector<DownloadPersistentStoreInfo>::iterator found = |
| 240 std::find_if(history_entries_.begin(), history_entries_.end(), | 329 std::find_if(history_entries_.begin(), history_entries_.end(), |
| 241 DownloadPersistentStoreInfoMatch(url, path, num_files)); | 330 DownloadPersistentStoreInfoMatch(url, path, num_files, |
| 331 state)); | |
| 242 | 332 |
| 243 if (found == history_entries_.end()) { | 333 if (found == history_entries_.end()) { |
| 244 LOG(ERROR) << "Missing url=" << url.spec() | 334 LOG(ERROR) << "Missing url=" << url.spec() |
| 245 << " path=" << path.value() | 335 << " path=" << path.value() |
| 246 << " received=" << num_files; | 336 << " received=" << num_files; |
| 247 for (size_t index = 0; index < history_entries_.size(); ++index) { | 337 for (size_t index = 0; index < history_entries_.size(); ++index) { |
| 248 LOG(ERROR) << "History@" << index << ": url=" | 338 LOG(ERROR) << "History@" << index << ": url=" |
| 249 << history_entries_[index].url.spec() | 339 << history_entries_[index].url.spec() |
| 250 << " path=" << history_entries_[index].path.value() | 340 << " path=" << history_entries_[index].path.value() |
| 251 << " received=" << history_entries_[index].received_bytes | 341 << " received=" << history_entries_[index].received_bytes |
| 252 << " total=" << history_entries_[index].total_bytes | 342 << " total=" << history_entries_[index].total_bytes |
| 253 << " state=" << history_entries_[index].state; | 343 << " state=" << history_entries_[index].state; |
| 254 } | 344 } |
| 255 EXPECT_TRUE(false); | 345 EXPECT_TRUE(false); |
| 256 } | 346 } |
| 257 } | 347 } |
| 258 | 348 |
| 259 std::vector<DownloadPersistentStoreInfo> history_entries_; | 349 std::vector<DownloadPersistentStoreInfo> history_entries_; |
| 260 | 350 |
| 261 // Path to directory containing test data. | 351 // Path to directory containing test data. |
| 262 FilePath test_dir_; | 352 FilePath test_dir_; |
| 263 | 353 |
| 264 // Temporary directory we will save pages to. | 354 // Temporary directory we will save pages to. |
| 265 ScopedTempDir save_dir_; | 355 ScopedTempDir save_dir_; |
| 266 | 356 |
| 267 private: | 357 private: |
| 358 scoped_ptr<DownloadItemCreatedObserver> item_creation_observer_; | |
| 359 | |
| 268 DISALLOW_COPY_AND_ASSIGN(SavePageBrowserTest); | 360 DISALLOW_COPY_AND_ASSIGN(SavePageBrowserTest); |
| 269 }; | 361 }; |
| 270 | 362 |
| 271 SavePageBrowserTest::~SavePageBrowserTest() { | 363 SavePageBrowserTest::~SavePageBrowserTest() { |
| 272 } | 364 } |
| 273 | 365 |
| 274 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { | 366 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { |
| 275 GURL url = NavigateToMockURL("a"); | 367 GURL url = NavigateToMockURL("a"); |
| 276 | 368 |
| 277 FilePath full_file_name, dir; | 369 FilePath full_file_name, dir; |
| 278 GetDestinationPaths("a", &full_file_name, &dir); | 370 GetDestinationPaths("a", &full_file_name, &dir); |
| 279 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, | 371 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, |
| 280 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); | 372 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); |
| 281 | 373 |
| 282 EXPECT_EQ(url, WaitForSavePackageToFinish()); | 374 EXPECT_EQ(url, WaitForSavePackageToFinish()); |
| 283 | 375 |
| 284 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | 376 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
| 285 CheckDownloadHistory(url, full_file_name, 1); // a.htm is 1 file. | 377 // a.htm is 1 file. |
| 378 CheckDownloadHistory(url, full_file_name, 1, DownloadItem::COMPLETE); | |
| 286 | 379 |
| 287 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 380 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
| 288 EXPECT_FALSE(file_util::PathExists(dir)); | 381 EXPECT_FALSE(file_util::PathExists(dir)); |
| 289 EXPECT_TRUE(file_util::ContentsEqual( | 382 EXPECT_TRUE(file_util::ContentsEqual( |
| 290 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), | 383 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), |
| 291 full_file_name)); | 384 full_file_name)); |
| 292 } | 385 } |
| 293 | 386 |
| 294 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnlyCancel) { | 387 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnlyCancel) { |
| 295 GURL url = NavigateToMockURL("a"); | 388 GURL url = NavigateToMockURL("a"); |
| 296 DownloadManager* manager(GetDownloadManager()); | 389 DownloadManager* manager(GetDownloadManager()); |
| 297 std::vector<DownloadItem*> downloads; | 390 std::vector<DownloadItem*> downloads; |
| 298 manager->SearchDownloads(string16(), &downloads); | 391 manager->SearchDownloads(string16(), &downloads); |
| 299 ASSERT_EQ(0u, downloads.size()); | 392 ASSERT_EQ(0u, downloads.size()); |
| 300 | 393 |
| 301 FilePath full_file_name, dir; | 394 FilePath full_file_name, dir; |
| 302 GetDestinationPaths("a", &full_file_name, &dir); | 395 GetDestinationPaths("a", &full_file_name, &dir); |
| 303 DownloadItemCreatedObserver creation_observer(manager); | 396 DownloadItemCreatedObserver creation_observer(manager); |
| 304 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, | 397 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, |
| 305 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); | 398 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); |
| 306 DownloadItem* item = creation_observer.WaitForNewDownloadItem(); | 399 const std::vector<DownloadItem*>& items( |
| 307 item->Cancel(true); | 400 creation_observer.WaitForDownloadItem()); |
| 401 ASSERT_TRUE(items.size() == 1); | |
| 402 items[0]->Cancel(true); | |
| 308 | 403 |
| 309 // TODO(rdsmith): Fix DII::Cancel() to actually cancel the save package. | 404 // TODO(rdsmith): Fix DII::Cancel() to actually cancel the save package. |
| 310 // Currently it's ignored. | 405 // Currently it's ignored. |
| 311 EXPECT_EQ(url, WaitForSavePackageToFinish()); | 406 EXPECT_EQ(url, WaitForSavePackageToFinish()); |
| 312 | 407 |
| 408 // -1 to disable number of files check; we don't update after cancel, and | |
| 409 // we don't know when the single file completed in relationship to | |
| 410 // the cancel. | |
| 411 CheckDownloadHistory(url, full_file_name, -1, DownloadItem::CANCELLED); | |
| 412 | |
| 313 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | 413 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
| 314 CheckDownloadHistory(url, full_file_name, 1); // a.htm is 1 file. | |
| 315 | 414 |
| 316 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 415 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
| 317 EXPECT_FALSE(file_util::PathExists(dir)); | 416 EXPECT_FALSE(file_util::PathExists(dir)); |
| 318 EXPECT_TRUE(file_util::ContentsEqual( | 417 EXPECT_TRUE(file_util::ContentsEqual( |
| 319 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), | 418 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), |
| 320 full_file_name)); | 419 full_file_name)); |
| 321 } | 420 } |
| 322 | 421 |
| 323 // SavePageBrowserTest.SaveHTMLOnlyTabDestroy is flaky. | 422 // SavePageBrowserTest.SaveHTMLOnlyTabDestroy is flaky. |
| 324 // See http://crbug.com/144751. | 423 // See http://crbug.com/144751. |
| 325 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, DISABLED_SaveHTMLOnlyTabDestroy) { | 424 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, DISABLED_SaveHTMLOnlyTabDestroy) { |
| 326 GURL url = NavigateToMockURL("a"); | 425 GURL url = NavigateToMockURL("a"); |
| 327 DownloadManager* manager(GetDownloadManager()); | 426 DownloadManager* manager(GetDownloadManager()); |
| 328 std::vector<DownloadItem*> downloads; | 427 std::vector<DownloadItem*> downloads; |
| 329 manager->SearchDownloads(string16(), &downloads); | 428 manager->SearchDownloads(string16(), &downloads); |
| 330 ASSERT_EQ(0u, downloads.size()); | 429 ASSERT_EQ(0u, downloads.size()); |
| 331 | 430 |
| 332 FilePath full_file_name, dir; | 431 FilePath full_file_name, dir; |
| 333 GetDestinationPaths("a", &full_file_name, &dir); | 432 GetDestinationPaths("a", &full_file_name, &dir); |
| 334 DownloadItemCreatedObserver creation_observer(manager); | 433 DownloadItemCreatedObserver creation_observer(manager); |
| 335 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, | 434 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, |
| 336 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); | 435 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); |
| 337 DownloadItem* item = creation_observer.WaitForNewDownloadItem(); | 436 const std::vector<DownloadItem*>& items( |
| 437 creation_observer.WaitForDownloadItem()); | |
| 438 ASSERT_TRUE(items.size() == 1); | |
| 338 | 439 |
| 339 // Close the tab; does this cancel the download? | 440 // Close the tab; does this cancel the download? |
| 340 GetCurrentTab()->Close(); | 441 GetCurrentTab()->Close(); |
| 341 EXPECT_TRUE(item->IsCancelled()); | 442 EXPECT_TRUE(items[0]->IsCancelled()); |
| 342 | 443 |
| 343 EXPECT_FALSE(file_util::PathExists(full_file_name)); | 444 EXPECT_FALSE(file_util::PathExists(full_file_name)); |
| 344 EXPECT_FALSE(file_util::PathExists(dir)); | 445 EXPECT_FALSE(file_util::PathExists(dir)); |
| 345 } | 446 } |
| 346 | 447 |
| 347 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveViewSourceHTMLOnly) { | 448 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveViewSourceHTMLOnly) { |
| 348 FilePath file_name(FILE_PATH_LITERAL("a.htm")); | 449 FilePath file_name(FILE_PATH_LITERAL("a.htm")); |
| 349 GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl( | 450 GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl( |
| 350 FilePath(kTestDir).Append(file_name)); | 451 FilePath(kTestDir).Append(file_name)); |
| 351 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl( | 452 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl( |
| 352 FilePath(kTestDir).Append(file_name)); | 453 FilePath(kTestDir).Append(file_name)); |
| 353 ui_test_utils::NavigateToURL(browser(), view_source_url); | 454 ui_test_utils::NavigateToURL(browser(), view_source_url); |
| 354 | 455 |
| 355 FilePath full_file_name, dir; | 456 FilePath full_file_name, dir; |
| 356 GetDestinationPaths("a", &full_file_name, &dir); | 457 GetDestinationPaths("a", &full_file_name, &dir); |
| 357 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, | 458 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, |
| 358 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); | 459 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); |
| 359 | 460 |
| 360 EXPECT_EQ(actual_page_url, WaitForSavePackageToFinish()); | 461 EXPECT_EQ(actual_page_url, WaitForSavePackageToFinish()); |
| 361 | 462 |
| 362 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | 463 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
| 363 CheckDownloadHistory(actual_page_url, full_file_name, 1); // a.htm is 1 file. | 464 // a.htm is 1 file. |
| 465 CheckDownloadHistory(actual_page_url, full_file_name, 1, | |
| 466 DownloadItem::COMPLETE); | |
| 364 | 467 |
| 365 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 468 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
| 366 EXPECT_FALSE(file_util::PathExists(dir)); | 469 EXPECT_FALSE(file_util::PathExists(dir)); |
| 367 EXPECT_TRUE(file_util::ContentsEqual( | 470 EXPECT_TRUE(file_util::ContentsEqual( |
| 368 test_dir_.Append(FilePath(kTestDir)).Append(file_name), | 471 test_dir_.Append(FilePath(kTestDir)).Append(file_name), |
| 369 full_file_name)); | 472 full_file_name)); |
| 370 } | 473 } |
| 371 | 474 |
| 372 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) { | 475 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) { |
| 373 GURL url = NavigateToMockURL("b"); | 476 GURL url = NavigateToMockURL("b"); |
| 374 | 477 |
| 375 FilePath full_file_name, dir; | 478 FilePath full_file_name, dir; |
| 376 GetDestinationPaths("b", &full_file_name, &dir); | 479 GetDestinationPaths("b", &full_file_name, &dir); |
| 377 ASSERT_TRUE(GetCurrentTab()->SavePage( | 480 ASSERT_TRUE(GetCurrentTab()->SavePage( |
| 378 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML)); | 481 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML)); |
| 379 | 482 |
| 380 EXPECT_EQ(url, WaitForSavePackageToFinish()); | 483 EXPECT_EQ(url, WaitForSavePackageToFinish()); |
| 381 | 484 |
| 382 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | 485 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
| 383 CheckDownloadHistory(url, full_file_name, 3); // b.htm is 3 files. | 486 // b.htm is 3 files. |
| 487 CheckDownloadHistory(url, full_file_name, 3, DownloadItem::COMPLETE); | |
| 384 | 488 |
| 385 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 489 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
| 386 EXPECT_TRUE(file_util::PathExists(dir)); | 490 EXPECT_TRUE(file_util::PathExists(dir)); |
| 387 EXPECT_TRUE(file_util::TextContentsEqual( | 491 EXPECT_TRUE(file_util::TextContentsEqual( |
| 388 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"), | 492 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"), |
| 389 full_file_name)); | 493 full_file_name)); |
| 390 EXPECT_TRUE(file_util::ContentsEqual( | 494 EXPECT_TRUE(file_util::ContentsEqual( |
| 391 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), | 495 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), |
| 392 dir.AppendASCII("1.png"))); | 496 dir.AppendASCII("1.png"))); |
| 393 EXPECT_TRUE(file_util::ContentsEqual( | 497 EXPECT_TRUE(file_util::ContentsEqual( |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 406 FilePath full_file_name = save_dir_.path().AppendASCII( | 510 FilePath full_file_name = save_dir_.path().AppendASCII( |
| 407 std::string("Test page for saving page feature") + kAppendedExtension); | 511 std::string("Test page for saving page feature") + kAppendedExtension); |
| 408 FilePath dir = save_dir_.path().AppendASCII( | 512 FilePath dir = save_dir_.path().AppendASCII( |
| 409 "Test page for saving page feature_files"); | 513 "Test page for saving page feature_files"); |
| 410 ASSERT_TRUE(GetCurrentTab()->SavePage( | 514 ASSERT_TRUE(GetCurrentTab()->SavePage( |
| 411 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML)); | 515 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML)); |
| 412 | 516 |
| 413 EXPECT_EQ(url, WaitForSavePackageToFinish()); | 517 EXPECT_EQ(url, WaitForSavePackageToFinish()); |
| 414 | 518 |
| 415 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | 519 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
| 416 CheckDownloadHistory(url, full_file_name, 3); // b.htm is 3 files. | 520 // b.htm is 3 files. |
| 521 CheckDownloadHistory(url, full_file_name, 3, DownloadItem::COMPLETE); | |
| 417 | 522 |
| 418 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 523 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
| 419 EXPECT_TRUE(file_util::PathExists(dir)); | 524 EXPECT_TRUE(file_util::PathExists(dir)); |
| 420 EXPECT_TRUE(file_util::TextContentsEqual( | 525 EXPECT_TRUE(file_util::TextContentsEqual( |
| 421 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"), | 526 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"), |
| 422 full_file_name)); | 527 full_file_name)); |
| 423 EXPECT_TRUE(file_util::ContentsEqual( | 528 EXPECT_TRUE(file_util::ContentsEqual( |
| 424 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), | 529 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), |
| 425 dir.AppendASCII("1.png"))); | 530 dir.AppendASCII("1.png"))); |
| 426 EXPECT_TRUE(file_util::ContentsEqual( | 531 EXPECT_TRUE(file_util::ContentsEqual( |
| 427 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), | 532 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), |
| 428 dir.AppendASCII("1.css"))); | 533 dir.AppendASCII("1.css"))); |
| 429 } | 534 } |
| 430 | 535 |
| 431 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, RemoveFromList) { | 536 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, RemoveFromList) { |
| 432 GURL url = NavigateToMockURL("a"); | 537 GURL url = NavigateToMockURL("a"); |
| 433 | 538 |
| 434 FilePath full_file_name, dir; | 539 FilePath full_file_name, dir; |
| 435 GetDestinationPaths("a", &full_file_name, &dir); | 540 GetDestinationPaths("a", &full_file_name, &dir); |
| 436 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, | 541 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, |
| 437 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); | 542 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); |
| 438 | 543 |
| 439 EXPECT_EQ(url, WaitForSavePackageToFinish()); | 544 EXPECT_EQ(url, WaitForSavePackageToFinish()); |
| 440 | 545 |
| 441 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | 546 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
| 442 CheckDownloadHistory(url, full_file_name, 1); // a.htm is 1 file. | 547 // a.htm is 1 file. |
| 548 CheckDownloadHistory(url, full_file_name, 1, DownloadItem::COMPLETE); | |
| 443 | 549 |
| 444 EXPECT_EQ(GetDownloadManager()->RemoveAllDownloads(), 1); | 550 EXPECT_EQ(GetDownloadManager()->RemoveAllDownloads(), 1); |
| 445 | 551 |
| 446 // Should not be in history. | 552 // Should not be in history. |
| 447 QueryDownloadHistory(); | 553 QueryDownloadHistory(); |
| 448 EXPECT_EQ(std::find_if(history_entries_.begin(), history_entries_.end(), | 554 EXPECT_EQ(std::find_if(history_entries_.begin(), history_entries_.end(), |
| 449 DownloadPersistentStoreInfoMatch(url, full_file_name, 1)), | 555 DownloadPersistentStoreInfoMatch( |
| 556 url, full_file_name, 1, DownloadItem::COMPLETE)), | |
| 450 history_entries_.end()); | 557 history_entries_.end()); |
| 451 | 558 |
| 452 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 559 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
| 453 EXPECT_FALSE(file_util::PathExists(dir)); | 560 EXPECT_FALSE(file_util::PathExists(dir)); |
| 454 EXPECT_TRUE(file_util::ContentsEqual( | 561 EXPECT_TRUE(file_util::ContentsEqual( |
| 455 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), | 562 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), |
| 456 full_file_name)); | 563 full_file_name)); |
| 457 } | 564 } |
| 458 | 565 |
| 459 // This tests that a webpage with the title "test.exe" is saved as | 566 // This tests that a webpage with the title "test.exe" is saved as |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 513 #if defined(OS_CHROMEOS) | 620 #if defined(OS_CHROMEOS) |
| 514 SavePackageFilePickerChromeOS::SetShouldPromptUser(false); | 621 SavePackageFilePickerChromeOS::SetShouldPromptUser(false); |
| 515 #else | 622 #else |
| 516 SavePackageFilePicker::SetShouldPromptUser(false); | 623 SavePackageFilePicker::SetShouldPromptUser(false); |
| 517 #endif | 624 #endif |
| 518 content::WindowedNotificationObserver observer( | 625 content::WindowedNotificationObserver observer( |
| 519 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, | 626 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, |
| 520 content::NotificationService::AllSources()); | 627 content::NotificationService::AllSources()); |
| 521 chrome::SavePage(browser()); | 628 chrome::SavePage(browser()); |
| 522 observer.Wait(); | 629 observer.Wait(); |
| 523 CheckDownloadHistory(url, full_file_name, -1); | 630 CheckDownloadHistory(url, full_file_name, -1, DownloadItem::COMPLETE); |
| 524 | 631 |
| 525 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 632 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
| 526 int64 actual_file_size = -1; | 633 int64 actual_file_size = -1; |
| 527 EXPECT_TRUE(file_util::GetFileSize(full_file_name, &actual_file_size)); | 634 EXPECT_TRUE(file_util::GetFileSize(full_file_name, &actual_file_size)); |
| 528 EXPECT_LE(kFileSizeMin, actual_file_size); | 635 EXPECT_LE(kFileSizeMin, actual_file_size); |
| 529 } | 636 } |
| OLD | NEW |