OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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/callback.h" | |
9 #include "base/file_path.h" | |
10 #include "base/memory/ref_counted.h" | |
11 #include "base/message_loop.h" | |
12 #include "chrome/browser/browsing_data_file_system_helper.h" | |
13 #include "chrome/browser/browsing_data_helper_browsertest.h" | |
14 #include "chrome/test/in_process_browser_test.h" | |
15 #include "chrome/test/testing_profile.h" | |
16 #include "chrome/test/ui_test_utils.h" | |
17 #include "content/browser/browser_thread.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 #include "webkit/fileapi/file_system_context.h" | |
20 #include "webkit/fileapi/file_system_path_manager.h" | |
21 #include "webkit/fileapi/file_system_types.h" | |
22 #include "webkit/fileapi/file_system_usage_cache.h" | |
23 #include "webkit/fileapi/sandbox_mount_point_provider.h" | |
24 | |
25 namespace { | |
26 | |
27 typedef BrowsingDataHelperCallback<BrowsingDataFileSystemHelper::FileSystemInfo> | |
28 TestCompletionCallback; | |
29 | |
30 const char kTestOrigin1[] = "http://host1:1/"; | |
31 const char kTestOrigin2[] = "http://host2:1/"; | |
32 const char kTestOrigin3[] = "http://host3:1/"; | |
33 | |
34 const char kTestOriginExtension[] = | |
35 "chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj:1/"; | |
36 | |
37 const int kEmptyFileSystemSize = fileapi::FileSystemUsageCache::kUsageFileSize; | |
38 | |
39 class BrowsingDataFileSystemHelperTest : public InProcessBrowserTest { | |
40 public: | |
41 virtual void PopulateTestData() { | |
42 const GURL origin1(kTestOrigin1); | |
43 const GURL origin2(kTestOrigin2); | |
44 const GURL origin3(kTestOrigin3); | |
45 | |
46 sandbox_ = testing_profile_.GetFileSystemContext()-> | |
47 path_manager()->sandbox_provider(); | |
48 | |
49 CreateDirectoryForOriginAndType(origin1, fileapi::kFileSystemTypeTemporary); | |
50 CreateDirectoryForOriginAndType(origin2, | |
51 fileapi::kFileSystemTypePersistent); | |
52 CreateDirectoryForOriginAndType(origin3, fileapi::kFileSystemTypeTemporary); | |
53 CreateDirectoryForOriginAndType(origin3, | |
54 fileapi::kFileSystemTypePersistent); | |
55 } | |
56 | |
57 protected: | |
58 void CreateDirectoryForOriginAndType(const GURL& origin, | |
59 fileapi::FileSystemType type) { | |
60 FilePath target = sandbox_->ValidateFileSystemRootAndGetPathOnFileThread( | |
61 origin, type, FilePath(), true); | |
62 ASSERT_TRUE(file_util::CreateDirectory(target)); | |
63 } | |
64 | |
65 TestingProfile testing_profile_; | |
66 fileapi::SandboxMountPointProvider* sandbox_; | |
67 }; | |
68 | |
69 // Called back by BrowsingDataFileSystemHelper on the UI thread once the | |
70 // database information has been retrieved. | |
71 class StopTestOnCallback { | |
72 public: | |
73 explicit StopTestOnCallback( | |
74 BrowsingDataFileSystemHelper* file_system_helper) | |
75 : file_system_helper_(file_system_helper) { | |
76 DCHECK(file_system_helper_); | |
77 } | |
78 | |
79 void CallbackFetchData( | |
80 const std::vector<BrowsingDataFileSystemHelper::FileSystemInfo>& | |
81 file_system_info_list) { | |
82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
83 EXPECT_EQ(3UL, file_system_info_list.size()); | |
84 | |
85 // Order is arbitrary, verify all three origins. | |
86 bool test_hosts_found[3] = {false, false, false}; | |
87 for (size_t i = 0; i < file_system_info_list.size(); i++) { | |
88 BrowsingDataFileSystemHelper::FileSystemInfo info = | |
89 file_system_info_list.at(i); | |
90 if (info.origin == GURL(kTestOrigin1)) { | |
91 test_hosts_found[0] = true; | |
92 EXPECT_FALSE(info.has_persistent); | |
93 EXPECT_TRUE(info.has_temporary); | |
94 EXPECT_EQ(0, info.usage_persistent); | |
95 EXPECT_EQ(kEmptyFileSystemSize, info.usage_temporary); | |
96 } else if (info.origin == GURL(kTestOrigin2)) { | |
97 test_hosts_found[1] = true; | |
98 EXPECT_TRUE(info.has_persistent); | |
99 EXPECT_FALSE(info.has_temporary); | |
100 EXPECT_EQ(kEmptyFileSystemSize, info.usage_persistent); | |
101 EXPECT_EQ(0, info.usage_temporary); | |
102 } else if (info.origin == GURL(kTestOrigin3)) { | |
103 test_hosts_found[2] = true; | |
104 EXPECT_TRUE(info.has_persistent); | |
105 EXPECT_TRUE(info.has_temporary); | |
106 EXPECT_EQ(kEmptyFileSystemSize, info.usage_persistent); | |
107 EXPECT_EQ(kEmptyFileSystemSize, info.usage_temporary); | |
108 } else { | |
109 ADD_FAILURE() << info.origin.spec() << " isn't an origin we added."; | |
110 } | |
111 } | |
112 for (size_t i = 0; i < arraysize(test_hosts_found); i++) { | |
113 EXPECT_TRUE(test_hosts_found[i]); | |
114 } | |
115 MessageLoop::current()->Quit(); | |
116 } | |
117 | |
118 void CallbackDeleteData( | |
119 const std::vector<BrowsingDataFileSystemHelper::FileSystemInfo>& | |
120 file_system_info_list) { | |
121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
122 EXPECT_EQ(3UL, file_system_info_list.size()); | |
123 for (size_t i = 0; i < file_system_info_list.size(); ++i) { | |
124 BrowsingDataFileSystemHelper::FileSystemInfo info = | |
125 file_system_info_list.at(0); | |
126 if (info.origin == GURL(kTestOrigin3)) { | |
127 EXPECT_TRUE(info.has_persistent); | |
128 EXPECT_TRUE(info.has_temporary); | |
129 EXPECT_EQ(kEmptyFileSystemSize, info.usage_persistent); | |
130 EXPECT_EQ(kEmptyFileSystemSize, info.usage_temporary); | |
131 } else { | |
132 EXPECT_FALSE(info.has_persistent); | |
133 EXPECT_FALSE(info.has_temporary); | |
134 } | |
135 } | |
136 MessageLoop::current()->Quit(); | |
137 } | |
138 | |
139 private: | |
140 BrowsingDataFileSystemHelper* file_system_helper_; | |
141 }; | |
142 | |
143 | |
144 IN_PROC_BROWSER_TEST_F(BrowsingDataFileSystemHelperTest, FetchData) { | |
145 PopulateTestData(); | |
146 scoped_refptr<BrowsingDataFileSystemHelper> file_system_helper( | |
147 BrowsingDataFileSystemHelper::Create(&testing_profile_)); | |
148 StopTestOnCallback stop_test_on_callback(file_system_helper); | |
149 file_system_helper->StartFetching( | |
150 NewCallback(&stop_test_on_callback, | |
151 &StopTestOnCallback::CallbackFetchData)); | |
152 // Blocks until StopTestOnCallback::CallbackFetchData is notified. | |
153 ui_test_utils::RunMessageLoop(); | |
154 } | |
155 | |
156 IN_PROC_BROWSER_TEST_F(BrowsingDataFileSystemHelperTest, DeleteData) { | |
157 PopulateTestData(); | |
158 scoped_refptr<BrowsingDataFileSystemHelper> file_system_helper( | |
159 BrowsingDataFileSystemHelper::Create(&testing_profile_)); | |
160 StopTestOnCallback stop_test_on_callback(file_system_helper); | |
161 file_system_helper->DeleteFileSystemOrigin(GURL(kTestOrigin1)); | |
162 file_system_helper->DeleteFileSystemOrigin(GURL(kTestOrigin2)); | |
163 file_system_helper->StartFetching( | |
164 NewCallback(&stop_test_on_callback, | |
165 &StopTestOnCallback::CallbackDeleteData)); | |
166 // Blocks until StopTestOnCallback::CallbackDeleteData is notified. | |
167 ui_test_utils::RunMessageLoop(); | |
168 } | |
169 | |
170 IN_PROC_BROWSER_TEST_F(BrowsingDataFileSystemHelperTest, CannedAddFileSystem) { | |
171 const GURL origin1(kTestOrigin1); | |
172 const GURL origin2(kTestOrigin2); | |
173 | |
174 scoped_refptr<CannedBrowsingDataFileSystemHelper> helper( | |
175 new CannedBrowsingDataFileSystemHelper(&testing_profile_)); | |
176 helper->AddFileSystem(origin1, fileapi::kFileSystemTypePersistent, 200); | |
177 helper->AddFileSystem(origin2, fileapi::kFileSystemTypeTemporary, 100); | |
178 | |
179 TestCompletionCallback callback; | |
180 helper->StartFetching( | |
181 NewCallback(&callback, &TestCompletionCallback::callback)); | |
182 | |
183 std::vector<BrowsingDataFileSystemHelper::FileSystemInfo> result = | |
184 callback.result(); | |
185 | |
186 EXPECT_EQ(2U, result.size()); | |
187 EXPECT_EQ(origin1, result[0].origin); | |
188 EXPECT_TRUE(result[0].has_persistent); | |
189 EXPECT_FALSE(result[0].has_temporary); | |
190 EXPECT_EQ(200, result[0].usage_persistent); | |
191 EXPECT_EQ(0, result[0].usage_temporary); | |
192 EXPECT_EQ(origin2, result[1].origin); | |
193 EXPECT_FALSE(result[1].has_persistent); | |
194 EXPECT_TRUE(result[1].has_temporary); | |
195 EXPECT_EQ(0, result[1].usage_persistent); | |
196 EXPECT_EQ(100, result[1].usage_temporary); | |
197 } | |
198 | |
199 IN_PROC_BROWSER_TEST_F(BrowsingDataFileSystemHelperTest, CannedUnique) { | |
200 const GURL origin3(kTestOrigin3); | |
201 | |
202 scoped_refptr<CannedBrowsingDataFileSystemHelper> helper( | |
203 new CannedBrowsingDataFileSystemHelper(&testing_profile_)); | |
204 helper->AddFileSystem(origin3, fileapi::kFileSystemTypePersistent, 200); | |
205 helper->AddFileSystem(origin3, fileapi::kFileSystemTypeTemporary, 100); | |
206 | |
207 TestCompletionCallback callback; | |
208 helper->StartFetching( | |
209 NewCallback(&callback, &TestCompletionCallback::callback)); | |
210 | |
211 std::vector<BrowsingDataFileSystemHelper::FileSystemInfo> result = | |
212 callback.result(); | |
213 | |
214 EXPECT_EQ(1U, result.size()); | |
215 EXPECT_EQ(origin3, result[0].origin); | |
216 EXPECT_TRUE(result[0].has_persistent); | |
217 EXPECT_TRUE(result[0].has_temporary); | |
218 EXPECT_EQ(200, result[0].usage_persistent); | |
219 EXPECT_EQ(100, result[0].usage_temporary); | |
220 } | |
221 | |
222 } // namespace | |
OLD | NEW |