- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / storage / settings_test_util.cc
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/extensions/api/storage/settings_test_util.h"
6
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"
12
13 namespace extensions {
14
15 namespace settings_test_util {
16
17 // Intended as a StorageCallback from GetStorage.
18 static void AssignStorage(ValueStore** dst, ValueStore* src) {
19   *dst = src;
20 }
21
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(
28       extension_id,
29       settings_namespace,
30       base::Bind(&AssignStorage, &storage));
31   base::MessageLoop::current()->RunUntilIdle();
32   return storage;
33 }
34
35 ValueStore* GetStorage(
36     const std::string& extension_id, SettingsFrontend* frontend) {
37   return GetStorage(extension_id, settings_namespace::SYNC, frontend);
38 }
39
40 // MockExtensionService
41
42 MockExtensionService::MockExtensionService() {}
43
44 MockExtensionService::~MockExtensionService() {}
45
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();
52 }
53
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);
58 }
59
60 void MockExtensionService::AddExtensionWithIdAndPermissions(
61     const std::string& id,
62     Manifest::Type type,
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");
67
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));
72   }
73   manifest.Set("permissions", permissions.release());
74
75   switch (type) {
76     case Manifest::TYPE_EXTENSION:
77       break;
78
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);
85       break;
86     }
87
88     default:
89       NOTREACHED();
90   }
91
92   std::string error;
93   scoped_refptr<Extension> extension(Extension::Create(
94       base::FilePath(),
95       Manifest::INTERNAL,
96       manifest,
97       Extension::NO_FLAGS,
98       id,
99       &error));
100   DCHECK(extension.get());
101   DCHECK(error.empty());
102   extensions_[id] = extension;
103
104   for (std::set<std::string>::const_iterator it = permissions_set.begin();
105       it != permissions_set.end(); ++it) {
106     DCHECK(extension->HasAPIPermission(*it));
107   }
108 }
109
110 // MockExtensionSystem
111
112 MockExtensionSystem::MockExtensionSystem(Profile* profile)
113       : TestExtensionSystem(profile) {}
114 MockExtensionSystem::~MockExtensionSystem() {}
115
116 EventRouter* MockExtensionSystem::event_router() {
117   if (!event_router_.get())
118     event_router_.reset(new EventRouter(profile_, NULL));
119   return event_router_.get();
120 }
121
122 ExtensionService* MockExtensionSystem::extension_service() {
123   ExtensionServiceInterface* as_interface =
124       static_cast<ExtensionServiceInterface*>(&extension_service_);
125   return static_cast<ExtensionService*>(as_interface);
126 }
127
128 BrowserContextKeyedService* BuildMockExtensionSystem(
129     content::BrowserContext* profile) {
130   return new MockExtensionSystem(static_cast<Profile*>(profile));
131 }
132
133 // MockProfile
134
135 MockProfile::MockProfile(const base::FilePath& file_path)
136     : TestingProfile(file_path) {
137   ExtensionSystemFactory::GetInstance()->SetTestingFactoryAndUse(this,
138       &BuildMockExtensionSystem);
139 }
140
141 MockProfile::~MockProfile() {}
142
143 // ScopedSettingsFactory
144
145 ScopedSettingsStorageFactory::ScopedSettingsStorageFactory() {}
146
147 ScopedSettingsStorageFactory::ScopedSettingsStorageFactory(
148     const scoped_refptr<SettingsStorageFactory>& delegate)
149     : delegate_(delegate) {}
150
151 ScopedSettingsStorageFactory::~ScopedSettingsStorageFactory() {}
152
153 void ScopedSettingsStorageFactory::Reset(
154     const scoped_refptr<SettingsStorageFactory>& delegate) {
155   delegate_ = delegate;
156 }
157
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);
163 }
164
165 }  // namespace settings_test_util
166
167 }  // namespace extensions