Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(344)

Side by Side Diff: chrome/browser/browsing_data_file_system_helper_unittest.cc

Issue 10805015: Move browsing_data_helper files into a separate directory. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix chrome_frame build Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 "testing/gtest/include/gtest/gtest.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/file_util.h"
10 #include "base/platform_file.h"
11 #include "base/message_loop.h"
12 #include "base/utf_string_conversions.h"
13 #include "chrome/browser/browsing_data_file_system_helper.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "content/public/test/test_browser_thread.h"
16 #include "webkit/fileapi/file_system_context.h"
17 #include "webkit/fileapi/file_system_types.h"
18 #include "webkit/fileapi/file_system_usage_cache.h"
19 #include "webkit/fileapi/sandbox_mount_point_provider.h"
20
21 using content::BrowserContext;
22 using content::BrowserThread;
23
24 namespace {
25
26 // Shorter names for fileapi::* constants.
27 const fileapi::FileSystemType kTemporary = fileapi::kFileSystemTypeTemporary;
28 const fileapi::FileSystemType kPersistent = fileapi::kFileSystemTypePersistent;
29
30 // We'll use these three distinct origins for testing, both as strings and as
31 // GURLs in appropriate contexts.
32 const char kTestOrigin1[] = "http://host1:1/";
33 const char kTestOrigin2[] = "http://host2:2/";
34 const char kTestOrigin3[] = "http://host3:3/";
35
36 // Extensions and Devtools should be ignored.
37 const char kTestOriginExt[] = "chrome-extension://abcdefghijklmnopqrstuvwxyz/";
38 const char kTestOriginDevTools[] = "chrome-devtools://abcdefghijklmnopqrstuvw/";
39
40 const GURL kOrigin1(kTestOrigin1);
41 const GURL kOrigin2(kTestOrigin2);
42 const GURL kOrigin3(kTestOrigin3);
43 const GURL kOriginExt(kTestOriginExt);
44 const GURL kOriginDevTools(kTestOriginDevTools);
45
46 // TODO(mkwst): Update this size once the discussion in http://crbug.com/86114
47 // is concluded.
48 const int kEmptyFileSystemSize = 0;
49
50 typedef std::list<BrowsingDataFileSystemHelper::FileSystemInfo>
51 FileSystemInfoList;
52 typedef scoped_ptr<FileSystemInfoList> ScopedFileSystemInfoList;
53
54 // The FileSystem APIs are all asynchronous; this testing class wraps up the
55 // boilerplate code necessary to deal with waiting for responses. In a nutshell,
56 // any async call whose response we want to test ought to be followed by a call
57 // to BlockUntilNotified(), which will (shockingly!) block until Notify() is
58 // called. For this to work, you'll need to ensure that each async call is
59 // implemented as a class method that that calls Notify() at an appropriate
60 // point.
61 class BrowsingDataFileSystemHelperTest : public testing::Test {
62 public:
63 BrowsingDataFileSystemHelperTest()
64 : ui_thread_(BrowserThread::UI, &message_loop_),
65 db_thread_(BrowserThread::DB, &message_loop_),
66 webkit_thread_(BrowserThread::WEBKIT_DEPRECATED, &message_loop_),
67 file_thread_(BrowserThread::FILE, &message_loop_),
68 file_user_blocking_thread_(
69 BrowserThread::FILE_USER_BLOCKING, &message_loop_),
70 io_thread_(BrowserThread::IO, &message_loop_) {
71 profile_.reset(new TestingProfile());
72 helper_ = BrowsingDataFileSystemHelper::Create(profile_.get());
73 message_loop_.RunAllPending();
74 canned_helper_ = new CannedBrowsingDataFileSystemHelper(profile_.get());
75 }
76 virtual ~BrowsingDataFileSystemHelperTest() {
77 // Avoid memory leaks.
78 profile_.reset();
79 message_loop_.RunAllPending();
80 }
81
82 TestingProfile* GetProfile() {
83 return profile_.get();
84 }
85
86 // Blocks on the current MessageLoop until Notify() is called.
87 void BlockUntilNotified() {
88 MessageLoop::current()->Run();
89 }
90
91 // Unblocks the current MessageLoop. Should be called in response to some sort
92 // of async activity in a callback method.
93 void Notify() {
94 MessageLoop::current()->Quit();
95 }
96
97 // Callback that should be executed in response to
98 // fileapi::SandboxMountPointProvider::ValidateFileSystemRoot
99 void ValidateFileSystemCallback(base::PlatformFileError error) {
100 validate_file_system_result_ = error;
101 Notify();
102 }
103
104 // Calls fileapi::SandboxMountPointProvider::ValidateFileSystemRootAndGetURL
105 // to verify the existence of a file system for a specified type and origin,
106 // blocks until a response is available, then returns the result
107 // synchronously to it's caller.
108 bool FileSystemContainsOriginAndType(const GURL& origin,
109 fileapi::FileSystemType type) {
110 sandbox_->ValidateFileSystemRoot(
111 origin, type, false,
112 base::Bind(
113 &BrowsingDataFileSystemHelperTest::ValidateFileSystemCallback,
114 base::Unretained(this)));
115 BlockUntilNotified();
116 return validate_file_system_result_ == base::PLATFORM_FILE_OK;
117 }
118
119 // Callback that should be executed in response to StartFetching(), and stores
120 // found file systems locally so that they are available via GetFileSystems().
121 void CallbackStartFetching(
122 const std::list<BrowsingDataFileSystemHelper::FileSystemInfo>&
123 file_system_info_list) {
124 file_system_info_list_.reset(
125 new std::list<BrowsingDataFileSystemHelper::FileSystemInfo>(
126 file_system_info_list));
127 Notify();
128 }
129
130 // Calls StartFetching() on the test's BrowsingDataFileSystemHelper
131 // object, then blocks until the callback is executed.
132 void FetchFileSystems() {
133 helper_->StartFetching(
134 base::Bind(&BrowsingDataFileSystemHelperTest::CallbackStartFetching,
135 base::Unretained(this)));
136 BlockUntilNotified();
137 }
138
139 // Calls StartFetching() on the test's CannedBrowsingDataFileSystemHelper
140 // object, then blocks until the callback is executed.
141 void FetchCannedFileSystems() {
142 canned_helper_->StartFetching(
143 base::Bind(&BrowsingDataFileSystemHelperTest::CallbackStartFetching,
144 base::Unretained(this)));
145 BlockUntilNotified();
146 }
147
148 // Sets up kOrigin1 with a temporary file system, kOrigin2 with a persistent
149 // file system, and kOrigin3 with both.
150 virtual void PopulateTestFileSystemData() {
151 sandbox_ = BrowserContext::GetFileSystemContext(profile_.get())->
152 sandbox_provider();
153
154 CreateDirectoryForOriginAndType(kOrigin1, kTemporary);
155 CreateDirectoryForOriginAndType(kOrigin2, kPersistent);
156 CreateDirectoryForOriginAndType(kOrigin3, kTemporary);
157 CreateDirectoryForOriginAndType(kOrigin3, kPersistent);
158
159 EXPECT_FALSE(FileSystemContainsOriginAndType(kOrigin1, kPersistent));
160 EXPECT_TRUE(FileSystemContainsOriginAndType(kOrigin1, kTemporary));
161 EXPECT_TRUE(FileSystemContainsOriginAndType(kOrigin2, kPersistent));
162 EXPECT_FALSE(FileSystemContainsOriginAndType(kOrigin2, kTemporary));
163 EXPECT_TRUE(FileSystemContainsOriginAndType(kOrigin3, kPersistent));
164 EXPECT_TRUE(FileSystemContainsOriginAndType(kOrigin3, kTemporary));
165 }
166
167 // Uses the fileapi methods to create a filesystem of a given type for a
168 // specified origin.
169 void CreateDirectoryForOriginAndType(const GURL& origin,
170 fileapi::FileSystemType type) {
171 FilePath target = sandbox_->GetFileSystemRootPathOnFileThread(
172 origin, type, FilePath(), true);
173 EXPECT_TRUE(file_util::DirectoryExists(target));
174 }
175
176 // Returns a list of the FileSystemInfo objects gathered in the most recent
177 // call to StartFetching().
178 FileSystemInfoList* GetFileSystems() {
179 return file_system_info_list_.get();
180 }
181
182
183 // Temporary storage to pass information back from callbacks.
184 base::PlatformFileError validate_file_system_result_;
185 ScopedFileSystemInfoList file_system_info_list_;
186
187 scoped_refptr<BrowsingDataFileSystemHelper> helper_;
188 scoped_refptr<CannedBrowsingDataFileSystemHelper> canned_helper_;
189
190 private:
191 // message_loop_, as well as all the threads associated with it must be
192 // defined before profile_ to prevent explosions. The threads also must be
193 // defined in the order they're listed here. Oh how I love C++.
194 MessageLoopForUI message_loop_;
195 content::TestBrowserThread ui_thread_;
196 content::TestBrowserThread db_thread_;
197 content::TestBrowserThread webkit_thread_;
198 content::TestBrowserThread file_thread_;
199 content::TestBrowserThread file_user_blocking_thread_;
200 content::TestBrowserThread io_thread_;
201 scoped_ptr<TestingProfile> profile_;
202
203 // We don't own this pointer: don't delete it.
204 fileapi::SandboxMountPointProvider* sandbox_;
205
206 DISALLOW_COPY_AND_ASSIGN(BrowsingDataFileSystemHelperTest);
207 };
208
209 // Verifies that the BrowsingDataFileSystemHelper correctly finds the test file
210 // system data, and that each file system returned contains the expected data.
211 TEST_F(BrowsingDataFileSystemHelperTest, FetchData) {
212 PopulateTestFileSystemData();
213
214 FetchFileSystems();
215
216 EXPECT_EQ(3UL, file_system_info_list_->size());
217
218 // Order is arbitrary, verify all three origins.
219 bool test_hosts_found[3] = {false, false, false};
220 for (std::list<BrowsingDataFileSystemHelper::FileSystemInfo>::iterator info =
221 file_system_info_list_->begin(); info != file_system_info_list_->end();
222 ++info) {
223 if (info->origin == kOrigin1) {
224 EXPECT_FALSE(test_hosts_found[0]);
225 test_hosts_found[0] = true;
226 EXPECT_FALSE(info->has_persistent);
227 EXPECT_TRUE(info->has_temporary);
228 EXPECT_EQ(0, info->usage_persistent);
229 EXPECT_EQ(kEmptyFileSystemSize, info->usage_temporary);
230 } else if (info->origin == kOrigin2) {
231 EXPECT_FALSE(test_hosts_found[1]);
232 test_hosts_found[1] = true;
233 EXPECT_TRUE(info->has_persistent);
234 EXPECT_FALSE(info->has_temporary);
235 EXPECT_EQ(kEmptyFileSystemSize, info->usage_persistent);
236 EXPECT_EQ(0, info->usage_temporary);
237 } else if (info->origin == kOrigin3) {
238 EXPECT_FALSE(test_hosts_found[2]);
239 test_hosts_found[2] = true;
240 EXPECT_TRUE(info->has_persistent);
241 EXPECT_TRUE(info->has_temporary);
242 EXPECT_EQ(kEmptyFileSystemSize, info->usage_persistent);
243 EXPECT_EQ(kEmptyFileSystemSize, info->usage_temporary);
244 } else {
245 ADD_FAILURE() << info->origin.spec() << " isn't an origin we added.";
246 }
247 }
248 for (size_t i = 0; i < arraysize(test_hosts_found); i++) {
249 EXPECT_TRUE(test_hosts_found[i]);
250 }
251 }
252
253 // Verifies that the BrowsingDataFileSystemHelper correctly deletes file
254 // systems via DeleteFileSystemOrigin().
255 TEST_F(BrowsingDataFileSystemHelperTest, DeleteData) {
256 PopulateTestFileSystemData();
257
258 helper_->DeleteFileSystemOrigin(kOrigin1);
259 helper_->DeleteFileSystemOrigin(kOrigin2);
260
261 FetchFileSystems();
262
263 EXPECT_EQ(1UL, file_system_info_list_->size());
264 BrowsingDataFileSystemHelper::FileSystemInfo info =
265 *(file_system_info_list_->begin());
266 EXPECT_EQ(kOrigin3, info.origin);
267 EXPECT_TRUE(info.has_persistent);
268 EXPECT_TRUE(info.has_temporary);
269 EXPECT_EQ(kEmptyFileSystemSize, info.usage_persistent);
270 EXPECT_EQ(kEmptyFileSystemSize, info.usage_temporary);
271 }
272
273 // Verifies that the CannedBrowsingDataFileSystemHelper correctly reports
274 // whether or not it currently contains file systems.
275 TEST_F(BrowsingDataFileSystemHelperTest, Empty) {
276 ASSERT_TRUE(canned_helper_->empty());
277 canned_helper_->AddFileSystem(kOrigin1, kTemporary, 0);
278 ASSERT_FALSE(canned_helper_->empty());
279 canned_helper_->Reset();
280 ASSERT_TRUE(canned_helper_->empty());
281 }
282
283 // Verifies that AddFileSystem correctly adds file systems, and that both
284 // the type and usage metadata are reported as provided.
285 TEST_F(BrowsingDataFileSystemHelperTest, CannedAddFileSystem) {
286 canned_helper_->AddFileSystem(kOrigin1, kPersistent, 200);
287 canned_helper_->AddFileSystem(kOrigin2, kTemporary, 100);
288
289 FetchCannedFileSystems();
290
291 EXPECT_EQ(2U, file_system_info_list_->size());
292 std::list<BrowsingDataFileSystemHelper::FileSystemInfo>::iterator info =
293 file_system_info_list_->begin();
294 EXPECT_EQ(kOrigin1, info->origin);
295 EXPECT_TRUE(info->has_persistent);
296 EXPECT_FALSE(info->has_temporary);
297 EXPECT_EQ(200, info->usage_persistent);
298 EXPECT_EQ(0, info->usage_temporary);
299
300 info++;
301 EXPECT_EQ(kOrigin2, info->origin);
302 EXPECT_FALSE(info->has_persistent);
303 EXPECT_TRUE(info->has_temporary);
304 EXPECT_EQ(0, info->usage_persistent);
305 EXPECT_EQ(100, info->usage_temporary);
306 }
307
308 // Verifies that the CannedBrowsingDataFileSystemHelper correctly ignores
309 // extension and devtools schemes.
310 TEST_F(BrowsingDataFileSystemHelperTest, IgnoreExtensionsAndDevTools) {
311 ASSERT_TRUE(canned_helper_->empty());
312 canned_helper_->AddFileSystem(kOriginExt, kTemporary, 0);
313 ASSERT_TRUE(canned_helper_->empty());
314 canned_helper_->AddFileSystem(kOriginDevTools, kTemporary, 0);
315 ASSERT_TRUE(canned_helper_->empty());
316 }
317
318 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/browsing_data_file_system_helper.cc ('k') | chrome/browser/browsing_data_helper.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698