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

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

Powered by Google App Engine
This is Rietveld 408576698