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

Side by Side Diff: chrome/browser/browsing_data/browsing_data_remover_browsertest.cc

Issue 2008323002: Add tests for the cache deletion in BrowsingDataRemover (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix memory leak Created 4 years, 6 months 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
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 <stddef.h> 5 #include <stddef.h>
6 #include <memory>
6 7
8 #include "base/bind.h"
9 #include "base/callback.h"
7 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
8 #include "base/path_service.h" 11 #include "base/path_service.h"
12 #include "base/run_loop.h"
9 #include "chrome/browser/browsing_data/browsing_data_helper.h" 13 #include "chrome/browser/browsing_data/browsing_data_helper.h"
10 #include "chrome/browser/browsing_data/browsing_data_remover.h" 14 #include "chrome/browser/browsing_data/browsing_data_remover.h"
11 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h" 15 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h"
12 #include "chrome/browser/browsing_data/browsing_data_remover_test_util.h" 16 #include "chrome/browser/browsing_data/browsing_data_remover_test_util.h"
17 #include "chrome/browser/browsing_data/cache_counter.h"
18 #include "chrome/browser/browsing_data/origin_filter_builder.h"
13 #include "chrome/browser/chrome_notification_types.h" 19 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/profiles/profile.h" 20 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser.h" 21 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h" 22 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/common/pref_names.h" 23 #include "chrome/common/pref_names.h"
18 #include "chrome/test/base/in_process_browser_test.h" 24 #include "chrome/test/base/in_process_browser_test.h"
19 #include "chrome/test/base/ui_test_utils.h" 25 #include "chrome/test/base/ui_test_utils.h"
20 #include "components/prefs/pref_service.h" 26 #include "components/prefs/pref_service.h"
21 #include "content/public/browser/browser_context.h" 27 #include "content/public/browser/browser_context.h"
22 #include "content/public/browser/browser_thread.h" 28 #include "content/public/browser/browser_thread.h"
23 #include "content/public/browser/download_manager.h" 29 #include "content/public/browser/download_manager.h"
24 #include "content/public/browser/web_contents.h" 30 #include "content/public/browser/web_contents.h"
25 #include "content/public/common/content_paths.h" 31 #include "content/public/common/content_paths.h"
26 #include "content/public/test/browser_test_utils.h" 32 #include "content/public/test/browser_test_utils.h"
27 #include "content/public/test/download_test_observer.h" 33 #include "content/public/test/download_test_observer.h"
34 #include "net/dns/mock_host_resolver.h"
28 #include "net/http/transport_security_state.h" 35 #include "net/http/transport_security_state.h"
29 #include "net/test/url_request/url_request_mock_http_job.h" 36 #include "net/test/embedded_test_server/embedded_test_server.h"
30 #include "net/url_request/url_request_context.h" 37 #include "net/url_request/url_request_context.h"
31 #include "net/url_request/url_request_context_getter.h" 38 #include "net/url_request/url_request_context_getter.h"
32 #include "testing/gtest/include/gtest/gtest.h" 39 #include "testing/gtest/include/gtest/gtest.h"
33 40
34 using content::BrowserThread; 41 using content::BrowserThread;
35 42
36 namespace { 43 namespace {
37 void SetUrlRequestMock(const base::FilePath& path) { 44 static const char* kExampleHost = "example.com";
38 net::URLRequestMockHTTPJob::AddUrlHandlers(path,
39 BrowserThread::GetBlockingPool());
40 }
41 } 45 }
42 46
43 class BrowsingDataRemoverBrowserTest : public InProcessBrowserTest { 47 class BrowsingDataRemoverBrowserTest : public InProcessBrowserTest {
44 public: 48 public:
45 BrowsingDataRemoverBrowserTest() {} 49 BrowsingDataRemoverBrowserTest() {}
46 50
47 void SetUpOnMainThread() override { 51 void SetUpOnMainThread() override {
48 base::FilePath path; 52 base::FilePath path;
49 PathService::Get(content::DIR_TEST_DATA, &path); 53 PathService::Get(content::DIR_TEST_DATA, &path);
50 BrowserThread::PostTask( 54 host_resolver()->AddRule(kExampleHost, "127.0.0.1");
51 BrowserThread::IO, FROM_HERE, base::Bind(&SetUrlRequestMock, path)); 55 embedded_test_server()->ServeFilesFromDirectory(path);
56 ASSERT_TRUE(embedded_test_server()->Start());
52 } 57 }
53 58
54 void RunScriptAndCheckResult(const std::string& script, 59 void RunScriptAndCheckResult(const std::string& script,
55 const std::string& result) { 60 const std::string& result) {
56 std::string data; 61 std::string data;
57 ASSERT_TRUE(content::ExecuteScriptAndExtractString( 62 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
58 browser()->tab_strip_model()->GetActiveWebContents(), script, &data)); 63 browser()->tab_strip_model()->GetActiveWebContents(), script, &data));
59 ASSERT_EQ(data, result); 64 ASSERT_EQ(data, result);
60 } 65 }
61 66
(...skipping 21 matching lines...) Expand all
83 88
84 GURL download_url = ui_test_utils::GetTestUrl( 89 GURL download_url = ui_test_utils::GetTestUrl(
85 base::FilePath().AppendASCII("downloads"), 90 base::FilePath().AppendASCII("downloads"),
86 base::FilePath().AppendASCII("a_zip_file.zip")); 91 base::FilePath().AppendASCII("a_zip_file.zip"));
87 ui_test_utils::NavigateToURL(browser(), download_url); 92 ui_test_utils::NavigateToURL(browser(), download_url);
88 observer->WaitForFinished(); 93 observer->WaitForFinished();
89 94
90 VerifyDownloadCount(1u); 95 VerifyDownloadCount(1u);
91 } 96 }
92 97
98 BrowsingDataCounter::ResultInt GetCacheSize() {
99 base::RunLoop run_loop;
100 BrowsingDataCounter::ResultInt size;
101
102 CacheCounter counter;
103 counter.Init(browser()->profile(),
104 base::Bind(&BrowsingDataRemoverBrowserTest::OnCacheSizeResult,
105 base::Unretained(this),
106 base::Unretained(&run_loop),
107 base::Unretained(&size)));
108 counter.Restart();
109 run_loop.Run();
110 return size;
111 }
112
93 void RemoveAndWait(int remove_mask) { 113 void RemoveAndWait(int remove_mask) {
94 BrowsingDataRemover* remover = 114 BrowsingDataRemover* remover =
95 BrowsingDataRemoverFactory::GetForBrowserContext(browser()->profile()); 115 BrowsingDataRemoverFactory::GetForBrowserContext(browser()->profile());
96 BrowsingDataRemoverCompletionObserver completion_observer(remover); 116 BrowsingDataRemoverCompletionObserver completion_observer(remover);
97 remover->Remove(BrowsingDataRemover::Period(BrowsingDataRemover::LAST_HOUR), 117 remover->Remove(BrowsingDataRemover::Period(BrowsingDataRemover::LAST_HOUR),
98 remove_mask, BrowsingDataHelper::UNPROTECTED_WEB); 118 remove_mask, BrowsingDataHelper::UNPROTECTED_WEB);
99 completion_observer.BlockUntilCompletion(); 119 completion_observer.BlockUntilCompletion();
100 } 120 }
121
122 void RemoveWithFilterAndWait(
123 int remove_mask,
124 const BrowsingDataFilterBuilder& filter_builder) {
125 BrowsingDataRemover* remover =
126 BrowsingDataRemoverFactory::GetForBrowserContext(browser()->profile());
127 BrowsingDataRemoverCompletionObserver completion_observer(remover);
128 remover->RemoveWithFilter(
129 BrowsingDataRemover::Period(BrowsingDataRemover::LAST_HOUR),
130 remove_mask, BrowsingDataHelper::UNPROTECTED_WEB, filter_builder);
131 completion_observer.BlockUntilCompletion();
132 }
133
134 private:
135 void OnCacheSizeResult(
136 base::RunLoop* run_loop,
137 BrowsingDataCounter::ResultInt* out_size,
138 std::unique_ptr<BrowsingDataCounter::Result> result) {
139 if (!result->Finished())
140 return;
141
142 *out_size = static_cast<BrowsingDataCounter::FinishedResult*>(
143 result.get())->Value();
144 run_loop->Quit();
145 }
101 }; 146 };
102 147
103 class BrowsingDataRemoverTransportSecurityStateBrowserTest 148 class BrowsingDataRemoverTransportSecurityStateBrowserTest
104 : public BrowsingDataRemoverBrowserTest { 149 : public BrowsingDataRemoverBrowserTest {
105 public: 150 public:
106 BrowsingDataRemoverTransportSecurityStateBrowserTest() {} 151 BrowsingDataRemoverTransportSecurityStateBrowserTest() {}
107 152
108 void SetUpOnMainThread() override { 153 void SetUpOnMainThread() override {
109 BrowserThread::PostTask( 154 BrowserThread::PostTask(
110 BrowserThread::IO, FROM_HERE, 155 BrowserThread::IO, FROM_HERE,
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 prefs->SetBoolean(prefs::kAllowDeletingBrowserHistory, false); 199 prefs->SetBoolean(prefs::kAllowDeletingBrowserHistory, false);
155 200
156 DownloadAnItem(); 201 DownloadAnItem();
157 RemoveAndWait(BrowsingDataRemover::REMOVE_DOWNLOADS); 202 RemoveAndWait(BrowsingDataRemover::REMOVE_DOWNLOADS);
158 VerifyDownloadCount(1u); 203 VerifyDownloadCount(1u);
159 } 204 }
160 #endif 205 #endif
161 206
162 // Verify can modify database after deleting it. 207 // Verify can modify database after deleting it.
163 IN_PROC_BROWSER_TEST_F(BrowsingDataRemoverBrowserTest, Database) { 208 IN_PROC_BROWSER_TEST_F(BrowsingDataRemoverBrowserTest, Database) {
164 GURL url(net::URLRequestMockHTTPJob::GetMockUrl("simple_database.html")); 209 GURL url = embedded_test_server()->GetURL("/simple_database.html");
210 LOG(ERROR) << url;
165 ui_test_utils::NavigateToURL(browser(), url); 211 ui_test_utils::NavigateToURL(browser(), url);
166 212
167 RunScriptAndCheckResult("createTable()", "done"); 213 RunScriptAndCheckResult("createTable()", "done");
168 RunScriptAndCheckResult("insertRecord('text')", "done"); 214 RunScriptAndCheckResult("insertRecord('text')", "done");
169 RunScriptAndCheckResult("getRecords()", "text"); 215 RunScriptAndCheckResult("getRecords()", "text");
170 216
171 RemoveAndWait(BrowsingDataRemover::REMOVE_SITE_DATA); 217 RemoveAndWait(BrowsingDataRemover::REMOVE_SITE_DATA);
172 218
173 ui_test_utils::NavigateToURL(browser(), url); 219 ui_test_utils::NavigateToURL(browser(), url);
174 RunScriptAndCheckResult("createTable()", "done"); 220 RunScriptAndCheckResult("createTable()", "done");
175 RunScriptAndCheckResult("insertRecord('text2')", "done"); 221 RunScriptAndCheckResult("insertRecord('text2')", "done");
176 RunScriptAndCheckResult("getRecords()", "text2"); 222 RunScriptAndCheckResult("getRecords()", "text2");
177 } 223 }
178 224
225 // Verify that cache deleting cache finishes successfully. Complete deletion
226 // of cache should leave it empty, and partial deletion should leave nonzero
227 // amount of data. Note that this tests the integration of BrowsingDataRemover
228 // with ConditionalCacheDeletionHelper. Whether ConditionalCacheDeletionHelper
229 // actually deletes the correct entries is tested
230 // in ConditionalCacheDeletionHelperBrowsertest.
231 IN_PROC_BROWSER_TEST_F(BrowsingDataRemoverBrowserTest, Cache) {
232 // Load several resources.
233 GURL url1 = embedded_test_server()->GetURL("/simple.html");
234 GURL url2 = embedded_test_server()->GetURL(kExampleHost, "/simple.html");
235 ASSERT_FALSE(url::IsSameOriginWith(url1, url2));
236 ui_test_utils::NavigateToURL(browser(), url1);
237 ui_test_utils::NavigateToURL(browser(), url2);
238
239 // The cache is nonempty, because we created entries by visiting websites.
240 BrowsingDataCounter::ResultInt original_size = GetCacheSize();
241 EXPECT_GT(original_size, 0);
242
243 // Partially delete cache data. Delete data for localhost, which is the origin
244 // of |url1|, but not for |kExampleHost|, which is the origin of |url2|.
245 OriginFilterBuilder filter_builder(OriginFilterBuilder::WHITELIST);
246 filter_builder.AddOrigin(url::Origin(url1));
247 RemoveWithFilterAndWait(BrowsingDataRemover::REMOVE_CACHE, filter_builder);
248
249 // After the partial deletion, the cache should be smaller but still nonempty.
250 BrowsingDataCounter::ResultInt new_size = GetCacheSize();
251 EXPECT_LT(new_size, original_size);
252
253 // Another partial deletion with the same filter should have no effect.
254 RemoveWithFilterAndWait(BrowsingDataRemover::REMOVE_CACHE, filter_builder);
255 EXPECT_EQ(new_size, GetCacheSize());
256
257 // Delete the remaining data.
258 RemoveAndWait(BrowsingDataRemover::REMOVE_CACHE);
259
260 // The cache is empty.
261 EXPECT_EQ(0, GetCacheSize());
262 }
263
179 // Verify that TransportSecurityState data is cleared for REMOVE_CACHE. 264 // Verify that TransportSecurityState data is cleared for REMOVE_CACHE.
180 IN_PROC_BROWSER_TEST_F(BrowsingDataRemoverTransportSecurityStateBrowserTest, 265 IN_PROC_BROWSER_TEST_F(BrowsingDataRemoverTransportSecurityStateBrowserTest,
181 ClearTransportSecurityState) { 266 ClearTransportSecurityState) {
182 RemoveAndWait(BrowsingDataRemover::REMOVE_CACHE); 267 RemoveAndWait(BrowsingDataRemover::REMOVE_CACHE);
183 base::RunLoop run_loop; 268 base::RunLoop run_loop;
184 BrowserThread::PostTaskAndReply( 269 BrowserThread::PostTaskAndReply(
185 BrowserThread::IO, FROM_HERE, 270 BrowserThread::IO, FROM_HERE,
186 base::Bind(&BrowsingDataRemoverTransportSecurityStateBrowserTest:: 271 base::Bind(&BrowsingDataRemoverTransportSecurityStateBrowserTest::
187 CheckTransportSecurityState, 272 CheckTransportSecurityState,
188 this, 273 this,
(...skipping 10 matching lines...) Expand all
199 base::RunLoop run_loop; 284 base::RunLoop run_loop;
200 BrowserThread::PostTaskAndReply( 285 BrowserThread::PostTaskAndReply(
201 BrowserThread::IO, FROM_HERE, 286 BrowserThread::IO, FROM_HERE,
202 base::Bind(&BrowsingDataRemoverTransportSecurityStateBrowserTest:: 287 base::Bind(&BrowsingDataRemoverTransportSecurityStateBrowserTest::
203 CheckTransportSecurityState, 288 CheckTransportSecurityState,
204 this, 289 this,
205 base::RetainedRef(browser()->profile()->GetRequestContext()), 290 base::RetainedRef(browser()->profile()->GetRequestContext()),
206 false /* should not be cleared */), 291 false /* should not be cleared */),
207 run_loop.QuitClosure()); 292 run_loop.QuitClosure());
208 } 293 }
OLDNEW
« no previous file with comments | « chrome/browser/browsing_data/browsing_data_remover.cc ('k') | components/browsing_data/conditional_cache_deletion_helper.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698