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

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: @r166419 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 1251 matching lines...) Expand 10 before | Expand all | Expand 10 after
1406 ExpectWindowCountAfterDownload(1); 1355 ExpectWindowCountAfterDownload(1);
1407 #endif 1356 #endif
1408 1357
1409 EXPECT_EQ(1, browser()->tab_count()); 1358 EXPECT_EQ(1, browser()->tab_count());
1410 // Download shelf should close. Download panel stays open on ChromeOS. 1359 // Download shelf should close. Download panel stays open on ChromeOS.
1411 EXPECT_FALSE(browser()->window()->IsDownloadShelfVisible()); 1360 EXPECT_FALSE(browser()->window()->IsDownloadShelfVisible());
1412 1361
1413 CheckDownload(browser(), file, file); 1362 CheckDownload(browser(), file, file);
1414 } 1363 }
1415 1364
1416 // Confirm a download makes it into the history properly. 1365 class HistoryObserver : public DownloadHistory::Observer {
1366 public:
1367 explicit HistoryObserver(Profile* profile)
1368 : profile_(profile),
1369 waiting_(false),
1370 seen_stored_(false) {
1371 DownloadServiceFactory::GetForProfile(profile_)->
1372 GetDownloadHistory()->AddObserver(this);
1373 }
1374
1375 virtual ~HistoryObserver() {
1376 DownloadService* service = DownloadServiceFactory::GetForProfile(profile_);
1377 if (service && service->GetDownloadManagerDelegate())
1378 service->GetDownloadHistory()->RemoveObserver(this);
1379 }
1380
1381 virtual void OnDownloadStored(
1382 content::DownloadItem* item,
1383 const DownloadPersistentStoreInfo& info) OVERRIDE {
1384 seen_stored_ = true;
1385 if (waiting_)
1386 MessageLoopForUI::current()->Quit();
1387 }
1388
1389 void WaitForStored() {
1390 if (seen_stored_)
1391 return;
1392 waiting_ = true;
1393 content::RunMessageLoop();
1394 waiting_ = false;
1395 }
1396
1397 private:
1398 Profile* profile_;
1399 bool waiting_;
1400 bool seen_stored_;
1401 DISALLOW_COPY_AND_ASSIGN(HistoryObserver);
1402 };
1403
1417 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadHistoryCheck) { 1404 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadHistoryCheck) {
1418 FilePath file(FILE_PATH_LITERAL("download-test1.lib")); 1405 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
1419 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); 1406 GURL download_url(URLRequestMockHTTPJob::GetMockUrl(file));
1420 FilePath origin_file(OriginFile(file)); 1407 HistoryObserver observer(browser()->profile());
1421 int64 origin_size; 1408 DownloadAndWait(browser(), download_url);
1422 file_util::GetFileSize(origin_file, &origin_size); 1409 observer.WaitForStored();
1423
1424 // Download the file and wait. We do not expect the Select File dialog.
1425 DownloadAndWait(browser(), url);
1426
1427 // Get details of what downloads have just happened.
1428 std::vector<DownloadItem*> downloads;
1429 GetDownloads(browser(), &downloads);
1430 ASSERT_EQ(1u, downloads.size());
1431 int64 db_handle = downloads[0]->GetDbHandle();
1432
1433 // Check state.
1434 EXPECT_EQ(1, browser()->tab_count());
1435 CheckDownload(browser(), file, file);
1436 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
1437
1438 // Check history results.
1439 DownloadsHistoryDataCollector history_collector(
1440 db_handle,
1441 DownloadManagerForBrowser(browser()));
1442 DownloadPersistentStoreInfo info;
1443 EXPECT_TRUE(history_collector.GetDownloadsHistoryEntry(&info)) << db_handle;
1444 EXPECT_EQ(file, info.path.BaseName());
1445 EXPECT_EQ(url, info.url);
1446 // Ignore start_time.
1447 EXPECT_EQ(origin_size, info.received_bytes);
1448 EXPECT_EQ(origin_size, info.total_bytes);
1449 EXPECT_EQ(DownloadItem::COMPLETE, info.state);
1450 } 1410 }
1451 1411
1452 // Test for crbug.com/14505. This tests that chrome:// urls are still functional 1412 // Test for crbug.com/14505. This tests that chrome:// urls are still functional
1453 // after download of a file while viewing another chrome://. 1413 // after download of a file while viewing another chrome://.
1454 IN_PROC_BROWSER_TEST_F(DownloadTest, ChromeURLAfterDownload) { 1414 IN_PROC_BROWSER_TEST_F(DownloadTest, ChromeURLAfterDownload) {
1455 FilePath file(FILE_PATH_LITERAL("download-test1.lib")); 1415 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
1456 GURL download_url(URLRequestMockHTTPJob::GetMockUrl(file)); 1416 GURL download_url(URLRequestMockHTTPJob::GetMockUrl(file));
1457 GURL flags_url(chrome::kChromeUIFlagsURL); 1417 GURL flags_url(chrome::kChromeUIFlagsURL);
1458 GURL extensions_url(chrome::kChromeUIExtensionsFrameURL); 1418 GURL extensions_url(chrome::kChromeUIExtensionsFrameURL);
1459 1419
(...skipping 806 matching lines...) Expand 10 before | Expand all | Expand 10 after
2266 WebContents* web_contents = chrome::GetActiveWebContents(browser()); 2226 WebContents* web_contents = chrome::GetActiveWebContents(browser());
2267 scoped_ptr<DownloadUrlParameters> params( 2227 scoped_ptr<DownloadUrlParameters> params(
2268 DownloadUrlParameters::FromWebContents(web_contents, url)); 2228 DownloadUrlParameters::FromWebContents(web_contents, url));
2269 params->set_callback(base::Bind(&SetHiddenDownloadCallback)); 2229 params->set_callback(base::Bind(&SetHiddenDownloadCallback));
2270 download_manager->DownloadUrl(params.Pass()); 2230 download_manager->DownloadUrl(params.Pass());
2271 observer->WaitForFinished(); 2231 observer->WaitForFinished();
2272 2232
2273 // Verify that download shelf is not shown. 2233 // Verify that download shelf is not shown.
2274 EXPECT_FALSE(browser()->window()->IsDownloadShelfVisible()); 2234 EXPECT_FALSE(browser()->window()->IsDownloadShelfVisible());
2275 } 2235 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698