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