OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
6 | 6 |
7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
8 #include "chrome/browser/browsing_data_file_system_helper.h" | 8 #include "chrome/browser/browsing_data_file_system_helper.h" |
9 #include "chrome/test/testing_browser_process_test.h" | 9 #include "chrome/test/testing_browser_process_test.h" |
10 #include "chrome/test/testing_profile.h" | 10 #include "chrome/test/testing_profile.h" |
| 11 #include "content/browser/browser_thread.h" |
| 12 #include "webkit/fileapi/file_system_context.h" |
| 13 #include "webkit/fileapi/file_system_path_manager.h" |
| 14 #include "webkit/fileapi/file_system_types.h" |
| 15 #include "webkit/fileapi/file_system_usage_cache.h" |
| 16 #include "webkit/fileapi/sandbox_mount_point_provider.h" |
11 | 17 |
12 namespace { | 18 namespace { |
13 | 19 |
14 typedef TestingBrowserProcessTest CannedBrowsingDataFileSystemHelperTest; | 20 const fileapi::FileSystemType kTemporary = fileapi::kFileSystemTypeTemporary; |
15 | 21 const fileapi::FileSystemType kPersistent = fileapi::kFileSystemTypePersistent; |
16 TEST_F(CannedBrowsingDataFileSystemHelperTest, Empty) { | 22 |
17 TestingProfile profile; | 23 const char kTestOrigin1[] = "http://host1:1/"; |
18 | 24 const char kTestOrigin2[] = "http://host2:1/"; |
19 const GURL origin("http://host1:1/"); | 25 const char kTestOrigin3[] = "http://host3:1/"; |
20 | 26 |
21 scoped_refptr<CannedBrowsingDataFileSystemHelper> helper( | 27 const GURL kOrigin1(kTestOrigin1); |
22 new CannedBrowsingDataFileSystemHelper(&profile)); | 28 const GURL kOrigin2(kTestOrigin2); |
23 | 29 const GURL kOrigin3(kTestOrigin3); |
24 ASSERT_TRUE(helper->empty()); | 30 |
25 helper->AddFileSystem(origin, fileapi::kFileSystemTypeTemporary, 0); | 31 const int kEmptyFileSystemSize = fileapi::FileSystemUsageCache::kUsageFileSize; |
26 ASSERT_FALSE(helper->empty()); | 32 |
27 helper->Reset(); | 33 typedef std::vector<BrowsingDataFileSystemHelper::FileSystemInfo> |
28 ASSERT_TRUE(helper->empty()); | 34 FileSystemInfoVector; |
| 35 typedef scoped_ptr<FileSystemInfoVector> ScopedFileSystemInfoVector; |
| 36 |
| 37 class BrowsingDataFileSystemHelperTest : public testing::Test { |
| 38 public: |
| 39 BrowsingDataFileSystemHelperTest() |
| 40 : helper_(BrowsingDataFileSystemHelper::Create(&profile_)), |
| 41 canned_helper_(new CannedBrowsingDataFileSystemHelper(&profile_)), |
| 42 ui_thread_(BrowserThread::UI, &message_loop_), |
| 43 file_thread_(BrowserThread::FILE, &message_loop_), |
| 44 io_thread_(BrowserThread::IO, &message_loop_) { |
| 45 } |
| 46 virtual ~BrowsingDataFileSystemHelperTest() {} |
| 47 |
| 48 TestingProfile* GetProfile() { |
| 49 return &profile_; |
| 50 } |
| 51 |
| 52 void FindFileSystemPathCallback(bool success, |
| 53 const FilePath& path, |
| 54 const std::string& name) { |
| 55 found_file_system_ = success; |
| 56 Notify(); |
| 57 } |
| 58 |
| 59 bool FileSystemContainsOriginAndType(const GURL& origin, |
| 60 fileapi::FileSystemType type) { |
| 61 sandbox_->ValidateFileSystemRootAndGetURL( |
| 62 origin, type, false, NewCallback(this, |
| 63 &BrowsingDataFileSystemHelperTest::FindFileSystemPathCallback)); |
| 64 BlockUntilNotified(); |
| 65 return found_file_system_; |
| 66 } |
| 67 |
| 68 void CallbackStartFetching( |
| 69 const std::vector<BrowsingDataFileSystemHelper::FileSystemInfo>& |
| 70 file_system_info_list) { |
| 71 file_system_info_list_.reset( |
| 72 new std::vector<BrowsingDataFileSystemHelper::FileSystemInfo>( |
| 73 file_system_info_list)); |
| 74 Notify(); |
| 75 } |
| 76 |
| 77 void FetchFileSystems() { |
| 78 helper_->StartFetching(NewCallback(this, |
| 79 &BrowsingDataFileSystemHelperTest::CallbackStartFetching)); |
| 80 BlockUntilNotified(); |
| 81 } |
| 82 |
| 83 void FetchCannedFileSystems() { |
| 84 canned_helper_->StartFetching(NewCallback(this, |
| 85 &BrowsingDataFileSystemHelperTest::CallbackStartFetching)); |
| 86 BlockUntilNotified(); |
| 87 } |
| 88 |
| 89 virtual void PopulateTestFileSystemData() { |
| 90 // Set up kOrigin1 with a temporary file system, kOrigin2 with a persistent |
| 91 // file system, and kOrigin3 with both. |
| 92 sandbox_ = profile_.GetFileSystemContext()->path_manager()-> |
| 93 sandbox_provider(); |
| 94 |
| 95 CreateDirectoryForOriginAndType(kOrigin1, kTemporary); |
| 96 CreateDirectoryForOriginAndType(kOrigin2, kPersistent); |
| 97 CreateDirectoryForOriginAndType(kOrigin3, kTemporary); |
| 98 CreateDirectoryForOriginAndType(kOrigin3, kPersistent); |
| 99 |
| 100 EXPECT_FALSE(FileSystemContainsOriginAndType(kOrigin1, kPersistent)); |
| 101 EXPECT_TRUE(FileSystemContainsOriginAndType(kOrigin1, kTemporary)); |
| 102 EXPECT_TRUE(FileSystemContainsOriginAndType(kOrigin2, kPersistent)); |
| 103 EXPECT_FALSE(FileSystemContainsOriginAndType(kOrigin2, kTemporary)); |
| 104 EXPECT_TRUE(FileSystemContainsOriginAndType(kOrigin3, kPersistent)); |
| 105 EXPECT_TRUE(FileSystemContainsOriginAndType(kOrigin3, kTemporary)); |
| 106 } |
| 107 |
| 108 void CreateDirectoryForOriginAndType(const GURL& origin, |
| 109 fileapi::FileSystemType type) { |
| 110 FilePath target = sandbox_->ValidateFileSystemRootAndGetPathOnFileThread( |
| 111 origin, type, FilePath(), true); |
| 112 EXPECT_TRUE(file_util::DirectoryExists(target)); |
| 113 } |
| 114 |
| 115 FileSystemInfoVector* GetFileSystems() { |
| 116 return file_system_info_list_.get(); |
| 117 } |
| 118 |
| 119 void BlockUntilNotified() { |
| 120 MessageLoop::current()->Run(); |
| 121 } |
| 122 |
| 123 void Notify() { |
| 124 MessageLoop::current()->Quit(); |
| 125 } |
| 126 |
| 127 // Temporary storage to pass information back from callbacks. |
| 128 bool found_file_system_; |
| 129 ScopedFileSystemInfoVector file_system_info_list_; |
| 130 |
| 131 scoped_refptr<BrowsingDataFileSystemHelper> helper_; |
| 132 scoped_refptr<CannedBrowsingDataFileSystemHelper> canned_helper_; |
| 133 |
| 134 private: |
| 135 // message_loop_, as well as all the threads associated with it must be |
| 136 // defined before profile_ to prevent explosions. Oh how I love C++. |
| 137 MessageLoopForUI message_loop_; |
| 138 BrowserThread ui_thread_; |
| 139 BrowserThread file_thread_; |
| 140 BrowserThread io_thread_; |
| 141 TestingProfile profile_; |
| 142 |
| 143 // We don't own this pointer: don't delete it. |
| 144 fileapi::SandboxMountPointProvider* sandbox_; |
| 145 |
| 146 DISALLOW_COPY_AND_ASSIGN(BrowsingDataFileSystemHelperTest); |
| 147 }; |
| 148 |
| 149 TEST_F(BrowsingDataFileSystemHelperTest, FetchData) { |
| 150 PopulateTestFileSystemData(); |
| 151 |
| 152 FetchFileSystems(); |
| 153 |
| 154 EXPECT_EQ(3UL, file_system_info_list_->size()); |
| 155 |
| 156 // Order is arbitrary, verify all three origins. |
| 157 bool test_hosts_found[3] = {false, false, false}; |
| 158 for (size_t i = 0; i < file_system_info_list_->size(); i++) { |
| 159 BrowsingDataFileSystemHelper::FileSystemInfo info = |
| 160 file_system_info_list_->at(i); |
| 161 if (info.origin == kOrigin1) { |
| 162 test_hosts_found[0] = true; |
| 163 EXPECT_FALSE(info.has_persistent); |
| 164 EXPECT_TRUE(info.has_temporary); |
| 165 EXPECT_EQ(0, info.usage_persistent); |
| 166 EXPECT_EQ(kEmptyFileSystemSize, info.usage_temporary); |
| 167 } else if (info.origin == kOrigin2) { |
| 168 test_hosts_found[1] = true; |
| 169 EXPECT_TRUE(info.has_persistent); |
| 170 EXPECT_FALSE(info.has_temporary); |
| 171 EXPECT_EQ(kEmptyFileSystemSize, info.usage_persistent); |
| 172 EXPECT_EQ(0, info.usage_temporary); |
| 173 } else if (info.origin == kOrigin3) { |
| 174 test_hosts_found[2] = true; |
| 175 EXPECT_TRUE(info.has_persistent); |
| 176 EXPECT_TRUE(info.has_temporary); |
| 177 EXPECT_EQ(kEmptyFileSystemSize, info.usage_persistent); |
| 178 EXPECT_EQ(kEmptyFileSystemSize, info.usage_temporary); |
| 179 } else { |
| 180 ADD_FAILURE() << info.origin.spec() << " isn't an origin we added."; |
| 181 } |
| 182 } |
| 183 for (size_t i = 0; i < arraysize(test_hosts_found); i++) { |
| 184 EXPECT_TRUE(test_hosts_found[i]); |
| 185 } |
| 186 } |
| 187 |
| 188 TEST_F(BrowsingDataFileSystemHelperTest, DeleteData) { |
| 189 PopulateTestFileSystemData(); |
| 190 |
| 191 helper_->DeleteFileSystemOrigin(kOrigin1); |
| 192 helper_->DeleteFileSystemOrigin(kOrigin2); |
| 193 |
| 194 FetchFileSystems(); |
| 195 |
| 196 EXPECT_EQ(3UL, file_system_info_list_->size()); |
| 197 for (size_t i = 0; i < file_system_info_list_->size(); ++i) { |
| 198 BrowsingDataFileSystemHelper::FileSystemInfo info = |
| 199 file_system_info_list_->at(0); |
| 200 if (info.origin == kOrigin3) { |
| 201 EXPECT_TRUE(info.has_persistent); |
| 202 EXPECT_TRUE(info.has_temporary); |
| 203 EXPECT_EQ(kEmptyFileSystemSize, info.usage_persistent); |
| 204 EXPECT_EQ(kEmptyFileSystemSize, info.usage_temporary); |
| 205 } else { |
| 206 EXPECT_FALSE(info.has_persistent); |
| 207 EXPECT_FALSE(info.has_temporary); |
| 208 } |
| 209 } |
| 210 } |
| 211 |
| 212 TEST_F(BrowsingDataFileSystemHelperTest, Empty) { |
| 213 ASSERT_TRUE(canned_helper_->empty()); |
| 214 canned_helper_->AddFileSystem(kOrigin1, kTemporary, 0); |
| 215 ASSERT_FALSE(canned_helper_->empty()); |
| 216 canned_helper_->Reset(); |
| 217 ASSERT_TRUE(canned_helper_->empty()); |
| 218 } |
| 219 |
| 220 TEST_F(BrowsingDataFileSystemHelperTest, CannedAddFileSystem) { |
| 221 canned_helper_->AddFileSystem(kOrigin1, kPersistent, 200); |
| 222 canned_helper_->AddFileSystem(kOrigin2, kTemporary, 100); |
| 223 |
| 224 FetchCannedFileSystems(); |
| 225 |
| 226 EXPECT_EQ(2U, file_system_info_list_->size()); |
| 227 EXPECT_EQ(kOrigin1, file_system_info_list_->at(0).origin); |
| 228 EXPECT_TRUE(file_system_info_list_->at(0).has_persistent); |
| 229 EXPECT_FALSE(file_system_info_list_->at(0).has_temporary); |
| 230 EXPECT_EQ(200, file_system_info_list_->at(0).usage_persistent); |
| 231 EXPECT_EQ(0, file_system_info_list_->at(0).usage_temporary); |
| 232 EXPECT_EQ(kOrigin2, file_system_info_list_->at(1).origin); |
| 233 EXPECT_FALSE(file_system_info_list_->at(1).has_persistent); |
| 234 EXPECT_TRUE(file_system_info_list_->at(1).has_temporary); |
| 235 EXPECT_EQ(0, file_system_info_list_->at(1).usage_persistent); |
| 236 EXPECT_EQ(100, file_system_info_list_->at(1).usage_temporary); |
| 237 } |
| 238 |
| 239 TEST_F(BrowsingDataFileSystemHelperTest, CannedUnique) { |
| 240 canned_helper_->AddFileSystem(kOrigin3, kPersistent, 200); |
| 241 canned_helper_->AddFileSystem(kOrigin3, kTemporary, 100); |
| 242 |
| 243 FetchCannedFileSystems(); |
| 244 |
| 245 EXPECT_EQ(1U, file_system_info_list_->size()); |
| 246 EXPECT_EQ(kOrigin3, file_system_info_list_->at(0).origin); |
| 247 EXPECT_TRUE(file_system_info_list_->at(0).has_persistent); |
| 248 EXPECT_TRUE(file_system_info_list_->at(0).has_temporary); |
| 249 EXPECT_EQ(200, file_system_info_list_->at(0).usage_persistent); |
| 250 EXPECT_EQ(100, file_system_info_list_->at(0).usage_temporary); |
29 } | 251 } |
30 | 252 |
31 } // namespace | 253 } // namespace |
OLD | NEW |