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/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 scoped_ptr<TestingProfile> profile; | |
|
Lei Zhang
2012/11/15 18:02:32
I think |profile| can be private now.
vandebo (ex-Chrome)
2012/11/16 06:04:32
Done.
| |
| 188 | |
| 189 scoped_refptr<extensions::Extension> all_permission_extension; | |
|
Lei Zhang
2012/11/15 18:02:32
And these can be private with a simple accessor.
vandebo (ex-Chrome)
2012/11/16 06:04:32
Done.
| |
| 190 scoped_refptr<extensions::Extension> regular_permission_extension; | |
| 191 scoped_refptr<extensions::Extension> no_permissions_extension; | |
| 192 | |
| 193 private: | |
| 194 void CompareResults(const std::string& test, | |
| 195 const std::vector<MediaFileSystemInfo>& expected, | |
| 196 const std::vector<MediaFileSystemInfo>& actual); | |
| 197 | |
| 198 int GetAndClearComparisonCount(); | |
| 199 | |
| 200 int num_comparisons_; | |
| 201 | |
| 202 scoped_ptr<content::WebContents> single_web_contents_; | |
| 203 scoped_ptr<content::WebContents> shared_web_contents1_; | |
| 204 scoped_ptr<content::WebContents> shared_web_contents2_; | |
| 205 | |
| 206 // The RenderProcessHosts are freed when their respective WebContents / | |
| 207 // RenderViewHosts go away. | |
| 208 content::MockRenderProcessHost* single_rph_; | |
| 209 content::MockRenderProcessHost* shared_rph_; | |
| 210 | |
| 211 DISALLOW_COPY_AND_ASSIGN(ProfileState); | |
| 212 }; | |
| 213 | |
| 214 class MediaFileSystemRegistryTest : public ChromeRenderViewHostTestHarness { | |
| 215 public: | |
| 216 MediaFileSystemRegistryTest(); | |
| 217 virtual ~MediaFileSystemRegistryTest() {} | |
| 218 | |
| 219 void CreateProfileState(int profile_count); | |
| 220 | |
| 221 ProfileState* GetProfileState(int i); | |
| 222 | |
| 223 FilePath empty_dir() { | |
| 224 return empty_dir_; | |
| 225 } | |
| 226 | |
| 227 FilePath dcim_dir() { | |
| 228 return dcim_dir_; | |
| 229 } | |
| 230 | |
| 231 // Create a user added gallery based on the information passed and add it to | |
| 232 // |profiles|. Returns the device id. | |
| 233 std::string AddUserGallery(MediaStorageUtil::Type type, | |
| 234 const std::string& unique_id, | |
| 235 const FilePath& path); | |
| 236 | |
| 237 void AttachDevice(MediaStorageUtil::Type type, const std::string& unique_id, | |
| 238 const FilePath& location); | |
| 239 | |
| 240 void DetachDevice(const std::string& device_id); | |
| 241 | |
| 242 void SetGalleryPermission(size_t profile, extensions::Extension* extension, | |
| 243 const std::string& device_id, bool has_access); | |
| 244 | |
| 245 void AssertAllAutoAddedGalleries(); | |
| 246 | |
| 247 std::vector<MediaFileSystemInfo> GetAutoAddedGalleries(size_t profile); | |
| 248 | |
| 249 protected: | |
| 250 void SetUp(); | |
| 251 void TearDown(); | |
| 252 | |
| 253 private: | |
| 254 // This makes sure that at least one default gallery exists on the file | |
| 255 // system. | |
| 256 EnsureMediaDirectoriesExists media_directories_; | |
| 257 | |
| 258 // Some test gallery directories. | |
| 259 ScopedTempDir galleries_dir_; | |
| 260 // An empty directory in |galleries_dir_| | |
| 261 FilePath empty_dir_; | |
| 262 // A directory in |galleries_dir_| with a DCIM directory in it. | |
| 263 FilePath dcim_dir_; | |
| 264 | |
| 265 // MediaFileSystemRegistry owns this. | |
| 266 TestMediaFileSystemContext* test_file_system_context_; | |
| 267 | |
| 268 // Needed for extension service & friends to work. | |
| 269 content::TestBrowserThread ui_thread_; | |
| 270 content::TestBrowserThread file_thread_; | |
| 271 | |
| 272 MockProfileSharedRenderProcessHostFactory rph_factory_; | |
| 273 | |
| 274 ScopedVector<ProfileState> profile_states_; | |
| 275 | |
| 276 DISALLOW_COPY_AND_ASSIGN(MediaFileSystemRegistryTest); | |
| 277 }; | |
| 278 | |
| 279 bool MediaFileSystemInfoComparator(const MediaFileSystemInfo& a, | |
| 280 const MediaFileSystemInfo& b) { | |
| 281 if (a.name != b.name) | |
| 282 return a.name < b.name; | |
| 283 if (a.path.value() != b.path.value()) | |
| 284 return a.path.value() < b.path.value(); | |
| 285 return a.fsid < b.fsid; | |
| 286 } | |
| 287 | |
| 288 ////////////////////////// | |
| 289 // TestMediaStorageUtil // | |
| 290 ////////////////////////// | |
| 291 | |
| 292 // static | |
| 293 void TestMediaStorageUtil::SetTestingMode() { | |
| 294 SetGetDeviceInfoFromPathFunctionForTesting( | |
| 295 &GetDeviceInfoFromPathTestFunction); | |
| 296 } | |
| 297 | |
| 298 // static | |
| 299 bool TestMediaStorageUtil::GetDeviceInfoFromPathTestFunction( | |
| 300 const FilePath& path, std::string* device_id, string16* device_name, | |
| 301 FilePath* relative_path) { | |
| 302 if (device_id) | |
| 303 *device_id = MakeDeviceId(FIXED_MASS_STORAGE, path.AsUTF8Unsafe()); | |
| 304 if (device_name) | |
| 305 *device_name = path.BaseName().LossyDisplayName(); | |
| 306 if (relative_path) | |
| 307 *relative_path = FilePath(); | |
| 308 return true; | |
| 309 } | |
| 310 | |
| 311 /////////////////////////////////////////////// | |
| 312 // MockProfileSharedRenderProcessHostFactory // | |
| 313 /////////////////////////////////////////////// | |
| 314 | |
| 315 MockProfileSharedRenderProcessHostFactory:: | |
| 316 ~MockProfileSharedRenderProcessHostFactory() { | |
| 317 STLDeleteValues(&rph_map_); | |
| 318 } | |
| 319 | |
| 320 content::MockRenderProcessHost* | |
| 321 MockProfileSharedRenderProcessHostFactory::ReleaseRPH( | |
| 322 content::BrowserContext* browser_context) { | |
| 323 ProfileRPHMap::iterator existing = rph_map_.find(browser_context); | |
| 324 if (existing == rph_map_.end()) | |
| 325 return NULL; | |
| 326 content::MockRenderProcessHost* result = existing->second; | |
| 327 rph_map_.erase(existing); | |
| 328 return result; | |
| 329 } | |
| 330 | |
| 331 content::RenderProcessHost* | |
| 332 MockProfileSharedRenderProcessHostFactory::CreateRenderProcessHost( | |
| 333 content::BrowserContext* browser_context) const { | |
| 334 ProfileRPHMap::const_iterator existing = rph_map_.find(browser_context); | |
| 335 if (existing != rph_map_.end()) | |
| 336 return existing->second; | |
| 337 rph_map_[browser_context] = | |
| 338 new content::MockRenderProcessHost(browser_context); | |
| 339 return rph_map_[browser_context]; | |
| 340 } | |
| 341 | |
| 342 ////////////////// | |
| 343 // ProfileState // | |
| 344 ////////////////// | |
| 345 | |
| 346 ProfileState::ProfileState( | |
| 347 MockProfileSharedRenderProcessHostFactory* rph_factory) | |
| 348 : profile(new TestingProfile()), | |
| 349 num_comparisons_(0) { | |
| 350 extensions::TestExtensionSystem* extension_system( | |
| 351 static_cast<extensions::TestExtensionSystem*>( | |
| 352 extensions::ExtensionSystem::Get(profile.get()))); | |
| 353 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 = | |
| 363 AddMediaGalleriesApp("all", all_permissions, profile.get()); | |
| 364 regular_permission_extension = | |
| 365 AddMediaGalleriesApp("regular", read_permissions, profile.get()); | |
| 366 no_permissions_extension = | |
| 367 AddMediaGalleriesApp("no", read_permissions, profile.get()); | |
| 368 | |
| 369 single_web_contents_.reset( | |
| 370 content::WebContentsTester::CreateTestWebContents(profile.get(), NULL)); | |
| 371 single_rph_ = rph_factory->ReleaseRPH(profile.get()); | |
| 372 | |
| 373 shared_web_contents1_.reset( | |
| 374 content::WebContentsTester::CreateTestWebContents(profile.get(), NULL)); | |
| 375 shared_web_contents2_.reset( | |
| 376 content::WebContentsTester::CreateTestWebContents(profile.get(), NULL)); | |
| 377 shared_rph_ = rph_factory->ReleaseRPH(profile.get()); | |
| 378 } | |
| 379 | |
| 380 ProfileState::~ProfileState() { | |
| 381 // TestExtensionSystem uses DeleteSoon, so we need to delete the profiles | |
| 382 // and then run the message queue to clean up. But first we have to | |
| 383 // delete everything that references the profile. | |
| 384 single_web_contents_.reset(); | |
| 385 shared_web_contents1_.reset(); | |
| 386 shared_web_contents2_.reset(); | |
| 387 profile.reset(); | |
| 388 | |
| 389 MessageLoop::current()->RunUntilIdle(); | |
| 390 } | |
| 391 | |
| 392 MediaGalleriesPreferences* ProfileState::GetMediaGalleriesPrefs() { | |
| 393 return MediaGalleriesPreferencesFactory::GetForProfile(profile.get()); | |
| 394 } | |
| 395 | |
| 396 void ProfileState::CheckGalleries( | |
| 397 const std::string& test, | |
| 398 const std::vector<MediaFileSystemInfo>& regular_extension_galleries, | |
| 399 const std::vector<MediaFileSystemInfo>& all_extension_galleries) { | |
| 400 content::RenderViewHost* rvh = single_web_contents_->GetRenderViewHost(); | |
| 401 | |
| 402 // No Media Galleries permissions. | |
| 403 std::vector<MediaFileSystemInfo> empty_expectation; | |
| 404 MediaFileSystemRegistry::GetInstance()->GetMediaFileSystemsForExtension( | |
| 405 rvh, no_permissions_extension.get(), | |
| 406 base::Bind(&ProfileState::CompareResults, base::Unretained(this), | |
| 407 StringPrintf("%s (no permission)", test.c_str()), | |
| 408 base::ConstRef(empty_expectation))); | |
| 409 MessageLoop::current()->RunUntilIdle(); | |
| 410 EXPECT_EQ(1, GetAndClearComparisonCount()); | |
| 411 | |
| 412 // Read permission only. | |
| 413 MediaFileSystemRegistry::GetInstance()->GetMediaFileSystemsForExtension( | |
| 414 rvh, regular_permission_extension.get(), | |
| 415 base::Bind(&ProfileState::CompareResults, base::Unretained(this), | |
| 416 StringPrintf("%s (regular permission)", test.c_str()), | |
| 417 base::ConstRef(regular_extension_galleries))); | |
| 418 MessageLoop::current()->RunUntilIdle(); | |
| 419 EXPECT_EQ(1, GetAndClearComparisonCount()); | |
| 420 | |
| 421 // All galleries permission. | |
| 422 MediaFileSystemRegistry::GetInstance()->GetMediaFileSystemsForExtension( | |
| 423 rvh, all_permission_extension.get(), | |
| 424 base::Bind(&ProfileState::CompareResults, base::Unretained(this), | |
| 425 StringPrintf("%s (all permission)", test.c_str()), | |
| 426 base::ConstRef(all_extension_galleries))); | |
| 427 MessageLoop::current()->RunUntilIdle(); | |
| 428 EXPECT_EQ(1, GetAndClearComparisonCount()); | |
| 429 } | |
| 430 | |
| 431 void ProfileState::CompareResults( | |
| 432 const std::string& test, | |
| 433 const std::vector<MediaFileSystemInfo>& expected, | |
| 434 const std::vector<MediaFileSystemInfo>& actual) { | |
| 435 // Order isn't important, so sort the results. Assume that expected | |
| 436 // is already sorted. | |
| 437 std::vector<MediaFileSystemInfo> sorted(actual); | |
| 438 std::sort(sorted.begin(), sorted.end(), MediaFileSystemInfoComparator); | |
| 439 | |
| 440 num_comparisons_++; | |
| 441 EXPECT_EQ(expected.size(), actual.size()) << test; | |
| 442 for (size_t i = 0; i < expected.size() && i < actual.size(); i++) { | |
| 443 EXPECT_EQ(expected[i].path.value(), actual[i].path.value()) << test; | |
| 444 EXPECT_FALSE(actual[i].fsid.empty()) << test; | |
| 445 if (!expected[i].fsid.empty()) | |
| 446 EXPECT_EQ(expected[i].fsid, actual[i].fsid) << test; | |
| 447 } | |
| 448 } | |
| 449 | |
| 450 int ProfileState::GetAndClearComparisonCount() { | |
| 451 int result = num_comparisons_; | |
| 452 num_comparisons_ = 0; | |
| 453 return result; | |
| 454 } | |
| 455 | |
| 456 ///////////////////////////////// | |
| 457 // MediaFileSystemRegistryTest // | |
| 458 ///////////////////////////////// | |
| 459 | |
| 460 MediaFileSystemRegistryTest::MediaFileSystemRegistryTest() | |
| 461 : ui_thread_(content::BrowserThread::UI, MessageLoop::current()), | |
| 462 file_thread_(content::BrowserThread::FILE, MessageLoop::current()) { | |
| 463 } | |
| 464 | |
| 465 void MediaFileSystemRegistryTest::CreateProfileState(int profile_count) { | |
| 466 for (int i = 0; i < profile_count; i++) { | |
| 467 ProfileState * state = new ProfileState(&rph_factory_); | |
| 468 profile_states_.push_back(state); | |
| 469 } | |
| 470 } | |
| 471 | |
| 472 ProfileState* MediaFileSystemRegistryTest::GetProfileState(int i) { | |
| 473 return profile_states_[i]; | |
| 474 } | |
| 475 | |
| 476 std::string MediaFileSystemRegistryTest::AddUserGallery( | |
| 477 MediaStorageUtil::Type type, | |
| 478 const std::string& unique_id, | |
| 479 const FilePath& path) { | |
| 480 std::string device_id = MediaStorageUtil::MakeDeviceId(type, unique_id); | |
| 481 string16 name = path.LossyDisplayName(); | |
| 482 DCHECK(!MediaStorageUtil::IsMediaDevice(device_id)); | |
| 483 | |
| 484 for (size_t i = 0; i < profile_states_.size(); i++) { | |
| 485 profile_states_[i]->GetMediaGalleriesPrefs()->AddGallery( | |
| 486 device_id, name, FilePath(), true /*user_added*/); | |
| 487 } | |
| 488 return device_id; | |
| 489 } | |
| 490 | |
| 491 void MediaFileSystemRegistryTest::AttachDevice(MediaStorageUtil::Type type, | |
| 492 const std::string& unique_id, | |
| 493 const FilePath& location) { | |
| 494 std::string device_id = MediaStorageUtil::MakeDeviceId(type, unique_id); | |
| 495 DCHECK(MediaStorageUtil::IsRemovableDevice(device_id)); | |
| 496 string16 name = location.LossyDisplayName(); | |
| 497 base::SystemMonitor::Get()->ProcessRemovableStorageAttached(device_id, name, | |
| 498 location.value()); | |
| 499 } | |
| 500 | |
| 501 void MediaFileSystemRegistryTest::DetachDevice(const std::string& device_id) { | |
| 502 DCHECK(MediaStorageUtil::IsRemovableDevice(device_id)); | |
| 503 base::SystemMonitor::Get()->ProcessRemovableStorageDetached(device_id); | |
| 504 } | |
| 505 | |
| 506 void MediaFileSystemRegistryTest::SetGalleryPermission( | |
| 507 size_t profile, extensions::Extension* extension, | |
| 508 const std::string& device_id, bool has_access) { | |
| 509 MediaGalleriesPreferences* preferences = | |
| 510 GetProfileState(profile)->GetMediaGalleriesPrefs(); | |
| 511 MediaGalleryPrefIdSet pref_id = | |
| 512 preferences->LookUpGalleriesByDeviceId(device_id); | |
| 513 DCHECK_EQ(1U, pref_id.size()); | |
| 514 preferences->SetGalleryPermissionForExtension(*extension, *pref_id.begin(), | |
| 515 has_access); | |
| 516 } | |
| 517 | |
| 518 void MediaFileSystemRegistryTest::AssertAllAutoAddedGalleries() { | |
| 519 for (size_t i = 0; i < profile_states_.size(); i++) { | |
| 520 MediaGalleriesPreferences* prefs = | |
| 521 profile_states_[0]->GetMediaGalleriesPrefs(); | |
| 522 | |
| 523 // Make sure that we have at least one gallery and that they are all | |
| 524 // auto added galleries. | |
| 525 const MediaGalleriesPrefInfoMap& galleries = prefs->known_galleries(); | |
| 526 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID) | |
| 527 ASSERT_GT(galleries.size(), 0U); | |
| 528 #endif | |
| 529 for (MediaGalleriesPrefInfoMap::const_iterator it = galleries.begin(); | |
| 530 it != galleries.end(); | |
| 531 ++it) { | |
| 532 ASSERT_EQ(MediaGalleryPrefInfo::kAutoDetected, it->second.type); | |
| 533 } | |
| 534 } | |
| 535 } | |
| 536 | |
| 537 std::vector<MediaFileSystemInfo> | |
| 538 MediaFileSystemRegistryTest::GetAutoAddedGalleries(size_t profile) { | |
| 539 DCHECK_LT(profile, profile_states_.size()); | |
| 540 const MediaGalleriesPrefInfoMap& galleries = | |
| 541 GetProfileState(profile)->GetMediaGalleriesPrefs()->known_galleries(); | |
| 542 std::vector<MediaFileSystemInfo> result; | |
| 543 for (MediaGalleriesPrefInfoMap::const_iterator it = galleries.begin(); | |
| 544 it != galleries.end(); | |
| 545 ++it) { | |
| 546 if (it->second.type == MediaGalleryPrefInfo::kAutoDetected) { | |
| 547 FilePath path = it->second.AbsolutePath(); | |
| 548 MediaFileSystemInfo info(std::string(), path, std::string()); | |
| 549 result.push_back(info); | |
| 550 } | |
| 551 } | |
| 552 std::sort(result.begin(), result.end(), MediaFileSystemInfoComparator); | |
| 553 return result; | |
| 554 } | |
| 555 | |
| 556 void MediaFileSystemRegistryTest::SetUp() { | |
| 557 ChromeRenderViewHostTestHarness::SetUp(); | |
| 558 DeleteContents(); | |
| 559 SetRenderProcessHostFactory(&rph_factory_); | |
| 560 | |
| 561 TestMediaStorageUtil::SetTestingMode(); | |
| 562 test_file_system_context_ = | |
| 563 new TestMediaFileSystemContext(MediaFileSystemRegistry::GetInstance()); | |
| 564 | |
| 565 ASSERT_TRUE(galleries_dir_.CreateUniqueTempDir()); | |
| 566 empty_dir_ = galleries_dir_.path().AppendASCII("empty"); | |
| 567 ASSERT_TRUE(file_util::CreateDirectory(empty_dir_)); | |
| 568 dcim_dir_ = galleries_dir_.path().AppendASCII("with_dcim"); | |
| 569 ASSERT_TRUE(file_util::CreateDirectory(dcim_dir_)); | |
| 570 ASSERT_TRUE(file_util::CreateDirectory(dcim_dir_.Append(kDCIMDirectoryName))); | |
| 571 } | |
| 572 | |
| 573 void MediaFileSystemRegistryTest::TearDown() { | |
| 574 profile_states_.clear(); | |
| 575 ChromeRenderViewHostTestHarness::TearDown(); | |
| 576 MediaFileSystemRegistry* registry = MediaFileSystemRegistry::GetInstance(); | |
| 577 EXPECT_EQ(0U, registry->GetExtensionHostCountForTests()); | |
| 578 } | |
| 579 | |
| 580 /////////// | |
| 581 // Tests // | |
| 582 /////////// | |
| 583 | |
| 584 TEST_F(MediaFileSystemRegistryTest, Basic) { | |
| 585 CreateProfileState(1); | |
| 586 AssertAllAutoAddedGalleries(); | |
| 587 | |
| 588 std::vector<MediaFileSystemInfo> empty_expectation; | |
| 589 std::vector<MediaFileSystemInfo> auto_galleries = GetAutoAddedGalleries(0); | |
| 590 GetProfileState(0)->CheckGalleries("basic", empty_expectation, | |
| 591 auto_galleries); | |
| 592 } | |
| 593 | |
| 594 TEST_F(MediaFileSystemRegistryTest, UserAddedGallery) { | |
| 595 CreateProfileState(1); | |
| 596 AssertAllAutoAddedGalleries(); | |
| 597 | |
| 598 std::vector<MediaFileSystemInfo> added_galleries; | |
| 599 std::vector<MediaFileSystemInfo> auto_galleries = GetAutoAddedGalleries(0); | |
| 600 GetProfileState(0)->CheckGalleries("user added init", added_galleries, | |
| 601 auto_galleries); | |
| 602 | |
| 603 // Add a user gallery to the regular permission extension. | |
| 604 std::string device_id = AddUserGallery(MediaStorageUtil::FIXED_MASS_STORAGE, | |
| 605 empty_dir().AsUTF8Unsafe(), | |
| 606 empty_dir()); | |
| 607 SetGalleryPermission(0, | |
| 608 GetProfileState(0)->regular_permission_extension.get(), | |
| 609 device_id, | |
| 610 true /*has access*/); | |
| 611 MediaFileSystemInfo added_info(empty_dir().AsUTF8Unsafe(), empty_dir(), | |
| 612 std::string()); | |
| 613 added_galleries.push_back(added_info); | |
| 614 GetProfileState(0)->CheckGalleries("user added regular", added_galleries, | |
| 615 auto_galleries); | |
| 616 | |
| 617 // Add it to the all galleries extension. | |
| 618 SetGalleryPermission(0, GetProfileState(0)->all_permission_extension.get(), | |
| 619 device_id, true /*has access*/); | |
| 620 auto_galleries.push_back(added_info); | |
| 621 GetProfileState(0)->CheckGalleries("user added all", added_galleries, | |
| 622 auto_galleries); | |
| 623 } | |
| 624 | |
| 625 } // namespace | |
| 626 | |
| 627 } // namespace chrome | |
| OLD | NEW |