| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // MediaGalleriesPreferences unit tests. | 5 // MediaGalleriesPreferences unit tests. |
| 6 | 6 |
| 7 #include "chrome/browser/media_gallery/media_galleries_preferences.h" | 7 #include "chrome/browser/media_gallery/media_galleries_preferences.h" |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
| 15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "chrome/browser/extensions/extension_service.h" | |
| 17 #include "chrome/browser/extensions/extension_system.h" | 16 #include "chrome/browser/extensions/extension_system.h" |
| 18 #include "chrome/browser/extensions/test_extension_system.h" | 17 #include "chrome/browser/extensions/test_extension_system.h" |
| 19 #include "chrome/browser/media_gallery/media_file_system_registry.h" | 18 #include "chrome/browser/media_gallery/media_file_system_registry.h" |
| 19 #include "chrome/browser/media_gallery/media_galleries_test_util.h" |
| 20 #include "chrome/browser/system_monitor/media_storage_util.h" | 20 #include "chrome/browser/system_monitor/media_storage_util.h" |
| 21 #include "chrome/common/extensions/extension.h" | 21 #include "chrome/common/extensions/extension.h" |
| 22 #include "chrome/common/extensions/extension_manifest_constants.h" | |
| 23 #include "chrome/test/base/testing_profile.h" | 22 #include "chrome/test/base/testing_profile.h" |
| 24 #include "content/public/test/test_browser_thread.h" | 23 #include "content/public/test/test_browser_thread.h" |
| 25 #include "sync/api/string_ordinal.h" | 24 #include "sync/api/string_ordinal.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 26 |
| 28 namespace chrome { | 27 namespace chrome { |
| 29 | 28 |
| 30 namespace { | 29 namespace { |
| 31 | 30 |
| 32 class MediaStorageUtilTest : public MediaStorageUtil { | 31 class MediaStorageUtilTest : public MediaStorageUtil { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 52 | 51 |
| 53 class MediaGalleriesPreferencesTest : public testing::Test { | 52 class MediaGalleriesPreferencesTest : public testing::Test { |
| 54 public: | 53 public: |
| 55 typedef std::map<std::string /*device id*/, MediaGalleryPrefIdSet> | 54 typedef std::map<std::string /*device id*/, MediaGalleryPrefIdSet> |
| 56 DeviceIdPrefIdsMap; | 55 DeviceIdPrefIdsMap; |
| 57 | 56 |
| 58 MediaGalleriesPreferencesTest() | 57 MediaGalleriesPreferencesTest() |
| 59 : ui_thread_(content::BrowserThread::UI, &loop_), | 58 : ui_thread_(content::BrowserThread::UI, &loop_), |
| 60 file_thread_(content::BrowserThread::FILE, &loop_), | 59 file_thread_(content::BrowserThread::FILE, &loop_), |
| 61 profile_(new TestingProfile()), | 60 profile_(new TestingProfile()), |
| 62 extension_service_(NULL), | |
| 63 default_galleries_count_(0) { | 61 default_galleries_count_(0) { |
| 64 MediaStorageUtilTest::SetTestingMode(); | 62 MediaStorageUtilTest::SetTestingMode(); |
| 65 } | 63 } |
| 66 | 64 |
| 67 virtual ~MediaGalleriesPreferencesTest() { | 65 virtual ~MediaGalleriesPreferencesTest() { |
| 68 // TestExtensionSystem uses DeleteSoon, so we need to delete the profile | 66 // TestExtensionSystem uses DeleteSoon, so we need to delete the profile |
| 69 // and then run the message queue to clean up. | 67 // and then run the message queue to clean up. |
| 70 profile_.reset(); | 68 profile_.reset(); |
| 71 MessageLoop::current()->RunAllPending(); | 69 MessageLoop::current()->RunAllPending(); |
| 72 } | 70 } |
| 73 | 71 |
| 74 virtual void SetUp() OVERRIDE { | 72 virtual void SetUp() OVERRIDE { |
| 75 extensions_dir_ = profile_->GetPath().AppendASCII("Extensions"); | |
| 76 ASSERT_TRUE(file_util::CreateDirectory(extensions_dir_)); | |
| 77 | |
| 78 extensions::TestExtensionSystem* extension_system( | 73 extensions::TestExtensionSystem* extension_system( |
| 79 static_cast<extensions::TestExtensionSystem*>( | 74 static_cast<extensions::TestExtensionSystem*>( |
| 80 extensions::ExtensionSystem::Get(profile_.get()))); | 75 extensions::ExtensionSystem::Get(profile_.get()))); |
| 81 extension_service_ = extension_system->CreateExtensionService( | 76 extension_system->CreateExtensionService( |
| 82 CommandLine::ForCurrentProcess(), extensions_dir_, false); | 77 CommandLine::ForCurrentProcess(), FilePath(), false); |
| 83 | 78 |
| 84 gallery_prefs_.reset(new MediaGalleriesPreferences(profile_.get())); | 79 gallery_prefs_.reset(new MediaGalleriesPreferences(profile_.get())); |
| 85 | 80 |
| 86 // Load the default galleries into the expectations. | 81 // Load the default galleries into the expectations. |
| 87 const MediaGalleriesPrefInfoMap& known_galleries = | 82 const MediaGalleriesPrefInfoMap& known_galleries = |
| 88 gallery_prefs_->known_galleries(); | 83 gallery_prefs_->known_galleries(); |
| 89 if (known_galleries.size()) { | 84 if (known_galleries.size()) { |
| 90 ASSERT_EQ(3U, known_galleries.size()); | 85 ASSERT_EQ(3U, known_galleries.size()); |
| 91 default_galleries_count_ = 3; | 86 default_galleries_count_ = 3; |
| 92 MediaGalleriesPrefInfoMap::const_iterator it; | 87 MediaGalleriesPrefInfoMap::const_iterator it; |
| 93 for (it = known_galleries.begin(); it != known_galleries.end(); ++it) { | 88 for (it = known_galleries.begin(); it != known_galleries.end(); ++it) { |
| 94 expected_galleries_[it->first] = it->second; | 89 expected_galleries_[it->first] = it->second; |
| 95 if (it->second.type == MediaGalleryPrefInfo::kAutoDetected) | 90 if (it->second.type == MediaGalleryPrefInfo::kAutoDetected) |
| 96 expected_galleries_for_all.insert(it->first); | 91 expected_galleries_for_all.insert(it->first); |
| 97 } | 92 } |
| 98 } | 93 } |
| 99 | 94 |
| 100 std::vector<std::string> all_permissions; | 95 std::vector<std::string> all_permissions; |
| 101 all_permissions.push_back("allAutoDetected"); | 96 all_permissions.push_back("allAutoDetected"); |
| 102 all_permissions.push_back("read"); | 97 all_permissions.push_back("read"); |
| 103 std::vector<std::string> read_permissions; | 98 std::vector<std::string> read_permissions; |
| 104 read_permissions.push_back("read"); | 99 read_permissions.push_back("read"); |
| 105 | 100 |
| 106 all_permission_extension = AddApp("all", all_permissions); | 101 all_permission_extension = |
| 107 regular_permission_extension = AddApp("regular", read_permissions); | 102 AddMediaGalleriesApp("all", all_permissions, profile_.get()); |
| 108 no_permissions_extension = AddApp("no", read_permissions); | 103 regular_permission_extension = |
| 104 AddMediaGalleriesApp("regular", read_permissions, profile_.get()); |
| 105 no_permissions_extension = |
| 106 AddMediaGalleriesApp("no", read_permissions, profile_.get()); |
| 109 } | 107 } |
| 110 | 108 |
| 111 virtual void TearDown() OVERRIDE { | 109 virtual void TearDown() OVERRIDE { |
| 112 Verify(); | 110 Verify(); |
| 113 } | 111 } |
| 114 | 112 |
| 115 void Verify() { | 113 void Verify() { |
| 116 const MediaGalleriesPrefInfoMap& known_galleries = | 114 const MediaGalleriesPrefInfoMap& known_galleries = |
| 117 gallery_prefs_->known_galleries(); | 115 gallery_prefs_->known_galleries(); |
| 118 EXPECT_EQ(expected_galleries_.size(), known_galleries.size()); | 116 EXPECT_EQ(expected_galleries_.size(), known_galleries.size()); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 scoped_refptr<extensions::Extension> no_permissions_extension; | 182 scoped_refptr<extensions::Extension> no_permissions_extension; |
| 185 | 183 |
| 186 std::set<MediaGalleryPrefId> expected_galleries_for_all; | 184 std::set<MediaGalleryPrefId> expected_galleries_for_all; |
| 187 std::set<MediaGalleryPrefId> expected_galleries_for_regular; | 185 std::set<MediaGalleryPrefId> expected_galleries_for_regular; |
| 188 | 186 |
| 189 DeviceIdPrefIdsMap expected_device_map; | 187 DeviceIdPrefIdsMap expected_device_map; |
| 190 | 188 |
| 191 MediaGalleriesPrefInfoMap expected_galleries_; | 189 MediaGalleriesPrefInfoMap expected_galleries_; |
| 192 | 190 |
| 193 private: | 191 private: |
| 194 scoped_refptr<extensions::Extension> AddApp( | |
| 195 std::string name, | |
| 196 std::vector<std::string> media_galleries_permissions) { | |
| 197 scoped_ptr<DictionaryValue> manifest(new DictionaryValue); | |
| 198 manifest->SetString(extension_manifest_keys::kName, name); | |
| 199 manifest->SetString(extension_manifest_keys::kVersion, "0.1"); | |
| 200 manifest->SetInteger(extension_manifest_keys::kManifestVersion, 2); | |
| 201 ListValue* background_script_list = new ListValue; | |
| 202 background_script_list->Append(Value::CreateStringValue("background.js")); | |
| 203 manifest->Set(extension_manifest_keys::kPlatformAppBackgroundScripts, | |
| 204 background_script_list); | |
| 205 | |
| 206 ListValue* permission_detail_list = new ListValue; | |
| 207 for (size_t i = 0; i < media_galleries_permissions.size(); i++) | |
| 208 permission_detail_list->Append( | |
| 209 Value::CreateStringValue(media_galleries_permissions[i])); | |
| 210 DictionaryValue* media_galleries_permission = new DictionaryValue(); | |
| 211 media_galleries_permission->Set("mediaGalleries", permission_detail_list); | |
| 212 ListValue* permission_list = new ListValue; | |
| 213 permission_list->Append(media_galleries_permission); | |
| 214 manifest->Set(extension_manifest_keys::kPermissions, permission_list); | |
| 215 | |
| 216 FilePath path = extensions_dir_.AppendASCII(name); | |
| 217 std::string errors; | |
| 218 scoped_refptr<extensions::Extension> extension = | |
| 219 extensions::Extension::Create(path, | |
| 220 extensions::Extension::INTERNAL, | |
| 221 *manifest.get(), | |
| 222 extensions::Extension::NO_FLAGS, | |
| 223 &errors); | |
| 224 EXPECT_TRUE(extension.get() != NULL) << errors; | |
| 225 EXPECT_TRUE(extensions::Extension::IdIsValid(extension->id())); | |
| 226 if (!extension.get() || | |
| 227 !extensions::Extension::IdIsValid(extension->id())) { | |
| 228 return NULL; | |
| 229 } | |
| 230 | |
| 231 extension_service_->extension_prefs()->OnExtensionInstalled( | |
| 232 extension, extensions::Extension::ENABLED, | |
| 233 syncer::StringOrdinal::CreateInitialOrdinal()); | |
| 234 | |
| 235 return extension; | |
| 236 } | |
| 237 | |
| 238 // Needed for extension service & friends to work. | 192 // Needed for extension service & friends to work. |
| 239 MessageLoop loop_; | 193 MessageLoop loop_; |
| 240 content::TestBrowserThread ui_thread_; | 194 content::TestBrowserThread ui_thread_; |
| 241 content::TestBrowserThread file_thread_; | 195 content::TestBrowserThread file_thread_; |
| 242 | 196 |
| 243 scoped_ptr<TestingProfile> profile_; | 197 scoped_ptr<TestingProfile> profile_; |
| 244 scoped_ptr<MediaGalleriesPreferences> gallery_prefs_; | 198 scoped_ptr<MediaGalleriesPreferences> gallery_prefs_; |
| 245 FilePath extensions_dir_; | |
| 246 ExtensionService* extension_service_; | |
| 247 | 199 |
| 248 uint64 default_galleries_count_; | 200 uint64 default_galleries_count_; |
| 249 | 201 |
| 250 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPreferencesTest); | 202 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPreferencesTest); |
| 251 }; | 203 }; |
| 252 | 204 |
| 253 FilePath MakePath(std::string dir) { | 205 FilePath MakePath(std::string dir) { |
| 254 #if defined(OS_WIN) | 206 #if defined(OS_WIN) |
| 255 return FilePath(FILE_PATH_LITERAL("C:\\")).Append(UTF8ToWide(dir)); | 207 return FilePath(FILE_PATH_LITERAL("C:\\")).Append(UTF8ToWide(dir)); |
| 256 #elif defined(OS_POSIX) | 208 #elif defined(OS_POSIX) |
| (...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 603 // Check that adding one of them again works as expected. | 555 // Check that adding one of them again works as expected. |
| 604 MediaGalleryPrefId id = gallery_prefs()->AddGallery( | 556 MediaGalleryPrefId id = gallery_prefs()->AddGallery( |
| 605 device_id, device_name, relative_path, true /*user*/); | 557 device_id, device_name, relative_path, true /*user*/); |
| 606 EXPECT_EQ(dev2_path2_id, id); | 558 EXPECT_EQ(dev2_path2_id, id); |
| 607 Verify(); | 559 Verify(); |
| 608 } | 560 } |
| 609 | 561 |
| 610 } // namespace | 562 } // namespace |
| 611 | 563 |
| 612 } // namespace chrome | 564 } // namespace chrome |
| OLD | NEW |