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

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

Issue 10915180: Make DownloadHistory observe manager, items (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 <sstream>
6
5 #include "base/bind.h" 7 #include "base/bind.h"
6 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
7 #include "base/command_line.h" 9 #include "base/command_line.h"
8 #include "base/file_path.h" 10 #include "base/file_path.h"
9 #include "base/file_util.h" 11 #include "base/file_util.h"
10 #include "base/path_service.h" 12 #include "base/path_service.h"
11 #include "base/scoped_temp_dir.h" 13 #include "base/scoped_temp_dir.h"
12 #include "base/test/test_file_util.h" 14 #include "base/test/test_file_util.h"
13 #include "chrome/app/chrome_command_ids.h" 15 #include "chrome/app/chrome_command_ids.h"
14 #include "chrome/browser/api/prefs/pref_member.h" 16 #include "chrome/browser/api/prefs/pref_member.h"
15 #include "chrome/browser/download/chrome_download_manager_delegate.h" 17 #include "chrome/browser/download/chrome_download_manager_delegate.h"
16 #include "chrome/browser/download/download_history.h" 18 #include "chrome/browser/download/download_history.h"
17 #include "chrome/browser/download/download_prefs.h" 19 #include "chrome/browser/download/download_prefs.h"
18 #include "chrome/browser/download/download_service.h" 20 #include "chrome/browser/download/download_service.h"
19 #include "chrome/browser/download/download_service_factory.h" 21 #include "chrome/browser/download/download_service_factory.h"
22 #include "chrome/browser/history/download_persistent_store_info.h"
23 #include "chrome/browser/history/history_service_factory.h"
20 #include "chrome/browser/net/url_request_mock_util.h" 24 #include "chrome/browser/net/url_request_mock_util.h"
21 #include "chrome/browser/prefs/pref_service.h" 25 #include "chrome/browser/prefs/pref_service.h"
22 #include "chrome/browser/profiles/profile.h" 26 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/ui/browser.h" 27 #include "chrome/browser/ui/browser.h"
24 #include "chrome/browser/ui/browser_commands.h" 28 #include "chrome/browser/ui/browser_commands.h"
25 #include "chrome/browser/ui/browser_tabstrip.h" 29 #include "chrome/browser/ui/browser_tabstrip.h"
26 #include "chrome/browser/ui/browser_window.h" 30 #include "chrome/browser/ui/browser_window.h"
27 #include "chrome/common/chrome_paths.h" 31 #include "chrome/common/chrome_paths.h"
28 #include "chrome/common/chrome_switches.h" 32 #include "chrome/common/chrome_switches.h"
29 #include "chrome/common/pref_names.h" 33 #include "chrome/common/pref_names.h"
30 #include "chrome/common/url_constants.h" 34 #include "chrome/common/url_constants.h"
31 #include "chrome/test/base/in_process_browser_test.h" 35 #include "chrome/test/base/in_process_browser_test.h"
32 #include "chrome/test/base/ui_test_utils.h" 36 #include "chrome/test/base/ui_test_utils.h"
33 #include "content/public/browser/download_item.h" 37 #include "content/public/browser/download_item.h"
34 #include "content/public/browser/download_manager.h" 38 #include "content/public/browser/download_manager.h"
35 #include "content/public/browser/download_persistent_store_info.h"
36 #include "content/public/browser/notification_service.h" 39 #include "content/public/browser/notification_service.h"
37 #include "content/public/browser/notification_types.h" 40 #include "content/public/browser/notification_types.h"
38 #include "content/public/browser/web_contents.h" 41 #include "content/public/browser/web_contents.h"
39 #include "content/public/test/test_utils.h" 42 #include "content/public/test/test_utils.h"
40 #include "content/test/net/url_request_mock_http_job.h" 43 #include "content/test/net/url_request_mock_http_job.h"
41 #include "testing/gtest/include/gtest/gtest.h" 44 #include "testing/gtest/include/gtest/gtest.h"
42 45
43 #if defined(OS_CHROMEOS) 46 #if defined(OS_CHROMEOS)
44 #include "chrome/browser/download/save_package_file_picker_chromeos.h" 47 #include "chrome/browser/download/save_package_file_picker_chromeos.h"
45 #else 48 #else
46 #include "chrome/browser/download/save_package_file_picker.h" 49 #include "chrome/browser/download/save_package_file_picker.h"
47 #endif 50 #endif
48 51
49 using content::BrowserContext; 52 using content::BrowserContext;
50 using content::BrowserThread; 53 using content::BrowserThread;
51 using content::DownloadItem; 54 using content::DownloadItem;
52 using content::DownloadManager; 55 using content::DownloadManager;
53 using content::DownloadPersistentStoreInfo;
54 using content::WebContents; 56 using content::WebContents;
55 57
56 namespace { 58 namespace {
57 59
60 class DownloadPersistedObserver : public DownloadHistory::Observer {
61 public:
62 typedef base::Callback<bool(const DownloadPersistentStoreInfo&)>
63 PersistedFilter;
64
65 DownloadPersistedObserver(Profile* profile, const PersistedFilter& filter)
66 : profile_(profile),
67 filter_(filter) {
68 DownloadServiceFactory::GetForProfile(profile)->
69 GetDownloadManagerDelegate()->AddHistoryObserver(this);
70 }
71
72 virtual ~DownloadPersistedObserver() {
73 DownloadService* service = DownloadServiceFactory::GetForProfile(profile_);
74 if (service && service->GetDownloadManagerDelegate())
75 service->GetDownloadManagerDelegate()->RemoveHistoryObserver(this);
76 }
77
78 bool WaitForPersisted() {
79 if (persisted_)
80 return true;
81 waiting_ = true;
82 content::RunMessageLoop();
83 waiting_ = false;
84 return persisted_;
85 }
86
87 virtual void OnDownloadStored(const DownloadPersistentStoreInfo& info) {
88 persisted_ = filter_.Run(info);
89 if (persisted_ && waiting_)
90 MessageLoopForUI::current()->Quit();
91 }
92
93 private:
94 Profile* profile_;
95 DownloadItem* item_;
96 PersistedFilter filter_;
97 bool waiting_;
98 bool persisted_;
99
100 DISALLOW_COPY_AND_ASSIGN(DownloadPersistedObserver);
101 };
102
58 static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("save_page"); 103 static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("save_page");
59 104
60 static const char* kAppendedExtension = 105 static const char* kAppendedExtension =
61 #if defined(OS_WIN) 106 #if defined(OS_WIN)
62 ".htm"; 107 ".htm";
63 #else 108 #else
64 ".html"; 109 ".html";
65 #endif 110 #endif
66 111
67 void NullFunction() { 112 void NullFunction() {
(...skipping 30 matching lines...) Expand all
98 waiting_ = true; 143 waiting_ = true;
99 content::RunMessageLoop(); 144 content::RunMessageLoop();
100 waiting_ = false; 145 waiting_ = false;
101 } 146 }
102 147
103 *items_seen = items_seen_; 148 *items_seen = items_seen_;
104 return; 149 return;
105 } 150 }
106 151
107 private: 152 private:
108
109 // DownloadManager::Observer 153 // DownloadManager::Observer
110 virtual void OnDownloadCreated( 154 virtual void OnDownloadCreated(
111 DownloadManager* manager, DownloadItem* item) OVERRIDE { 155 DownloadManager* manager, DownloadItem* item) OVERRIDE {
112 DCHECK_EQ(manager, manager_); 156 DCHECK_EQ(manager, manager_);
113 items_seen_.push_back(item); 157 items_seen_.push_back(item);
114 158
115 if (waiting_) 159 if (waiting_)
116 MessageLoopForUI::current()->Quit(); 160 MessageLoopForUI::current()->Quit();
117 } 161 }
118 162
119 virtual void ManagerGoingDown(DownloadManager* manager) OVERRIDE { 163 virtual void ManagerGoingDown(DownloadManager* manager) OVERRIDE {
120 manager_->RemoveObserver(this); 164 manager_->RemoveObserver(this);
121 manager_ = NULL; 165 manager_ = NULL;
122 if (waiting_) 166 if (waiting_)
123 MessageLoopForUI::current()->Quit(); 167 MessageLoopForUI::current()->Quit();
124 } 168 }
125 169
126 bool waiting_; 170 bool waiting_;
127 DownloadManager* manager_; 171 DownloadManager* manager_;
128 std::vector<DownloadItem*> items_seen_; 172 std::vector<DownloadItem*> items_seen_;
129 173
130 DISALLOW_COPY_AND_ASSIGN(DownloadItemCreatedObserver); 174 DISALLOW_COPY_AND_ASSIGN(DownloadItemCreatedObserver);
131 }; 175 };
132 176
133 class DownloadPersistedObserver : public DownloadItem::Observer { 177 bool DownloadStoredProperly(
134 public: 178 const GURL& expected_url,
135 explicit DownloadPersistedObserver(DownloadItem* item) 179 const FilePath& expected_path,
136 : waiting_(false), item_(item) { 180 int64 num_files,
137 item->AddObserver(this); 181 DownloadItem::DownloadState expected_state,
182 std::stringstream* errors,
183 const DownloadPersistentStoreInfo& info) {
184 if (info.path != expected_path) {
185 *errors << "Expected path " << expected_path.value()
186 << " but received " << info.path.value() << std::endl;
187 return false;
138 } 188 }
139 189 if (info.url != expected_url) {
140 ~DownloadPersistedObserver() { 190 *errors << "Expected url " << expected_url.spec()
141 if (item_) 191 << " but received " << info.url.spec() << std::endl;
142 item_->RemoveObserver(this); 192 return false;
143 } 193 }
144 194 if (num_files >= 0 &&
145 // Wait for download item to get the persisted bit set. 195 info.received_bytes != num_files) {
146 // Note that this class provides no protection against the download 196 *errors << "Expected received_bytes " << num_files
147 // being destroyed between creation and return of WaitForPersisted(); 197 << " but received " << info.received_bytes << std::endl;
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 } 198 }
163 199 if (info.state != expected_state) {
164 private: 200 *errors << "Expected state " << expected_state
165 // DownloadItem::Observer 201 << " but received " << info.state << std::endl;
166 virtual void OnDownloadUpdated(DownloadItem* item) OVERRIDE { 202 return false;
167 DCHECK_EQ(item, item_);
168
169 if (waiting_ && item->IsPersisted())
170 MessageLoopForUI::current()->Quit();
171 } 203 }
172 204 return true;
173 virtual void OnDownloadDestroyed(DownloadItem* item) OVERRIDE { 205 }
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 206
187 class SavePageBrowserTest : public InProcessBrowserTest { 207 class SavePageBrowserTest : public InProcessBrowserTest {
188 public: 208 public:
189 SavePageBrowserTest() {} 209 SavePageBrowserTest() {}
190 virtual ~SavePageBrowserTest(); 210 virtual ~SavePageBrowserTest();
191 211
192 protected: 212 protected:
193 void SetUp() OVERRIDE { 213 void SetUp() OVERRIDE {
194 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_)); 214 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_));
195 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); 215 ASSERT_TRUE(save_dir_.CreateUniqueTempDir());
(...skipping 24 matching lines...) Expand all
220 *full_file_name = save_dir_.path().AppendASCII(prefix + ".htm"); 240 *full_file_name = save_dir_.path().AppendASCII(prefix + ".htm");
221 *dir = save_dir_.path().AppendASCII(prefix + "_files"); 241 *dir = save_dir_.path().AppendASCII(prefix + "_files");
222 } 242 }
223 243
224 WebContents* GetCurrentTab() const { 244 WebContents* GetCurrentTab() const {
225 WebContents* current_tab = chrome::GetActiveWebContents(browser()); 245 WebContents* current_tab = chrome::GetActiveWebContents(browser());
226 EXPECT_TRUE(current_tab); 246 EXPECT_TRUE(current_tab);
227 return current_tab; 247 return current_tab;
228 } 248 }
229 249
230 bool WaitForSavePackageToFinish(GURL* url_at_finish) const { 250 // Returns true if and when there was a single download created, and its url
251 // is |expected_url|.
252 bool WaitForSavePackageToFinish(const GURL& expected_url) const {
231 content::WindowedNotificationObserver observer( 253 content::WindowedNotificationObserver observer(
232 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, 254 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED,
233 content::NotificationService::AllSources()); 255 content::NotificationService::AllSources());
234 observer.Wait(); 256 observer.Wait();
235 257
236 // Generally, there should only be one download item created 258 // Generally, there should only be one download item created
237 // in all of these tests. Wait for it, and wait for it to 259 // in all of these tests. Wait for it, and wait for it to
238 // be persisted. 260 // be persisted.
239 std::vector<DownloadItem*> items; 261 std::vector<DownloadItem*> items;
240 item_creation_observer_->WaitForDownloadItem(&items); 262 item_creation_observer_->WaitForDownloadItem(&items);
241 263
242 EXPECT_EQ(1u, items.size()); 264 EXPECT_EQ(1u, items.size());
243 if (1u != items.size()) 265 if (1u != items.size())
244 return false; 266 return false;
245 DownloadItem* download_item(items[0]); 267 DownloadItem* download_item(items[0]);
246 268
269 return ((expected_url == download_item->GetOriginalUrl()) &&
270 (expected_url == content::Details<DownloadItem>(
271 observer.details()).ptr()->GetOriginalUrl()));
272 }
273
274 void WaitForPersisted(
275 const GURL& expected_url,
276 const FilePath& expected_path,
277 int64 num_files,
278 DownloadItem::DownloadState state) {
279 errors_.reset(new std::stringstream);
247 // Note on synchronization: 280 // Note on synchronization:
248 // 281 //
249 // For each Save Page As operation, we create a corresponding shell 282 // For each Save Page As operation, we create a corresponding shell
250 // DownloadItem to display progress to the user. That DownloadItem 283 // DownloadItem to display progress to the user. That DownloadItem
251 // goes through its own state transitions, including being persisted 284 // goes through its own state transitions, including being persisted
252 // out to the history database, and the download shelf is not shown 285 // out to the history database, and the download shelf is not shown
253 // until after the persistence occurs. Save Package completion (and 286 // until after the persistence occurs. Save Package completion (and
254 // marking the DownloadItem as completed) occurs asynchronously from 287 // marking the DownloadItem as completed) occurs asynchronously from
255 // persistence. Thus if we want to examine either UI state or DB 288 // 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 289 // state, we need to wait until both the save package operation is
257 // complete and the relevant download item has been persisted. 290 // complete and the relevant download item has been persisted.
258 DownloadPersistedObserver(download_item).WaitForPersisted(); 291 DownloadPersistedObserver(browser()->profile(), base::Bind(
259 292 &DownloadStoredProperly,
260 *url_at_finish = content::Details<DownloadItem>(observer.details()).ptr()-> 293 expected_url,
261 GetOriginalUrl(); 294 expected_path,
262 return true; 295 num_files,
296 state,
297 errors_.get()))
298 .WaitForPersisted();
299 errors_.reset();
263 } 300 }
264 301
265 DownloadManager* GetDownloadManager() const { 302 DownloadManager* GetDownloadManager() const {
266 DownloadManager* download_manager = 303 DownloadManager* download_manager =
267 BrowserContext::GetDownloadManager(browser()->profile()); 304 BrowserContext::GetDownloadManager(browser()->profile());
268 EXPECT_TRUE(download_manager); 305 EXPECT_TRUE(download_manager);
269 return download_manager; 306 return download_manager;
270 } 307 }
271 308
272 void QueryDownloadHistory() {
273 // Query the history system.
274 ChromeDownloadManagerDelegate* delegate =
275 static_cast<ChromeDownloadManagerDelegate*>(
276 GetDownloadManager()->GetDelegate());
277 delegate->download_history()->Load(
278 base::Bind(&SavePageBrowserTest::OnQueryDownloadEntriesComplete,
279 base::Unretained(this)));
280
281 // Run message loop until a quit message is sent from
282 // OnQueryDownloadEntriesComplete().
283 content::RunMessageLoop();
284 }
285
286 void OnQueryDownloadEntriesComplete(
287 std::vector<DownloadPersistentStoreInfo>* entries) {
288 history_entries_ = *entries;
289
290 // Indicate thet we have received the history and can continue.
291 MessageLoopForUI::current()->Quit();
292 }
293
294 struct DownloadPersistentStoreInfoMatch
295 : public std::unary_function<DownloadPersistentStoreInfo, bool> {
296
297 DownloadPersistentStoreInfoMatch(const GURL& url,
298 const FilePath& path,
299 int64 num_files,
300 DownloadItem::DownloadState state)
301 : url_(url),
302 path_(path),
303 num_files_(num_files),
304 state_(state) {}
305
306 bool operator() (const DownloadPersistentStoreInfo& info) const {
307 return info.url == url_ &&
308 info.path == path_ &&
309 // For non-MHTML save packages, received_bytes is actually the
310 // number of files.
311 ((num_files_ < 0) ||
312 (info.received_bytes == num_files_)) &&
313 info.total_bytes == 0 &&
314 info.state == state_;
315 }
316
317 GURL url_;
318 FilePath path_;
319 int64 num_files_;
320 DownloadItem::DownloadState state_;
321 };
322
323 void CheckDownloadHistory(const GURL& url,
324 const FilePath& path,
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()->GetAllDownloads(&downloads);
330 ASSERT_EQ(1u, downloads.size());
331
332 QueryDownloadHistory();
333
334 std::vector<DownloadPersistentStoreInfo>::iterator found =
335 std::find_if(history_entries_.begin(), history_entries_.end(),
336 DownloadPersistentStoreInfoMatch(url, path, num_files,
337 state));
338
339 if (found == history_entries_.end()) {
340 LOG(ERROR) << "Missing url=" << url.spec()
341 << " path=" << path.value()
342 << " received=" << num_files;
343 for (size_t index = 0; index < history_entries_.size(); ++index) {
344 LOG(ERROR) << "History@" << index << ": url="
345 << history_entries_[index].url.spec()
346 << " path=" << history_entries_[index].path.value()
347 << " received=" << history_entries_[index].received_bytes
348 << " total=" << history_entries_[index].total_bytes
349 << " state=" << history_entries_[index].state;
350 }
351 EXPECT_TRUE(false);
352 }
353 }
354
355 std::vector<DownloadPersistentStoreInfo> history_entries_;
356
357 // Path to directory containing test data. 309 // Path to directory containing test data.
358 FilePath test_dir_; 310 FilePath test_dir_;
359 311
360 // Temporary directory we will save pages to. 312 // Temporary directory we will save pages to.
361 ScopedTempDir save_dir_; 313 ScopedTempDir save_dir_;
362 314
363 private: 315 private:
364 scoped_ptr<DownloadItemCreatedObserver> item_creation_observer_; 316 scoped_ptr<DownloadItemCreatedObserver> item_creation_observer_;
317 scoped_ptr<std::stringstream> errors_;
365 318
366 DISALLOW_COPY_AND_ASSIGN(SavePageBrowserTest); 319 DISALLOW_COPY_AND_ASSIGN(SavePageBrowserTest);
367 }; 320 };
368 321
369 SavePageBrowserTest::~SavePageBrowserTest() { 322 SavePageBrowserTest::~SavePageBrowserTest() {
323 if (errors_.get()) {
324 LOG(ERROR) << errors_->str();
325 }
370 } 326 }
371 327
372 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { 328 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) {
373 GURL url = NavigateToMockURL("a"); 329 GURL url = NavigateToMockURL("a");
374 330
375 FilePath full_file_name, dir; 331 FilePath full_file_name, dir;
376 GetDestinationPaths("a", &full_file_name, &dir); 332 GetDestinationPaths("a", &full_file_name, &dir);
377 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, 333 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir,
378 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); 334 content::SAVE_PAGE_TYPE_AS_ONLY_HTML));
379 335
380 GURL output_url; 336 ASSERT_TRUE(WaitForSavePackageToFinish(url));
381 ASSERT_TRUE(WaitForSavePackageToFinish(&output_url)); 337 WaitForPersisted(url, full_file_name, 1, DownloadItem::COMPLETE);
382 EXPECT_EQ(url, output_url);
383 338
384 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); 339 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
385 // a.htm is 1 file.
386 CheckDownloadHistory(url, full_file_name, 1, DownloadItem::COMPLETE);
387 340
388 EXPECT_TRUE(file_util::PathExists(full_file_name)); 341 EXPECT_TRUE(file_util::PathExists(full_file_name));
389 EXPECT_FALSE(file_util::PathExists(dir)); 342 EXPECT_FALSE(file_util::PathExists(dir));
390 EXPECT_TRUE(file_util::ContentsEqual( 343 EXPECT_TRUE(file_util::ContentsEqual(
391 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), 344 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")),
392 full_file_name)); 345 full_file_name));
393 } 346 }
394 347
395 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnlyCancel) { 348 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnlyCancel) {
396 GURL url = NavigateToMockURL("a"); 349 GURL url = NavigateToMockURL("a");
397 DownloadManager* manager(GetDownloadManager()); 350 DownloadManager* manager(GetDownloadManager());
398 std::vector<DownloadItem*> downloads; 351 std::vector<DownloadItem*> downloads;
399 manager->GetAllDownloads(&downloads); 352 manager->GetAllDownloads(&downloads);
400 ASSERT_EQ(0u, downloads.size()); 353 ASSERT_EQ(0u, downloads.size());
401 354
402 FilePath full_file_name, dir; 355 FilePath full_file_name, dir;
403 GetDestinationPaths("a", &full_file_name, &dir); 356 GetDestinationPaths("a", &full_file_name, &dir);
404 DownloadItemCreatedObserver creation_observer(manager); 357 DownloadItemCreatedObserver creation_observer(manager);
405 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, 358 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir,
406 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); 359 content::SAVE_PAGE_TYPE_AS_ONLY_HTML));
407 std::vector<DownloadItem*> items; 360 std::vector<DownloadItem*> items;
408 creation_observer.WaitForDownloadItem(&items); 361 creation_observer.WaitForDownloadItem(&items);
409 ASSERT_TRUE(items.size() == 1); 362 ASSERT_TRUE(items.size() == 1);
410 items[0]->Cancel(true); 363 items[0]->Cancel(true);
411 364
412 // TODO(rdsmith): Fix DII::Cancel() to actually cancel the save package. 365 // TODO(rdsmith): Fix DII::Cancel() to actually cancel the save package.
413 // Currently it's ignored. 366 // Currently it's ignored.
414 GURL output_url; 367 ASSERT_TRUE(WaitForSavePackageToFinish(url));
415 ASSERT_TRUE(WaitForSavePackageToFinish(&output_url));
416 EXPECT_EQ(url, output_url);
417 368
418 // -1 to disable number of files check; we don't update after cancel, and 369 // -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 370 // we don't know when the single file completed in relationship to
420 // the cancel. 371 // the cancel.
421 CheckDownloadHistory(url, full_file_name, -1, DownloadItem::CANCELLED); 372 WaitForPersisted(url, full_file_name, -1, DownloadItem::CANCELLED);
422 373
423 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); 374 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
424 375
425 EXPECT_TRUE(file_util::PathExists(full_file_name)); 376 EXPECT_TRUE(file_util::PathExists(full_file_name));
426 EXPECT_FALSE(file_util::PathExists(dir)); 377 EXPECT_FALSE(file_util::PathExists(dir));
427 EXPECT_TRUE(file_util::ContentsEqual( 378 EXPECT_TRUE(file_util::ContentsEqual(
428 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), 379 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")),
429 full_file_name)); 380 full_file_name));
430 } 381 }
431 382
(...skipping 29 matching lines...) Expand all
461 FilePath(kTestDir).Append(file_name)); 412 FilePath(kTestDir).Append(file_name));
462 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl( 413 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl(
463 FilePath(kTestDir).Append(file_name)); 414 FilePath(kTestDir).Append(file_name));
464 ui_test_utils::NavigateToURL(browser(), view_source_url); 415 ui_test_utils::NavigateToURL(browser(), view_source_url);
465 416
466 FilePath full_file_name, dir; 417 FilePath full_file_name, dir;
467 GetDestinationPaths("a", &full_file_name, &dir); 418 GetDestinationPaths("a", &full_file_name, &dir);
468 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, 419 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir,
469 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); 420 content::SAVE_PAGE_TYPE_AS_ONLY_HTML));
470 421
471 GURL output_url; 422 ASSERT_TRUE(WaitForSavePackageToFinish(actual_page_url));
472 ASSERT_TRUE(WaitForSavePackageToFinish(&output_url));
473 EXPECT_EQ(actual_page_url, output_url);
474 423
475 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); 424 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
476 // a.htm is 1 file. 425 WaitForPersisted(actual_page_url, full_file_name, 1, DownloadItem::COMPLETE);
477 CheckDownloadHistory(actual_page_url, full_file_name, 1,
478 DownloadItem::COMPLETE);
479 426
480 EXPECT_TRUE(file_util::PathExists(full_file_name)); 427 EXPECT_TRUE(file_util::PathExists(full_file_name));
481 EXPECT_FALSE(file_util::PathExists(dir)); 428 EXPECT_FALSE(file_util::PathExists(dir));
482 EXPECT_TRUE(file_util::ContentsEqual( 429 EXPECT_TRUE(file_util::ContentsEqual(
483 test_dir_.Append(FilePath(kTestDir)).Append(file_name), 430 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
484 full_file_name)); 431 full_file_name));
485 } 432 }
486 433
487 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) { 434 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) {
488 GURL url = NavigateToMockURL("b"); 435 GURL url = NavigateToMockURL("b");
489 436
490 FilePath full_file_name, dir; 437 FilePath full_file_name, dir;
491 GetDestinationPaths("b", &full_file_name, &dir); 438 GetDestinationPaths("b", &full_file_name, &dir);
492 ASSERT_TRUE(GetCurrentTab()->SavePage( 439 ASSERT_TRUE(GetCurrentTab()->SavePage(
493 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML)); 440 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML));
494 441
495 GURL output_url; 442 ASSERT_TRUE(WaitForSavePackageToFinish(url));
496 ASSERT_TRUE(WaitForSavePackageToFinish(&output_url));
497 EXPECT_EQ(url, output_url);
498 443
499 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); 444 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
500 // b.htm is 3 files. 445 WaitForPersisted(url, full_file_name, 3, DownloadItem::COMPLETE);
501 CheckDownloadHistory(url, full_file_name, 3, DownloadItem::COMPLETE);
502 446
503 EXPECT_TRUE(file_util::PathExists(full_file_name)); 447 EXPECT_TRUE(file_util::PathExists(full_file_name));
504 EXPECT_TRUE(file_util::PathExists(dir)); 448 EXPECT_TRUE(file_util::PathExists(dir));
505 EXPECT_TRUE(file_util::TextContentsEqual( 449 EXPECT_TRUE(file_util::TextContentsEqual(
506 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"), 450 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"),
507 full_file_name)); 451 full_file_name));
508 EXPECT_TRUE(file_util::ContentsEqual( 452 EXPECT_TRUE(file_util::ContentsEqual(
509 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), 453 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"),
510 dir.AppendASCII("1.png"))); 454 dir.AppendASCII("1.png")));
511 EXPECT_TRUE(file_util::ContentsEqual( 455 EXPECT_TRUE(file_util::ContentsEqual(
512 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), 456 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"),
513 dir.AppendASCII("1.css"))); 457 dir.AppendASCII("1.css")));
514 } 458 }
515 459
516 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) { 460 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) {
517 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL)); 461 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL));
518 EXPECT_FALSE(chrome::CanSavePage(browser())); 462 EXPECT_FALSE(chrome::CanSavePage(browser()));
519 } 463 }
520 464
521 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) { 465 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) {
522 GURL url = NavigateToMockURL("b"); 466 GURL url = NavigateToMockURL("b");
523 467
524 FilePath full_file_name = save_dir_.path().AppendASCII( 468 FilePath full_file_name = save_dir_.path().AppendASCII(
525 std::string("Test page for saving page feature") + kAppendedExtension); 469 std::string("Test page for saving page feature") + kAppendedExtension);
526 FilePath dir = save_dir_.path().AppendASCII( 470 FilePath dir = save_dir_.path().AppendASCII(
527 "Test page for saving page feature_files"); 471 "Test page for saving page feature_files");
528 ASSERT_TRUE(GetCurrentTab()->SavePage( 472 ASSERT_TRUE(GetCurrentTab()->SavePage(
529 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML)); 473 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML));
530 474
531 GURL output_url; 475 ASSERT_TRUE(WaitForSavePackageToFinish(url));
532 ASSERT_TRUE(WaitForSavePackageToFinish(&output_url));
533 EXPECT_EQ(url, output_url);
534 476
535 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); 477 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
536 // b.htm is 3 files. 478 WaitForPersisted(url, full_file_name, 3, DownloadItem::COMPLETE);
537 CheckDownloadHistory(url, full_file_name, 3, DownloadItem::COMPLETE);
538 479
539 EXPECT_TRUE(file_util::PathExists(full_file_name)); 480 EXPECT_TRUE(file_util::PathExists(full_file_name));
540 EXPECT_TRUE(file_util::PathExists(dir)); 481 EXPECT_TRUE(file_util::PathExists(dir));
541 EXPECT_TRUE(file_util::TextContentsEqual( 482 EXPECT_TRUE(file_util::TextContentsEqual(
542 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"), 483 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"),
543 full_file_name)); 484 full_file_name));
544 EXPECT_TRUE(file_util::ContentsEqual( 485 EXPECT_TRUE(file_util::ContentsEqual(
545 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), 486 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"),
546 dir.AppendASCII("1.png"))); 487 dir.AppendASCII("1.png")));
547 EXPECT_TRUE(file_util::ContentsEqual( 488 EXPECT_TRUE(file_util::ContentsEqual(
548 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), 489 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"),
549 dir.AppendASCII("1.css"))); 490 dir.AppendASCII("1.css")));
550 } 491 }
551 492
552 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, RemoveFromList) { 493 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, RemoveFromList) {
553 GURL url = NavigateToMockURL("a"); 494 GURL url = NavigateToMockURL("a");
554 495
555 FilePath full_file_name, dir; 496 FilePath full_file_name, dir;
556 GetDestinationPaths("a", &full_file_name, &dir); 497 GetDestinationPaths("a", &full_file_name, &dir);
557 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, 498 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir,
558 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); 499 content::SAVE_PAGE_TYPE_AS_ONLY_HTML));
559 500
560 GURL output_url; 501 ASSERT_TRUE(WaitForSavePackageToFinish(url));
561 ASSERT_TRUE(WaitForSavePackageToFinish(&output_url));
562 EXPECT_EQ(url, output_url);
563 502
564 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); 503 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
565 // a.htm is 1 file.
566 CheckDownloadHistory(url, full_file_name, 1, DownloadItem::COMPLETE);
567 504
568 EXPECT_EQ(GetDownloadManager()->RemoveAllDownloads(), 1); 505 EXPECT_EQ(GetDownloadManager()->RemoveAllDownloads(), 1);
569 506
570 // Should not be in history. 507 // TODO(benjhayden) Should not be in history.
571 QueryDownloadHistory();
572 EXPECT_EQ(std::find_if(history_entries_.begin(), history_entries_.end(),
573 DownloadPersistentStoreInfoMatch(
574 url, full_file_name, 1, DownloadItem::COMPLETE)),
575 history_entries_.end());
576 508
577 EXPECT_TRUE(file_util::PathExists(full_file_name)); 509 EXPECT_TRUE(file_util::PathExists(full_file_name));
578 EXPECT_FALSE(file_util::PathExists(dir)); 510 EXPECT_FALSE(file_util::PathExists(dir));
579 EXPECT_TRUE(file_util::ContentsEqual( 511 EXPECT_TRUE(file_util::ContentsEqual(
580 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), 512 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")),
581 full_file_name)); 513 full_file_name));
582 } 514 }
583 515
584 // This tests that a webpage with the title "test.exe" is saved as 516 // This tests that a webpage with the title "test.exe" is saved as
585 // "test.exe.htm". 517 // "test.exe.htm".
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
639 #if defined(OS_CHROMEOS) 571 #if defined(OS_CHROMEOS)
640 SavePackageFilePickerChromeOS::SetShouldPromptUser(false); 572 SavePackageFilePickerChromeOS::SetShouldPromptUser(false);
641 #else 573 #else
642 SavePackageFilePicker::SetShouldPromptUser(false); 574 SavePackageFilePicker::SetShouldPromptUser(false);
643 #endif 575 #endif
644 content::WindowedNotificationObserver observer( 576 content::WindowedNotificationObserver observer(
645 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, 577 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED,
646 content::NotificationService::AllSources()); 578 content::NotificationService::AllSources());
647 chrome::SavePage(browser()); 579 chrome::SavePage(browser());
648 observer.Wait(); 580 observer.Wait();
649 CheckDownloadHistory(url, full_file_name, -1, DownloadItem::COMPLETE); 581 WaitForPersisted(url, full_file_name, -1, DownloadItem::COMPLETE);
650 582
651 EXPECT_TRUE(file_util::PathExists(full_file_name)); 583 EXPECT_TRUE(file_util::PathExists(full_file_name));
652 int64 actual_file_size = -1; 584 int64 actual_file_size = -1;
653 EXPECT_TRUE(file_util::GetFileSize(full_file_name, &actual_file_size)); 585 EXPECT_TRUE(file_util::GetFileSize(full_file_name, &actual_file_size));
654 EXPECT_LE(kFileSizeMin, actual_file_size); 586 EXPECT_LE(kFileSizeMin, actual_file_size);
655 } 587 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698