media_galleries_test_util.cc revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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#include "chrome/browser/media_galleries/media_galleries_test_util.h" 6 7#if defined(OS_WIN) 8#include <windows.h> 9#endif 10 11#include "base/base_paths.h" 12#include "base/file_util.h" 13#include "base/files/file_path.h" 14#include "base/path_service.h" 15#include "base/strings/stringprintf.h" 16#include "chrome/browser/extensions/extension_prefs.h" 17#include "chrome/browser/extensions/extension_service.h" 18#include "chrome/browser/extensions/extension_system.h" 19#include "chrome/browser/media_galleries/fileapi/picasa_finder.h" 20#include "chrome/browser/profiles/profile.h" 21#include "chrome/common/chrome_paths.h" 22#include "extensions/common/extension.h" 23#include "extensions/common/manifest_constants.h" 24#include "testing/gtest/include/gtest/gtest.h" 25 26#if defined(OS_MACOSX) 27#include "base/mac/foundation_util.h" 28#include "base/strings/sys_string_conversions.h" 29#include "chrome/browser/media_galleries/fileapi/iapps_finder_impl.h" 30#include "components/policy/core/common/preferences_mock_mac.h" 31#endif // OS_MACOSX 32 33#if defined(OS_WIN) 34#include "base/test/test_reg_util_win.h" 35#include "base/win/registry.h" 36#endif // OS_WIN 37 38scoped_refptr<extensions::Extension> AddMediaGalleriesApp( 39 const std::string& name, 40 const std::vector<std::string>& media_galleries_permissions, 41 Profile* profile) { 42 scoped_ptr<DictionaryValue> manifest(new DictionaryValue); 43 manifest->SetString(extensions::manifest_keys::kName, name); 44 manifest->SetString(extensions::manifest_keys::kVersion, "0.1"); 45 manifest->SetInteger(extensions::manifest_keys::kManifestVersion, 2); 46 ListValue* background_script_list = new ListValue; 47 background_script_list->Append(Value::CreateStringValue("background.js")); 48 manifest->Set(extensions::manifest_keys::kPlatformAppBackgroundScripts, 49 background_script_list); 50 51 ListValue* permission_detail_list = new ListValue; 52 for (size_t i = 0; i < media_galleries_permissions.size(); i++) 53 permission_detail_list->Append( 54 Value::CreateStringValue(media_galleries_permissions[i])); 55 DictionaryValue* media_galleries_permission = new DictionaryValue(); 56 media_galleries_permission->Set("mediaGalleries", permission_detail_list); 57 ListValue* permission_list = new ListValue; 58 permission_list->Append(media_galleries_permission); 59 manifest->Set(extensions::manifest_keys::kPermissions, permission_list); 60 61 extensions::ExtensionPrefs* extension_prefs = 62 extensions::ExtensionPrefs::Get(profile); 63 base::FilePath path = extension_prefs->install_directory().AppendASCII(name); 64 std::string errors; 65 scoped_refptr<extensions::Extension> extension = 66 extensions::Extension::Create(path, extensions::Manifest::INTERNAL, 67 *manifest.get(), 68 extensions::Extension::NO_FLAGS, &errors); 69 EXPECT_TRUE(extension.get() != NULL) << errors; 70 EXPECT_TRUE(extensions::Extension::IdIsValid(extension->id())); 71 if (!extension.get() || !extensions::Extension::IdIsValid(extension->id())) 72 return NULL; 73 74 extension_prefs->OnExtensionInstalled( 75 extension.get(), 76 extensions::Extension::ENABLED, 77 false, 78 syncer::StringOrdinal::CreateInitialOrdinal()); 79 ExtensionService* extension_service = 80 extensions::ExtensionSystem::Get(profile)->extension_service(); 81 extension_service->AddExtension(extension.get()); 82 extension_service->EnableExtension(extension->id()); 83 84 return extension; 85} 86 87EnsureMediaDirectoriesExists::EnsureMediaDirectoriesExists() 88 : num_galleries_(0) { 89 Init(); 90} 91 92EnsureMediaDirectoriesExists::~EnsureMediaDirectoriesExists() { 93#if defined(OS_MACOSX) 94 iapps::SetMacPreferencesForTesting(NULL); 95 picasa::SetMacPreferencesForTesting(NULL); 96#endif // OS_MACOSX 97} 98 99base::FilePath EnsureMediaDirectoriesExists::GetFakeAppDataPath() const { 100 DCHECK(fake_dir_.IsValid()); 101 return fake_dir_.path().AppendASCII("appdata"); 102} 103 104#if defined(OS_WIN) 105base::FilePath EnsureMediaDirectoriesExists::GetFakeLocalAppDataPath() const { 106 DCHECK(fake_dir_.IsValid()); 107 return fake_dir_.path().AppendASCII("localappdata"); 108} 109 110void EnsureMediaDirectoriesExists::SetCustomPicasaAppDataPath( 111 const base::FilePath& path) { 112 base::win::RegKey key(HKEY_CURRENT_USER, picasa::kPicasaRegistryPath, 113 KEY_SET_VALUE); 114 key.WriteValue(picasa::kPicasaRegistryAppDataPathKey, path.value().c_str()); 115} 116#endif // OS_WIN 117 118#if defined(OS_MACOSX) 119void EnsureMediaDirectoriesExists::SetCustomPicasaAppDataPath( 120 const base::FilePath& path) { 121 mac_preferences_->AddTestItem( 122 base::mac::NSToCFCast(picasa::kPicasaAppDataPathMacPreferencesKey), 123 base::SysUTF8ToNSString(path.value()), 124 false); 125} 126#endif // OS_MACOSX 127 128#if defined(OS_WIN) || defined(OS_MACOSX) 129base::FilePath 130EnsureMediaDirectoriesExists::GetFakePicasaFoldersRootPath() const { 131 DCHECK(fake_dir_.IsValid()); 132 return fake_dir_.path().AppendASCII("picasa_folders"); 133} 134#endif // OS_WIN || OS_MACOSX 135 136void EnsureMediaDirectoriesExists::Init() { 137#if defined(OS_CHROMEOS) || defined(OS_ANDROID) 138 return; 139#else 140 141 ASSERT_TRUE(fake_dir_.CreateUniqueTempDir()); 142 143#if defined(OS_WIN) || defined(OS_MACOSX) 144 // This is to control whether or not tests think iTunes (on Windows) and 145 // Picasa are installed. 146 app_data_override_.reset(new base::ScopedPathOverride( 147 base::DIR_APP_DATA, GetFakeAppDataPath())); 148#endif // OS_WIN || OS_MACOSX 149 150#if defined(OS_WIN) 151 // Picasa on Windows is by default in the DIR_LOCAL_APP_DATA directory. 152 local_app_data_override_.reset(new base::ScopedPathOverride( 153 base::DIR_LOCAL_APP_DATA, GetFakeLocalAppDataPath())); 154 // Picasa also looks in the registry for an alternate path. 155 registry_override_.OverrideRegistry(HKEY_CURRENT_USER, L"hkcu_picasa"); 156#endif // OS_WIN 157 158#if defined(OS_MACOSX) 159 mac_preferences_.reset(new MockPreferences); 160 iapps::SetMacPreferencesForTesting(mac_preferences_.get()); 161 picasa::SetMacPreferencesForTesting(mac_preferences_.get()); 162 163 // iTunes override. 164 mac_preferences_->AddTestItem( 165 base::mac::NSToCFCast(iapps::kITunesRecentDatabasePathsKey), 166 base::SysUTF8ToNSString(fake_dir_.path().AppendASCII("itunes").value()), 167 false); 168 169 // iPhoto override. 170 mac_preferences_->AddTestItem( 171 base::mac::NSToCFCast(iapps::kIPhotoRecentDatabasesKey), 172 base::SysUTF8ToNSString(fake_dir_.path().AppendASCII("iphoto").value()), 173 false); 174#endif // OS_MACOSX 175 176 music_override_.reset(new base::ScopedPathOverride( 177 chrome::DIR_USER_MUSIC, fake_dir_.path().AppendASCII("music"))); 178 pictures_override_.reset(new base::ScopedPathOverride( 179 chrome::DIR_USER_PICTURES, fake_dir_.path().AppendASCII("pictures"))); 180 video_override_.reset(new base::ScopedPathOverride( 181 chrome::DIR_USER_VIDEOS, fake_dir_.path().AppendASCII("videos"))); 182 num_galleries_ = 3; 183#endif // OS_CHROMEOS || OS_ANDROID 184} 185