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"
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"
34 #include "base/test/test_reg_util_win.h"
35 #include "base/win/registry.h"
38 scoped_refptr
<extensions::Extension
> AddMediaGalleriesApp(
39 const std::string
& name
,
40 const std::vector
<std::string
>& media_galleries_permissions
,
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
);
67 scoped_refptr
<extensions::Extension
> extension
=
68 extensions::Extension::Create(path
, extensions::Manifest::INTERNAL
,
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()))
76 extension_prefs
->OnExtensionInstalled(
78 extensions::Extension::ENABLED
,
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());
89 EnsureMediaDirectoriesExists::EnsureMediaDirectoriesExists()
94 EnsureMediaDirectoriesExists::~EnsureMediaDirectoriesExists() {
95 #if defined(OS_MACOSX)
96 iapps::SetMacPreferencesForTesting(NULL
);
97 picasa::SetMacPreferencesForTesting(NULL
);
101 base::FilePath
EnsureMediaDirectoriesExists::GetFakeAppDataPath() const {
102 DCHECK(fake_dir_
.IsValid());
103 return fake_dir_
.path().AppendASCII("appdata");
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
,
116 key
.WriteValue(picasa::kPicasaRegistryAppDataPathKey
, path
.value().c_str());
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()),
130 #if defined(OS_WIN) || defined(OS_MACOSX)
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");
150 void EnsureMediaDirectoriesExists::Init() {
151 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
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
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");
172 #if defined(OS_MACOSX)
173 mac_preferences_
.reset(new MockPreferences
);
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
)),
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
)),
191 iapps::SetMacPreferencesForTesting(mac_preferences_
.get());
192 picasa::SetMacPreferencesForTesting(mac_preferences_
.get());
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")));
202 #endif // OS_CHROMEOS || OS_ANDROID
205 base::FilePath
MakeMediaGalleriesTestingPath(const std::string
& dir
) {
207 return base::FilePath(FILE_PATH_LITERAL("C:\\")).AppendASCII(dir
);
208 #elif defined(OS_POSIX)
209 return base::FilePath(FILE_PATH_LITERAL("/")).Append(dir
);