Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_local_storage_helper_browsertest.cc
blobf2ac74f8e4411e94f96aaabe277d257ecee9a389
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, NULL, 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 typedef std::list<BrowsingDataLocalStorageHelper::LocalStorageInfo>
92 LocalStorageInfoList;
93 for (size_t i = 0; i < arraysize(kTestHosts); ++i) {
94 for (LocalStorageInfoList::const_iterator info =
95 local_storage_info.begin(); info != local_storage_info.end();
96 ++info) {
97 ASSERT_TRUE(info->origin_url.SchemeIs("http"));
98 if (info->origin_url.host() == kTestHosts[i]) {
99 ASSERT_FALSE(test_hosts_found[i]);
100 test_hosts_found[i] = true;
104 for (size_t i = 0; i < arraysize(kTestHosts); ++i) {
105 ASSERT_TRUE(test_hosts_found[i]) << kTestHosts[i];
107 base::MessageLoop::current()->Quit();
110 private:
111 BrowsingDataLocalStorageHelper* local_storage_helper_;
114 IN_PROC_BROWSER_TEST_F(BrowsingDataLocalStorageHelperTest, CallbackCompletes) {
115 scoped_refptr<BrowsingDataLocalStorageHelper> local_storage_helper(
116 new BrowsingDataLocalStorageHelper(browser()->profile()));
117 CreateLocalStorageFilesForTest();
118 StopTestOnCallback stop_test_on_callback(local_storage_helper.get());
119 local_storage_helper->StartFetching(base::Bind(
120 &StopTestOnCallback::Callback, base::Unretained(&stop_test_on_callback)));
121 // Blocks until StopTestOnCallback::Callback is notified.
122 content::RunMessageLoop();
125 IN_PROC_BROWSER_TEST_F(BrowsingDataLocalStorageHelperTest, DeleteSingleFile) {
126 scoped_refptr<BrowsingDataLocalStorageHelper> local_storage_helper(
127 new BrowsingDataLocalStorageHelper(browser()->profile()));
128 CreateLocalStorageFilesForTest();
129 local_storage_helper->DeleteOrigin(GURL(kOriginOfTestFile0));
130 content::RunAllBlockingPoolTasksUntilIdle();
132 // Ensure the file has been deleted.
133 base::FileEnumerator file_enumerator(
134 GetLocalStoragePathForTestingProfile(),
135 false,
136 base::FileEnumerator::FILES);
137 int num_files = 0;
138 for (base::FilePath file_path = file_enumerator.Next();
139 !file_path.empty();
140 file_path = file_enumerator.Next()) {
141 ASSERT_FALSE(base::FilePath(kTestFile0) == file_path.BaseName());
142 ++num_files;
144 ASSERT_EQ(3, num_files);
147 IN_PROC_BROWSER_TEST_F(BrowsingDataLocalStorageHelperTest,
148 CannedAddLocalStorage) {
149 const GURL origin1("http://host1:1/");
150 const GURL origin2("http://host2:1/");
152 scoped_refptr<CannedBrowsingDataLocalStorageHelper> helper(
153 new CannedBrowsingDataLocalStorageHelper(browser()->profile()));
154 helper->AddLocalStorage(origin1);
155 helper->AddLocalStorage(origin2);
157 TestCompletionCallback callback;
158 helper->StartFetching(
159 base::Bind(&TestCompletionCallback::callback,
160 base::Unretained(&callback)));
162 std::list<BrowsingDataLocalStorageHelper::LocalStorageInfo> result =
163 callback.result();
165 ASSERT_EQ(2u, result.size());
166 std::list<BrowsingDataLocalStorageHelper::LocalStorageInfo>::iterator info =
167 result.begin();
168 EXPECT_EQ(origin1, info->origin_url);
169 info++;
170 EXPECT_EQ(origin2, info->origin_url);
173 IN_PROC_BROWSER_TEST_F(BrowsingDataLocalStorageHelperTest, CannedUnique) {
174 const GURL origin("http://host1:1/");
176 scoped_refptr<CannedBrowsingDataLocalStorageHelper> helper(
177 new CannedBrowsingDataLocalStorageHelper(browser()->profile()));
178 helper->AddLocalStorage(origin);
179 helper->AddLocalStorage(origin);
181 TestCompletionCallback callback;
182 helper->StartFetching(
183 base::Bind(&TestCompletionCallback::callback,
184 base::Unretained(&callback)));
186 std::list<BrowsingDataLocalStorageHelper::LocalStorageInfo> result =
187 callback.result();
189 ASSERT_EQ(1u, result.size());
190 EXPECT_EQ(origin, result.begin()->origin_url);
192 } // namespace