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

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: @r166491 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 class DownloadPersistedObserver : public DownloadHistory::Observer {
57 public:
58 typedef base::Callback<bool(
59 DownloadItem* item,
60 const DownloadPersistentStoreInfo&)> PersistedFilter;
61
62 DownloadPersistedObserver(Profile* profile, const PersistedFilter& filter)
63 : profile_(profile),
64 filter_(filter) {
65 DownloadServiceFactory::GetForProfile(profile_)->
66 GetDownloadHistory()->AddObserver(this);
67 }
68
69 virtual ~DownloadPersistedObserver() {
70 DownloadService* service = DownloadServiceFactory::GetForProfile(profile_);
71 if (service && service->GetDownloadManagerDelegate())
72 service->GetDownloadHistory()->RemoveObserver(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(item, 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
101 bool DownloadStoredProperly(
102 const GURL& expected_url,
103 const FilePath& expected_path,
104 int64 num_files,
105 DownloadItem::DownloadState expected_state,
106 DownloadItem* expected_item,
107 DownloadItem* item,
108 const DownloadPersistentStoreInfo& info) {
109 if (item != expected_item)
110 return false;
111 if (info.path != expected_path) {
112 LOG(ERROR) << __FUNCTION__ << " " << info.path.value()
113 << " != " << expected_path.value();
114 return false;
115 }
116 if (info.url != expected_url) {
117 LOG(ERROR) << __FUNCTION__ << " " << info.url.spec()
118 << " != " << expected_url.spec();
119 return false;
120 }
121 if ((num_files >= 0) && (info.received_bytes != num_files)) {
122 LOG(ERROR) << __FUNCTION__ << " " << num_files
123 << " != " << info.received_bytes;
124 return false;
125 }
126 if (info.state != expected_state) {
127 LOG(ERROR) << __FUNCTION__ << " " << info.state
128 << " != " << expected_state;
129 return false;
130 }
131 return true;
132 }
133
57 const FilePath::CharType kTestDir[] = FILE_PATH_LITERAL("save_page"); 134 const FilePath::CharType kTestDir[] = FILE_PATH_LITERAL("save_page");
58 135
59 const char kAppendedExtension[] = 136 static const char kAppendedExtension[] =
60 #if defined(OS_WIN) 137 #if defined(OS_WIN)
61 ".htm"; 138 ".htm";
62 #else 139 #else
63 ".html"; 140 ".html";
64 #endif 141 #endif
65 142
66 // Loosely based on logic in DownloadTestObserver. 143 // Loosely based on logic in DownloadTestObserver.
67 class DownloadItemCreatedObserver : public DownloadManager::Observer { 144 class DownloadItemCreatedObserver : public DownloadManager::Observer {
68 public: 145 public:
69 explicit DownloadItemCreatedObserver(DownloadManager* manager) 146 explicit DownloadItemCreatedObserver(DownloadManager* manager)
(...skipping 22 matching lines...) Expand all
92 waiting_ = true; 169 waiting_ = true;
93 content::RunMessageLoop(); 170 content::RunMessageLoop();
94 waiting_ = false; 171 waiting_ = false;
95 } 172 }
96 173
97 *items_seen = items_seen_; 174 *items_seen = items_seen_;
98 return; 175 return;
99 } 176 }
100 177
101 private: 178 private:
102
103 // DownloadManager::Observer 179 // DownloadManager::Observer
104 virtual void OnDownloadCreated( 180 virtual void OnDownloadCreated(
105 DownloadManager* manager, DownloadItem* item) OVERRIDE { 181 DownloadManager* manager, DownloadItem* item) OVERRIDE {
106 DCHECK_EQ(manager, manager_); 182 DCHECK_EQ(manager, manager_);
107 items_seen_.push_back(item); 183 items_seen_.push_back(item);
108 184
109 if (waiting_) 185 if (waiting_)
110 MessageLoopForUI::current()->Quit(); 186 MessageLoopForUI::current()->Quit();
111 } 187 }
112 188
113 virtual void ManagerGoingDown(DownloadManager* manager) OVERRIDE { 189 virtual void ManagerGoingDown(DownloadManager* manager) OVERRIDE {
114 manager_->RemoveObserver(this); 190 manager_->RemoveObserver(this);
115 manager_ = NULL; 191 manager_ = NULL;
116 if (waiting_) 192 if (waiting_)
117 MessageLoopForUI::current()->Quit(); 193 MessageLoopForUI::current()->Quit();
118 } 194 }
119 195
120 bool waiting_; 196 bool waiting_;
121 DownloadManager* manager_; 197 DownloadManager* manager_;
122 std::vector<DownloadItem*> items_seen_; 198 std::vector<DownloadItem*> items_seen_;
123 199
124 DISALLOW_COPY_AND_ASSIGN(DownloadItemCreatedObserver); 200 DISALLOW_COPY_AND_ASSIGN(DownloadItemCreatedObserver);
125 }; 201 };
126 202
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 { 203 class SavePageBrowserTest : public InProcessBrowserTest {
182 public: 204 public:
183 SavePageBrowserTest() {} 205 SavePageBrowserTest() {}
184 virtual ~SavePageBrowserTest(); 206 virtual ~SavePageBrowserTest();
185 207
186 protected: 208 protected:
187 void SetUp() OVERRIDE { 209 void SetUp() OVERRIDE {
188 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_)); 210 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_));
189 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); 211 ASSERT_TRUE(save_dir_.CreateUniqueTempDir());
190 InProcessBrowserTest::SetUp(); 212 InProcessBrowserTest::SetUp();
(...skipping 21 matching lines...) Expand all
212 *full_file_name = save_dir_.path().AppendASCII(prefix + ".htm"); 234 *full_file_name = save_dir_.path().AppendASCII(prefix + ".htm");
213 *dir = save_dir_.path().AppendASCII(prefix + "_files"); 235 *dir = save_dir_.path().AppendASCII(prefix + "_files");
214 } 236 }
215 237
216 WebContents* GetCurrentTab(Browser* browser) const { 238 WebContents* GetCurrentTab(Browser* browser) const {
217 WebContents* current_tab = chrome::GetActiveWebContents(browser); 239 WebContents* current_tab = chrome::GetActiveWebContents(browser);
218 EXPECT_TRUE(current_tab); 240 EXPECT_TRUE(current_tab);
219 return current_tab; 241 return current_tab;
220 } 242 }
221 243
222 bool WaitForSavePackageToFinish(Browser* browser, GURL* url_at_finish) const { 244 typedef base::Callback<bool(
245 DownloadItem*,
246 DownloadItem*,
Randy Smith (Not in Mondays) 2012/11/07 21:10:29 I'm confused enough about this interface to think
benjhayden 2012/11/08 18:57:04 I was going to do something clever, but it isn't n
247 const DownloadPersistentStoreInfo&)> DownloadPersistedFilter;
Randy Smith (Not in Mondays) 2012/11/07 21:10:29 nit: Style guide violation for ordering within a c
benjhayden 2012/11/08 18:57:04 Moot.
248
249 // Returns true if and when there was a single download created, and its url
250 // is |expected_url|.
251 bool WaitForSavePackageToFinish(
252 Browser* browser,
253 const GURL& expected_url,
254 const DownloadPersistedFilter& filter) const {
223 content::WindowedNotificationObserver observer( 255 content::WindowedNotificationObserver observer(
224 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, 256 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED,
225 content::NotificationService::AllSources()); 257 content::NotificationService::AllSources());
226 observer.Wait(); 258 observer.Wait();
227 259
228 // Generally, there should only be one download item created 260 // Generally, there should only be one download item created
229 // in all of these tests. If it's already here, grab it; if not, 261 // in all of these tests. If it's already here, grab it; if not,
230 // wait for it to show up. 262 // wait for it to show up.
231 std::vector<DownloadItem*> items; 263 std::vector<DownloadItem*> items;
232 DownloadManager* manager( 264 DownloadManager* manager(
233 BrowserContext::GetDownloadManager(browser->profile())); 265 BrowserContext::GetDownloadManager(browser->profile()));
234 manager->GetAllDownloads(&items); 266 manager->GetAllDownloads(&items);
235 if (items.size() == 0u) { 267 if (items.size() == 0u) {
236 DownloadItemCreatedObserver(manager).WaitForDownloadItem(&items); 268 DownloadItemCreatedObserver(manager).WaitForDownloadItem(&items);
237 } 269 }
238 270
239 EXPECT_EQ(1u, items.size()); 271 EXPECT_EQ(1u, items.size());
240 if (1u != items.size()) 272 if (1u != items.size())
241 return false; 273 return false;
242 DownloadItem* download_item(items[0]); 274 DownloadItem* download_item(items[0]);
243 275
244 // Note on synchronization: 276 if (!filter.is_null() && !DownloadHistory::IsPersisted(download_item)) {
245 // 277 DownloadPersistedObserver(browser->profile(), base::Bind(
246 // For each Save Page As operation, we create a corresponding shell 278 filter, download_item)).WaitForPersisted();
247 // DownloadItem to display progress to the user. That DownloadItem 279 }
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 280
257 *url_at_finish = content::Details<DownloadItem>(observer.details()).ptr()-> 281 return ((expected_url == download_item->GetOriginalUrl()) &&
258 GetOriginalUrl(); 282 (expected_url == content::Details<DownloadItem>(
259 return true; 283 observer.details()).ptr()->GetOriginalUrl()));
260 } 284 }
261 285
286 // Note on synchronization:
287 //
288 // For each Save Page As operation, we create a corresponding shell
289 // DownloadItem to display progress to the user. That DownloadItem goes
290 // through its own state transitions, including being persisted out to the
291 // history database, and the download shelf is not shown until after the
292 // persistence occurs. Save Package completion (and marking the DownloadItem
293 // as completed) occurs asynchronously from persistence. Thus if we want to
294 // examine either UI state or DB state, we need to wait until both the save
295 // package operation is complete and the relevant download item has been
296 // persisted.
297
262 DownloadManager* GetDownloadManager() const { 298 DownloadManager* GetDownloadManager() const {
263 DownloadManager* download_manager = 299 DownloadManager* download_manager =
264 BrowserContext::GetDownloadManager(browser()->profile()); 300 BrowserContext::GetDownloadManager(browser()->profile());
265 EXPECT_TRUE(download_manager); 301 EXPECT_TRUE(download_manager);
266 return download_manager; 302 return download_manager;
267 } 303 }
268 304
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. 305 // Path to directory containing test data.
356 FilePath test_dir_; 306 FilePath test_dir_;
357 307
358 // Temporary directory we will save pages to. 308 // Temporary directory we will save pages to.
359 ScopedTempDir save_dir_; 309 ScopedTempDir save_dir_;
360 310
361 private: 311 private:
362 DISALLOW_COPY_AND_ASSIGN(SavePageBrowserTest); 312 DISALLOW_COPY_AND_ASSIGN(SavePageBrowserTest);
363 }; 313 };
364 314
365 SavePageBrowserTest::~SavePageBrowserTest() { 315 SavePageBrowserTest::~SavePageBrowserTest() {
366 } 316 }
367 317
368 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { 318 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) {
369 GURL url = NavigateToMockURL("a"); 319 GURL url = NavigateToMockURL("a");
370 320
371 FilePath full_file_name, dir; 321 FilePath full_file_name, dir;
372 GetDestinationPaths("a", &full_file_name, &dir); 322 GetDestinationPaths("a", &full_file_name, &dir);
373 ASSERT_TRUE(GetCurrentTab(browser())->SavePage(full_file_name, dir, 323 ASSERT_TRUE(GetCurrentTab(browser())->SavePage(full_file_name, dir,
374 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); 324 content::SAVE_PAGE_TYPE_AS_ONLY_HTML));
375 325 ASSERT_TRUE(WaitForSavePackageToFinish(browser(), url, base::Bind(
376 GURL output_url; 326 &DownloadStoredProperly, url, full_file_name, 1,
377 ASSERT_TRUE(WaitForSavePackageToFinish(browser(), &output_url)); 327 DownloadItem::COMPLETE)));
378 EXPECT_EQ(url, output_url);
379
380 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); 328 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)); 329 EXPECT_TRUE(file_util::PathExists(full_file_name));
385 EXPECT_FALSE(file_util::PathExists(dir)); 330 EXPECT_FALSE(file_util::PathExists(dir));
386 EXPECT_TRUE(file_util::ContentsEqual( 331 EXPECT_TRUE(file_util::ContentsEqual(
387 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), 332 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")),
388 full_file_name)); 333 full_file_name));
389 } 334 }
390 335
391 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnlyCancel) { 336 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnlyCancel) {
392 GURL url = NavigateToMockURL("a"); 337 GURL url = NavigateToMockURL("a");
393 DownloadManager* manager(GetDownloadManager()); 338 DownloadManager* manager(GetDownloadManager());
394 std::vector<DownloadItem*> downloads; 339 std::vector<DownloadItem*> downloads;
395 manager->GetAllDownloads(&downloads); 340 manager->GetAllDownloads(&downloads);
396 ASSERT_EQ(0u, downloads.size()); 341 ASSERT_EQ(0u, downloads.size());
397 342
398 FilePath full_file_name, dir; 343 FilePath full_file_name, dir;
399 GetDestinationPaths("a", &full_file_name, &dir); 344 GetDestinationPaths("a", &full_file_name, &dir);
400 DownloadItemCreatedObserver creation_observer(manager); 345 DownloadItemCreatedObserver creation_observer(manager);
401 ASSERT_TRUE(GetCurrentTab(browser())->SavePage(full_file_name, dir, 346 ASSERT_TRUE(GetCurrentTab(browser())->SavePage(full_file_name, dir,
402 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); 347 content::SAVE_PAGE_TYPE_AS_ONLY_HTML));
403 std::vector<DownloadItem*> items; 348 std::vector<DownloadItem*> items;
404 creation_observer.WaitForDownloadItem(&items); 349 creation_observer.WaitForDownloadItem(&items);
405 ASSERT_TRUE(items.size() == 1); 350 ASSERT_TRUE(items.size() == 1);
406 items[0]->Cancel(true); 351 items[0]->Cancel(true);
407 352
408 // TODO(rdsmith): Fix DII::Cancel() to actually cancel the save package. 353 // TODO(rdsmith): Fix DII::Cancel() to actually cancel the save package.
409 // Currently it's ignored. 354 // Currently it's ignored.
410 GURL output_url; 355 ASSERT_TRUE(WaitForSavePackageToFinish(browser(), url, base::Bind(
411 ASSERT_TRUE(WaitForSavePackageToFinish(browser(), &output_url)); 356 &DownloadStoredProperly, url, full_file_name, -1,
412 EXPECT_EQ(url, output_url); 357 DownloadItem::CANCELLED)));
413
414 // -1 to disable number of files check; we don't update after cancel, and 358 // -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 359 // we don't know when the single file completed in relationship to
416 // the cancel. 360 // the cancel.
417 CheckDownloadHistory(url, full_file_name, -1, DownloadItem::CANCELLED);
418 361
419 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); 362 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
420 363
421 EXPECT_TRUE(file_util::PathExists(full_file_name)); 364 EXPECT_TRUE(file_util::PathExists(full_file_name));
422 EXPECT_FALSE(file_util::PathExists(dir)); 365 EXPECT_FALSE(file_util::PathExists(dir));
423 EXPECT_TRUE(file_util::ContentsEqual( 366 EXPECT_TRUE(file_util::ContentsEqual(
424 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), 367 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")),
425 full_file_name)); 368 full_file_name));
426 } 369 }
427 370
(...skipping 26 matching lines...) Expand all
454 GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl( 397 GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl(
455 FilePath(kTestDir).Append(file_name)); 398 FilePath(kTestDir).Append(file_name));
456 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl( 399 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl(
457 FilePath(kTestDir).Append(file_name)); 400 FilePath(kTestDir).Append(file_name));
458 ui_test_utils::NavigateToURL(browser(), view_source_url); 401 ui_test_utils::NavigateToURL(browser(), view_source_url);
459 402
460 FilePath full_file_name, dir; 403 FilePath full_file_name, dir;
461 GetDestinationPaths("a", &full_file_name, &dir); 404 GetDestinationPaths("a", &full_file_name, &dir);
462 ASSERT_TRUE(GetCurrentTab(browser())->SavePage(full_file_name, dir, 405 ASSERT_TRUE(GetCurrentTab(browser())->SavePage(full_file_name, dir,
463 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); 406 content::SAVE_PAGE_TYPE_AS_ONLY_HTML));
464 407 ASSERT_TRUE(WaitForSavePackageToFinish(browser(), actual_page_url, base::Bind(
465 GURL output_url; 408 &DownloadStoredProperly, actual_page_url, full_file_name, 1,
466 ASSERT_TRUE(WaitForSavePackageToFinish(browser(), &output_url)); 409 DownloadItem::COMPLETE)));
467 EXPECT_EQ(actual_page_url, output_url);
468 410
469 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); 411 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
470 // a.htm is 1 file.
471 CheckDownloadHistory(actual_page_url, full_file_name, 1,
472 DownloadItem::COMPLETE);
473 412
474 EXPECT_TRUE(file_util::PathExists(full_file_name)); 413 EXPECT_TRUE(file_util::PathExists(full_file_name));
475 EXPECT_FALSE(file_util::PathExists(dir)); 414 EXPECT_FALSE(file_util::PathExists(dir));
476 EXPECT_TRUE(file_util::ContentsEqual( 415 EXPECT_TRUE(file_util::ContentsEqual(
477 test_dir_.Append(FilePath(kTestDir)).Append(file_name), 416 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
478 full_file_name)); 417 full_file_name));
479 } 418 }
480 419
481 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) { 420 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) {
482 GURL url = NavigateToMockURL("b"); 421 GURL url = NavigateToMockURL("b");
483 422
484 FilePath full_file_name, dir; 423 FilePath full_file_name, dir;
485 GetDestinationPaths("b", &full_file_name, &dir); 424 GetDestinationPaths("b", &full_file_name, &dir);
486 ASSERT_TRUE(GetCurrentTab(browser())->SavePage( 425 ASSERT_TRUE(GetCurrentTab(browser())->SavePage(
487 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML)); 426 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML));
488 427 ASSERT_TRUE(WaitForSavePackageToFinish(browser(), url, base::Bind(
489 GURL output_url; 428 &DownloadStoredProperly, url, full_file_name, 3,
490 ASSERT_TRUE(WaitForSavePackageToFinish(browser(), &output_url)); 429 DownloadItem::COMPLETE)));
491 EXPECT_EQ(url, output_url);
492 430
493 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); 431 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
494 // b.htm is 3 files.
495 CheckDownloadHistory(url, full_file_name, 3, DownloadItem::COMPLETE);
496 432
497 EXPECT_TRUE(file_util::PathExists(full_file_name)); 433 EXPECT_TRUE(file_util::PathExists(full_file_name));
498 EXPECT_TRUE(file_util::PathExists(dir)); 434 EXPECT_TRUE(file_util::PathExists(dir));
499 EXPECT_TRUE(file_util::TextContentsEqual( 435 EXPECT_TRUE(file_util::TextContentsEqual(
500 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"), 436 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"),
501 full_file_name)); 437 full_file_name));
502 EXPECT_TRUE(file_util::ContentsEqual( 438 EXPECT_TRUE(file_util::ContentsEqual(
503 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), 439 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"),
504 dir.AppendASCII("1.png"))); 440 dir.AppendASCII("1.png")));
505 EXPECT_TRUE(file_util::ContentsEqual( 441 EXPECT_TRUE(file_util::ContentsEqual(
(...skipping 18 matching lines...) Expand all
524 FilePath(kTestDir).AppendASCII("b.htm")); 460 FilePath(kTestDir).AppendASCII("b.htm"));
525 NavigateToURLWithDisposition(incognito, url, NEW_FOREGROUND_TAB, 461 NavigateToURLWithDisposition(incognito, url, NEW_FOREGROUND_TAB,
526 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); 462 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
527 463
528 // Save the page before completion. 464 // Save the page before completion.
529 FilePath full_file_name, dir; 465 FilePath full_file_name, dir;
530 GetDestinationPaths("b", &full_file_name, &dir); 466 GetDestinationPaths("b", &full_file_name, &dir);
531 ASSERT_TRUE(GetCurrentTab(incognito)->SavePage( 467 ASSERT_TRUE(GetCurrentTab(incognito)->SavePage(
532 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML)); 468 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML));
533 469
534 GURL output_url; 470 ASSERT_TRUE(WaitForSavePackageToFinish(
535 WaitForSavePackageToFinish(incognito, &output_url); 471 incognito, url, DownloadPersistedFilter()));
536 EXPECT_EQ(url, output_url);
537 472
538 // Confirm download shelf is visible. 473 // Confirm download shelf is visible.
539 EXPECT_TRUE(incognito->window()->IsDownloadShelfVisible()); 474 EXPECT_TRUE(incognito->window()->IsDownloadShelfVisible());
540 475
541 // We can't check more than this because SavePackage is racing with 476 // 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 477 // the page load. If the page load won the race, then SavePackage
543 // might have completed. If the page load lost the race, then 478 // might have completed. If the page load lost the race, then
544 // SavePackage will cancel because there aren't any resources to 479 // SavePackage will cancel because there aren't any resources to
545 // save. 480 // save.
546 } 481 }
547 482
548 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) { 483 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) {
549 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL)); 484 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL));
550 EXPECT_FALSE(chrome::CanSavePage(browser())); 485 EXPECT_FALSE(chrome::CanSavePage(browser()));
551 } 486 }
552 487
553 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) { 488 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) {
554 GURL url = NavigateToMockURL("b"); 489 GURL url = NavigateToMockURL("b");
555 490
556 FilePath full_file_name = save_dir_.path().AppendASCII( 491 FilePath full_file_name = save_dir_.path().AppendASCII(
557 std::string("Test page for saving page feature") + kAppendedExtension); 492 std::string("Test page for saving page feature") + kAppendedExtension);
558 FilePath dir = save_dir_.path().AppendASCII( 493 FilePath dir = save_dir_.path().AppendASCII(
559 "Test page for saving page feature_files"); 494 "Test page for saving page feature_files");
560 ASSERT_TRUE(GetCurrentTab(browser())->SavePage( 495 ASSERT_TRUE(GetCurrentTab(browser())->SavePage(
561 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML)); 496 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML));
562 497
563 GURL output_url; 498 ASSERT_TRUE(WaitForSavePackageToFinish(browser(), url, base::Bind(
564 ASSERT_TRUE(WaitForSavePackageToFinish(browser(), &output_url)); 499 &DownloadStoredProperly, url, full_file_name, 3,
565 EXPECT_EQ(url, output_url); 500 DownloadItem::COMPLETE)));
566 501
567 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); 502 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
568 // b.htm is 3 files.
569 CheckDownloadHistory(url, full_file_name, 3, DownloadItem::COMPLETE);
570 503
571 EXPECT_TRUE(file_util::PathExists(full_file_name)); 504 EXPECT_TRUE(file_util::PathExists(full_file_name));
572 EXPECT_TRUE(file_util::PathExists(dir)); 505 EXPECT_TRUE(file_util::PathExists(dir));
573 EXPECT_TRUE(file_util::TextContentsEqual( 506 EXPECT_TRUE(file_util::TextContentsEqual(
574 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"), 507 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"),
575 full_file_name)); 508 full_file_name));
576 EXPECT_TRUE(file_util::ContentsEqual( 509 EXPECT_TRUE(file_util::ContentsEqual(
577 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), 510 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"),
578 dir.AppendASCII("1.png"))); 511 dir.AppendASCII("1.png")));
579 EXPECT_TRUE(file_util::ContentsEqual( 512 EXPECT_TRUE(file_util::ContentsEqual(
580 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), 513 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"),
581 dir.AppendASCII("1.css"))); 514 dir.AppendASCII("1.css")));
582 } 515 }
583 516
517 class DownloadRemovedObserver : public DownloadPersistedObserver {
Randy Smith (Not in Mondays) 2012/11/07 21:10:29 Why here? I don't think it's a style guide issue,
benjhayden 2012/11/08 18:57:04 Done.
518 public:
519 DownloadRemovedObserver(Profile* profile, int32 download_id)
520 : DownloadPersistedObserver(profile, PersistedFilter()),
521 removed_(false),
522 waiting_(false),
523 download_id_(download_id) {
524 }
525 virtual ~DownloadRemovedObserver() {}
526
527 bool WaitForRemoved() {
528 if (removed_)
529 return true;
530 waiting_ = true;
531 content::RunMessageLoop();
532 waiting_ = false;
533 return removed_;
534 }
535
536 virtual void OnDownloadStored(DownloadItem* item,
537 const DownloadPersistentStoreInfo& info) {
538 }
539
540 virtual void OnDownloadsRemoved(const DownloadHistory::IdSet& ids) {
541 removed_ = ids.find(download_id_) != ids.end();
542 if (removed_ && waiting_)
543 MessageLoopForUI::current()->Quit();
544 }
545
546 private:
547 bool removed_;
548 bool waiting_;
549 int32 download_id_;
550
551 DISALLOW_COPY_AND_ASSIGN(DownloadRemovedObserver);
552 };
553
584 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, RemoveFromList) { 554 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, RemoveFromList) {
585 GURL url = NavigateToMockURL("a"); 555 GURL url = NavigateToMockURL("a");
586 556
587 FilePath full_file_name, dir; 557 FilePath full_file_name, dir;
588 GetDestinationPaths("a", &full_file_name, &dir); 558 GetDestinationPaths("a", &full_file_name, &dir);
589 ASSERT_TRUE(GetCurrentTab(browser())->SavePage(full_file_name, dir, 559 ASSERT_TRUE(GetCurrentTab(browser())->SavePage(full_file_name, dir,
590 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); 560 content::SAVE_PAGE_TYPE_AS_ONLY_HTML));
591 561
592 GURL output_url; 562 ASSERT_TRUE(WaitForSavePackageToFinish(browser(), url, base::Bind(
593 ASSERT_TRUE(WaitForSavePackageToFinish(browser(), &output_url)); 563 &DownloadStoredProperly, url, full_file_name, 1,
594 EXPECT_EQ(url, output_url); 564 DownloadItem::COMPLETE)));
595 565
596 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); 566 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
597 // a.htm is 1 file.
598 CheckDownloadHistory(url, full_file_name, 1, DownloadItem::COMPLETE);
599 567
600 EXPECT_EQ(GetDownloadManager()->RemoveAllDownloads(), 1); 568 DownloadManager* manager(GetDownloadManager());
569 std::vector<DownloadItem*> downloads;
570 manager->GetAllDownloads(&downloads);
571 ASSERT_EQ(1UL, downloads.size());
572 DownloadRemovedObserver removed(browser()->profile(), downloads[0]->GetId());
601 573
602 // Should not be in history. 574 EXPECT_EQ(manager->RemoveAllDownloads(), 1);
603 QueryDownloadHistory(); 575
604 EXPECT_EQ(std::find_if(history_entries_.begin(), history_entries_.end(), 576 removed.WaitForRemoved();
605 DownloadPersistentStoreInfoMatch(
606 url, full_file_name, 1, DownloadItem::COMPLETE)),
607 history_entries_.end());
608 577
609 EXPECT_TRUE(file_util::PathExists(full_file_name)); 578 EXPECT_TRUE(file_util::PathExists(full_file_name));
610 EXPECT_FALSE(file_util::PathExists(dir)); 579 EXPECT_FALSE(file_util::PathExists(dir));
611 EXPECT_TRUE(file_util::ContentsEqual( 580 EXPECT_TRUE(file_util::ContentsEqual(
612 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), 581 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")),
613 full_file_name)); 582 full_file_name));
614 } 583 }
615 584
616 // This tests that a webpage with the title "test.exe" is saved as 585 // This tests that a webpage with the title "test.exe" is saved as
617 // "test.exe.htm". 586 // "test.exe.htm".
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
666 FilePath download_dir = DownloadPrefs::FromDownloadManager( 635 FilePath download_dir = DownloadPrefs::FromDownloadManager(
667 GetDownloadManager())->DownloadPath(); 636 GetDownloadManager())->DownloadPath();
668 FilePath full_file_name = download_dir.AppendASCII(std::string( 637 FilePath full_file_name = download_dir.AppendASCII(std::string(
669 "Test page for saving page feature.mhtml")); 638 "Test page for saving page feature.mhtml"));
670 #if defined(OS_CHROMEOS) 639 #if defined(OS_CHROMEOS)
671 SavePackageFilePickerChromeOS::SetShouldPromptUser(false); 640 SavePackageFilePickerChromeOS::SetShouldPromptUser(false);
672 #else 641 #else
673 SavePackageFilePicker::SetShouldPromptUser(false); 642 SavePackageFilePicker::SetShouldPromptUser(false);
674 #endif 643 #endif
675 chrome::SavePage(browser()); 644 chrome::SavePage(browser());
676 GURL output_url; 645 ASSERT_TRUE(WaitForSavePackageToFinish(browser(), url, base::Bind(
677 ASSERT_TRUE(WaitForSavePackageToFinish(browser(), &output_url)); 646 &DownloadStoredProperly, url, full_file_name, -1,
678 EXPECT_EQ(url, output_url); 647 DownloadItem::COMPLETE)));
679 CheckDownloadHistory(url, full_file_name, -1, DownloadItem::COMPLETE);
680 648
681 EXPECT_TRUE(file_util::PathExists(full_file_name)); 649 EXPECT_TRUE(file_util::PathExists(full_file_name));
682 int64 actual_file_size = -1; 650 int64 actual_file_size = -1;
683 EXPECT_TRUE(file_util::GetFileSize(full_file_name, &actual_file_size)); 651 EXPECT_TRUE(file_util::GetFileSize(full_file_name, &actual_file_size));
684 EXPECT_LE(kFileSizeMin, actual_file_size); 652 EXPECT_LE(kFileSizeMin, actual_file_size);
685 } 653 }
686 654
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698