OLD | NEW |
---|---|
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
11 #include "base/ref_counted.h" | 11 #include "base/ref_counted.h" |
12 #include "chrome/browser/in_process_webkit/webkit_context.h" | 12 #include "chrome/browser/in_process_webkit/webkit_context.h" |
13 #include "chrome/browser/in_process_webkit/webkit_thread.h" | 13 #include "chrome/browser/in_process_webkit/webkit_thread.h" |
14 #include "chrome/browser/browsing_data_local_storage_helper.h" | 14 #include "chrome/browser/browsing_data_local_storage_helper.h" |
15 #include "chrome/test/in_process_browser_test.h" | 15 #include "chrome/test/in_process_browser_test.h" |
16 #include "chrome/test/testing_profile.h" | 16 #include "chrome/test/testing_profile.h" |
17 #include "chrome/test/thread_test_helper.h" | 17 #include "chrome/test/thread_test_helper.h" |
18 #include "chrome/test/ui_test_utils.h" | 18 #include "chrome/test/ui_test_utils.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
20 | 20 |
21 static const FilePath::CharType kTestFile0[] = | 21 namespace { |
22 class TestCompletionCallback { | |
23 public: | |
24 TestCompletionCallback() | |
25 : have_result_(false), | |
26 waiting_for_result_(false) { | |
27 } | |
28 | |
29 const std::vector<BrowsingDataLocalStorageHelper::LocalStorageInfo>& result() | |
30 { | |
31 WaitForResult(); | |
32 return result_; | |
33 } | |
34 | |
35 void callback(const std::vector< | |
36 BrowsingDataLocalStorageHelper::LocalStorageInfo>& info) { | |
37 have_result_ = true; | |
38 result_ = info; | |
39 if (waiting_for_result_) | |
40 MessageLoop::current()->Quit(); | |
41 } | |
42 | |
43 private: | |
44 void WaitForResult() { | |
45 while (!have_result_) { | |
Paweł Hajdan Jr.
2011/02/05 12:44:51
Is this loop needed? Generally we should only run
| |
46 waiting_for_result_ = true; | |
47 MessageLoop::current()->Run(); | |
48 waiting_for_result_ = false; | |
49 } | |
50 } | |
51 | |
52 bool have_result_; | |
53 bool waiting_for_result_; | |
54 std::vector<BrowsingDataLocalStorageHelper::LocalStorageInfo> result_; | |
55 | |
56 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallback); | |
57 }; | |
58 | |
59 const FilePath::CharType kTestFile0[] = | |
22 FILE_PATH_LITERAL("http_www.chromium.org_0.localstorage"); | 60 FILE_PATH_LITERAL("http_www.chromium.org_0.localstorage"); |
23 | 61 |
24 static const FilePath::CharType kTestFile1[] = | 62 const FilePath::CharType kTestFile1[] = |
25 FILE_PATH_LITERAL("http_www.google.com_0.localstorage"); | 63 FILE_PATH_LITERAL("http_www.google.com_0.localstorage"); |
26 | 64 |
27 static const FilePath::CharType kTestFileInvalid[] = | 65 const FilePath::CharType kTestFileInvalid[] = |
28 FILE_PATH_LITERAL("http_www.google.com_localstorage_0.foo"); | 66 FILE_PATH_LITERAL("http_www.google.com_localstorage_0.foo"); |
29 | 67 |
30 // This is only here to test that extension state is not listed by the helper. | 68 // This is only here to test that extension state is not listed by the helper. |
31 static const FilePath::CharType kTestFileExtension[] = FILE_PATH_LITERAL( | 69 const FilePath::CharType kTestFileExtension[] = FILE_PATH_LITERAL( |
32 "chrome-extension_behllobkkfkfnphdnhnkndlbkcpglgmj_0.localstorage"); | 70 "chrome-extension_behllobkkfkfnphdnhnkndlbkcpglgmj_0.localstorage"); |
71 } // namespace | |
33 | 72 |
34 | 73 |
35 class BrowsingDataLocalStorageHelperTest : public InProcessBrowserTest { | 74 class BrowsingDataLocalStorageHelperTest : public InProcessBrowserTest { |
36 protected: | 75 protected: |
37 void CreateLocalStorageFilesForTest() { | 76 void CreateLocalStorageFilesForTest() { |
38 FilePath storage_path = GetLocalStoragePathForTestingProfile(); | 77 FilePath storage_path = GetLocalStoragePathForTestingProfile(); |
39 file_util::CreateDirectory(storage_path); | 78 file_util::CreateDirectory(storage_path); |
40 const FilePath::CharType* kFilesToCreate[] = { | 79 const FilePath::CharType* kFilesToCreate[] = { |
41 kTestFile0, kTestFile1, kTestFileInvalid, kTestFileExtension | 80 kTestFile0, kTestFile1, kTestFileInvalid, kTestFileExtension |
42 }; | 81 }; |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
121 file_util::FileEnumerator::FILES); | 160 file_util::FileEnumerator::FILES); |
122 int num_files = 0; | 161 int num_files = 0; |
123 for (FilePath file_path = file_enumerator.Next(); | 162 for (FilePath file_path = file_enumerator.Next(); |
124 !file_path.empty(); | 163 !file_path.empty(); |
125 file_path = file_enumerator.Next()) { | 164 file_path = file_enumerator.Next()) { |
126 ASSERT_FALSE(FilePath(kTestFile0) == file_path.BaseName()); | 165 ASSERT_FALSE(FilePath(kTestFile0) == file_path.BaseName()); |
127 ++num_files; | 166 ++num_files; |
128 } | 167 } |
129 ASSERT_EQ(3, num_files); | 168 ASSERT_EQ(3, num_files); |
130 } | 169 } |
170 | |
171 IN_PROC_BROWSER_TEST_F(BrowsingDataLocalStorageHelperTest, | |
172 CannedAddLocalStorage) { | |
173 const GURL origin1("http://host1:1/"); | |
174 const GURL origin2("http://host2:1/"); | |
175 const FilePath::CharType file1[] = | |
176 FILE_PATH_LITERAL("http_host1_1.localstorage"); | |
177 const FilePath::CharType file2[] = | |
178 FILE_PATH_LITERAL("http_host2_1.localstorage"); | |
179 | |
180 scoped_refptr<CannedBrowsingDataLocalStorageHelper> helper( | |
181 new CannedBrowsingDataLocalStorageHelper(&testing_profile_)); | |
182 helper->AddLocalStorage(origin1); | |
183 helper->AddLocalStorage(origin2); | |
184 | |
185 TestCompletionCallback callback; | |
186 helper->StartFetching( | |
187 NewCallback(&callback, &TestCompletionCallback::callback)); | |
188 | |
189 std::vector<BrowsingDataLocalStorageHelper::LocalStorageInfo> result = | |
190 callback.result(); | |
191 | |
192 ASSERT_EQ(2u, result.size()); | |
193 EXPECT_EQ(FilePath(file1).value(), result[0].file_path.BaseName().value()); | |
194 EXPECT_EQ(FilePath(file2).value(), result[1].file_path.BaseName().value()); | |
195 } | |
196 | |
197 IN_PROC_BROWSER_TEST_F(BrowsingDataLocalStorageHelperTest, CannedUnique) { | |
198 const GURL origin("http://host1:1/"); | |
199 const FilePath::CharType file[] = | |
200 FILE_PATH_LITERAL("http_host1_1.localstorage"); | |
201 | |
202 scoped_refptr<CannedBrowsingDataLocalStorageHelper> helper( | |
203 new CannedBrowsingDataLocalStorageHelper(&testing_profile_)); | |
204 helper->AddLocalStorage(origin); | |
205 helper->AddLocalStorage(origin); | |
206 | |
207 TestCompletionCallback callback; | |
208 helper->StartFetching( | |
209 NewCallback(&callback, &TestCompletionCallback::callback)); | |
210 | |
211 std::vector<BrowsingDataLocalStorageHelper::LocalStorageInfo> result = | |
212 callback.result(); | |
213 | |
214 ASSERT_EQ(1u, result.size()); | |
215 EXPECT_EQ(FilePath(file).value(), result[0].file_path.BaseName().value()); | |
216 } | |
217 | |
OLD | NEW |