Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 // MediaFileSystemRegistry unit tests. | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/file_util.h" | |
| 9 #include "base/memory/ref_counted.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/memory/scoped_vector.h" | |
| 12 #include "base/message_loop.h" | |
| 13 #include "base/scoped_temp_dir.h" | |
| 14 #include "base/stringprintf.h" | |
| 15 #include "base/utf_string_conversions.h" | |
| 16 #include "base/values.h" | |
| 17 #include "chrome/browser/extensions/extension_service.h" | |
| 18 #include "chrome/browser/extensions/extension_system.h" | |
| 19 #include "chrome/browser/extensions/test_extension_system.h" | |
| 20 #include "chrome/browser/media_gallery/media_file_system_registry.h" | |
| 21 #include "chrome/browser/media_gallery/media_galleries_preferences_factory.h" | |
| 22 #include "chrome/browser/media_gallery/media_galleries_test_util.h" | |
| 23 #include "chrome/browser/system_monitor/media_storage_util.h" | |
| 24 #include "chrome/common/extensions/extension.h" | |
| 25 #include "chrome/common/extensions/extension_manifest_constants.h" | |
| 26 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | |
| 27 #include "chrome/test/base/testing_profile.h" | |
| 28 #include "content/public/browser/render_process_host_factory.h" | |
| 29 #include "content/public/browser/render_process_host.h" | |
| 30 #include "content/public/browser/render_view_host.h" | |
| 31 #include "content/public/browser/web_contents.h" | |
| 32 #include "content/public/test/mock_render_process_host.h" | |
| 33 #include "content/public/test/test_browser_thread.h" | |
| 34 #include "sync/api/string_ordinal.h" | |
| 35 #include "testing/gtest/include/gtest/gtest.h" | |
| 36 | |
| 37 namespace chrome { | |
| 38 | |
| 39 class TestMediaFileSystemContext : public MediaFileSystemContext { | |
| 40 public: | |
| 41 struct FSInfo { | |
| 42 FSInfo() {}; | |
|
Lei Zhang
2012/11/09 09:14:53
nit: no ;
vandebo (ex-Chrome)
2012/11/13 06:50:18
Done.
| |
| 43 FSInfo(const std::string& device_id, const FilePath& path, | |
| 44 const std::string& fsid); | |
| 45 | |
| 46 bool operator<(const FSInfo& other) const; | |
| 47 | |
| 48 std::string device_id; | |
| 49 FilePath path; | |
| 50 std::string fsid; | |
| 51 }; | |
| 52 | |
| 53 explicit TestMediaFileSystemContext(MediaFileSystemRegistry* registry); | |
| 54 virtual ~TestMediaFileSystemContext() {} | |
| 55 | |
| 56 // MediaFileSystemContext implementation. | |
| 57 virtual std::string RegisterFileSystemForMassStorage( | |
| 58 const std::string& device_id, const FilePath& path) OVERRIDE; | |
| 59 | |
| 60 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) | |
| 61 virtual std::string RegisterFileSystemForMTPDevice( | |
| 62 const std::string& device_id, const FilePath& path, | |
| 63 scoped_refptr<ScopedMTPDeviceMapEntry>* entry) OVERRIDE; | |
| 64 #endif | |
| 65 | |
| 66 virtual void RevokeFileSystem(const std::string& fsid) OVERRIDE; | |
| 67 | |
| 68 private: | |
| 69 std::string AddFSEntry(const std::string& device_id, const FilePath& path); | |
| 70 | |
| 71 MediaFileSystemRegistry* registry_; | |
| 72 int fsid_; | |
|
Lei Zhang
2012/11/09 09:14:53
Looks a bit weird when fsid_ is an int but fsid is
vandebo (ex-Chrome)
2012/11/13 06:50:18
Done.
| |
| 73 std::map<std::string /*fsid*/, FSInfo> file_systems_by_id_; | |
| 74 }; | |
| 75 | |
| 76 TestMediaFileSystemContext::FSInfo::FSInfo(const std::string& device_id, | |
| 77 const FilePath& path, | |
| 78 const std::string& fsid) | |
| 79 : device_id(device_id), | |
| 80 path(path), | |
| 81 fsid(fsid) { | |
| 82 } | |
| 83 | |
| 84 bool TestMediaFileSystemContext::FSInfo::operator<(const FSInfo& other) const { | |
| 85 if (device_id != other.device_id) | |
| 86 return device_id < other.device_id; | |
| 87 if (path.value() != other.path.value()) | |
| 88 return path.value() < other.path.value(); | |
| 89 return fsid < other.fsid; | |
| 90 } | |
| 91 | |
| 92 TestMediaFileSystemContext::TestMediaFileSystemContext( | |
| 93 MediaFileSystemRegistry* registry) | |
| 94 : registry_(registry), | |
| 95 fsid_(0) { | |
| 96 registry_->file_system_context_.reset(this); | |
| 97 } | |
| 98 | |
| 99 std::string TestMediaFileSystemContext::RegisterFileSystemForMassStorage( | |
| 100 const std::string& device_id, const FilePath& path) { | |
| 101 CHECK(MediaStorageUtil::IsMassStorageDevice(device_id)); | |
| 102 return AddFSEntry(device_id, path); | |
| 103 } | |
| 104 | |
| 105 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) | |
| 106 std::string TestMediaFileSystemContext::RegisterFileSystemForMTPDevice( | |
| 107 const std::string& device_id, const FilePath& path, | |
| 108 scoped_refptr<ScopedMTPDeviceMapEntry>* entry) { | |
| 109 CHECK(!MediaStorageUtil::IsMassStorageDevice(device_id)); | |
| 110 DCHECK(entry); | |
|
Lei Zhang
2012/11/09 09:14:53
No need for this. If we crash on the next line, it
vandebo (ex-Chrome)
2012/11/13 06:50:18
The segfault might end up being somewhat mysteriou
| |
| 111 *entry = registry_->GetOrCreateScopedMTPDeviceMapEntry(path.value()); | |
| 112 return AddFSEntry(device_id, path); | |
| 113 } | |
| 114 #endif | |
| 115 | |
| 116 void TestMediaFileSystemContext::RevokeFileSystem(const std::string& fsid) { | |
| 117 if (!ContainsKey(file_systems_by_id_, fsid)) | |
| 118 return; | |
| 119 EXPECT_EQ(1U, file_systems_by_id_.erase(fsid)); | |
| 120 } | |
| 121 | |
| 122 std::string TestMediaFileSystemContext::AddFSEntry(const std::string& device_id, | |
| 123 const FilePath& path) { | |
| 124 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
|
Lei Zhang
2012/11/09 09:14:53
we usually dcheck these.
vandebo (ex-Chrome)
2012/11/13 06:50:18
Done.
| |
| 125 CHECK(path.IsAbsolute()); | |
| 126 CHECK(!path.ReferencesParent()); | |
| 127 | |
| 128 std::string fsid = base::StringPrintf("FSID:%d", ++fsid_); | |
| 129 FSInfo info(device_id, path, fsid); | |
| 130 file_systems_by_id_[fsid] = info; | |
| 131 return fsid; | |
| 132 } | |
| 133 | |
| 134 namespace { | |
| 135 | |
| 136 class MediaStorageUtilTest : public MediaStorageUtil { | |
| 137 public: | |
| 138 static void SetTestingMode(); | |
| 139 | |
| 140 static bool GetDeviceInfoFromPathTestFunction(const FilePath& path, | |
| 141 std::string* device_id, | |
| 142 string16* device_name, | |
| 143 FilePath* relative_path); | |
| 144 }; | |
| 145 | |
| 146 class MockProfileSharedRenderProcessHostFactory | |
| 147 : public content::RenderProcessHostFactory { | |
| 148 public: | |
| 149 MockProfileSharedRenderProcessHostFactory() {} | |
| 150 virtual ~MockProfileSharedRenderProcessHostFactory(); | |
| 151 | |
| 152 // RPH created with this factory are owned by it. If the RPH is destroyed | |
| 153 // for testing purposes, it must be removed from the factory first. | |
| 154 content::MockRenderProcessHost* ReleaseRPH( | |
| 155 content::BrowserContext* browser_context); | |
| 156 | |
| 157 virtual content::RenderProcessHost* CreateRenderProcessHost( | |
| 158 content::BrowserContext* browser_context) const OVERRIDE; | |
| 159 | |
| 160 private: | |
| 161 typedef std::map<content::BrowserContext*, content::MockRenderProcessHost*> | |
| 162 ProfileRPHMap; | |
| 163 mutable ProfileRPHMap rph_map_; | |
|
Lei Zhang
2012/11/09 09:14:53
why mutable?
vandebo (ex-Chrome)
2012/11/13 06:50:18
Because RenderProcessHostFactory::CreateRenderProc
| |
| 164 | |
| 165 DISALLOW_COPY_AND_ASSIGN(MockProfileSharedRenderProcessHostFactory); | |
| 166 }; | |
| 167 | |
| 168 class ProfileState { | |
| 169 public: | |
| 170 ProfileState(); | |
| 171 ~ProfileState(); | |
| 172 | |
| 173 MediaGalleriesPreferences* GetPrefs(); | |
|
Lei Zhang
2012/11/09 09:14:53
GetMediaGalleryPrefs? I usually think GetPrefs wou
vandebo (ex-Chrome)
2012/11/13 06:50:18
Done.
| |
| 174 | |
| 175 scoped_ptr<TestingProfile> profile; | |
| 176 | |
| 177 scoped_ptr<content::WebContents> single_web_contents; | |
| 178 scoped_ptr<content::WebContents> shared_web_contents1; | |
| 179 scoped_ptr<content::WebContents> shared_web_contents2; | |
| 180 | |
| 181 // The RenderProcessHosts are freed when their respective WebContents / | |
| 182 // RenderViewHosts go away. | |
| 183 content::MockRenderProcessHost* single_rph; | |
| 184 content::MockRenderProcessHost* shared_rph; | |
| 185 | |
| 186 scoped_refptr<extensions::Extension> all_permission_extension; | |
| 187 scoped_refptr<extensions::Extension> regular_permission_extension; | |
| 188 scoped_refptr<extensions::Extension> no_permissions_extension; | |
| 189 | |
| 190 private: | |
| 191 scoped_refptr<extensions::Extension> AddApp( | |
| 192 std::string name, | |
|
Lei Zhang
2012/11/09 09:14:53
const ref both parameters?
vandebo (ex-Chrome)
2012/11/13 06:50:18
Done.
| |
| 193 std::vector<std::string> media_galleries_permissions); | |
| 194 | |
| 195 ExtensionService* extension_service_; | |
| 196 | |
| 197 DISALLOW_COPY_AND_ASSIGN(ProfileState); | |
| 198 }; | |
| 199 | |
| 200 class MediaFileSystemRegistryTest : public ChromeRenderViewHostTestHarness { | |
| 201 public: | |
| 202 MediaFileSystemRegistryTest(); | |
| 203 virtual ~MediaFileSystemRegistryTest() {} | |
| 204 | |
| 205 void CreateProfileState(int profile_count); | |
| 206 | |
| 207 ProfileState* GetProfileState(int i); | |
| 208 | |
| 209 FilePath empty_dir() { | |
| 210 return empty_dir_; | |
| 211 } | |
| 212 | |
| 213 FilePath dcim_dir() { | |
| 214 return dcim_dir_; | |
| 215 } | |
| 216 | |
| 217 // Create a user added gallery based on the information passed and add it to | |
| 218 // |profiles|. Returns the device id. | |
| 219 std::string AddUserGallery(MediaStorageUtil::Type type, | |
| 220 const std::string& unique_id, | |
| 221 const FilePath& path); | |
| 222 | |
| 223 void AttachDevice(MediaStorageUtil::Type type, const std::string& unique_id, | |
| 224 const FilePath& location); | |
| 225 | |
| 226 void DetachDevice(const std::string& device_id); | |
| 227 | |
| 228 void SetGalleryPermission(size_t profile, extensions::Extension* extension, | |
| 229 const std::string& device_id, bool has_access); | |
| 230 | |
| 231 void AssertAllAutoAddedGalleries(); | |
| 232 | |
| 233 std::vector<MediaFileSystemInfo> GetAutoAddedGalleries(size_t profile); | |
| 234 | |
| 235 void CompareResults(const std::string& test, | |
| 236 const std::vector<MediaFileSystemInfo>& expected, | |
| 237 const std::vector<MediaFileSystemInfo>& actual); | |
| 238 | |
| 239 int GetAndClearComparisonCount(); | |
| 240 | |
| 241 void CheckGalleriesForProfile(size_t profile, const std::string& test, | |
| 242 const std::vector<MediaFileSystemInfo>& regular_extension_galleries, | |
| 243 const std::vector<MediaFileSystemInfo>& all_extension_galleries); | |
| 244 | |
| 245 protected: | |
| 246 void SetUp(); | |
| 247 void TearDown(); | |
| 248 | |
| 249 private: | |
| 250 // This makes sure that at least one default gallery exists on the file | |
| 251 // system. | |
| 252 EnsureMediaDirectoriesExists media_directories_; | |
| 253 | |
| 254 // Some test gallery directories. | |
| 255 ScopedTempDir galleries_dir_; | |
| 256 FilePath empty_dir_; | |
|
Lei Zhang
2012/11/09 09:14:53
Can you mention these are inside |galleries_dir_|
vandebo (ex-Chrome)
2012/11/13 06:50:18
Done.
| |
| 257 FilePath dcim_dir_; | |
| 258 | |
| 259 // MediaFileSystemRegistry owns this. | |
| 260 TestMediaFileSystemContext* test_file_system_context_; | |
| 261 | |
| 262 // Needed for extension service & friends to work. | |
| 263 content::TestBrowserThread ui_thread_; | |
| 264 content::TestBrowserThread file_thread_; | |
| 265 | |
| 266 MockProfileSharedRenderProcessHostFactory rph_factory_; | |
| 267 | |
| 268 ScopedVector<ProfileState> profile_state_; | |
|
Lei Zhang
2012/11/09 09:14:53
nit: profile_states_
vandebo (ex-Chrome)
2012/11/13 06:50:18
Done.
| |
| 269 | |
| 270 int num_comparisons_; | |
| 271 | |
| 272 DISALLOW_COPY_AND_ASSIGN(MediaFileSystemRegistryTest); | |
| 273 }; | |
| 274 | |
| 275 bool MediaFileSystemInfoComparator(const MediaFileSystemInfo& a, | |
| 276 const MediaFileSystemInfo& b) { | |
| 277 if (a.name != b.name) | |
| 278 return a.name < b.name; | |
| 279 if (a.path.value() != b.path.value()) | |
| 280 return a.path.value() < b.path.value(); | |
| 281 return a.fsid < b.fsid; | |
| 282 } | |
| 283 | |
| 284 ////////////////////////// | |
| 285 // MediaStorageUtilTest // | |
| 286 ////////////////////////// | |
| 287 | |
| 288 // static | |
| 289 void MediaStorageUtilTest::SetTestingMode() { | |
| 290 SetGetDeviceInfoFromPathFunctionForTesting( | |
| 291 &GetDeviceInfoFromPathTestFunction); | |
| 292 } | |
| 293 | |
| 294 // static | |
| 295 bool MediaStorageUtilTest::GetDeviceInfoFromPathTestFunction( | |
| 296 const FilePath& path, std::string* device_id, string16* device_name, | |
| 297 FilePath* relative_path) { | |
| 298 if (device_id) | |
| 299 *device_id = MakeDeviceId(FIXED_MASS_STORAGE, path.AsUTF8Unsafe()); | |
| 300 if (device_name) | |
| 301 *device_name = path.BaseName().LossyDisplayName(); | |
| 302 if (relative_path) | |
| 303 *relative_path = FilePath(); | |
| 304 return true; | |
| 305 } | |
| 306 | |
| 307 /////////////////////////////////////////////// | |
| 308 // MockProfileSharedRenderProcessHostFactory // | |
| 309 /////////////////////////////////////////////// | |
| 310 | |
| 311 MockProfileSharedRenderProcessHostFactory:: | |
| 312 ~MockProfileSharedRenderProcessHostFactory() { | |
| 313 for (ProfileRPHMap::const_iterator it = rph_map_.begin(); | |
|
Lei Zhang
2012/11/09 09:14:53
This entire dtor is just a call to STLDeleteValues
vandebo (ex-Chrome)
2012/11/13 06:50:18
Done.
| |
| 314 it != rph_map_.end(); | |
| 315 ++it) { | |
| 316 delete it->second; | |
| 317 } | |
| 318 rph_map_.clear(); | |
| 319 } | |
| 320 | |
| 321 content::MockRenderProcessHost* | |
| 322 MockProfileSharedRenderProcessHostFactory::ReleaseRPH( | |
| 323 content::BrowserContext* browser_context) { | |
| 324 ProfileRPHMap::iterator existing = rph_map_.find(browser_context); | |
| 325 if (existing == rph_map_.end()) | |
| 326 return NULL; | |
| 327 content::MockRenderProcessHost* result = existing->second; | |
| 328 rph_map_.erase(existing); | |
| 329 return result; | |
| 330 } | |
| 331 | |
| 332 content::RenderProcessHost* | |
| 333 MockProfileSharedRenderProcessHostFactory::CreateRenderProcessHost( | |
| 334 content::BrowserContext* browser_context) const { | |
| 335 ProfileRPHMap::const_iterator existing = rph_map_.find(browser_context); | |
| 336 if (existing != rph_map_.end()) | |
| 337 return existing->second; | |
| 338 rph_map_[browser_context] = | |
| 339 new content::MockRenderProcessHost(browser_context); | |
| 340 return rph_map_[browser_context]; | |
| 341 } | |
| 342 | |
| 343 ////////////////// | |
| 344 // ProfileState // | |
| 345 ////////////////// | |
| 346 | |
| 347 ProfileState::ProfileState() | |
| 348 : profile(new TestingProfile()), | |
| 349 extension_service_(NULL) { | |
| 350 extensions::TestExtensionSystem* extension_system( | |
| 351 static_cast<extensions::TestExtensionSystem*>( | |
| 352 extensions::ExtensionSystem::Get(profile.get()))); | |
| 353 extension_service_ = extension_system->CreateExtensionService( | |
| 354 CommandLine::ForCurrentProcess(), FilePath(), false); | |
| 355 | |
| 356 std::vector<std::string> all_permissions; | |
| 357 all_permissions.push_back("allAutoDetected"); | |
| 358 all_permissions.push_back("read"); | |
| 359 std::vector<std::string> read_permissions; | |
| 360 read_permissions.push_back("read"); | |
| 361 | |
| 362 all_permission_extension = AddApp("all", all_permissions); | |
| 363 regular_permission_extension = AddApp("regular", read_permissions); | |
| 364 no_permissions_extension = AddApp("no", read_permissions); | |
| 365 } | |
| 366 | |
| 367 ProfileState::~ProfileState() { | |
| 368 // TestExtensionSystem uses DeleteSoon, so we need to delete the profiles | |
| 369 // and then run the message queue to clean up. But first we have to | |
| 370 // delete everything that references the profile. | |
| 371 single_web_contents.reset(); | |
| 372 shared_web_contents1.reset(); | |
| 373 shared_web_contents2.reset(); | |
| 374 profile.reset(); | |
| 375 | |
| 376 MessageLoop::current()->RunUntilIdle(); | |
| 377 } | |
| 378 | |
| 379 MediaGalleriesPreferences* ProfileState::GetPrefs() { | |
| 380 return MediaGalleriesPreferencesFactory::GetForProfile(profile.get()); | |
| 381 } | |
| 382 | |
| 383 scoped_refptr<extensions::Extension> ProfileState::AddApp( | |
|
Lei Zhang
2012/11/09 09:14:53
Don't we have something similar to this in chrome/
vandebo (ex-Chrome)
2012/11/13 06:50:18
Done.
| |
| 384 std::string name, | |
| 385 std::vector<std::string> media_galleries_permissions) { | |
| 386 scoped_ptr<DictionaryValue> manifest(new DictionaryValue); | |
| 387 manifest->SetString(extension_manifest_keys::kName, name); | |
| 388 manifest->SetString(extension_manifest_keys::kVersion, "0.1"); | |
| 389 manifest->SetInteger(extension_manifest_keys::kManifestVersion, 2); | |
| 390 ListValue* background_script_list = new ListValue; | |
| 391 background_script_list->Append(Value::CreateStringValue("background.js")); | |
| 392 manifest->Set(extension_manifest_keys::kPlatformAppBackgroundScripts, | |
| 393 background_script_list); | |
| 394 | |
| 395 ListValue* permission_detail_list = new ListValue; | |
| 396 for (size_t i = 0; i < media_galleries_permissions.size(); i++) | |
| 397 permission_detail_list->Append( | |
| 398 Value::CreateStringValue(media_galleries_permissions[i])); | |
| 399 DictionaryValue* media_galleries_permission = new DictionaryValue(); | |
| 400 media_galleries_permission->Set("mediaGalleries", permission_detail_list); | |
| 401 ListValue* permission_list = new ListValue; | |
| 402 permission_list->Append(media_galleries_permission); | |
| 403 manifest->Set(extension_manifest_keys::kPermissions, permission_list); | |
| 404 | |
| 405 FilePath path = profile->GetPath().Append(name); | |
| 406 std::string errors; | |
| 407 scoped_refptr<extensions::Extension> extension = | |
| 408 extensions::Extension::Create(path, extensions::Extension::INTERNAL, | |
| 409 *manifest.get(), | |
| 410 extensions::Extension::NO_FLAGS, &errors); | |
| 411 EXPECT_TRUE(extension.get() != NULL) << errors; | |
| 412 EXPECT_TRUE(extensions::Extension::IdIsValid(extension->id())); | |
| 413 if (!extension.get() || !extensions::Extension::IdIsValid(extension->id())) | |
| 414 return NULL; | |
| 415 | |
| 416 extension_service_->extension_prefs()->OnExtensionInstalled( | |
| 417 extension.get(), extensions::Extension::ENABLED, | |
| 418 syncer::StringOrdinal::CreateInitialOrdinal()); | |
| 419 | |
| 420 extension_service_->AddExtension(extension); | |
| 421 extension_service_->EnableExtension(extension->id()); | |
| 422 | |
| 423 return extension; | |
| 424 } | |
| 425 | |
| 426 ///////////////////////////////// | |
| 427 // MediaFileSystemRegistryTest // | |
| 428 ///////////////////////////////// | |
| 429 | |
| 430 MediaFileSystemRegistryTest::MediaFileSystemRegistryTest() | |
| 431 : ui_thread_(content::BrowserThread::UI, MessageLoop::current()), | |
| 432 file_thread_(content::BrowserThread::FILE, MessageLoop::current()), | |
| 433 num_comparisons_(0) { | |
| 434 } | |
| 435 | |
| 436 void MediaFileSystemRegistryTest::CreateProfileState(int profile_count) { | |
| 437 for (int i = 0; i < profile_count; i++) { | |
| 438 ProfileState * state = new ProfileState; | |
| 439 profile_state_.push_back(state); | |
| 440 | |
| 441 // Temporarily set the profile while we use it. | |
| 442 content::BrowserContext* profile = state->profile.get(); | |
| 443 browser_context_.reset(profile); | |
|
Lei Zhang
2012/11/09 09:14:53
Do you have to do this? Can you use WebContentsTes
vandebo (ex-Chrome)
2012/11/13 06:50:18
Done.
| |
| 444 | |
| 445 state->single_web_contents.reset(CreateTestWebContents()); | |
| 446 state->single_rph = rph_factory_.ReleaseRPH(profile); | |
| 447 | |
| 448 state->shared_web_contents1.reset(CreateTestWebContents()); | |
| 449 state->shared_web_contents2.reset(CreateTestWebContents()); | |
| 450 state->shared_rph = rph_factory_.ReleaseRPH(profile); | |
| 451 | |
| 452 // And release it when we're done, assign it to appease the compile. | |
| 453 profile = browser_context_.release(); | |
| 454 } | |
| 455 } | |
| 456 | |
| 457 ProfileState* MediaFileSystemRegistryTest::GetProfileState(int i) { | |
| 458 return profile_state_[i]; | |
| 459 } | |
| 460 | |
| 461 // Create a user added gallery based on the information passed and add it to | |
|
Lei Zhang
2012/11/09 09:14:53
no need for a second copy of comments.
vandebo (ex-Chrome)
2012/11/13 06:50:18
Done.
| |
| 462 // |profiles|. Returns the device id. | |
| 463 std::string MediaFileSystemRegistryTest::AddUserGallery( | |
| 464 MediaStorageUtil::Type type, | |
| 465 const std::string& unique_id, | |
| 466 const FilePath& path) { | |
| 467 std::string device_id = MediaStorageUtil::MakeDeviceId(type, unique_id); | |
| 468 string16 name = path.LossyDisplayName(); | |
| 469 DCHECK(!MediaStorageUtil::IsMediaDevice(device_id)); | |
| 470 | |
| 471 for (size_t i = 0; i < profile_state_.size(); i++) { | |
| 472 profile_state_[i]->GetPrefs()->AddGallery(device_id, name, FilePath(), | |
| 473 true /*user_added*/); | |
| 474 } | |
| 475 return device_id; | |
| 476 } | |
| 477 | |
| 478 void MediaFileSystemRegistryTest::AttachDevice(MediaStorageUtil::Type type, | |
| 479 const std::string& unique_id, | |
| 480 const FilePath& location) { | |
| 481 std::string device_id = MediaStorageUtil::MakeDeviceId(type, unique_id); | |
| 482 DCHECK(MediaStorageUtil::IsRemovableDevice(device_id)); | |
| 483 string16 name = location.LossyDisplayName(); | |
| 484 base::SystemMonitor::Get()->ProcessRemovableStorageAttached(device_id, name, | |
| 485 location.value()); | |
|
Lei Zhang
2012/11/09 09:14:53
indentation
vandebo (ex-Chrome)
2012/11/13 06:50:18
Done.
| |
| 486 } | |
| 487 | |
| 488 void MediaFileSystemRegistryTest::DetachDevice(const std::string& device_id) { | |
| 489 DCHECK(MediaStorageUtil::IsRemovableDevice(device_id)); | |
| 490 base::SystemMonitor::Get()->ProcessRemovableStorageDetached(device_id); | |
| 491 } | |
| 492 | |
| 493 void MediaFileSystemRegistryTest::SetGalleryPermission( | |
| 494 size_t profile, extensions::Extension* extension, | |
| 495 const std::string& device_id, bool has_access) { | |
| 496 MediaGalleriesPreferences* preferences = GetProfileState(profile)->GetPrefs(); | |
| 497 MediaGalleryPrefIdSet pref_id = | |
| 498 preferences->LookUpGalleriesByDeviceId(device_id); | |
| 499 DCHECK_EQ(1U, pref_id.size()); | |
| 500 preferences->SetGalleryPermissionForExtension(*extension, *pref_id.begin(), | |
| 501 has_access); | |
| 502 } | |
| 503 | |
| 504 void MediaFileSystemRegistryTest::AssertAllAutoAddedGalleries() { | |
| 505 for (size_t i = 0; i < profile_state_.size(); i++) { | |
| 506 MediaGalleriesPreferences* prefs = profile_state_[0]->GetPrefs(); | |
| 507 | |
| 508 // Make sure that we have at least one gallery and that they are all | |
| 509 // auto added galleries. | |
| 510 const MediaGalleriesPrefInfoMap& galleries = prefs->known_galleries(); | |
| 511 ASSERT_LT(0U, galleries.size()); | |
|
Lei Zhang
2012/11/09 09:14:53
Unlike _EQ, there's not "expected" value here. So
vandebo (ex-Chrome)
2012/11/13 06:50:18
Done.
| |
| 512 for (MediaGalleriesPrefInfoMap::const_iterator it = galleries.begin(); | |
| 513 it != galleries.end(); | |
| 514 ++it) { | |
| 515 ASSERT_EQ(MediaGalleryPrefInfo::kAutoDetected, it->second.type); | |
| 516 } | |
| 517 } | |
| 518 } | |
| 519 | |
| 520 std::vector<MediaFileSystemInfo> | |
| 521 MediaFileSystemRegistryTest::GetAutoAddedGalleries(size_t profile) { | |
| 522 DCHECK_LT(profile, profile_state_.size()); | |
| 523 const MediaGalleriesPrefInfoMap& galleries = | |
| 524 GetProfileState(profile)->GetPrefs()->known_galleries(); | |
| 525 std::vector<MediaFileSystemInfo> result; | |
| 526 for (MediaGalleriesPrefInfoMap::const_iterator it = galleries.begin(); | |
| 527 it != galleries.end(); | |
| 528 ++it) { | |
| 529 if (it->second.type == MediaGalleryPrefInfo::kAutoDetected) { | |
| 530 FilePath path = it->second.AbsolutePath(); | |
| 531 MediaFileSystemInfo info(std::string(), path, std::string()); | |
| 532 result.push_back(info); | |
| 533 } | |
| 534 } | |
| 535 std::sort(result.begin(), result.end(), MediaFileSystemInfoComparator); | |
| 536 return result; | |
| 537 } | |
| 538 | |
| 539 void MediaFileSystemRegistryTest::CompareResults( | |
| 540 const std::string& test, | |
| 541 const std::vector<MediaFileSystemInfo>& expected, | |
| 542 const std::vector<MediaFileSystemInfo>& actual) { | |
| 543 // Order isn't important, so sort the results. Assume that expected | |
| 544 // is already sorted. | |
| 545 std::vector<MediaFileSystemInfo> sorted(actual); | |
| 546 std::sort(sorted.begin(), sorted.end(), MediaFileSystemInfoComparator); | |
| 547 | |
| 548 num_comparisons_++; | |
| 549 EXPECT_EQ(expected.size(), actual.size()) << test; | |
| 550 for (size_t i = 0; i < expected.size(); i++) { | |
| 551 EXPECT_EQ(expected[i].path.value(), actual[i].path.value()) << test; | |
| 552 EXPECT_FALSE(actual[i].fsid.empty()) << test; | |
| 553 if (!expected[i].fsid.empty()) | |
| 554 EXPECT_EQ(expected[i].fsid, actual[i].fsid) << test; | |
| 555 } | |
| 556 } | |
| 557 | |
| 558 int MediaFileSystemRegistryTest::GetAndClearComparisonCount() { | |
| 559 int result = num_comparisons_; | |
| 560 num_comparisons_ = 0; | |
| 561 return result; | |
| 562 } | |
| 563 | |
| 564 void MediaFileSystemRegistryTest::CheckGalleriesForProfile( | |
| 565 size_t profile, | |
| 566 const std::string& test, | |
| 567 const std::vector<MediaFileSystemInfo>& regular_extension_galleries, | |
| 568 const std::vector<MediaFileSystemInfo>& all_extension_galleries) { | |
| 569 content::RenderViewHost* rvh = | |
| 570 GetProfileState(profile)->single_web_contents->GetRenderViewHost(); | |
| 571 | |
| 572 // No Media Galleries permissions. | |
| 573 std::vector<MediaFileSystemInfo> empty_expectation; | |
| 574 MediaFileSystemRegistry::GetInstance()->GetMediaFileSystemsForExtension( | |
| 575 rvh, GetProfileState(profile)->no_permissions_extension.get(), | |
| 576 base::Bind(&MediaFileSystemRegistryTest::CompareResults, | |
| 577 base::Unretained(this), | |
| 578 StringPrintf("%s (no permission)", test.c_str()), | |
| 579 base::ConstRef(empty_expectation))); | |
| 580 MessageLoop::current()->RunUntilIdle(); | |
| 581 EXPECT_EQ(1, GetAndClearComparisonCount()); | |
| 582 | |
| 583 // Read permission only. | |
| 584 MediaFileSystemRegistry::GetInstance()->GetMediaFileSystemsForExtension( | |
| 585 rvh, GetProfileState(profile)->regular_permission_extension.get(), | |
| 586 base::Bind(&MediaFileSystemRegistryTest::CompareResults, | |
| 587 base::Unretained(this), | |
| 588 StringPrintf("%s (regular permission)", test.c_str()), | |
| 589 base::ConstRef(regular_extension_galleries))); | |
| 590 MessageLoop::current()->RunUntilIdle(); | |
| 591 EXPECT_EQ(1, GetAndClearComparisonCount()); | |
| 592 | |
| 593 // All galleries permission. | |
| 594 MediaFileSystemRegistry::GetInstance()->GetMediaFileSystemsForExtension( | |
| 595 rvh, GetProfileState(profile)->all_permission_extension.get(), | |
| 596 base::Bind(&MediaFileSystemRegistryTest::CompareResults, | |
| 597 base::Unretained(this), | |
| 598 StringPrintf("%s (all permission)", test.c_str()), | |
| 599 base::ConstRef(all_extension_galleries))); | |
| 600 MessageLoop::current()->RunUntilIdle(); | |
| 601 EXPECT_EQ(1, GetAndClearComparisonCount()); | |
| 602 } | |
| 603 | |
| 604 void MediaFileSystemRegistryTest::SetUp() { | |
| 605 ChromeRenderViewHostTestHarness::SetUp(); | |
| 606 DeleteContents(); | |
| 607 SetRenderProcessHostFactory(&rph_factory_); | |
| 608 | |
| 609 MediaStorageUtilTest::SetTestingMode(); | |
| 610 test_file_system_context_ = | |
| 611 new TestMediaFileSystemContext(MediaFileSystemRegistry::GetInstance()); | |
| 612 | |
| 613 ASSERT_TRUE(galleries_dir_.CreateUniqueTempDir()); | |
| 614 empty_dir_ = galleries_dir_.path().Append(FILE_PATH_LITERAL("empty")); | |
| 615 ASSERT_TRUE(file_util::CreateDirectory(empty_dir_)); | |
| 616 dcim_dir_ = galleries_dir_.path().Append(FILE_PATH_LITERAL("with_dcim")); | |
| 617 ASSERT_TRUE(file_util::CreateDirectory(dcim_dir_)); | |
| 618 ASSERT_TRUE(file_util::CreateDirectory(dcim_dir_.Append("DCIM"))); | |
| 619 } | |
| 620 | |
| 621 void MediaFileSystemRegistryTest::TearDown() { | |
| 622 profile_state_.clear(); | |
| 623 ChromeRenderViewHostTestHarness::TearDown(); | |
| 624 MediaFileSystemRegistry* registry = MediaFileSystemRegistry::GetInstance(); | |
| 625 EXPECT_EQ(0U, registry->GetExtensionHostCountForTests()); | |
| 626 } | |
| 627 | |
| 628 /////////// | |
| 629 // Tests // | |
| 630 /////////// | |
| 631 | |
| 632 TEST_F(MediaFileSystemRegistryTest, Basic) { | |
| 633 CreateProfileState(1); | |
| 634 AssertAllAutoAddedGalleries(); | |
| 635 | |
| 636 std::vector<MediaFileSystemInfo> empty_expectation; | |
| 637 std::vector<MediaFileSystemInfo> auto_galleries = GetAutoAddedGalleries(0); | |
| 638 CheckGalleriesForProfile(0, "basic", empty_expectation, auto_galleries); | |
| 639 } | |
| 640 | |
| 641 TEST_F(MediaFileSystemRegistryTest, UserAddedGallery) { | |
| 642 CreateProfileState(1); | |
| 643 AssertAllAutoAddedGalleries(); | |
| 644 | |
| 645 std::vector<MediaFileSystemInfo> added_galleries; | |
| 646 std::vector<MediaFileSystemInfo> auto_galleries = GetAutoAddedGalleries(0); | |
| 647 CheckGalleriesForProfile(0, "user added init", added_galleries, | |
| 648 auto_galleries); | |
| 649 | |
| 650 // Add a user gallery to the regular permission extension. | |
| 651 std::string device_id = AddUserGallery(MediaStorageUtil::FIXED_MASS_STORAGE, | |
| 652 empty_dir().AsUTF8Unsafe(), | |
| 653 empty_dir()); | |
| 654 SetGalleryPermission(0, | |
| 655 GetProfileState(0)->regular_permission_extension.get(), | |
| 656 device_id, | |
| 657 true /*has access*/); | |
| 658 MediaFileSystemInfo added_info(empty_dir().AsUTF8Unsafe(), empty_dir(), | |
| 659 std::string()); | |
| 660 added_galleries.push_back(added_info); | |
| 661 CheckGalleriesForProfile(0, "user added regular", added_galleries, | |
| 662 auto_galleries); | |
| 663 | |
| 664 // Add it to the all galleries extension. | |
| 665 SetGalleryPermission(0, GetProfileState(0)->all_permission_extension.get(), | |
| 666 device_id, true /*has access*/); | |
| 667 auto_galleries.push_back(added_info); | |
| 668 CheckGalleriesForProfile(0, "user added all", added_galleries, | |
| 669 auto_galleries); | |
| 670 } | |
| 671 | |
| 672 } // namespace | |
| 673 | |
| 674 } // namespace chrome | |
| OLD | NEW |