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

Side by Side Diff: chrome/browser/download/download_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 <sstream> 5 #include <sstream>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/file_path.h" 9 #include "base/file_path.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
11 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
12 #include "base/path_service.h" 12 #include "base/path_service.h"
13 #include "base/scoped_temp_dir.h" 13 #include "base/scoped_temp_dir.h"
14 #include "base/stl_util.h" 14 #include "base/stl_util.h"
15 #include "base/stringprintf.h" 15 #include "base/stringprintf.h"
16 #include "base/test/test_file_util.h" 16 #include "base/test/test_file_util.h"
17 #include "base/utf_string_conversions.h" 17 #include "base/utf_string_conversions.h"
18 #include "chrome/app/chrome_command_ids.h" 18 #include "chrome/app/chrome_command_ids.h"
19 #include "chrome/browser/browser_process.h" 19 #include "chrome/browser/browser_process.h"
20 #include "chrome/browser/common/cancelable_request.h" 20 #include "chrome/browser/common/cancelable_request.h"
21 #include "chrome/browser/download/chrome_download_manager_delegate.h" 21 #include "chrome/browser/download/chrome_download_manager_delegate.h"
22 #include "chrome/browser/download/download_crx_util.h" 22 #include "chrome/browser/download/download_crx_util.h"
23 #include "chrome/browser/download/download_history.h" 23 #include "chrome/browser/download/download_history.h"
24 #include "chrome/browser/download/download_prefs.h" 24 #include "chrome/browser/download/download_prefs.h"
25 #include "chrome/browser/download/download_request_limiter.h" 25 #include "chrome/browser/download/download_request_limiter.h"
26 #include "chrome/browser/download/download_service.h"
27 #include "chrome/browser/download/download_service_factory.h"
26 #include "chrome/browser/download/download_shelf.h" 28 #include "chrome/browser/download/download_shelf.h"
27 #include "chrome/browser/download/download_test_file_chooser_observer.h" 29 #include "chrome/browser/download/download_test_file_chooser_observer.h"
28 #include "chrome/browser/download/download_util.h" 30 #include "chrome/browser/download/download_util.h"
29 #include "chrome/browser/extensions/extension_install_prompt.h" 31 #include "chrome/browser/extensions/extension_install_prompt.h"
30 #include "chrome/browser/extensions/extension_service.h" 32 #include "chrome/browser/extensions/extension_service.h"
33 #include "chrome/browser/history/download_persistent_store_info.h"
31 #include "chrome/browser/history/history.h" 34 #include "chrome/browser/history/history.h"
32 #include "chrome/browser/history/history_service_factory.h" 35 #include "chrome/browser/history/history_service_factory.h"
33 #include "chrome/browser/net/url_request_mock_util.h" 36 #include "chrome/browser/net/url_request_mock_util.h"
34 #include "chrome/browser/prefs/pref_service.h" 37 #include "chrome/browser/prefs/pref_service.h"
35 #include "chrome/browser/profiles/profile.h" 38 #include "chrome/browser/profiles/profile.h"
36 #include "chrome/browser/tab_contents/render_view_context_menu.h" 39 #include "chrome/browser/tab_contents/render_view_context_menu.h"
37 #include "chrome/browser/ui/browser.h" 40 #include "chrome/browser/ui/browser.h"
38 #include "chrome/browser/ui/browser_commands.h" 41 #include "chrome/browser/ui/browser_commands.h"
39 #include "chrome/browser/ui/browser_list.h" 42 #include "chrome/browser/ui/browser_list.h"
40 #include "chrome/browser/ui/browser_tabstrip.h" 43 #include "chrome/browser/ui/browser_tabstrip.h"
41 #include "chrome/browser/ui/browser_window.h" 44 #include "chrome/browser/ui/browser_window.h"
42 #include "chrome/browser/ui/chrome_pages.h" 45 #include "chrome/browser/ui/chrome_pages.h"
43 #include "chrome/browser/ui/host_desktop.h" 46 #include "chrome/browser/ui/host_desktop.h"
44 #include "chrome/common/chrome_notification_types.h" 47 #include "chrome/common/chrome_notification_types.h"
45 #include "chrome/common/chrome_paths.h" 48 #include "chrome/common/chrome_paths.h"
46 #include "chrome/common/extensions/feature_switch.h" 49 #include "chrome/common/extensions/feature_switch.h"
47 #include "chrome/common/pref_names.h" 50 #include "chrome/common/pref_names.h"
48 #include "chrome/common/url_constants.h" 51 #include "chrome/common/url_constants.h"
49 #include "chrome/test/base/in_process_browser_test.h" 52 #include "chrome/test/base/in_process_browser_test.h"
50 #include "chrome/test/base/ui_test_utils.h" 53 #include "chrome/test/base/ui_test_utils.h"
51 #include "content/public/browser/download_item.h" 54 #include "content/public/browser/download_item.h"
52 #include "content/public/browser/download_manager.h" 55 #include "content/public/browser/download_manager.h"
53 #include "content/public/browser/download_persistent_store_info.h"
54 #include "content/public/browser/download_save_info.h" 56 #include "content/public/browser/download_save_info.h"
55 #include "content/public/browser/download_url_parameters.h" 57 #include "content/public/browser/download_url_parameters.h"
56 #include "content/public/browser/notification_source.h" 58 #include "content/public/browser/notification_source.h"
57 #include "content/public/browser/render_view_host.h" 59 #include "content/public/browser/render_view_host.h"
58 #include "content/public/browser/resource_context.h" 60 #include "content/public/browser/resource_context.h"
59 #include "content/public/browser/web_contents.h" 61 #include "content/public/browser/web_contents.h"
60 #include "content/public/common/context_menu_params.h" 62 #include "content/public/common/context_menu_params.h"
61 #include "content/public/common/page_transition_types.h" 63 #include "content/public/common/page_transition_types.h"
62 #include "content/public/test/browser_test_utils.h" 64 #include "content/public/test/browser_test_utils.h"
63 #include "content/public/test/download_test_observer.h" 65 #include "content/public/test/download_test_observer.h"
64 #include "content/public/test/test_file_error_injector.h" 66 #include "content/public/test/test_file_error_injector.h"
65 #include "content/public/test/test_navigation_observer.h" 67 #include "content/public/test/test_navigation_observer.h"
66 #include "content/test/net/url_request_mock_http_job.h" 68 #include "content/test/net/url_request_mock_http_job.h"
67 #include "content/test/net/url_request_slow_download_job.h" 69 #include "content/test/net/url_request_slow_download_job.h"
68 #include "net/base/net_util.h" 70 #include "net/base/net_util.h"
69 #include "net/test/test_server.h" 71 #include "net/test/test_server.h"
70 #include "testing/gtest/include/gtest/gtest.h" 72 #include "testing/gtest/include/gtest/gtest.h"
71 73
72 using content::BrowserContext; 74 using content::BrowserContext;
73 using content::BrowserThread; 75 using content::BrowserThread;
74 using content::DownloadItem; 76 using content::DownloadItem;
75 using content::DownloadManager; 77 using content::DownloadManager;
76 using content::DownloadPersistentStoreInfo;
77 using content::DownloadUrlParameters; 78 using content::DownloadUrlParameters;
78 using content::URLRequestMockHTTPJob; 79 using content::URLRequestMockHTTPJob;
79 using content::URLRequestSlowDownloadJob; 80 using content::URLRequestSlowDownloadJob;
80 using content::WebContents; 81 using content::WebContents;
81 using extensions::Extension; 82 using extensions::Extension;
82 using extensions::FeatureSwitch; 83 using extensions::FeatureSwitch;
83 84
84 namespace { 85 namespace {
85 86
86 // IDs and paths of CRX files used in tests. 87 // IDs and paths of CRX files used in tests.
87 const char kGoodCrxId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf"; 88 const char kGoodCrxId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
88 const FilePath kGoodCrxPath(FILE_PATH_LITERAL("extensions/good.crx")); 89 const FilePath kGoodCrxPath(FILE_PATH_LITERAL("extensions/good.crx"));
89 90
90 const char kLargeThemeCrxId[] = "pjpgmfcmabopnnfonnhmdjglfpjjfkbf"; 91 const char kLargeThemeCrxId[] = "pjpgmfcmabopnnfonnhmdjglfpjjfkbf";
91 const FilePath kLargeThemePath(FILE_PATH_LITERAL("extensions/theme2.crx")); 92 const FilePath kLargeThemePath(FILE_PATH_LITERAL("extensions/theme2.crx"));
92 93
93 // Get History Information.
94 class DownloadsHistoryDataCollector {
95 public:
96 DownloadsHistoryDataCollector(int64 download_db_handle,
97 DownloadManager* manager)
98 : result_valid_(false),
99 download_db_handle_(download_db_handle) {
100 HistoryService* hs = HistoryServiceFactory::GetForProfile(
101 Profile::FromBrowserContext(manager->GetBrowserContext()),
102 Profile::EXPLICIT_ACCESS);
103 DCHECK(hs);
104 hs->QueryDownloads(
105 &callback_consumer_,
106 base::Bind(&DownloadsHistoryDataCollector::OnQueryDownloadsComplete,
107 base::Unretained(this)));
108
109 // TODO(rdsmith): Move message loop out of constructor.
110 // Cannot complete immediately because the history backend runs on a
111 // separate thread, so we can assume that the RunMessageLoop below will
112 // be exited by the Quit in OnQueryDownloadsComplete.
113 content::RunMessageLoop();
114 }
115
116 bool GetDownloadsHistoryEntry(DownloadPersistentStoreInfo* result) {
117 DCHECK(result);
118 *result = result_;
119 return result_valid_;
120 }
121
122 private:
123 void OnQueryDownloadsComplete(
124 std::vector<DownloadPersistentStoreInfo>* entries) {
125 result_valid_ = false;
126 for (std::vector<DownloadPersistentStoreInfo>::const_iterator it =
127 entries->begin();
128 it != entries->end(); ++it) {
129 if (it->db_handle == download_db_handle_) {
130 result_ = *it;
131 result_valid_ = true;
132 }
133 }
134 MessageLoopForUI::current()->Quit();
135 }
136
137 DownloadPersistentStoreInfo result_;
138 bool result_valid_;
139 int64 download_db_handle_;
140 CancelableRequestConsumer callback_consumer_;
141
142 DISALLOW_COPY_AND_ASSIGN(DownloadsHistoryDataCollector);
143 };
144
145 // Mock that simulates a permissions dialog where the user denies 94 // Mock that simulates a permissions dialog where the user denies
146 // permission to install. TODO(skerner): This could be shared with 95 // permission to install. TODO(skerner): This could be shared with
147 // extensions tests. Find a common place for this class. 96 // extensions tests. Find a common place for this class.
148 class MockAbortExtensionInstallPrompt : public ExtensionInstallPrompt { 97 class MockAbortExtensionInstallPrompt : public ExtensionInstallPrompt {
149 public: 98 public:
150 MockAbortExtensionInstallPrompt() : 99 MockAbortExtensionInstallPrompt() :
151 ExtensionInstallPrompt(NULL) { 100 ExtensionInstallPrompt(NULL) {
152 } 101 }
153 102
154 // Simulate a user abort on an extension installation. 103 // Simulate a user abort on an extension installation.
(...skipping 1257 matching lines...) Expand 10 before | Expand all | Expand 10 after
1412 ExpectWindowCountAfterDownload(1); 1361 ExpectWindowCountAfterDownload(1);
1413 #endif 1362 #endif
1414 1363
1415 EXPECT_EQ(1, browser()->tab_count()); 1364 EXPECT_EQ(1, browser()->tab_count());
1416 // Download shelf should close. Download panel stays open on ChromeOS. 1365 // Download shelf should close. Download panel stays open on ChromeOS.
1417 EXPECT_FALSE(browser()->window()->IsDownloadShelfVisible()); 1366 EXPECT_FALSE(browser()->window()->IsDownloadShelfVisible());
1418 1367
1419 CheckDownload(browser(), file, file); 1368 CheckDownload(browser(), file, file);
1420 } 1369 }
1421 1370
1422 // Confirm a download makes it into the history properly. 1371 class HistoryObserver : public DownloadHistory::Observer {
1372 public:
1373 explicit HistoryObserver(ChromeDownloadManagerDelegate* manager_delegate)
1374 : manager_delegate_(manager_delegate),
1375 waiting_(false),
1376 seen_stored_(false) {
1377 manager_delegate_->AddHistoryObserver(this);
1378 }
1379 virtual ~HistoryObserver() {
1380 manager_delegate_->RemoveHistoryObserver(this);
1381 }
1382 virtual void OnDownloadStored(
1383 content::DownloadItem* item,
1384 const DownloadPersistentStoreInfo& info) OVERRIDE {
1385 seen_stored_ = true;
1386 if (waiting_)
1387 MessageLoopForUI::current()->Quit();
1388 }
1389
1390 void WaitForStored() {
1391 if (seen_stored_)
1392 return;
1393 waiting_ = true;
1394 content::RunMessageLoop();
1395 waiting_ = false;
1396 }
1397
1398 private:
1399 ChromeDownloadManagerDelegate* manager_delegate_;
1400 bool waiting_;
1401 bool seen_stored_;
1402 DISALLOW_COPY_AND_ASSIGN(HistoryObserver);
1403 };
1404
1423 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadHistoryCheck) { 1405 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadHistoryCheck) {
1424 FilePath file(FILE_PATH_LITERAL("download-test1.lib")); 1406 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
1425 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); 1407 GURL download_url(URLRequestMockHTTPJob::GetMockUrl(file));
1426 FilePath origin_file(OriginFile(file)); 1408 HistoryObserver observer(DownloadServiceFactory::GetForProfile(
1427 int64 origin_size; 1409 browser()->profile())->GetDownloadManagerDelegate());
1428 file_util::GetFileSize(origin_file, &origin_size); 1410 DownloadAndWait(browser(), download_url);
1429 1411 observer.WaitForStored();
1430 // Download the file and wait. We do not expect the Select File dialog.
1431 DownloadAndWait(browser(), url);
1432
1433 // Get details of what downloads have just happened.
1434 std::vector<DownloadItem*> downloads;
1435 GetDownloads(browser(), &downloads);
1436 ASSERT_EQ(1u, downloads.size());
1437 int64 db_handle = downloads[0]->GetDbHandle();
1438
1439 // Check state.
1440 EXPECT_EQ(1, browser()->tab_count());
1441 CheckDownload(browser(), file, file);
1442 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
1443
1444 // Check history results.
1445 DownloadsHistoryDataCollector history_collector(
1446 db_handle,
1447 DownloadManagerForBrowser(browser()));
1448 DownloadPersistentStoreInfo info;
1449 EXPECT_TRUE(history_collector.GetDownloadsHistoryEntry(&info)) << db_handle;
1450 EXPECT_EQ(file, info.path.BaseName());
1451 EXPECT_EQ(url, info.url);
1452 // Ignore start_time.
1453 EXPECT_EQ(origin_size, info.received_bytes);
1454 EXPECT_EQ(origin_size, info.total_bytes);
1455 EXPECT_EQ(DownloadItem::COMPLETE, info.state);
1456 } 1412 }
1457 1413
1458 // Test for crbug.com/14505. This tests that chrome:// urls are still functional 1414 // Test for crbug.com/14505. This tests that chrome:// urls are still functional
1459 // after download of a file while viewing another chrome://. 1415 // after download of a file while viewing another chrome://.
1460 IN_PROC_BROWSER_TEST_F(DownloadTest, ChromeURLAfterDownload) { 1416 IN_PROC_BROWSER_TEST_F(DownloadTest, ChromeURLAfterDownload) {
1461 FilePath file(FILE_PATH_LITERAL("download-test1.lib")); 1417 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
1462 GURL download_url(URLRequestMockHTTPJob::GetMockUrl(file)); 1418 GURL download_url(URLRequestMockHTTPJob::GetMockUrl(file));
1463 GURL flags_url(chrome::kChromeUIFlagsURL); 1419 GURL flags_url(chrome::kChromeUIFlagsURL);
1464 GURL extensions_url(chrome::kChromeUIExtensionsFrameURL); 1420 GURL extensions_url(chrome::kChromeUIExtensionsFrameURL);
1465 1421
(...skipping 817 matching lines...) Expand 10 before | Expand all | Expand 10 after
2283 scoped_ptr<DownloadUrlParameters> params( 2239 scoped_ptr<DownloadUrlParameters> params(
2284 DownloadUrlParameters::FromWebContents( 2240 DownloadUrlParameters::FromWebContents(
2285 web_contents, url, save_info.Pass())); 2241 web_contents, url, save_info.Pass()));
2286 params->set_callback(base::Bind(&SetHiddenDownloadCallback)); 2242 params->set_callback(base::Bind(&SetHiddenDownloadCallback));
2287 download_manager->DownloadUrl(params.Pass()); 2243 download_manager->DownloadUrl(params.Pass());
2288 observer->WaitForFinished(); 2244 observer->WaitForFinished();
2289 2245
2290 // Verify that download shelf is not shown. 2246 // Verify that download shelf is not shown.
2291 EXPECT_FALSE(browser()->window()->IsDownloadShelfVisible()); 2247 EXPECT_FALSE(browser()->window()->IsDownloadShelfVisible());
2292 } 2248 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698