media_galleries_test_util.cc revision a36e5920737c6adbddd3e43b760e5de8431db6e0
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#include "base/base_paths.h" 8#include "base/file_util.h" 9#include "base/files/file_path.h" 10#include "base/path_service.h" 11#include "base/strings/stringprintf.h" 12#include "chrome/browser/extensions/extension_prefs.h" 13#include "chrome/browser/extensions/extension_service.h" 14#include "chrome/browser/extensions/extension_system.h" 15#include "chrome/browser/profiles/profile.h" 16#include "chrome/common/chrome_paths.h" 17#include "chrome/common/extensions/extension.h" 18#include "chrome/common/extensions/extension_manifest_constants.h" 19#include "testing/gtest/include/gtest/gtest.h" 20 21namespace chrome { 22 23scoped_refptr<extensions::Extension> AddMediaGalleriesApp( 24 const std::string& name, 25 const std::vector<std::string>& media_galleries_permissions, 26 Profile* profile) { 27 scoped_ptr<DictionaryValue> manifest(new DictionaryValue); 28 manifest->SetString(extension_manifest_keys::kName, name); 29 manifest->SetString(extension_manifest_keys::kVersion, "0.1"); 30 manifest->SetInteger(extension_manifest_keys::kManifestVersion, 2); 31 ListValue* background_script_list = new ListValue; 32 background_script_list->Append(Value::CreateStringValue("background.js")); 33 manifest->Set(extension_manifest_keys::kPlatformAppBackgroundScripts, 34 background_script_list); 35 36 ListValue* permission_detail_list = new ListValue; 37 for (size_t i = 0; i < media_galleries_permissions.size(); i++) 38 permission_detail_list->Append( 39 Value::CreateStringValue(media_galleries_permissions[i])); 40 DictionaryValue* media_galleries_permission = new DictionaryValue(); 41 media_galleries_permission->Set("mediaGalleries", permission_detail_list); 42 ListValue* permission_list = new ListValue; 43 permission_list->Append(media_galleries_permission); 44 manifest->Set(extension_manifest_keys::kPermissions, permission_list); 45 46 extensions::ExtensionPrefs* extension_prefs = 47 extensions::ExtensionPrefs::Get(profile); 48 base::FilePath path = extension_prefs->install_directory().AppendASCII(name); 49 std::string errors; 50 scoped_refptr<extensions::Extension> extension = 51 extensions::Extension::Create(path, extensions::Manifest::INTERNAL, 52 *manifest.get(), 53 extensions::Extension::NO_FLAGS, &errors); 54 EXPECT_TRUE(extension.get() != NULL) << errors; 55 EXPECT_TRUE(extensions::Extension::IdIsValid(extension->id())); 56 if (!extension.get() || !extensions::Extension::IdIsValid(extension->id())) 57 return NULL; 58 59 extension_prefs->OnExtensionInstalled( 60 extension.get(), 61 extensions::Extension::ENABLED, 62 extensions::Blacklist::NOT_BLACKLISTED, 63 syncer::StringOrdinal::CreateInitialOrdinal()); 64 ExtensionService* extension_service = 65 extensions::ExtensionSystem::Get(profile)->extension_service(); 66 extension_service->AddExtension(extension.get()); 67 extension_service->EnableExtension(extension->id()); 68 69 return extension; 70} 71 72EnsureMediaDirectoriesExists::EnsureMediaDirectoriesExists() 73 : num_galleries_(0) { 74 Init(); 75} 76 77EnsureMediaDirectoriesExists::~EnsureMediaDirectoriesExists() { 78} 79 80void EnsureMediaDirectoriesExists::Init() { 81#if defined(OS_CHROMEOS) || defined(OS_ANDROID) 82 return; 83#else 84 85 ASSERT_TRUE(fake_dir_.CreateUniqueTempDir()); 86 87#if defined(OS_WIN) || defined(OS_MACOSX) 88 // This is to make sure the tests don't think iTunes is installed (unless 89 // we control it specifically). 90 appdir_override_.reset(new base::ScopedPathOverride( 91 base::DIR_APP_DATA, fake_dir_.path().AppendASCII("itunes"))); 92#endif 93 94 music_override_.reset(new base::ScopedPathOverride( 95 chrome::DIR_USER_MUSIC, fake_dir_.path().AppendASCII("music"))); 96 pictures_override_.reset(new base::ScopedPathOverride( 97 chrome::DIR_USER_PICTURES, fake_dir_.path().AppendASCII("pictures"))); 98 video_override_.reset(new base::ScopedPathOverride( 99 chrome::DIR_USER_VIDEOS, fake_dir_.path().AppendASCII("videos"))); 100 num_galleries_ = 3; 101#endif 102} 103 104} // namespace chrome 105