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