settings_test_util.cc revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
1// Copyright 2014 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 "extensions/browser/api/storage/settings_test_util.h" 6 7#include "base/files/file_path.h" 8#include "extensions/browser/api/storage/storage_frontend.h" 9#include "extensions/browser/extension_registry.h" 10#include "extensions/browser/extension_system_provider.h" 11#include "extensions/browser/extensions_browser_client.h" 12#include "extensions/common/extension.h" 13#include "extensions/common/permissions/permissions_data.h" 14 15namespace extensions { 16 17namespace settings_test_util { 18 19// Intended as a StorageCallback from GetStorage. 20static void AssignStorage(ValueStore** dst, ValueStore* src) { 21 *dst = src; 22} 23 24ValueStore* GetStorage(scoped_refptr<const Extension> extension, 25 settings_namespace::Namespace settings_namespace, 26 StorageFrontend* frontend) { 27 ValueStore* storage = NULL; 28 frontend->RunWithStorage( 29 extension, settings_namespace, base::Bind(&AssignStorage, &storage)); 30 base::MessageLoop::current()->RunUntilIdle(); 31 return storage; 32} 33 34ValueStore* GetStorage(scoped_refptr<const Extension> extension, 35 StorageFrontend* frontend) { 36 return GetStorage(extension, settings_namespace::SYNC, frontend); 37} 38 39scoped_refptr<const Extension> AddExtensionWithId(Profile* profile, 40 const std::string& id, 41 Manifest::Type type) { 42 return AddExtensionWithIdAndPermissions( 43 profile, id, type, std::set<std::string>()); 44} 45 46scoped_refptr<const Extension> AddExtensionWithIdAndPermissions( 47 Profile* profile, 48 const std::string& id, 49 Manifest::Type type, 50 const std::set<std::string>& permissions_set) { 51 base::DictionaryValue manifest; 52 manifest.SetString("name", std::string("Test extension ") + id); 53 manifest.SetString("version", "1.0"); 54 55 scoped_ptr<base::ListValue> permissions(new base::ListValue()); 56 for (std::set<std::string>::const_iterator it = permissions_set.begin(); 57 it != permissions_set.end(); ++it) { 58 permissions->Append(new base::StringValue(*it)); 59 } 60 manifest.Set("permissions", permissions.release()); 61 62 switch (type) { 63 case Manifest::TYPE_EXTENSION: 64 break; 65 66 case Manifest::TYPE_LEGACY_PACKAGED_APP: { 67 base::DictionaryValue* app = new base::DictionaryValue(); 68 base::DictionaryValue* app_launch = new base::DictionaryValue(); 69 app_launch->SetString("local_path", "fake.html"); 70 app->Set("launch", app_launch); 71 manifest.Set("app", app); 72 break; 73 } 74 75 default: 76 NOTREACHED(); 77 } 78 79 std::string error; 80 scoped_refptr<const Extension> extension( 81 Extension::Create(base::FilePath(), 82 Manifest::INTERNAL, 83 manifest, 84 Extension::NO_FLAGS, 85 id, 86 &error)); 87 DCHECK(extension.get()); 88 DCHECK(error.empty()); 89 90 // Ensure lookups via ExtensionRegistry (and ExtensionService) work even if 91 // the test discards the referenced to the returned extension. 92 ExtensionRegistry::Get(profile)->AddEnabled(extension); 93 94 for (std::set<std::string>::const_iterator it = permissions_set.begin(); 95 it != permissions_set.end(); ++it) { 96 DCHECK(extension->permissions_data()->HasAPIPermission(*it)); 97 } 98 99 return extension; 100} 101 102// MockExtensionSystem 103 104MockExtensionSystem::MockExtensionSystem(Profile* profile) 105 : TestExtensionSystem(profile) {} 106MockExtensionSystem::~MockExtensionSystem() {} 107 108EventRouter* MockExtensionSystem::event_router() { 109 if (!event_router_.get()) 110 event_router_.reset(new EventRouter(profile_, NULL)); 111 return event_router_.get(); 112} 113 114KeyedService* BuildMockExtensionSystem(content::BrowserContext* profile) { 115 return new MockExtensionSystem(static_cast<Profile*>(profile)); 116} 117 118// MockProfile 119 120MockProfile::MockProfile(const base::FilePath& file_path) 121 : TestingProfile(file_path) { 122 ExtensionsBrowserClient::Get() 123 ->GetExtensionSystemFactory() 124 ->SetTestingFactoryAndUse(this, &BuildMockExtensionSystem); 125} 126 127MockProfile::~MockProfile() {} 128 129// ScopedSettingsFactory 130 131ScopedSettingsStorageFactory::ScopedSettingsStorageFactory() {} 132 133ScopedSettingsStorageFactory::ScopedSettingsStorageFactory( 134 const scoped_refptr<SettingsStorageFactory>& delegate) 135 : delegate_(delegate) {} 136 137ScopedSettingsStorageFactory::~ScopedSettingsStorageFactory() {} 138 139void ScopedSettingsStorageFactory::Reset( 140 const scoped_refptr<SettingsStorageFactory>& delegate) { 141 delegate_ = delegate; 142} 143 144ValueStore* ScopedSettingsStorageFactory::Create( 145 const base::FilePath& base_path, 146 const std::string& extension_id) { 147 DCHECK(delegate_.get()); 148 return delegate_->Create(base_path, extension_id); 149} 150 151void ScopedSettingsStorageFactory::DeleteDatabaseIfExists( 152 const base::FilePath& base_path, 153 const std::string& extension_id) { 154 delegate_->DeleteDatabaseIfExists(base_path, extension_id); 155} 156 157} // namespace settings_test_util 158 159} // namespace extensions 160