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