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