| // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include "base/files/file_path.h" |
| #include "base/path_service.h" |
| #include "base/prefs/pref_service.h" |
| #include "chrome/browser/browsing_data/browsing_data_helper.h" |
| #include "chrome/browser/browsing_data/browsing_data_remover.h" |
| #include "chrome/browser/browsing_data/browsing_data_remover_test_util.h" |
| #include "chrome/browser/chrome_notification_types.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/ui/browser.h" |
| #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| #include "chrome/common/pref_names.h" |
| #include "chrome/test/base/in_process_browser_test.h" |
| #include "chrome/test/base/ui_test_utils.h" |
| #include "content/public/browser/browser_context.h" |
| #include "content/public/browser/browser_thread.h" |
| #include "content/public/browser/download_manager.h" |
| #include "content/public/browser/web_contents.h" |
| #include "content/public/common/content_paths.h" |
| #include "content/public/test/browser_test_utils.h" |
| #include "content/public/test/download_test_observer.h" |
| #include "content/test/net/url_request_mock_http_job.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| |
| using content::BrowserThread; |
| |
| namespace { |
| void SetUrlRequestMock(const base::FilePath& path) { |
| content::URLRequestMockHTTPJob::AddUrlHandler(path); |
| } |
| } |
| |
| class BrowsingDataRemoverBrowserTest : public InProcessBrowserTest { |
| public: |
| BrowsingDataRemoverBrowserTest() {} |
| |
| virtual void SetUpOnMainThread() OVERRIDE { |
| base::FilePath path; |
| PathService::Get(content::DIR_TEST_DATA, &path); |
| BrowserThread::PostTask( |
| BrowserThread::IO, FROM_HERE, base::Bind(&SetUrlRequestMock, path)); |
| } |
| |
| void RunScriptAndCheckResult(const std::string& script, |
| const std::string& result) { |
| std::string data; |
| ASSERT_TRUE(content::ExecuteScriptAndExtractString( |
| browser()->tab_strip_model()->GetActiveWebContents(), script, &data)); |
| ASSERT_EQ(data, result); |
| } |
| |
| void VerifyDownloadCount(size_t expected) { |
| content::DownloadManager* download_manager = |
| content::BrowserContext::GetDownloadManager(browser()->profile()); |
| std::vector<content::DownloadItem*> downloads; |
| download_manager->GetAllDownloads(&downloads); |
| EXPECT_EQ(expected, downloads.size()); |
| } |
| |
| void DownloadAnItem() { |
| base::ScopedTempDir downloads_directory; |
| ASSERT_TRUE(downloads_directory.CreateUniqueTempDir()); |
| browser()->profile()->GetPrefs()->SetFilePath( |
| prefs::kDownloadDefaultDirectory, downloads_directory.path()); |
| |
| // Start a download. |
| content::DownloadManager* download_manager = |
| content::BrowserContext::GetDownloadManager(browser()->profile()); |
| scoped_ptr<content::DownloadTestObserver> observer( |
| new content::DownloadTestObserverTerminal( |
| download_manager, 1, |
| content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_ACCEPT)); |
| |
| GURL download_url = ui_test_utils::GetTestUrl( |
| base::FilePath().AppendASCII("downloads"), |
| base::FilePath().AppendASCII("a_zip_file.zip")); |
| ui_test_utils::NavigateToURL(browser(), download_url); |
| observer->WaitForFinished(); |
| |
| VerifyDownloadCount(1u); |
| } |
| |
| void RemoveAndWait(int remove_mask) { |
| BrowsingDataRemover* remover = BrowsingDataRemover::CreateForPeriod( |
| browser()->profile(), BrowsingDataRemover::LAST_HOUR); |
| BrowsingDataRemoverCompletionObserver completion_observer(remover); |
| remover->Remove(remove_mask, BrowsingDataHelper::UNPROTECTED_WEB); |
| completion_observer.BlockUntilCompletion(); |
| } |
| }; |
| |
| // Test BrowsingDataRemover for downloads. |
| IN_PROC_BROWSER_TEST_F(BrowsingDataRemoverBrowserTest, Download) { |
| DownloadAnItem(); |
| RemoveAndWait(BrowsingDataRemover::REMOVE_DOWNLOADS); |
| VerifyDownloadCount(0u); |
| } |
| |
| // The call to Remove() should crash in debug (DCHECK), but the browser-test |
| // process model prevents using a death test. |
| #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| // Test BrowsingDataRemover for prohibited downloads. Note that this only |
| // really exercises the code in a Release build. |
| IN_PROC_BROWSER_TEST_F(BrowsingDataRemoverBrowserTest, DownloadProhibited) { |
| PrefService* prefs = browser()->profile()->GetPrefs(); |
| prefs->SetBoolean(prefs::kAllowDeletingBrowserHistory, false); |
| |
| DownloadAnItem(); |
| RemoveAndWait(BrowsingDataRemover::REMOVE_DOWNLOADS); |
| VerifyDownloadCount(1u); |
| } |
| #endif |
| |
| // Verify can modify database after deleting it. |
| IN_PROC_BROWSER_TEST_F(BrowsingDataRemoverBrowserTest, Database) { |
| GURL url(content::URLRequestMockHTTPJob::GetMockUrl( |
| base::FilePath().AppendASCII("simple_database.html"))); |
| ui_test_utils::NavigateToURL(browser(), url); |
| |
| RunScriptAndCheckResult("createTable()", "done"); |
| RunScriptAndCheckResult("insertRecord('text')", "done"); |
| RunScriptAndCheckResult("getRecords()", "text"); |
| |
| RemoveAndWait(BrowsingDataRemover::REMOVE_SITE_DATA); |
| |
| ui_test_utils::NavigateToURL(browser(), url); |
| RunScriptAndCheckResult("createTable()", "done"); |
| RunScriptAndCheckResult("insertRecord('text2')", "done"); |
| RunScriptAndCheckResult("getRecords()", "text2"); |
| } |
| |
| // Profile::ClearNetworkingHistorySince should be exercised here too see whether |
| // the call gets delegated through ProfileIO[Impl]Data properly, which is hard |
| // to write unit-tests for. Currently this is done by both of the above tests. |
| // Add standalone test if this changes. |