76246c938034c160719650570ca983f0d185f21d
[platform/framework/web/crosswalk.git] / src / chrome / browser / browsing_data / browsing_data_remover_browsertest.cc
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.
4
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"
27
28 using content::BrowserThread;
29
30 namespace {
31 void SetUrlRequestMock(const base::FilePath& path) {
32   net::URLRequestMockHTTPJob::AddUrlHandler(path,
33                                             BrowserThread::GetBlockingPool());
34 }
35 }
36
37 class BrowsingDataRemoverBrowserTest : public InProcessBrowserTest {
38  public:
39   BrowsingDataRemoverBrowserTest() {}
40
41   void SetUpOnMainThread() override {
42     base::FilePath path;
43     PathService::Get(content::DIR_TEST_DATA, &path);
44     BrowserThread::PostTask(
45         BrowserThread::IO, FROM_HERE, base::Bind(&SetUrlRequestMock, path));
46   }
47
48   void RunScriptAndCheckResult(const std::string& script,
49                                const std::string& result) {
50     std::string data;
51     ASSERT_TRUE(content::ExecuteScriptAndExtractString(
52         browser()->tab_strip_model()->GetActiveWebContents(), script, &data));
53     ASSERT_EQ(data, result);
54   }
55
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());
62   }
63
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());
69
70     // Start a download.
71     content::DownloadManager* download_manager =
72         content::BrowserContext::GetDownloadManager(browser()->profile());
73     scoped_ptr<content::DownloadTestObserver> observer(
74         new content::DownloadTestObserverTerminal(
75             download_manager, 1,
76             content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_ACCEPT));
77
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();
83
84     VerifyDownloadCount(1u);
85   }
86
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();
93   }
94 };
95
96 // Test BrowsingDataRemover for downloads.
97 IN_PROC_BROWSER_TEST_F(BrowsingDataRemoverBrowserTest, Download) {
98   DownloadAnItem();
99   RemoveAndWait(BrowsingDataRemover::REMOVE_DOWNLOADS);
100   VerifyDownloadCount(0u);
101 }
102
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);
111
112   DownloadAnItem();
113   RemoveAndWait(BrowsingDataRemover::REMOVE_DOWNLOADS);
114   VerifyDownloadCount(1u);
115 }
116 #endif
117
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);
123
124   RunScriptAndCheckResult("createTable()", "done");
125   RunScriptAndCheckResult("insertRecord('text')", "done");
126   RunScriptAndCheckResult("getRecords()", "text");
127
128   RemoveAndWait(BrowsingDataRemover::REMOVE_SITE_DATA);
129
130   ui_test_utils::NavigateToURL(browser(), url);
131   RunScriptAndCheckResult("createTable()", "done");
132   RunScriptAndCheckResult("insertRecord('text2')", "done");
133   RunScriptAndCheckResult("getRecords()", "text2");
134 }
135
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.