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