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

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: @r167394 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/path_service.h" 10 #include "base/path_service.h"
11 #include "base/scoped_temp_dir.h" 11 #include "base/scoped_temp_dir.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_persistent_store_info.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 DownloadPersistentStoreInfo&)> 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 DownloadPersistentStoreInfo& 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 DownloadPersistentStoreInfo& 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 DownloadPersistentStoreInfo& 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 ScopedTempDir save_dir_; 338 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));
401 ASSERT_TRUE(GetCurrentTab(browser())->SavePage(full_file_name, dir, 380 ASSERT_TRUE(GetCurrentTab(browser())->SavePage(full_file_name, dir,
402 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); 381 content::SAVE_PAGE_TYPE_AS_ONLY_HTML));
403 std::vector<DownloadItem*> items; 382 std::vector<DownloadItem*> items;
404 creation_observer.WaitForDownloadItem(&items); 383 creation_observer.WaitForDownloadItem(&items);
405 ASSERT_TRUE(items.size() == 1); 384 ASSERT_TRUE(items.size() == 1);
406 items[0]->Cancel(true); 385 items[0]->Cancel(true);
407 386
408 // TODO(rdsmith): Fix DII::Cancel() to actually cancel the save package. 387 // TODO(rdsmith): Fix DII::Cancel() to actually cancel the save package.
409 // Currently it's ignored. 388 // Currently it's ignored.
410 GURL output_url; 389 ASSERT_TRUE(WaitForSavePackageToFinish(browser(), url));
411 ASSERT_TRUE(WaitForSavePackageToFinish(browser(), &output_url)); 390 persisted.WaitForPersisted();
412 EXPECT_EQ(url, output_url);
413
414 // -1 to disable number of files check; we don't update after cancel, and 391 // -1 to disable number of files check; we don't update after cancel, and
415 // we don't know when the single file completed in relationship to 392 // we don't know when the single file completed in relationship to
416 // the cancel. 393 // the cancel.
417 CheckDownloadHistory(url, full_file_name, -1, DownloadItem::CANCELLED);
418 394
419 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); 395 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
420 396
421 EXPECT_TRUE(file_util::PathExists(full_file_name)); 397 EXPECT_TRUE(file_util::PathExists(full_file_name));
422 EXPECT_FALSE(file_util::PathExists(dir)); 398 EXPECT_FALSE(file_util::PathExists(dir));
423 EXPECT_TRUE(file_util::ContentsEqual( 399 EXPECT_TRUE(file_util::ContentsEqual(
424 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), 400 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")),
425 full_file_name)); 401 full_file_name));
426 } 402 }
427 403
(...skipping 24 matching lines...) Expand all
452 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveViewSourceHTMLOnly) { 428 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveViewSourceHTMLOnly) {
453 FilePath file_name(FILE_PATH_LITERAL("a.htm")); 429 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
454 GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl( 430 GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl(
455 FilePath(kTestDir).Append(file_name)); 431 FilePath(kTestDir).Append(file_name));
456 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl( 432 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl(
457 FilePath(kTestDir).Append(file_name)); 433 FilePath(kTestDir).Append(file_name));
458 ui_test_utils::NavigateToURL(browser(), view_source_url); 434 ui_test_utils::NavigateToURL(browser(), view_source_url);
459 435
460 FilePath full_file_name, dir; 436 FilePath full_file_name, dir;
461 GetDestinationPaths("a", &full_file_name, &dir); 437 GetDestinationPaths("a", &full_file_name, &dir);
438 DownloadPersistedObserver persisted(browser()->profile(), base::Bind(
439 &DownloadStoredProperly, actual_page_url, full_file_name, 1,
440 DownloadItem::COMPLETE));
462 ASSERT_TRUE(GetCurrentTab(browser())->SavePage(full_file_name, dir, 441 ASSERT_TRUE(GetCurrentTab(browser())->SavePage(full_file_name, dir,
463 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); 442 content::SAVE_PAGE_TYPE_AS_ONLY_HTML));
464 443 ASSERT_TRUE(WaitForSavePackageToFinish(browser(), actual_page_url));
465 GURL output_url; 444 persisted.WaitForPersisted();
466 ASSERT_TRUE(WaitForSavePackageToFinish(browser(), &output_url));
467 EXPECT_EQ(actual_page_url, output_url);
468 445
469 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); 446 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
470 // a.htm is 1 file.
471 CheckDownloadHistory(actual_page_url, full_file_name, 1,
472 DownloadItem::COMPLETE);
473 447
474 EXPECT_TRUE(file_util::PathExists(full_file_name)); 448 EXPECT_TRUE(file_util::PathExists(full_file_name));
475 EXPECT_FALSE(file_util::PathExists(dir)); 449 EXPECT_FALSE(file_util::PathExists(dir));
476 EXPECT_TRUE(file_util::ContentsEqual( 450 EXPECT_TRUE(file_util::ContentsEqual(
477 test_dir_.Append(FilePath(kTestDir)).Append(file_name), 451 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
478 full_file_name)); 452 full_file_name));
479 } 453 }
480 454
481 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) { 455 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) {
482 GURL url = NavigateToMockURL("b"); 456 GURL url = NavigateToMockURL("b");
483 457
484 FilePath full_file_name, dir; 458 FilePath full_file_name, dir;
485 GetDestinationPaths("b", &full_file_name, &dir); 459 GetDestinationPaths("b", &full_file_name, &dir);
460 DownloadPersistedObserver persisted(browser()->profile(), base::Bind(
461 &DownloadStoredProperly, url, full_file_name, 3,
462 DownloadItem::COMPLETE));
486 ASSERT_TRUE(GetCurrentTab(browser())->SavePage( 463 ASSERT_TRUE(GetCurrentTab(browser())->SavePage(
487 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML)); 464 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML));
488 465 ASSERT_TRUE(WaitForSavePackageToFinish(browser(), url));
489 GURL output_url; 466 persisted.WaitForPersisted();
490 ASSERT_TRUE(WaitForSavePackageToFinish(browser(), &output_url));
491 EXPECT_EQ(url, output_url);
492 467
493 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); 468 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
494 // b.htm is 3 files.
495 CheckDownloadHistory(url, full_file_name, 3, DownloadItem::COMPLETE);
496 469
497 EXPECT_TRUE(file_util::PathExists(full_file_name)); 470 EXPECT_TRUE(file_util::PathExists(full_file_name));
498 EXPECT_TRUE(file_util::PathExists(dir)); 471 EXPECT_TRUE(file_util::PathExists(dir));
499 EXPECT_TRUE(file_util::TextContentsEqual( 472 EXPECT_TRUE(file_util::TextContentsEqual(
500 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"), 473 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"),
501 full_file_name)); 474 full_file_name));
502 EXPECT_TRUE(file_util::ContentsEqual( 475 EXPECT_TRUE(file_util::ContentsEqual(
503 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), 476 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"),
504 dir.AppendASCII("1.png"))); 477 dir.AppendASCII("1.png")));
505 EXPECT_TRUE(file_util::ContentsEqual( 478 EXPECT_TRUE(file_util::ContentsEqual(
(...skipping 18 matching lines...) Expand all
524 FilePath(kTestDir).AppendASCII("b.htm")); 497 FilePath(kTestDir).AppendASCII("b.htm"));
525 NavigateToURLWithDisposition(incognito, url, NEW_FOREGROUND_TAB, 498 NavigateToURLWithDisposition(incognito, url, NEW_FOREGROUND_TAB,
526 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); 499 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
527 500
528 // Save the page before completion. 501 // Save the page before completion.
529 FilePath full_file_name, dir; 502 FilePath full_file_name, dir;
530 GetDestinationPaths("b", &full_file_name, &dir); 503 GetDestinationPaths("b", &full_file_name, &dir);
531 ASSERT_TRUE(GetCurrentTab(incognito)->SavePage( 504 ASSERT_TRUE(GetCurrentTab(incognito)->SavePage(
532 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML)); 505 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML));
533 506
534 GURL output_url; 507 ASSERT_TRUE(WaitForSavePackageToFinish(incognito, url));
535 WaitForSavePackageToFinish(incognito, &output_url);
536 EXPECT_EQ(url, output_url);
537 508
538 // Confirm download shelf is visible. 509 // Confirm download shelf is visible.
539 EXPECT_TRUE(incognito->window()->IsDownloadShelfVisible()); 510 EXPECT_TRUE(incognito->window()->IsDownloadShelfVisible());
540 511
541 // We can't check more than this because SavePackage is racing with 512 // 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 513 // the page load. If the page load won the race, then SavePackage
543 // might have completed. If the page load lost the race, then 514 // might have completed. If the page load lost the race, then
544 // SavePackage will cancel because there aren't any resources to 515 // SavePackage will cancel because there aren't any resources to
545 // save. 516 // save.
546 } 517 }
547 518
548 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) { 519 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) {
549 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL)); 520 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL));
550 EXPECT_FALSE(chrome::CanSavePage(browser())); 521 EXPECT_FALSE(chrome::CanSavePage(browser()));
551 } 522 }
552 523
553 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) { 524 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) {
554 GURL url = NavigateToMockURL("b"); 525 GURL url = NavigateToMockURL("b");
555 526
556 FilePath full_file_name = save_dir_.path().AppendASCII( 527 FilePath full_file_name = save_dir_.path().AppendASCII(
557 std::string("Test page for saving page feature") + kAppendedExtension); 528 std::string("Test page for saving page feature") + kAppendedExtension);
558 FilePath dir = save_dir_.path().AppendASCII( 529 FilePath dir = save_dir_.path().AppendASCII(
559 "Test page for saving page feature_files"); 530 "Test page for saving page feature_files");
531 DownloadPersistedObserver persisted(browser()->profile(), base::Bind(
532 &DownloadStoredProperly, url, full_file_name, 3,
533 DownloadItem::COMPLETE));
560 ASSERT_TRUE(GetCurrentTab(browser())->SavePage( 534 ASSERT_TRUE(GetCurrentTab(browser())->SavePage(
561 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML)); 535 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML));
562 536
563 GURL output_url; 537 ASSERT_TRUE(WaitForSavePackageToFinish(browser(), url));
564 ASSERT_TRUE(WaitForSavePackageToFinish(browser(), &output_url)); 538 persisted.WaitForPersisted();
565 EXPECT_EQ(url, output_url);
566 539
567 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); 540 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
568 // b.htm is 3 files.
569 CheckDownloadHistory(url, full_file_name, 3, DownloadItem::COMPLETE);
570 541
571 EXPECT_TRUE(file_util::PathExists(full_file_name)); 542 EXPECT_TRUE(file_util::PathExists(full_file_name));
572 EXPECT_TRUE(file_util::PathExists(dir)); 543 EXPECT_TRUE(file_util::PathExists(dir));
573 EXPECT_TRUE(file_util::TextContentsEqual( 544 EXPECT_TRUE(file_util::TextContentsEqual(
574 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"), 545 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"),
575 full_file_name)); 546 full_file_name));
576 EXPECT_TRUE(file_util::ContentsEqual( 547 EXPECT_TRUE(file_util::ContentsEqual(
577 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), 548 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"),
578 dir.AppendASCII("1.png"))); 549 dir.AppendASCII("1.png")));
579 EXPECT_TRUE(file_util::ContentsEqual( 550 EXPECT_TRUE(file_util::ContentsEqual(
580 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), 551 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"),
581 dir.AppendASCII("1.css"))); 552 dir.AppendASCII("1.css")));
582 } 553 }
583 554
584 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, RemoveFromList) { 555 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, RemoveFromList) {
585 GURL url = NavigateToMockURL("a"); 556 GURL url = NavigateToMockURL("a");
586 557
587 FilePath full_file_name, dir; 558 FilePath full_file_name, dir;
588 GetDestinationPaths("a", &full_file_name, &dir); 559 GetDestinationPaths("a", &full_file_name, &dir);
560 DownloadPersistedObserver persisted(browser()->profile(), base::Bind(
561 &DownloadStoredProperly, url, full_file_name, 1,
562 DownloadItem::COMPLETE));
589 ASSERT_TRUE(GetCurrentTab(browser())->SavePage(full_file_name, dir, 563 ASSERT_TRUE(GetCurrentTab(browser())->SavePage(full_file_name, dir,
590 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); 564 content::SAVE_PAGE_TYPE_AS_ONLY_HTML));
591 565
592 GURL output_url; 566 ASSERT_TRUE(WaitForSavePackageToFinish(browser(), url));
593 ASSERT_TRUE(WaitForSavePackageToFinish(browser(), &output_url)); 567 persisted.WaitForPersisted();
594 EXPECT_EQ(url, output_url);
595 568
596 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); 569 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
597 // a.htm is 1 file.
598 CheckDownloadHistory(url, full_file_name, 1, DownloadItem::COMPLETE);
599 570
600 EXPECT_EQ(GetDownloadManager()->RemoveAllDownloads(), 1); 571 DownloadManager* manager(GetDownloadManager());
572 std::vector<DownloadItem*> downloads;
573 manager->GetAllDownloads(&downloads);
574 ASSERT_EQ(1UL, downloads.size());
575 DownloadRemovedObserver removed(browser()->profile(), downloads[0]->GetId());
601 576
602 // Should not be in history. 577 EXPECT_EQ(manager->RemoveAllDownloads(), 1);
603 QueryDownloadHistory(); 578
604 EXPECT_EQ(std::find_if(history_entries_.begin(), history_entries_.end(), 579 removed.WaitForRemoved();
605 DownloadPersistentStoreInfoMatch(
606 url, full_file_name, 1, DownloadItem::COMPLETE)),
607 history_entries_.end());
608 580
609 EXPECT_TRUE(file_util::PathExists(full_file_name)); 581 EXPECT_TRUE(file_util::PathExists(full_file_name));
610 EXPECT_FALSE(file_util::PathExists(dir)); 582 EXPECT_FALSE(file_util::PathExists(dir));
611 EXPECT_TRUE(file_util::ContentsEqual( 583 EXPECT_TRUE(file_util::ContentsEqual(
612 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), 584 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")),
613 full_file_name)); 585 full_file_name));
614 } 586 }
615 587
616 // This tests that a webpage with the title "test.exe" is saved as 588 // This tests that a webpage with the title "test.exe" is saved as
617 // "test.exe.htm". 589 // "test.exe.htm".
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
665 GURL url = NavigateToMockURL("b"); 637 GURL url = NavigateToMockURL("b");
666 FilePath download_dir = DownloadPrefs::FromDownloadManager( 638 FilePath download_dir = DownloadPrefs::FromDownloadManager(
667 GetDownloadManager())->DownloadPath(); 639 GetDownloadManager())->DownloadPath();
668 FilePath full_file_name = download_dir.AppendASCII(std::string( 640 FilePath full_file_name = download_dir.AppendASCII(std::string(
669 "Test page for saving page feature.mhtml")); 641 "Test page for saving page feature.mhtml"));
670 #if defined(OS_CHROMEOS) 642 #if defined(OS_CHROMEOS)
671 SavePackageFilePickerChromeOS::SetShouldPromptUser(false); 643 SavePackageFilePickerChromeOS::SetShouldPromptUser(false);
672 #else 644 #else
673 SavePackageFilePicker::SetShouldPromptUser(false); 645 SavePackageFilePicker::SetShouldPromptUser(false);
674 #endif 646 #endif
647 DownloadPersistedObserver persisted(browser()->profile(), base::Bind(
648 &DownloadStoredProperly, url, full_file_name, -1,
649 DownloadItem::COMPLETE));
675 chrome::SavePage(browser()); 650 chrome::SavePage(browser());
676 GURL output_url; 651 ASSERT_TRUE(WaitForSavePackageToFinish(browser(), url));
677 ASSERT_TRUE(WaitForSavePackageToFinish(browser(), &output_url)); 652 persisted.WaitForPersisted();
678 EXPECT_EQ(url, output_url);
679 CheckDownloadHistory(url, full_file_name, -1, DownloadItem::COMPLETE);
680 653
681 EXPECT_TRUE(file_util::PathExists(full_file_name)); 654 EXPECT_TRUE(file_util::PathExists(full_file_name));
682 int64 actual_file_size = -1; 655 int64 actual_file_size = -1;
683 EXPECT_TRUE(file_util::GetFileSize(full_file_name, &actual_file_size)); 656 EXPECT_TRUE(file_util::GetFileSize(full_file_name, &actual_file_size));
684 EXPECT_LE(kFileSizeMin, actual_file_size); 657 EXPECT_LE(kFileSizeMin, actual_file_size);
685 } 658 }
686 659
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698