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