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/extensions/api/storage/settings_test_util.h"
7 #include "base/files/file_path.h"
8 #include "chrome/browser/extensions/api/storage/settings_frontend.h"
9 #include "chrome/browser/extensions/extension_system_factory.h"
10 #include "chrome/common/extensions/extension.h"
11 #include "chrome/common/extensions/permissions/permissions_data.h"
13 namespace extensions {
15 namespace settings_test_util {
17 // Intended as a StorageCallback from GetStorage.
18 static void AssignStorage(ValueStore** dst, ValueStore* src) {
22 ValueStore* GetStorage(
23 const std::string& extension_id,
24 settings_namespace::Namespace settings_namespace,
25 SettingsFrontend* frontend) {
26 ValueStore* storage = NULL;
27 frontend->RunWithStorage(
30 base::Bind(&AssignStorage, &storage));
31 base::MessageLoop::current()->RunUntilIdle();
35 ValueStore* GetStorage(
36 const std::string& extension_id, SettingsFrontend* frontend) {
37 return GetStorage(extension_id, settings_namespace::SYNC, frontend);
40 // MockExtensionService
42 MockExtensionService::MockExtensionService() {}
44 MockExtensionService::~MockExtensionService() {}
46 const Extension* MockExtensionService::GetExtensionById(
47 const std::string& id, bool include_disabled) const {
48 std::map<std::string, scoped_refptr<Extension> >::const_iterator
49 maybe_extension = extensions_.find(id);
50 return maybe_extension == extensions_.end() ?
51 NULL : maybe_extension->second.get();
54 void MockExtensionService::AddExtensionWithId(
55 const std::string& id, Manifest::Type type) {
56 std::set<std::string> empty_permissions;
57 AddExtensionWithIdAndPermissions(id, type, empty_permissions);
60 void MockExtensionService::AddExtensionWithIdAndPermissions(
61 const std::string& id,
63 const std::set<std::string>& permissions_set) {
64 base::DictionaryValue manifest;
65 manifest.SetString("name", std::string("Test extension ") + id);
66 manifest.SetString("version", "1.0");
68 scoped_ptr<base::ListValue> permissions(new base::ListValue());
69 for (std::set<std::string>::const_iterator it = permissions_set.begin();
70 it != permissions_set.end(); ++it) {
71 permissions->Append(new base::StringValue(*it));
73 manifest.Set("permissions", permissions.release());
76 case Manifest::TYPE_EXTENSION:
79 case Manifest::TYPE_LEGACY_PACKAGED_APP: {
80 base::DictionaryValue* app = new base::DictionaryValue();
81 base::DictionaryValue* app_launch = new base::DictionaryValue();
82 app_launch->SetString("local_path", "fake.html");
83 app->Set("launch", app_launch);
84 manifest.Set("app", app);
93 scoped_refptr<Extension> extension(Extension::Create(
100 DCHECK(extension.get());
101 DCHECK(error.empty());
102 extensions_[id] = extension;
104 for (std::set<std::string>::const_iterator it = permissions_set.begin();
105 it != permissions_set.end(); ++it) {
106 DCHECK(extension->HasAPIPermission(*it));
110 // MockExtensionSystem
112 MockExtensionSystem::MockExtensionSystem(Profile* profile)
113 : TestExtensionSystem(profile) {}
114 MockExtensionSystem::~MockExtensionSystem() {}
116 EventRouter* MockExtensionSystem::event_router() {
117 if (!event_router_.get())
118 event_router_.reset(new EventRouter(profile_, NULL));
119 return event_router_.get();
122 ExtensionService* MockExtensionSystem::extension_service() {
123 ExtensionServiceInterface* as_interface =
124 static_cast<ExtensionServiceInterface*>(&extension_service_);
125 return static_cast<ExtensionService*>(as_interface);
128 BrowserContextKeyedService* BuildMockExtensionSystem(
129 content::BrowserContext* profile) {
130 return new MockExtensionSystem(static_cast<Profile*>(profile));
135 MockProfile::MockProfile(const base::FilePath& file_path)
136 : TestingProfile(file_path) {
137 ExtensionSystemFactory::GetInstance()->SetTestingFactoryAndUse(this,
138 &BuildMockExtensionSystem);
141 MockProfile::~MockProfile() {}
143 // ScopedSettingsFactory
145 ScopedSettingsStorageFactory::ScopedSettingsStorageFactory() {}
147 ScopedSettingsStorageFactory::ScopedSettingsStorageFactory(
148 const scoped_refptr<SettingsStorageFactory>& delegate)
149 : delegate_(delegate) {}
151 ScopedSettingsStorageFactory::~ScopedSettingsStorageFactory() {}
153 void ScopedSettingsStorageFactory::Reset(
154 const scoped_refptr<SettingsStorageFactory>& delegate) {
155 delegate_ = delegate;
158 ValueStore* ScopedSettingsStorageFactory::Create(
159 const base::FilePath& base_path,
160 const std::string& extension_id) {
161 DCHECK(delegate_.get());
162 return delegate_->Create(base_path, extension_id);
165 } // namespace settings_test_util
167 } // namespace extensions