OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 "chrome/browser/chromeos/file_system_provider/registry.h" |
| 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/files/file_path.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info
.h" |
| 13 #include "chrome/browser/chromeos/file_system_provider/registry.h" |
| 14 #include "chrome/common/pref_names.h" |
| 15 #include "chrome/test/base/testing_browser_process.h" |
| 16 #include "chrome/test/base/testing_pref_service_syncable.h" |
| 17 #include "chrome/test/base/testing_profile.h" |
| 18 #include "chrome/test/base/testing_profile_manager.h" |
| 19 #include "components/user_prefs/user_prefs.h" |
| 20 #include "content/public/test/test_browser_thread_bundle.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 |
| 23 namespace chromeos { |
| 24 namespace file_system_provider { |
| 25 namespace { |
| 26 |
| 27 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; |
| 28 const char kDisplayName[] = "Camera Pictures"; |
| 29 |
| 30 // The dot in the file system ID is there in order to check that saving to |
| 31 // preferences works correctly. File System ID is used as a key in |
| 32 // a base::DictionaryValue, so it has to be stored without path expansion. |
| 33 const char kFileSystemId[] = "camera/pictures/id .!@#$%^&*()_+"; |
| 34 |
| 35 // Stores a provided file system information in preferences together with a |
| 36 // fake observed entry. |
| 37 void RememberFakeFileSystem(TestingProfile* profile, |
| 38 const std::string& extension_id, |
| 39 const std::string& file_system_id, |
| 40 const std::string& display_name, |
| 41 bool writable, |
| 42 bool supports_notify_tag, |
| 43 const ObservedEntry& observed_entry) { |
| 44 TestingPrefServiceSyncable* const pref_service = |
| 45 profile->GetTestingPrefService(); |
| 46 ASSERT_TRUE(pref_service); |
| 47 |
| 48 base::DictionaryValue extensions; |
| 49 base::DictionaryValue* const file_systems = new base::DictionaryValue(); |
| 50 base::DictionaryValue* const file_system = new base::DictionaryValue(); |
| 51 file_system->SetStringWithoutPathExpansion(kPrefKeyFileSystemId, |
| 52 kFileSystemId); |
| 53 file_system->SetStringWithoutPathExpansion(kPrefKeyDisplayName, kDisplayName); |
| 54 file_system->SetBooleanWithoutPathExpansion(kPrefKeyWritable, writable); |
| 55 file_system->SetBooleanWithoutPathExpansion(kPrefKeySupportsNotifyTag, |
| 56 supports_notify_tag); |
| 57 file_systems->SetWithoutPathExpansion(kFileSystemId, file_system); |
| 58 extensions.SetWithoutPathExpansion(kExtensionId, file_systems); |
| 59 |
| 60 // Remember observed entries. |
| 61 base::DictionaryValue* const observed_entries = new base::DictionaryValue(); |
| 62 file_system->SetWithoutPathExpansion(kPrefKeyObservedEntries, |
| 63 observed_entries); |
| 64 base::DictionaryValue* const observed_entry_value = |
| 65 new base::DictionaryValue(); |
| 66 observed_entries->SetWithoutPathExpansion(observed_entry.entry_path.value(), |
| 67 observed_entry_value); |
| 68 observed_entry_value->SetStringWithoutPathExpansion( |
| 69 kPrefKeyObservedEntryEntryPath, observed_entry.entry_path.value()); |
| 70 observed_entry_value->SetBooleanWithoutPathExpansion( |
| 71 kPrefKeyObservedEntryRecursive, observed_entry.recursive); |
| 72 observed_entry_value->SetStringWithoutPathExpansion( |
| 73 kPrefKeyObservedEntryLastTag, observed_entry.last_tag); |
| 74 |
| 75 pref_service->Set(prefs::kFileSystemProviderMounted, extensions); |
| 76 } |
| 77 |
| 78 } // namespace |
| 79 |
| 80 class FileSystemProviderRegistryTest : public testing::Test { |
| 81 protected: |
| 82 FileSystemProviderRegistryTest() : profile_(NULL) {} |
| 83 |
| 84 virtual ~FileSystemProviderRegistryTest() {} |
| 85 |
| 86 virtual void SetUp() override { |
| 87 profile_manager_.reset( |
| 88 new TestingProfileManager(TestingBrowserProcess::GetGlobal())); |
| 89 ASSERT_TRUE(profile_manager_->SetUp()); |
| 90 profile_ = profile_manager_->CreateTestingProfile("test-user@example.com"); |
| 91 registry_.reset(new Registry(profile_)); |
| 92 fake_observed_entry_.entry_path = |
| 93 base::FilePath(FILE_PATH_LITERAL("/a/b/c")); |
| 94 fake_observed_entry_.recursive = true; |
| 95 fake_observed_entry_.last_tag = "hello-world"; |
| 96 } |
| 97 |
| 98 content::TestBrowserThreadBundle thread_bundle_; |
| 99 scoped_ptr<TestingProfileManager> profile_manager_; |
| 100 TestingProfile* profile_; |
| 101 scoped_ptr<RegistryInterface> registry_; |
| 102 ObservedEntry fake_observed_entry_; |
| 103 }; |
| 104 |
| 105 TEST_F(FileSystemProviderRegistryTest, RestoreFileSystems) { |
| 106 // Create a fake entry in the preferences. |
| 107 RememberFakeFileSystem(profile_, |
| 108 kExtensionId, |
| 109 kFileSystemId, |
| 110 kDisplayName, |
| 111 true /* writable */, |
| 112 true /* supports_notify_tag */, |
| 113 fake_observed_entry_); |
| 114 |
| 115 scoped_ptr<RegistryInterface::RestoredFileSystems> restored_file_systems = |
| 116 registry_->RestoreFileSystems(kExtensionId); |
| 117 |
| 118 ASSERT_EQ(1u, restored_file_systems->size()); |
| 119 const RegistryInterface::RestoredFileSystem& restored_file_system = |
| 120 restored_file_systems->at(0); |
| 121 EXPECT_EQ(kExtensionId, restored_file_system.extension_id); |
| 122 EXPECT_EQ(kFileSystemId, restored_file_system.options.file_system_id); |
| 123 EXPECT_EQ(kDisplayName, restored_file_system.options.display_name); |
| 124 EXPECT_TRUE(restored_file_system.options.writable); |
| 125 EXPECT_TRUE(restored_file_system.options.supports_notify_tag); |
| 126 |
| 127 ASSERT_EQ(1u, restored_file_system.observed_entries.size()); |
| 128 const auto& restored_observed_entry_it = |
| 129 restored_file_system.observed_entries.find( |
| 130 fake_observed_entry_.entry_path); |
| 131 ASSERT_NE(restored_file_system.observed_entries.end(), |
| 132 restored_observed_entry_it); |
| 133 |
| 134 EXPECT_EQ(fake_observed_entry_.entry_path, |
| 135 restored_observed_entry_it->second.entry_path); |
| 136 EXPECT_EQ(fake_observed_entry_.recursive, |
| 137 restored_observed_entry_it->second.recursive); |
| 138 EXPECT_EQ(fake_observed_entry_.last_tag, |
| 139 restored_observed_entry_it->second.last_tag); |
| 140 } |
| 141 |
| 142 TEST_F(FileSystemProviderRegistryTest, RememberFileSystem) { |
| 143 MountOptions options(kFileSystemId, kDisplayName); |
| 144 options.writable = true; |
| 145 options.supports_notify_tag = true; |
| 146 |
| 147 ProvidedFileSystemInfo file_system_info( |
| 148 kExtensionId, options, base::FilePath(FILE_PATH_LITERAL("/a/b/c"))); |
| 149 |
| 150 ObservedEntries observed_entries; |
| 151 observed_entries[fake_observed_entry_.entry_path] = fake_observed_entry_; |
| 152 |
| 153 registry_->RememberFileSystem(file_system_info, observed_entries); |
| 154 |
| 155 TestingPrefServiceSyncable* const pref_service = |
| 156 profile_->GetTestingPrefService(); |
| 157 ASSERT_TRUE(pref_service); |
| 158 |
| 159 const base::DictionaryValue* const extensions = |
| 160 pref_service->GetDictionary(prefs::kFileSystemProviderMounted); |
| 161 ASSERT_TRUE(extensions); |
| 162 |
| 163 const base::DictionaryValue* file_systems = NULL; |
| 164 ASSERT_TRUE(extensions->GetDictionaryWithoutPathExpansion(kExtensionId, |
| 165 &file_systems)); |
| 166 EXPECT_EQ(1u, file_systems->size()); |
| 167 |
| 168 const base::Value* file_system_value = NULL; |
| 169 const base::DictionaryValue* file_system = NULL; |
| 170 ASSERT_TRUE( |
| 171 file_systems->GetWithoutPathExpansion(kFileSystemId, &file_system_value)); |
| 172 ASSERT_TRUE(file_system_value->GetAsDictionary(&file_system)); |
| 173 |
| 174 std::string file_system_id; |
| 175 EXPECT_TRUE(file_system->GetStringWithoutPathExpansion(kPrefKeyFileSystemId, |
| 176 &file_system_id)); |
| 177 EXPECT_EQ(kFileSystemId, file_system_id); |
| 178 |
| 179 std::string display_name; |
| 180 EXPECT_TRUE(file_system->GetStringWithoutPathExpansion(kPrefKeyDisplayName, |
| 181 &display_name)); |
| 182 EXPECT_EQ(kDisplayName, display_name); |
| 183 |
| 184 bool writable = false; |
| 185 EXPECT_TRUE( |
| 186 file_system->GetBooleanWithoutPathExpansion(kPrefKeyWritable, &writable)); |
| 187 EXPECT_TRUE(writable); |
| 188 |
| 189 bool supports_notify_tag = false; |
| 190 EXPECT_TRUE(file_system->GetBooleanWithoutPathExpansion( |
| 191 kPrefKeySupportsNotifyTag, &supports_notify_tag)); |
| 192 EXPECT_TRUE(supports_notify_tag); |
| 193 |
| 194 const base::DictionaryValue* observed_entries_value = NULL; |
| 195 ASSERT_TRUE(file_system->GetDictionaryWithoutPathExpansion( |
| 196 kPrefKeyObservedEntries, &observed_entries_value)); |
| 197 |
| 198 const base::DictionaryValue* observed_entry = NULL; |
| 199 ASSERT_TRUE(observed_entries_value->GetDictionaryWithoutPathExpansion( |
| 200 fake_observed_entry_.entry_path.value(), &observed_entry)); |
| 201 |
| 202 std::string entry_path; |
| 203 EXPECT_TRUE(observed_entry->GetStringWithoutPathExpansion( |
| 204 kPrefKeyObservedEntryEntryPath, &entry_path)); |
| 205 EXPECT_EQ(fake_observed_entry_.entry_path.value(), entry_path); |
| 206 |
| 207 bool recursive = false; |
| 208 EXPECT_TRUE(observed_entry->GetBooleanWithoutPathExpansion( |
| 209 kPrefKeyObservedEntryRecursive, &recursive)); |
| 210 EXPECT_EQ(fake_observed_entry_.recursive, recursive); |
| 211 |
| 212 std::string last_tag; |
| 213 EXPECT_TRUE(observed_entry->GetStringWithoutPathExpansion( |
| 214 kPrefKeyObservedEntryLastTag, &last_tag)); |
| 215 EXPECT_EQ(fake_observed_entry_.last_tag, last_tag); |
| 216 } |
| 217 |
| 218 TEST_F(FileSystemProviderRegistryTest, ForgetFileSystem) { |
| 219 // Create a fake file systems in the preferences. |
| 220 RememberFakeFileSystem(profile_, |
| 221 kExtensionId, |
| 222 kFileSystemId, |
| 223 kDisplayName, |
| 224 true /* writable */, |
| 225 true /* supports_notify_tag */, |
| 226 fake_observed_entry_); |
| 227 |
| 228 registry_->ForgetFileSystem(kExtensionId, kFileSystemId); |
| 229 |
| 230 TestingPrefServiceSyncable* const pref_service = |
| 231 profile_->GetTestingPrefService(); |
| 232 ASSERT_TRUE(pref_service); |
| 233 |
| 234 const base::DictionaryValue* const extensions = |
| 235 pref_service->GetDictionary(prefs::kFileSystemProviderMounted); |
| 236 ASSERT_TRUE(extensions); |
| 237 |
| 238 const base::DictionaryValue* file_systems = NULL; |
| 239 EXPECT_FALSE(extensions->GetDictionaryWithoutPathExpansion(kExtensionId, |
| 240 &file_systems)); |
| 241 } |
| 242 |
| 243 TEST_F(FileSystemProviderRegistryTest, UpdateObservedEntryTag) { |
| 244 MountOptions options(kFileSystemId, kDisplayName); |
| 245 options.writable = true; |
| 246 options.supports_notify_tag = true; |
| 247 |
| 248 ProvidedFileSystemInfo file_system_info( |
| 249 kExtensionId, options, base::FilePath(FILE_PATH_LITERAL("/a/b/c"))); |
| 250 |
| 251 ObservedEntries observed_entries; |
| 252 observed_entries[fake_observed_entry_.entry_path] = fake_observed_entry_; |
| 253 |
| 254 registry_->RememberFileSystem(file_system_info, observed_entries); |
| 255 |
| 256 fake_observed_entry_.last_tag = "updated-tag"; |
| 257 registry_->UpdateObservedEntryTag(file_system_info, fake_observed_entry_); |
| 258 |
| 259 TestingPrefServiceSyncable* const pref_service = |
| 260 profile_->GetTestingPrefService(); |
| 261 ASSERT_TRUE(pref_service); |
| 262 |
| 263 const base::DictionaryValue* const extensions = |
| 264 pref_service->GetDictionary(prefs::kFileSystemProviderMounted); |
| 265 ASSERT_TRUE(extensions); |
| 266 |
| 267 const base::DictionaryValue* file_systems = NULL; |
| 268 ASSERT_TRUE(extensions->GetDictionaryWithoutPathExpansion(kExtensionId, |
| 269 &file_systems)); |
| 270 EXPECT_EQ(1u, file_systems->size()); |
| 271 |
| 272 const base::Value* file_system_value = NULL; |
| 273 const base::DictionaryValue* file_system = NULL; |
| 274 ASSERT_TRUE( |
| 275 file_systems->GetWithoutPathExpansion(kFileSystemId, &file_system_value)); |
| 276 ASSERT_TRUE(file_system_value->GetAsDictionary(&file_system)); |
| 277 |
| 278 std::string file_system_id; |
| 279 EXPECT_TRUE(file_system->GetStringWithoutPathExpansion(kPrefKeyFileSystemId, |
| 280 &file_system_id)); |
| 281 EXPECT_EQ(kFileSystemId, file_system_id); |
| 282 |
| 283 std::string display_name; |
| 284 EXPECT_TRUE(file_system->GetStringWithoutPathExpansion(kPrefKeyDisplayName, |
| 285 &display_name)); |
| 286 EXPECT_EQ(kDisplayName, display_name); |
| 287 |
| 288 bool writable = false; |
| 289 EXPECT_TRUE( |
| 290 file_system->GetBooleanWithoutPathExpansion(kPrefKeyWritable, &writable)); |
| 291 EXPECT_TRUE(writable); |
| 292 |
| 293 bool supports_notify_tag = false; |
| 294 EXPECT_TRUE(file_system->GetBooleanWithoutPathExpansion( |
| 295 kPrefKeySupportsNotifyTag, &supports_notify_tag)); |
| 296 EXPECT_TRUE(supports_notify_tag); |
| 297 |
| 298 const base::DictionaryValue* observed_entries_value = NULL; |
| 299 ASSERT_TRUE(file_system->GetDictionaryWithoutPathExpansion( |
| 300 kPrefKeyObservedEntries, &observed_entries_value)); |
| 301 |
| 302 const base::DictionaryValue* observed_entry = NULL; |
| 303 ASSERT_TRUE(observed_entries_value->GetDictionaryWithoutPathExpansion( |
| 304 fake_observed_entry_.entry_path.value(), &observed_entry)); |
| 305 |
| 306 std::string entry_path; |
| 307 EXPECT_TRUE(observed_entry->GetStringWithoutPathExpansion( |
| 308 kPrefKeyObservedEntryEntryPath, &entry_path)); |
| 309 EXPECT_EQ(fake_observed_entry_.entry_path.value(), entry_path); |
| 310 |
| 311 bool recursive = false; |
| 312 EXPECT_TRUE(observed_entry->GetBooleanWithoutPathExpansion( |
| 313 kPrefKeyObservedEntryRecursive, &recursive)); |
| 314 EXPECT_EQ(fake_observed_entry_.recursive, recursive); |
| 315 |
| 316 std::string last_tag; |
| 317 EXPECT_TRUE(observed_entry->GetStringWithoutPathExpansion( |
| 318 kPrefKeyObservedEntryLastTag, &last_tag)); |
| 319 EXPECT_EQ(fake_observed_entry_.last_tag, last_tag); |
| 320 } |
| 321 |
| 322 } // namespace file_system_provider |
| 323 } // namespace chromeos |
OLD | NEW |