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

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: @r168573 Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <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/files/scoped_temp_dir.h" 11 #include "base/files/scoped_temp_dir.h"
12 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/path_service.h" 13 #include "base/path_service.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_row.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 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 DownloadManager* manager, DownloadItem* item) OVERRIDE { 181 DownloadManager* manager, DownloadItem* item) OVERRIDE {
233 item->MockDownloadOpenForTesting(); 182 item->MockDownloadOpenForTesting();
234 } 183 }
235 184
236 private: 185 private:
237 DownloadManager* download_manager_; 186 DownloadManager* download_manager_;
238 187
239 DISALLOW_COPY_AND_ASSIGN(MockDownloadOpeningObserver); 188 DISALLOW_COPY_AND_ASSIGN(MockDownloadOpeningObserver);
240 }; 189 };
241 190
191 class HistoryObserver : public DownloadHistory::Observer {
192 public:
193 explicit HistoryObserver(Profile* profile)
194 : profile_(profile),
195 waiting_(false),
196 seen_stored_(false) {
197 DownloadServiceFactory::GetForProfile(profile_)->
198 GetDownloadHistory()->AddObserver(this);
199 }
200
201 virtual ~HistoryObserver() {
202 DownloadService* service = DownloadServiceFactory::GetForProfile(profile_);
203 if (service && service->GetDownloadHistory())
204 service->GetDownloadHistory()->RemoveObserver(this);
205 }
206
207 virtual void OnDownloadStored(
208 content::DownloadItem* item,
209 const history::DownloadRow& info) OVERRIDE {
210 seen_stored_ = true;
211 if (waiting_)
212 MessageLoopForUI::current()->Quit();
213 }
214
215 virtual void OnDownloadHistoryDestroyed() OVERRIDE {
216 DownloadServiceFactory::GetForProfile(profile_)->
217 GetDownloadHistory()->RemoveObserver(this);
218 }
219
220 void WaitForStored() {
221 if (seen_stored_)
222 return;
223 waiting_ = true;
224 content::RunMessageLoop();
225 waiting_ = false;
226 }
227
228 private:
229 Profile* profile_;
230 bool waiting_;
231 bool seen_stored_;
232
233 DISALLOW_COPY_AND_ASSIGN(HistoryObserver);
234 };
235
242 class DownloadTest : public InProcessBrowserTest { 236 class DownloadTest : public InProcessBrowserTest {
243 public: 237 public:
244 // Choice of navigation or direct fetch. Used by |DownloadFileCheckErrors()|. 238 // Choice of navigation or direct fetch. Used by |DownloadFileCheckErrors()|.
245 enum DownloadMethod { 239 enum DownloadMethod {
246 DOWNLOAD_NAVIGATE, 240 DOWNLOAD_NAVIGATE,
247 DOWNLOAD_DIRECT 241 DOWNLOAD_DIRECT
248 }; 242 };
249 243
250 // Information passed in to |DownloadFileCheckErrors()|. 244 // Information passed in to |DownloadFileCheckErrors()|.
251 struct DownloadInfo { 245 struct DownloadInfo {
(...skipping 1147 matching lines...) Expand 10 before | Expand all | Expand 10 after
1399 ExpectWindowCountAfterDownload(1); 1393 ExpectWindowCountAfterDownload(1);
1400 #endif 1394 #endif
1401 1395
1402 EXPECT_EQ(1, browser()->tab_count()); 1396 EXPECT_EQ(1, browser()->tab_count());
1403 // Download shelf should close. Download panel stays open on ChromeOS. 1397 // Download shelf should close. Download panel stays open on ChromeOS.
1404 EXPECT_FALSE(browser()->window()->IsDownloadShelfVisible()); 1398 EXPECT_FALSE(browser()->window()->IsDownloadShelfVisible());
1405 1399
1406 CheckDownload(browser(), file, file); 1400 CheckDownload(browser(), file, file);
1407 } 1401 }
1408 1402
1409 // Confirm a download makes it into the history properly.
1410 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadHistoryCheck) { 1403 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadHistoryCheck) {
1411 FilePath file(FILE_PATH_LITERAL("download-test1.lib")); 1404 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
1412 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); 1405 GURL download_url(URLRequestMockHTTPJob::GetMockUrl(file));
1413 FilePath origin_file(OriginFile(file)); 1406 HistoryObserver observer(browser()->profile());
1414 int64 origin_size; 1407 DownloadAndWait(browser(), download_url);
1415 file_util::GetFileSize(origin_file, &origin_size); 1408 observer.WaitForStored();
1416
1417 // Download the file and wait. We do not expect the Select File dialog.
1418 DownloadAndWait(browser(), url);
1419
1420 // Get details of what downloads have just happened.
1421 std::vector<DownloadItem*> downloads;
1422 GetDownloads(browser(), &downloads);
1423 ASSERT_EQ(1u, downloads.size());
1424 int64 db_handle = downloads[0]->GetDbHandle();
1425
1426 // Check state.
1427 EXPECT_EQ(1, browser()->tab_count());
1428 CheckDownload(browser(), file, file);
1429 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
1430
1431 // Check history results.
1432 DownloadsHistoryDataCollector history_collector(
1433 db_handle,
1434 DownloadManagerForBrowser(browser()));
1435 DownloadPersistentStoreInfo info;
1436 EXPECT_TRUE(history_collector.GetDownloadsHistoryEntry(&info)) << db_handle;
1437 EXPECT_EQ(file, info.path.BaseName());
1438 EXPECT_EQ(url, info.url);
1439 // Ignore start_time.
1440 EXPECT_EQ(origin_size, info.received_bytes);
1441 EXPECT_EQ(origin_size, info.total_bytes);
1442 EXPECT_EQ(DownloadItem::COMPLETE, info.state);
1443 } 1409 }
1444 1410
1445 // Test for crbug.com/14505. This tests that chrome:// urls are still functional 1411 // Test for crbug.com/14505. This tests that chrome:// urls are still functional
1446 // after download of a file while viewing another chrome://. 1412 // after download of a file while viewing another chrome://.
1447 IN_PROC_BROWSER_TEST_F(DownloadTest, ChromeURLAfterDownload) { 1413 IN_PROC_BROWSER_TEST_F(DownloadTest, ChromeURLAfterDownload) {
1448 FilePath file(FILE_PATH_LITERAL("download-test1.lib")); 1414 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
1449 GURL download_url(URLRequestMockHTTPJob::GetMockUrl(file)); 1415 GURL download_url(URLRequestMockHTTPJob::GetMockUrl(file));
1450 GURL flags_url(chrome::kChromeUIFlagsURL); 1416 GURL flags_url(chrome::kChromeUIFlagsURL);
1451 GURL extensions_url(chrome::kChromeUIExtensionsFrameURL); 1417 GURL extensions_url(chrome::kChromeUIExtensionsFrameURL);
1452 1418
(...skipping 806 matching lines...) Expand 10 before | Expand all | Expand 10 after
2259 WebContents* web_contents = chrome::GetActiveWebContents(browser()); 2225 WebContents* web_contents = chrome::GetActiveWebContents(browser());
2260 scoped_ptr<DownloadUrlParameters> params( 2226 scoped_ptr<DownloadUrlParameters> params(
2261 DownloadUrlParameters::FromWebContents(web_contents, url)); 2227 DownloadUrlParameters::FromWebContents(web_contents, url));
2262 params->set_callback(base::Bind(&SetHiddenDownloadCallback)); 2228 params->set_callback(base::Bind(&SetHiddenDownloadCallback));
2263 download_manager->DownloadUrl(params.Pass()); 2229 download_manager->DownloadUrl(params.Pass());
2264 observer->WaitForFinished(); 2230 observer->WaitForFinished();
2265 2231
2266 // Verify that download shelf is not shown. 2232 // Verify that download shelf is not shown.
2267 EXPECT_FALSE(browser()->window()->IsDownloadShelfVisible()); 2233 EXPECT_FALSE(browser()->window()->IsDownloadShelfVisible());
2268 } 2234 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698