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

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

Powered by Google App Engine
This is Rietveld 408576698