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(new base::StringValue("background.js"));
48 manifest
->Set(extensions::manifest_keys::kPlatformAppBackgroundScripts
,
49 background_script_list
);
51 base::ListValue
* permission_detail_list
= new base::ListValue
;
52 for (size_t i
= 0; i
< media_galleries_permissions
.size(); i
++)
53 permission_detail_list
->Append(
54 new base::StringValue(media_galleries_permissions
[i
]));
55 base::DictionaryValue
* media_galleries_permission
=
56 new base::DictionaryValue();
57 media_galleries_permission
->Set("mediaGalleries", permission_detail_list
);
58 base::ListValue
* permission_list
= new base::ListValue
;
59 permission_list
->Append(media_galleries_permission
);
60 manifest
->Set(extensions::manifest_keys::kPermissions
, permission_list
);
62 extensions::ExtensionPrefs
* extension_prefs
=
63 extensions::ExtensionPrefs::Get(profile
);
64 base::FilePath path
= extension_prefs
->install_directory().AppendASCII(name
);
66 scoped_refptr
<extensions::Extension
> extension
=
67 extensions::Extension::Create(path
, extensions::Manifest::INTERNAL
,
69 extensions::Extension::NO_FLAGS
, &errors
);
70 EXPECT_TRUE(extension
.get() != NULL
) << errors
;
71 EXPECT_TRUE(extensions::Extension::IdIsValid(extension
->id()));
72 if (!extension
.get() || !extensions::Extension::IdIsValid(extension
->id()))
75 extension_prefs
->OnExtensionInstalled(
77 extensions::Extension::ENABLED
,
78 syncer::StringOrdinal::CreateInitialOrdinal(),
80 ExtensionService
* extension_service
=
81 extensions::ExtensionSystem::Get(profile
)->extension_service();
82 extension_service
->AddExtension(extension
.get());
83 extension_service
->EnableExtension(extension
->id());
88 EnsureMediaDirectoriesExists::EnsureMediaDirectoriesExists()
93 EnsureMediaDirectoriesExists::~EnsureMediaDirectoriesExists() {
94 #if defined(OS_MACOSX)
95 iapps::SetMacPreferencesForTesting(NULL
);
96 picasa::SetMacPreferencesForTesting(NULL
);
100 base::FilePath
EnsureMediaDirectoriesExists::GetFakeAppDataPath() const {
101 DCHECK(fake_dir_
.IsValid());
102 return fake_dir_
.path().AppendASCII("appdata");
106 base::FilePath
EnsureMediaDirectoriesExists::GetFakeLocalAppDataPath() const {
107 DCHECK(fake_dir_
.IsValid());
108 return fake_dir_
.path().AppendASCII("localappdata");
111 void EnsureMediaDirectoriesExists::SetCustomPicasaAppDataPath(
112 const base::FilePath
& path
) {
113 base::win::RegKey
key(HKEY_CURRENT_USER
, picasa::kPicasaRegistryPath
,
115 key
.WriteValue(picasa::kPicasaRegistryAppDataPathKey
, path
.value().c_str());
119 #if defined(OS_MACOSX)
120 void EnsureMediaDirectoriesExists::SetCustomPicasaAppDataPath(
121 const base::FilePath
& path
) {
122 mac_preferences_
->AddTestItem(
123 base::mac::NSToCFCast(picasa::kPicasaAppDataPathMacPreferencesKey
),
124 base::SysUTF8ToNSString(path
.value()),
129 #if defined(OS_WIN) || defined(OS_MACOSX)
131 EnsureMediaDirectoriesExists::GetFakePicasaFoldersRootPath() const {
132 DCHECK(fake_dir_
.IsValid());
133 return fake_dir_
.path().AppendASCII("picasa_folders");
135 #endif // OS_WIN || OS_MACOSX
137 #if defined(OS_MACOSX)
138 base::FilePath
EnsureMediaDirectoriesExists::GetFakeITunesRootPath() const {
139 DCHECK(fake_dir_
.IsValid());
140 return fake_dir_
.path().AppendASCII("itunes");
143 base::FilePath
EnsureMediaDirectoriesExists::GetFakeIPhotoRootPath() const {
144 DCHECK(fake_dir_
.IsValid());
145 return fake_dir_
.path().AppendASCII("iphoto");
149 void EnsureMediaDirectoriesExists::Init() {
150 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
154 ASSERT_TRUE(fake_dir_
.CreateUniqueTempDir());
156 #if defined(OS_WIN) || defined(OS_MACOSX)
157 // This is to control whether or not tests think iTunes (on Windows) and
158 // Picasa are installed.
159 app_data_override_
.reset(new base::ScopedPathOverride(
160 base::DIR_APP_DATA
, GetFakeAppDataPath()));
161 #endif // OS_WIN || OS_MACOSX
164 // Picasa on Windows is by default in the DIR_LOCAL_APP_DATA directory.
165 local_app_data_override_
.reset(new base::ScopedPathOverride(
166 base::DIR_LOCAL_APP_DATA
, GetFakeLocalAppDataPath()));
167 // Picasa also looks in the registry for an alternate path.
168 registry_override_
.OverrideRegistry(HKEY_CURRENT_USER
, L
"hkcu_picasa");
171 #if defined(OS_MACOSX)
172 mac_preferences_
.reset(new MockPreferences
);
175 base::FilePath itunes_xml
=
176 GetFakeITunesRootPath().AppendASCII("iTunes Library.xml");
177 mac_preferences_
->AddTestItem(
178 base::mac::NSToCFCast(iapps::kITunesRecentDatabasePathsKey
),
179 base::mac::NSToCFCast(iapps::NSArrayFromFilePath(itunes_xml
)),
183 base::FilePath iphoto_xml
=
184 GetFakeIPhotoRootPath().AppendASCII("AlbumData.xml");
185 mac_preferences_
->AddTestItem(
186 base::mac::NSToCFCast(iapps::kIPhotoRecentDatabasesKey
),
187 base::mac::NSToCFCast(iapps::NSArrayFromFilePath(iphoto_xml
)),
190 iapps::SetMacPreferencesForTesting(mac_preferences_
.get());
191 picasa::SetMacPreferencesForTesting(mac_preferences_
.get());
194 music_override_
.reset(new base::ScopedPathOverride(
195 chrome::DIR_USER_MUSIC
, fake_dir_
.path().AppendASCII("music")));
196 pictures_override_
.reset(new base::ScopedPathOverride(
197 chrome::DIR_USER_PICTURES
, fake_dir_
.path().AppendASCII("pictures")));
198 video_override_
.reset(new base::ScopedPathOverride(
199 chrome::DIR_USER_VIDEOS
, fake_dir_
.path().AppendASCII("videos")));
201 #endif // OS_CHROMEOS || OS_ANDROID
204 base::FilePath
MakeMediaGalleriesTestingPath(const std::string
& dir
) {
206 return base::FilePath(FILE_PATH_LITERAL("C:\\")).AppendASCII(dir
);
207 #elif defined(OS_POSIX)
208 return base::FilePath(FILE_PATH_LITERAL("/")).Append(dir
);