NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / media_galleries / media_galleries_test_util.cc
blobfe61ee6f5100cc8c8b65ccc62fb80247322ed89a
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.
5 #include "chrome/browser/media_galleries/media_galleries_test_util.h"
7 #if defined(OS_WIN)
8 #include <windows.h>
9 #endif
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_service.h"
17 #include "chrome/browser/media_galleries/fileapi/picasa_finder.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/common/chrome_paths.h"
20 #include "extensions/browser/extension_prefs.h"
21 #include "extensions/browser/extension_system.h"
22 #include "extensions/common/extension.h"
23 #include "extensions/common/manifest_constants.h"
24 #include "testing/gtest/include/gtest/gtest.h"
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
33 #if defined(OS_WIN)
34 #include "base/test/test_reg_util_win.h"
35 #include "base/win/registry.h"
36 #endif // OS_WIN
38 scoped_refptr<extensions::Extension> AddMediaGalleriesApp(
39 const std::string& name,
40 const std::vector<std::string>& media_galleries_permissions,
41 Profile* profile) {
42 scoped_ptr<base::DictionaryValue> manifest(new base::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 base::ListValue* background_script_list = new base::ListValue;
47 background_script_list->Append(
48 base::Value::CreateStringValue("background.js"));
49 manifest->Set(extensions::manifest_keys::kPlatformAppBackgroundScripts,
50 background_script_list);
52 base::ListValue* permission_detail_list = new base::ListValue;
53 for (size_t i = 0; i < media_galleries_permissions.size(); i++)
54 permission_detail_list->Append(
55 base::Value::CreateStringValue(media_galleries_permissions[i]));
56 base::DictionaryValue* media_galleries_permission =
57 new base::DictionaryValue();
58 media_galleries_permission->Set("mediaGalleries", permission_detail_list);
59 base::ListValue* permission_list = new base::ListValue;
60 permission_list->Append(media_galleries_permission);
61 manifest->Set(extensions::manifest_keys::kPermissions, permission_list);
63 extensions::ExtensionPrefs* extension_prefs =
64 extensions::ExtensionPrefs::Get(profile);
65 base::FilePath path = extension_prefs->install_directory().AppendASCII(name);
66 std::string errors;
67 scoped_refptr<extensions::Extension> extension =
68 extensions::Extension::Create(path, extensions::Manifest::INTERNAL,
69 *manifest.get(),
70 extensions::Extension::NO_FLAGS, &errors);
71 EXPECT_TRUE(extension.get() != NULL) << errors;
72 EXPECT_TRUE(extensions::Extension::IdIsValid(extension->id()));
73 if (!extension.get() || !extensions::Extension::IdIsValid(extension->id()))
74 return NULL;
76 extension_prefs->OnExtensionInstalled(
77 extension.get(),
78 extensions::Extension::ENABLED,
79 false,
80 syncer::StringOrdinal::CreateInitialOrdinal());
81 ExtensionService* extension_service =
82 extensions::ExtensionSystem::Get(profile)->extension_service();
83 extension_service->AddExtension(extension.get());
84 extension_service->EnableExtension(extension->id());
86 return extension;
89 EnsureMediaDirectoriesExists::EnsureMediaDirectoriesExists()
90 : num_galleries_(0) {
91 Init();
94 EnsureMediaDirectoriesExists::~EnsureMediaDirectoriesExists() {
95 #if defined(OS_MACOSX)
96 iapps::SetMacPreferencesForTesting(NULL);
97 picasa::SetMacPreferencesForTesting(NULL);
98 #endif // OS_MACOSX
101 base::FilePath EnsureMediaDirectoriesExists::GetFakeAppDataPath() const {
102 DCHECK(fake_dir_.IsValid());
103 return fake_dir_.path().AppendASCII("appdata");
106 #if defined(OS_WIN)
107 base::FilePath EnsureMediaDirectoriesExists::GetFakeLocalAppDataPath() const {
108 DCHECK(fake_dir_.IsValid());
109 return fake_dir_.path().AppendASCII("localappdata");
112 void EnsureMediaDirectoriesExists::SetCustomPicasaAppDataPath(
113 const base::FilePath& path) {
114 base::win::RegKey key(HKEY_CURRENT_USER, picasa::kPicasaRegistryPath,
115 KEY_SET_VALUE);
116 key.WriteValue(picasa::kPicasaRegistryAppDataPathKey, path.value().c_str());
118 #endif // OS_WIN
120 #if defined(OS_MACOSX)
121 void EnsureMediaDirectoriesExists::SetCustomPicasaAppDataPath(
122 const base::FilePath& path) {
123 mac_preferences_->AddTestItem(
124 base::mac::NSToCFCast(picasa::kPicasaAppDataPathMacPreferencesKey),
125 base::SysUTF8ToNSString(path.value()),
126 false);
128 #endif // OS_MACOSX
130 #if defined(OS_WIN) || defined(OS_MACOSX)
131 base::FilePath
132 EnsureMediaDirectoriesExists::GetFakePicasaFoldersRootPath() const {
133 DCHECK(fake_dir_.IsValid());
134 return fake_dir_.path().AppendASCII("picasa_folders");
136 #endif // OS_WIN || OS_MACOSX
138 #if defined(OS_MACOSX)
139 base::FilePath EnsureMediaDirectoriesExists::GetFakeITunesRootPath() const {
140 DCHECK(fake_dir_.IsValid());
141 return fake_dir_.path().AppendASCII("itunes");
144 base::FilePath EnsureMediaDirectoriesExists::GetFakeIPhotoRootPath() const {
145 DCHECK(fake_dir_.IsValid());
146 return fake_dir_.path().AppendASCII("iphoto");
148 #endif // OS_MACOSX
150 void EnsureMediaDirectoriesExists::Init() {
151 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
152 return;
153 #else
155 ASSERT_TRUE(fake_dir_.CreateUniqueTempDir());
157 #if defined(OS_WIN) || defined(OS_MACOSX)
158 // This is to control whether or not tests think iTunes (on Windows) and
159 // Picasa are installed.
160 app_data_override_.reset(new base::ScopedPathOverride(
161 base::DIR_APP_DATA, GetFakeAppDataPath()));
162 #endif // OS_WIN || OS_MACOSX
164 #if defined(OS_WIN)
165 // Picasa on Windows is by default in the DIR_LOCAL_APP_DATA directory.
166 local_app_data_override_.reset(new base::ScopedPathOverride(
167 base::DIR_LOCAL_APP_DATA, GetFakeLocalAppDataPath()));
168 // Picasa also looks in the registry for an alternate path.
169 registry_override_.OverrideRegistry(HKEY_CURRENT_USER, L"hkcu_picasa");
170 #endif // OS_WIN
172 #if defined(OS_MACOSX)
173 mac_preferences_.reset(new MockPreferences);
175 // iTunes override.
176 base::FilePath itunes_xml =
177 GetFakeITunesRootPath().AppendASCII("iTunes Library.xml");
178 mac_preferences_->AddTestItem(
179 base::mac::NSToCFCast(iapps::kITunesRecentDatabasePathsKey),
180 base::mac::NSToCFCast(iapps::NSArrayFromFilePath(itunes_xml)),
181 false);
183 // iPhoto override.
184 base::FilePath iphoto_xml =
185 GetFakeIPhotoRootPath().AppendASCII("AlbumData.xml");
186 mac_preferences_->AddTestItem(
187 base::mac::NSToCFCast(iapps::kIPhotoRecentDatabasesKey),
188 base::mac::NSToCFCast(iapps::NSArrayFromFilePath(iphoto_xml)),
189 false);
191 iapps::SetMacPreferencesForTesting(mac_preferences_.get());
192 picasa::SetMacPreferencesForTesting(mac_preferences_.get());
193 #endif // OS_MACOSX
195 music_override_.reset(new base::ScopedPathOverride(
196 chrome::DIR_USER_MUSIC, fake_dir_.path().AppendASCII("music")));
197 pictures_override_.reset(new base::ScopedPathOverride(
198 chrome::DIR_USER_PICTURES, fake_dir_.path().AppendASCII("pictures")));
199 video_override_.reset(new base::ScopedPathOverride(
200 chrome::DIR_USER_VIDEOS, fake_dir_.path().AppendASCII("videos")));
201 num_galleries_ = 3;
202 #endif // OS_CHROMEOS || OS_ANDROID
205 base::FilePath MakeMediaGalleriesTestingPath(const std::string& dir) {
206 #if defined(OS_WIN)
207 return base::FilePath(FILE_PATH_LITERAL("C:\\")).AppendASCII(dir);
208 #elif defined(OS_POSIX)
209 return base::FilePath(FILE_PATH_LITERAL("/")).Append(dir);
210 #else
211 NOTREACHED();
212 #endif