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/browsing_data/browsing_data_remover_test_util.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "chrome/common/pref_names.h"
16 #include "chrome/test/base/in_process_browser_test.h"
17 #include "chrome/test/base/ui_test_utils.h"
18 #include "content/public/browser/browser_context.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "content/public/browser/download_manager.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/common/content_paths.h"
23 #include "content/public/test/browser_test_utils.h"
24 #include "content/public/test/download_test_observer.h"
25 #include "net/test/url_request/url_request_mock_http_job.h"
26 #include "testing/gtest/include/gtest/gtest.h"
28 using content::BrowserThread
;
31 void SetUrlRequestMock(const base::FilePath
& path
) {
32 net::URLRequestMockHTTPJob::AddUrlHandlers(path
,
33 BrowserThread::GetBlockingPool());
37 class BrowsingDataRemoverBrowserTest
: public InProcessBrowserTest
{
39 BrowsingDataRemoverBrowserTest() {}
41 void SetUpOnMainThread() override
{
43 PathService::Get(content::DIR_TEST_DATA
, &path
);
44 BrowserThread::PostTask(
45 BrowserThread::IO
, FROM_HERE
, base::Bind(&SetUrlRequestMock
, path
));
48 void RunScriptAndCheckResult(const std::string
& script
,
49 const std::string
& result
) {
51 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
52 browser()->tab_strip_model()->GetActiveWebContents(), script
, &data
));
53 ASSERT_EQ(data
, result
);
56 void VerifyDownloadCount(size_t expected
) {
57 content::DownloadManager
* download_manager
=
58 content::BrowserContext::GetDownloadManager(browser()->profile());
59 std::vector
<content::DownloadItem
*> downloads
;
60 download_manager
->GetAllDownloads(&downloads
);
61 EXPECT_EQ(expected
, downloads
.size());
64 void DownloadAnItem() {
65 base::ScopedTempDir downloads_directory
;
66 ASSERT_TRUE(downloads_directory
.CreateUniqueTempDir());
67 browser()->profile()->GetPrefs()->SetFilePath(
68 prefs::kDownloadDefaultDirectory
, downloads_directory
.path());
71 content::DownloadManager
* download_manager
=
72 content::BrowserContext::GetDownloadManager(browser()->profile());
73 scoped_ptr
<content::DownloadTestObserver
> observer(
74 new content::DownloadTestObserverTerminal(
76 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_ACCEPT
));
78 GURL download_url
= ui_test_utils::GetTestUrl(
79 base::FilePath().AppendASCII("downloads"),
80 base::FilePath().AppendASCII("a_zip_file.zip"));
81 ui_test_utils::NavigateToURL(browser(), download_url
);
82 observer
->WaitForFinished();
84 VerifyDownloadCount(1u);
87 void RemoveAndWait(int remove_mask
) {
88 BrowsingDataRemover
* remover
= BrowsingDataRemover::CreateForPeriod(
89 browser()->profile(), BrowsingDataRemover::LAST_HOUR
);
90 BrowsingDataRemoverCompletionObserver
completion_observer(remover
);
91 remover
->Remove(remove_mask
, BrowsingDataHelper::UNPROTECTED_WEB
);
92 completion_observer
.BlockUntilCompletion();
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(net::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.