cros: Remove default pinned apps trial.
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_local_storage_helper_browsertest.cc
blob564e07f9e2d3419a75c42510e6fc19a0bc0200cc
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 <string>
7 #include "base/basictypes.h"
8 #include "base/bind.h"
9 #include "base/bind_helpers.h"
10 #include "base/callback.h"
11 #include "base/file_util.h"
12 #include "base/files/file_enumerator.h"
13 #include "base/files/file_path.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/test/thread_test_helper.h"
17 #include "base/threading/sequenced_worker_pool.h"
18 #include "chrome/browser/browsing_data/browsing_data_helper_browsertest.h"
19 #include "chrome/browser/browsing_data/browsing_data_local_storage_helper.h"
20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/ui/browser.h"
22 #include "chrome/test/base/in_process_browser_test.h"
23 #include "chrome/test/base/ui_test_utils.h"
24 #include "content/public/browser/dom_storage_context.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 using content::BrowserContext;
28 using content::BrowserThread;
29 using content::DOMStorageContext;
31 namespace {
32 typedef
33 BrowsingDataHelperCallback<BrowsingDataLocalStorageHelper::LocalStorageInfo>
34 TestCompletionCallback;
36 const base::FilePath::CharType kTestFile0[] =
37 FILE_PATH_LITERAL("http_www.chromium.org_0.localstorage");
39 const char kOriginOfTestFile0[] = "http://www.chromium.org/";
41 const base::FilePath::CharType kTestFile1[] =
42 FILE_PATH_LITERAL("http_www.google.com_0.localstorage");
44 const base::FilePath::CharType kTestFileInvalid[] =
45 FILE_PATH_LITERAL("http_www.google.com_localstorage_0.foo");
47 // This is only here to test that extension state is not listed by the helper.
48 const base::FilePath::CharType kTestFileExtension[] = FILE_PATH_LITERAL(
49 "chrome-extension_behllobkkfkfnphdnhnkndlbkcpglgmj_0.localstorage");
51 class BrowsingDataLocalStorageHelperTest : public InProcessBrowserTest {
52 protected:
53 void CreateLocalStorageFilesForTest() {
54 // Note: This helper depends on details of how the dom_storage library
55 // stores data in the host file system.
56 base::FilePath storage_path = GetLocalStoragePathForTestingProfile();
57 base::CreateDirectory(storage_path);
58 const base::FilePath::CharType* kFilesToCreate[] = {
59 kTestFile0, kTestFile1, kTestFileInvalid, kTestFileExtension
61 for (size_t i = 0; i < arraysize(kFilesToCreate); ++i) {
62 base::FilePath file_path = storage_path.Append(kFilesToCreate[i]);
63 file_util::WriteFile(file_path, NULL, 0);
67 base::FilePath GetLocalStoragePathForTestingProfile() {
68 return browser()->profile()->GetPath().AppendASCII("Local Storage");
72 // This class is notified by BrowsingDataLocalStorageHelper on the UI thread
73 // once it finishes fetching the local storage data.
74 class StopTestOnCallback {
75 public:
76 explicit StopTestOnCallback(
77 BrowsingDataLocalStorageHelper* local_storage_helper)
78 : local_storage_helper_(local_storage_helper) {
79 DCHECK(local_storage_helper_);
82 void Callback(
83 const std::list<BrowsingDataLocalStorageHelper::LocalStorageInfo>&
84 local_storage_info) {
85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
86 // There's no guarantee on the order, ensure these files are there.
87 const char* const kTestHosts[] = {"www.chromium.org", "www.google.com"};
88 bool test_hosts_found[arraysize(kTestHosts)] = {false, false};
89 ASSERT_EQ(arraysize(kTestHosts), local_storage_info.size());
90 typedef std::list<BrowsingDataLocalStorageHelper::LocalStorageInfo>
91 LocalStorageInfoList;
92 for (size_t i = 0; i < arraysize(kTestHosts); ++i) {
93 for (LocalStorageInfoList::const_iterator info =
94 local_storage_info.begin(); info != local_storage_info.end();
95 ++info) {
96 ASSERT_TRUE(info->origin_url.SchemeIs("http"));
97 if (info->origin_url.host() == kTestHosts[i]) {
98 ASSERT_FALSE(test_hosts_found[i]);
99 test_hosts_found[i] = true;
103 for (size_t i = 0; i < arraysize(kTestHosts); ++i) {
104 ASSERT_TRUE(test_hosts_found[i]) << kTestHosts[i];
106 base::MessageLoop::current()->Quit();
109 private:
110 BrowsingDataLocalStorageHelper* local_storage_helper_;
113 IN_PROC_BROWSER_TEST_F(BrowsingDataLocalStorageHelperTest, CallbackCompletes) {
114 scoped_refptr<BrowsingDataLocalStorageHelper> local_storage_helper(
115 new BrowsingDataLocalStorageHelper(browser()->profile()));
116 CreateLocalStorageFilesForTest();
117 StopTestOnCallback stop_test_on_callback(local_storage_helper.get());
118 local_storage_helper->StartFetching(base::Bind(
119 &StopTestOnCallback::Callback, base::Unretained(&stop_test_on_callback)));
120 // Blocks until StopTestOnCallback::Callback is notified.
121 content::RunMessageLoop();
124 IN_PROC_BROWSER_TEST_F(BrowsingDataLocalStorageHelperTest, DeleteSingleFile) {
125 scoped_refptr<BrowsingDataLocalStorageHelper> local_storage_helper(
126 new BrowsingDataLocalStorageHelper(browser()->profile()));
127 CreateLocalStorageFilesForTest();
128 local_storage_helper->DeleteOrigin(GURL(kOriginOfTestFile0));
129 BrowserThread::GetBlockingPool()->FlushForTesting();
131 // Ensure the file has been deleted.
132 base::FileEnumerator file_enumerator(
133 GetLocalStoragePathForTestingProfile(),
134 false,
135 base::FileEnumerator::FILES);
136 int num_files = 0;
137 for (base::FilePath file_path = file_enumerator.Next();
138 !file_path.empty();
139 file_path = file_enumerator.Next()) {
140 ASSERT_FALSE(base::FilePath(kTestFile0) == file_path.BaseName());
141 ++num_files;
143 ASSERT_EQ(3, num_files);
146 IN_PROC_BROWSER_TEST_F(BrowsingDataLocalStorageHelperTest,
147 CannedAddLocalStorage) {
148 const GURL origin1("http://host1:1/");
149 const GURL origin2("http://host2:1/");
151 scoped_refptr<CannedBrowsingDataLocalStorageHelper> helper(
152 new CannedBrowsingDataLocalStorageHelper(browser()->profile()));
153 helper->AddLocalStorage(origin1);
154 helper->AddLocalStorage(origin2);
156 TestCompletionCallback callback;
157 helper->StartFetching(
158 base::Bind(&TestCompletionCallback::callback,
159 base::Unretained(&callback)));
161 std::list<BrowsingDataLocalStorageHelper::LocalStorageInfo> result =
162 callback.result();
164 ASSERT_EQ(2u, result.size());
165 std::list<BrowsingDataLocalStorageHelper::LocalStorageInfo>::iterator info =
166 result.begin();
167 EXPECT_EQ(origin1, info->origin_url);
168 info++;
169 EXPECT_EQ(origin2, info->origin_url);
172 IN_PROC_BROWSER_TEST_F(BrowsingDataLocalStorageHelperTest, CannedUnique) {
173 const GURL origin("http://host1:1/");
175 scoped_refptr<CannedBrowsingDataLocalStorageHelper> helper(
176 new CannedBrowsingDataLocalStorageHelper(browser()->profile()));
177 helper->AddLocalStorage(origin);
178 helper->AddLocalStorage(origin);
180 TestCompletionCallback callback;
181 helper->StartFetching(
182 base::Bind(&TestCompletionCallback::callback,
183 base::Unretained(&callback)));
185 std::list<BrowsingDataLocalStorageHelper::LocalStorageInfo> result =
186 callback.result();
188 ASSERT_EQ(1u, result.size());
189 EXPECT_EQ(origin, result.begin()->origin_url);
191 } // namespace