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
3 // found in the LICENSE file.
5 #include "base/files/file_path.h"
6 #include "base/path_service.h"
7 #include "base/prefs/pref_service.h"
8 #include "chrome/browser/browsing_data/browsing_data_helper.h"
9 #include "chrome/browser/browsing_data/browsing_data_remover.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
14 #include "chrome/common/pref_names.h"
15 #include "chrome/test/base/in_process_browser_test.h"
16 #include "chrome/test/base/ui_test_utils.h"
17 #include "content/public/browser/browser_context.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/download_manager.h"
20 #include "content/public/browser/web_contents.h"
21 #include "content/public/common/content_paths.h"
22 #include "content/public/test/browser_test_utils.h"
23 #include "content/public/test/download_test_observer.h"
24 #include "content/test/net/url_request_mock_http_job.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 using content::BrowserThread
;
30 void SetUrlRequestMock(const base::FilePath
& path
) {
31 content::URLRequestMockHTTPJob::AddUrlHandler(path
);
35 class BrowsingDataRemoverBrowserTest
: public InProcessBrowserTest
{
37 BrowsingDataRemoverBrowserTest() {}
39 virtual void SetUpOnMainThread() OVERRIDE
{
41 PathService::Get(content::DIR_TEST_DATA
, &path
);
42 BrowserThread::PostTask(
43 BrowserThread::IO
, FROM_HERE
, base::Bind(&SetUrlRequestMock
, path
));
46 void RunScriptAndCheckResult(const std::string
& script
,
47 const std::string
& result
) {
49 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
50 browser()->tab_strip_model()->GetActiveWebContents(), script
, &data
));
51 ASSERT_EQ(data
, result
);
54 void VerifyDownloadCount(size_t expected
) {
55 content::DownloadManager
* download_manager
=
56 content::BrowserContext::GetDownloadManager(browser()->profile());
57 std::vector
<content::DownloadItem
*> downloads
;
58 download_manager
->GetAllDownloads(&downloads
);
59 EXPECT_EQ(expected
, downloads
.size());
62 void DownloadAnItem() {
63 base::ScopedTempDir downloads_directory
;
64 ASSERT_TRUE(downloads_directory
.CreateUniqueTempDir());
65 browser()->profile()->GetPrefs()->SetFilePath(
66 prefs::kDownloadDefaultDirectory
, downloads_directory
.path());
69 content::DownloadManager
* download_manager
=
70 content::BrowserContext::GetDownloadManager(browser()->profile());
71 scoped_ptr
<content::DownloadTestObserver
> observer(
72 new content::DownloadTestObserverTerminal(
74 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_ACCEPT
));
76 GURL download_url
= ui_test_utils::GetTestUrl(
77 base::FilePath().AppendASCII("downloads"),
78 base::FilePath().AppendASCII("a_zip_file.zip"));
79 ui_test_utils::NavigateToURL(browser(), download_url
);
80 observer
->WaitForFinished();
82 VerifyDownloadCount(1u);
85 void RemoveAndWait(int remove_mask
) {
86 content::WindowedNotificationObserver
signal(
87 chrome::NOTIFICATION_BROWSING_DATA_REMOVED
,
88 content::Source
<Profile
>(browser()->profile()));
89 BrowsingDataRemover
* remover
= BrowsingDataRemover::CreateForPeriod(
90 browser()->profile(), BrowsingDataRemover::LAST_HOUR
);
91 remover
->Remove(remove_mask
, BrowsingDataHelper::UNPROTECTED_WEB
);
96 // Test BrowsingDataRemover for downloads.
97 IN_PROC_BROWSER_TEST_F(BrowsingDataRemoverBrowserTest
, Download
) {
99 RemoveAndWait(BrowsingDataRemover::REMOVE_DOWNLOADS
);
100 VerifyDownloadCount(0u);
103 // The call to Remove() should crash in debug (DCHECK), but the browser-test
104 // process model prevents using a death test.
105 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
106 // Test BrowsingDataRemover for prohibited downloads. Note that this only
107 // really exercises the code in a Release build.
108 IN_PROC_BROWSER_TEST_F(BrowsingDataRemoverBrowserTest
, DownloadProhibited
) {
109 PrefService
* prefs
= browser()->profile()->GetPrefs();
110 prefs
->SetBoolean(prefs::kAllowDeletingBrowserHistory
, false);
113 RemoveAndWait(BrowsingDataRemover::REMOVE_DOWNLOADS
);
114 VerifyDownloadCount(1u);
118 // Verify can modify database after deleting it.
119 IN_PROC_BROWSER_TEST_F(BrowsingDataRemoverBrowserTest
, Database
) {
120 GURL
url(content::URLRequestMockHTTPJob::GetMockUrl(
121 base::FilePath().AppendASCII("simple_database.html")));
122 ui_test_utils::NavigateToURL(browser(), url
);
124 RunScriptAndCheckResult("createTable()", "done");
125 RunScriptAndCheckResult("insertRecord('text')", "done");
126 RunScriptAndCheckResult("getRecords()", "text");
128 RemoveAndWait(BrowsingDataRemover::REMOVE_SITE_DATA
);
130 ui_test_utils::NavigateToURL(browser(), url
);
131 RunScriptAndCheckResult("createTable()", "done");
132 RunScriptAndCheckResult("insertRecord('text2')", "done");
133 RunScriptAndCheckResult("getRecords()", "text2");
136 // Profile::ClearNetworkingHistorySince should be exercised here too see whether
137 // the call gets delegated through ProfileIO[Impl]Data properly, which is hard
138 // to write unit-tests for. Currently this is done by both of the above tests.
139 // Add standalone test if this changes.