ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_local_storage_helper_browsertest.cc
blob1dad27a89c7ac19d083be577218239327c29460a
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 "chrome/browser/browsing_data/browsing_data_local_storage_helper.h"
7 #include <string>
9 #include "base/basictypes.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/callback.h"
13 #include "base/files/file_enumerator.h"
14 #include "base/files/file_path.h"
15 #include "base/files/file_util.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/test/thread_test_helper.h"
19 #include "base/threading/sequenced_worker_pool.h"
20 #include "chrome/browser/browsing_data/browsing_data_helper_browsertest.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/ui/browser.h"
23 #include "chrome/test/base/in_process_browser_test.h"
24 #include "content/public/browser/dom_storage_context.h"
25 #include "content/public/test/test_utils.h"
26 #include "testing/gtest/include/gtest/gtest.h"
28 using content::BrowserContext;
29 using content::BrowserThread;
30 using content::DOMStorageContext;
32 namespace {
33 typedef
34 BrowsingDataHelperCallback<BrowsingDataLocalStorageHelper::LocalStorageInfo>
35 TestCompletionCallback;
37 const base::FilePath::CharType kTestFile0[] =
38 FILE_PATH_LITERAL("http_www.chromium.org_0.localstorage");
40 const char kOriginOfTestFile0[] = "http://www.chromium.org/";
42 const base::FilePath::CharType kTestFile1[] =
43 FILE_PATH_LITERAL("http_www.google.com_0.localstorage");
45 const base::FilePath::CharType kTestFileInvalid[] =
46 FILE_PATH_LITERAL("http_www.google.com_localstorage_0.foo");
48 // This is only here to test that extension state is not listed by the helper.
49 const base::FilePath::CharType kTestFileExtension[] = FILE_PATH_LITERAL(
50 "chrome-extension_behllobkkfkfnphdnhnkndlbkcpglgmj_0.localstorage");
52 class BrowsingDataLocalStorageHelperTest : public InProcessBrowserTest {
53 protected:
54 void CreateLocalStorageFilesForTest() {
55 // Note: This helper depends on details of how the dom_storage library
56 // stores data in the host file system.
57 base::FilePath storage_path = GetLocalStoragePathForTestingProfile();
58 base::CreateDirectory(storage_path);
59 const base::FilePath::CharType* kFilesToCreate[] = {
60 kTestFile0, kTestFile1, kTestFileInvalid, kTestFileExtension
62 for (size_t i = 0; i < arraysize(kFilesToCreate); ++i) {
63 base::FilePath file_path = storage_path.Append(kFilesToCreate[i]);
64 base::WriteFile(file_path, nullptr, 0);
68 base::FilePath GetLocalStoragePathForTestingProfile() {
69 return browser()->profile()->GetPath().AppendASCII("Local Storage");
73 // This class is notified by BrowsingDataLocalStorageHelper on the UI thread
74 // once it finishes fetching the local storage data.
75 class StopTestOnCallback {
76 public:
77 explicit StopTestOnCallback(
78 BrowsingDataLocalStorageHelper* local_storage_helper)
79 : local_storage_helper_(local_storage_helper) {
80 DCHECK(local_storage_helper_);
83 void Callback(
84 const std::list<BrowsingDataLocalStorageHelper::LocalStorageInfo>&
85 local_storage_info) {
86 DCHECK_CURRENTLY_ON(BrowserThread::UI);
87 // There's no guarantee on the order, ensure these files are there.
88 const char* const kTestHosts[] = {"www.chromium.org", "www.google.com"};
89 bool test_hosts_found[arraysize(kTestHosts)] = {false, false};
90 ASSERT_EQ(arraysize(kTestHosts), local_storage_info.size());
91 for (size_t i = 0; i < arraysize(kTestHosts); ++i) {
92 for (const auto& info : local_storage_info) {
93 ASSERT_TRUE(info.origin_url.SchemeIs("http"));
94 if (info.origin_url.host() == kTestHosts[i]) {
95 ASSERT_FALSE(test_hosts_found[i]);
96 test_hosts_found[i] = true;
100 for (size_t i = 0; i < arraysize(kTestHosts); ++i) {
101 ASSERT_TRUE(test_hosts_found[i]) << kTestHosts[i];
103 base::MessageLoop::current()->Quit();
106 private:
107 BrowsingDataLocalStorageHelper* local_storage_helper_;
110 IN_PROC_BROWSER_TEST_F(BrowsingDataLocalStorageHelperTest, CallbackCompletes) {
111 scoped_refptr<BrowsingDataLocalStorageHelper> local_storage_helper(
112 new BrowsingDataLocalStorageHelper(browser()->profile()));
113 CreateLocalStorageFilesForTest();
114 StopTestOnCallback stop_test_on_callback(local_storage_helper.get());
115 local_storage_helper->StartFetching(base::Bind(
116 &StopTestOnCallback::Callback, base::Unretained(&stop_test_on_callback)));
117 // Blocks until StopTestOnCallback::Callback is notified.
118 content::RunMessageLoop();
121 IN_PROC_BROWSER_TEST_F(BrowsingDataLocalStorageHelperTest, DeleteSingleFile) {
122 scoped_refptr<BrowsingDataLocalStorageHelper> local_storage_helper(
123 new BrowsingDataLocalStorageHelper(browser()->profile()));
124 CreateLocalStorageFilesForTest();
125 local_storage_helper->DeleteOrigin(GURL(kOriginOfTestFile0));
126 content::RunAllBlockingPoolTasksUntilIdle();
128 // Ensure the file has been deleted.
129 base::FileEnumerator file_enumerator(
130 GetLocalStoragePathForTestingProfile(),
131 false,
132 base::FileEnumerator::FILES);
133 int num_files = 0;
134 for (base::FilePath file_path = file_enumerator.Next();
135 !file_path.empty();
136 file_path = file_enumerator.Next()) {
137 ASSERT_FALSE(base::FilePath(kTestFile0) == file_path.BaseName());
138 ++num_files;
140 ASSERT_EQ(3, num_files);
143 IN_PROC_BROWSER_TEST_F(BrowsingDataLocalStorageHelperTest,
144 CannedAddLocalStorage) {
145 const GURL origin1("http://host1:1/");
146 const GURL origin2("http://host2:1/");
148 scoped_refptr<CannedBrowsingDataLocalStorageHelper> helper(
149 new CannedBrowsingDataLocalStorageHelper(browser()->profile()));
150 helper->AddLocalStorage(origin1);
151 helper->AddLocalStorage(origin2);
153 TestCompletionCallback callback;
154 helper->StartFetching(
155 base::Bind(&TestCompletionCallback::callback,
156 base::Unretained(&callback)));
158 std::list<BrowsingDataLocalStorageHelper::LocalStorageInfo> result =
159 callback.result();
161 ASSERT_EQ(2u, result.size());
162 std::list<BrowsingDataLocalStorageHelper::LocalStorageInfo>::iterator info =
163 result.begin();
164 EXPECT_EQ(origin1, info->origin_url);
165 info++;
166 EXPECT_EQ(origin2, info->origin_url);
169 IN_PROC_BROWSER_TEST_F(BrowsingDataLocalStorageHelperTest, CannedUnique) {
170 const GURL origin("http://host1:1/");
172 scoped_refptr<CannedBrowsingDataLocalStorageHelper> helper(
173 new CannedBrowsingDataLocalStorageHelper(browser()->profile()));
174 helper->AddLocalStorage(origin);
175 helper->AddLocalStorage(origin);
177 TestCompletionCallback callback;
178 helper->StartFetching(
179 base::Bind(&TestCompletionCallback::callback,
180 base::Unretained(&callback)));
182 std::list<BrowsingDataLocalStorageHelper::LocalStorageInfo> result =
183 callback.result();
185 ASSERT_EQ(1u, result.size());
186 EXPECT_EQ(origin, result.begin()->origin_url);
188 } // namespace