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 "testing/gtest/include/gtest/gtest.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/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"
21 using content::BrowserThread
;
25 // Shorter names for fileapi::* constants.
26 const fileapi::FileSystemType kTemporary
= fileapi::kFileSystemTypeTemporary
;
27 const fileapi::FileSystemType kPersistent
= fileapi::kFileSystemTypePersistent
;
29 // We'll use these three distinct origins for testing, both as strings and as
30 // GURLs in appropriate contexts.
31 const char kTestOrigin1
[] = "http://host1:1/";
32 const char kTestOrigin2
[] = "http://host2:2/";
33 const char kTestOrigin3
[] = "http://host3:3/";
35 const GURL
kOrigin1(kTestOrigin1
);
36 const GURL
kOrigin2(kTestOrigin2
);
37 const GURL
kOrigin3(kTestOrigin3
);
39 // TODO(mkwst): Update this size once the discussion in http://crbug.com/86114
41 const int kEmptyFileSystemSize
= 0;
43 typedef std::list
<BrowsingDataFileSystemHelper::FileSystemInfo
>
45 typedef scoped_ptr
<FileSystemInfoList
> ScopedFileSystemInfoList
;
47 // The FileSystem APIs are all asynchronous; this testing class wraps up the
48 // boilerplate code necessary to deal with waiting for responses. In a nutshell,
49 // any async call whose response we want to test ought to be followed by a call
50 // to BlockUntilNotified(), which will (shockingly!) block until Notify() is
51 // called. For this to work, you'll need to ensure that each async call is
52 // implemented as a class method that that calls Notify() at an appropriate
54 class BrowsingDataFileSystemHelperTest
: public testing::Test
{
56 BrowsingDataFileSystemHelperTest()
57 : helper_(BrowsingDataFileSystemHelper::Create(&profile_
)),
58 canned_helper_(new CannedBrowsingDataFileSystemHelper(&profile_
)),
59 ui_thread_(BrowserThread::UI
, &message_loop_
),
60 file_thread_(BrowserThread::FILE, &message_loop_
),
61 io_thread_(BrowserThread::IO
, &message_loop_
) {
63 virtual ~BrowsingDataFileSystemHelperTest() {}
65 TestingProfile
* GetProfile() {
69 // Blocks on the current MessageLoop until Notify() is called.
70 void BlockUntilNotified() {
71 MessageLoop::current()->Run();
74 // Unblocks the current MessageLoop. Should be called in response to some sort
75 // of async activity in a callback method.
77 MessageLoop::current()->Quit();
80 // Callback that should be executed in response to
81 // fileapi::SandboxMountPointProvider::ValidateFileSystemRoot
82 void ValidateFileSystemCallback(base::PlatformFileError error
) {
83 validate_file_system_result_
= error
;
87 // Calls fileapi::SandboxMountPointProvider::ValidateFileSystemRootAndGetURL
88 // to verify the existence of a file system for a specified type and origin,
89 // blocks until a response is available, then returns the result
90 // synchronously to it's caller.
91 bool FileSystemContainsOriginAndType(const GURL
& origin
,
92 fileapi::FileSystemType type
) {
93 sandbox_
->ValidateFileSystemRoot(
96 &BrowsingDataFileSystemHelperTest::ValidateFileSystemCallback
,
97 base::Unretained(this)));
99 return validate_file_system_result_
== base::PLATFORM_FILE_OK
;
102 // Callback that should be executed in response to StartFetching(), and stores
103 // found file systems locally so that they are available via GetFileSystems().
104 void CallbackStartFetching(
105 const std::list
<BrowsingDataFileSystemHelper::FileSystemInfo
>&
106 file_system_info_list
) {
107 file_system_info_list_
.reset(
108 new std::list
<BrowsingDataFileSystemHelper::FileSystemInfo
>(
109 file_system_info_list
));
113 // Calls StartFetching() on the test's BrowsingDataFileSystemHelper
114 // object, then blocks until the callback is executed.
115 void FetchFileSystems() {
116 helper_
->StartFetching(
117 base::Bind(&BrowsingDataFileSystemHelperTest::CallbackStartFetching
,
118 base::Unretained(this)));
119 BlockUntilNotified();
122 // Calls StartFetching() on the test's CannedBrowsingDataFileSystemHelper
123 // object, then blocks until the callback is executed.
124 void FetchCannedFileSystems() {
125 canned_helper_
->StartFetching(
126 base::Bind(&BrowsingDataFileSystemHelperTest::CallbackStartFetching
,
127 base::Unretained(this)));
128 BlockUntilNotified();
131 // Sets up kOrigin1 with a temporary file system, kOrigin2 with a persistent
132 // file system, and kOrigin3 with both.
133 virtual void PopulateTestFileSystemData() {
134 sandbox_
= profile_
.GetFileSystemContext()->sandbox_provider();
136 CreateDirectoryForOriginAndType(kOrigin1
, kTemporary
);
137 CreateDirectoryForOriginAndType(kOrigin2
, kPersistent
);
138 CreateDirectoryForOriginAndType(kOrigin3
, kTemporary
);
139 CreateDirectoryForOriginAndType(kOrigin3
, kPersistent
);
141 EXPECT_FALSE(FileSystemContainsOriginAndType(kOrigin1
, kPersistent
));
142 EXPECT_TRUE(FileSystemContainsOriginAndType(kOrigin1
, kTemporary
));
143 EXPECT_TRUE(FileSystemContainsOriginAndType(kOrigin2
, kPersistent
));
144 EXPECT_FALSE(FileSystemContainsOriginAndType(kOrigin2
, kTemporary
));
145 EXPECT_TRUE(FileSystemContainsOriginAndType(kOrigin3
, kPersistent
));
146 EXPECT_TRUE(FileSystemContainsOriginAndType(kOrigin3
, kTemporary
));
149 // Uses the fileapi methods to create a filesystem of a given type for a
151 void CreateDirectoryForOriginAndType(const GURL
& origin
,
152 fileapi::FileSystemType type
) {
153 FilePath target
= sandbox_
->GetFileSystemRootPathOnFileThread(
154 origin
, type
, FilePath(), true);
155 EXPECT_TRUE(file_util::DirectoryExists(target
));
158 // Returns a list of the FileSystemInfo objects gathered in the most recent
159 // call to StartFetching().
160 FileSystemInfoList
* GetFileSystems() {
161 return file_system_info_list_
.get();
165 // Temporary storage to pass information back from callbacks.
166 base::PlatformFileError validate_file_system_result_
;
167 ScopedFileSystemInfoList file_system_info_list_
;
169 scoped_refptr
<BrowsingDataFileSystemHelper
> helper_
;
170 scoped_refptr
<CannedBrowsingDataFileSystemHelper
> canned_helper_
;
173 // message_loop_, as well as all the threads associated with it must be
174 // defined before profile_ to prevent explosions. The threads also must be
175 // defined in the order they're listed here. Oh how I love C++.
176 MessageLoopForUI message_loop_
;
177 content::TestBrowserThread ui_thread_
;
178 content::TestBrowserThread file_thread_
;
179 content::TestBrowserThread io_thread_
;
180 TestingProfile profile_
;
182 // We don't own this pointer: don't delete it.
183 fileapi::SandboxMountPointProvider
* sandbox_
;
185 DISALLOW_COPY_AND_ASSIGN(BrowsingDataFileSystemHelperTest
);
188 // Verifies that the BrowsingDataFileSystemHelper correctly finds the test file
189 // system data, and that each file system returned contains the expected data.
190 TEST_F(BrowsingDataFileSystemHelperTest
, FetchData
) {
191 PopulateTestFileSystemData();
195 EXPECT_EQ(3UL, file_system_info_list_
->size());
197 // Order is arbitrary, verify all three origins.
198 bool test_hosts_found
[3] = {false, false, false};
199 for (std::list
<BrowsingDataFileSystemHelper::FileSystemInfo
>::iterator info
=
200 file_system_info_list_
->begin(); info
!= file_system_info_list_
->end();
202 if (info
->origin
== kOrigin1
) {
203 EXPECT_FALSE(test_hosts_found
[0]);
204 test_hosts_found
[0] = true;
205 EXPECT_FALSE(info
->has_persistent
);
206 EXPECT_TRUE(info
->has_temporary
);
207 EXPECT_EQ(0, info
->usage_persistent
);
208 EXPECT_EQ(kEmptyFileSystemSize
, info
->usage_temporary
);
209 } else if (info
->origin
== kOrigin2
) {
210 EXPECT_FALSE(test_hosts_found
[1]);
211 test_hosts_found
[1] = true;
212 EXPECT_TRUE(info
->has_persistent
);
213 EXPECT_FALSE(info
->has_temporary
);
214 EXPECT_EQ(kEmptyFileSystemSize
, info
->usage_persistent
);
215 EXPECT_EQ(0, info
->usage_temporary
);
216 } else if (info
->origin
== kOrigin3
) {
217 EXPECT_FALSE(test_hosts_found
[2]);
218 test_hosts_found
[2] = true;
219 EXPECT_TRUE(info
->has_persistent
);
220 EXPECT_TRUE(info
->has_temporary
);
221 EXPECT_EQ(kEmptyFileSystemSize
, info
->usage_persistent
);
222 EXPECT_EQ(kEmptyFileSystemSize
, info
->usage_temporary
);
224 ADD_FAILURE() << info
->origin
.spec() << " isn't an origin we added.";
227 for (size_t i
= 0; i
< arraysize(test_hosts_found
); i
++) {
228 EXPECT_TRUE(test_hosts_found
[i
]);
232 // Verifies that the BrowsingDataFileSystemHelper correctly deletes file
233 // systems via DeleteFileSystemOrigin().
234 TEST_F(BrowsingDataFileSystemHelperTest
, DeleteData
) {
235 PopulateTestFileSystemData();
237 helper_
->DeleteFileSystemOrigin(kOrigin1
);
238 helper_
->DeleteFileSystemOrigin(kOrigin2
);
242 EXPECT_EQ(1UL, file_system_info_list_
->size());
243 BrowsingDataFileSystemHelper::FileSystemInfo info
=
244 *(file_system_info_list_
->begin());
245 EXPECT_EQ(kOrigin3
, info
.origin
);
246 EXPECT_TRUE(info
.has_persistent
);
247 EXPECT_TRUE(info
.has_temporary
);
248 EXPECT_EQ(kEmptyFileSystemSize
, info
.usage_persistent
);
249 EXPECT_EQ(kEmptyFileSystemSize
, info
.usage_temporary
);
252 // Verifies that the CannedBrowsingDataFileSystemHelper correctly reports
253 // whether or not it currently contains file systems.
254 TEST_F(BrowsingDataFileSystemHelperTest
, Empty
) {
255 ASSERT_TRUE(canned_helper_
->empty());
256 canned_helper_
->AddFileSystem(kOrigin1
, kTemporary
, 0);
257 ASSERT_FALSE(canned_helper_
->empty());
258 canned_helper_
->Reset();
259 ASSERT_TRUE(canned_helper_
->empty());
262 // Verifies that AddFileSystem correctly adds file systems, and that both
263 // the type and usage metadata are reported as provided.
264 TEST_F(BrowsingDataFileSystemHelperTest
, CannedAddFileSystem
) {
265 canned_helper_
->AddFileSystem(kOrigin1
, kPersistent
, 200);
266 canned_helper_
->AddFileSystem(kOrigin2
, kTemporary
, 100);
268 FetchCannedFileSystems();
270 EXPECT_EQ(2U, file_system_info_list_
->size());
271 std::list
<BrowsingDataFileSystemHelper::FileSystemInfo
>::iterator info
=
272 file_system_info_list_
->begin();
273 EXPECT_EQ(kOrigin1
, info
->origin
);
274 EXPECT_TRUE(info
->has_persistent
);
275 EXPECT_FALSE(info
->has_temporary
);
276 EXPECT_EQ(200, info
->usage_persistent
);
277 EXPECT_EQ(0, info
->usage_temporary
);
280 EXPECT_EQ(kOrigin2
, info
->origin
);
281 EXPECT_FALSE(info
->has_persistent
);
282 EXPECT_TRUE(info
->has_temporary
);
283 EXPECT_EQ(0, info
->usage_persistent
);
284 EXPECT_EQ(100, info
->usage_temporary
);