Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / extensions / test_extension_environment.cc
blob7044795e23289ef3baf08572671abb0b6b7c49a7
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/test_extension_environment.h"
7 #include "base/command_line.h"
8 #include "base/json/json_writer.h"
9 #include "base/run_loop.h"
10 #include "base/values.h"
11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/extensions/test_extension_system.h"
13 #include "chrome/browser/sessions/session_tab_helper.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "content/public/test/test_browser_thread_bundle.h"
16 #include "content/public/test/test_utils.h"
17 #include "content/public/test/web_contents_tester.h"
18 #include "extensions/browser/extension_prefs.h"
19 #include "extensions/common/extension.h"
20 #include "extensions/common/extension_builder.h"
21 #include "extensions/common/value_builder.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 #if defined(OS_CHROMEOS)
25 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
26 #include "chrome/browser/chromeos/settings/cros_settings.h"
27 #include "chrome/browser/chromeos/settings/device_settings_service.h"
28 #endif
30 namespace extensions {
32 using content::BrowserThread;
34 namespace {
36 scoped_ptr<base::DictionaryValue> MakeExtensionManifest(
37 const base::Value& manifest_extra) {
38 scoped_ptr<base::DictionaryValue> manifest = DictionaryBuilder()
39 .Set("name", "Extension")
40 .Set("version", "1.0")
41 .Set("manifest_version", 2)
42 .Build();
43 const base::DictionaryValue* manifest_extra_dict;
44 if (manifest_extra.GetAsDictionary(&manifest_extra_dict)) {
45 manifest->MergeDictionary(manifest_extra_dict);
46 } else {
47 std::string manifest_json;
48 base::JSONWriter::Write(manifest_extra, &manifest_json);
49 ADD_FAILURE() << "Expected dictionary; got \"" << manifest_json << "\"";
51 return manifest;
54 scoped_ptr<base::DictionaryValue> MakePackagedAppManifest() {
55 return extensions::DictionaryBuilder()
56 .Set("name", "Test App Name")
57 .Set("version", "2.0")
58 .Set("manifest_version", 2)
59 .Set("app", extensions::DictionaryBuilder().Set(
60 "background",
61 extensions::DictionaryBuilder().Set(
62 "scripts",
63 extensions::ListBuilder().Append("background.js"))))
64 .Build();
67 } // namespace
69 // Extra environment state required for ChromeOS.
70 class TestExtensionEnvironment::ChromeOSEnv {
71 public:
72 ChromeOSEnv() {}
74 private:
75 #if defined(OS_CHROMEOS)
76 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
77 chromeos::ScopedTestCrosSettings test_cros_settings_;
78 chromeos::ScopedTestUserManager test_user_manager_;
79 #endif
81 DISALLOW_COPY_AND_ASSIGN(ChromeOSEnv);
84 // static
85 ExtensionService* TestExtensionEnvironment::CreateExtensionServiceForProfile(
86 TestingProfile* profile) {
87 TestExtensionSystem* extension_system =
88 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile));
89 return extension_system->CreateExtensionService(
90 base::CommandLine::ForCurrentProcess(), base::FilePath(), false);
93 TestExtensionEnvironment::TestExtensionEnvironment()
94 : thread_bundle_(new content::TestBrowserThreadBundle),
95 extension_service_(nullptr) {
96 Init();
99 TestExtensionEnvironment::TestExtensionEnvironment(
100 base::MessageLoopForUI* message_loop)
101 : extension_service_(nullptr) {
102 Init();
105 void TestExtensionEnvironment::Init() {
106 profile_.reset(new TestingProfile);
107 #if defined(OS_CHROMEOS)
108 if (!chromeos::DeviceSettingsService::IsInitialized())
109 chromeos_env_.reset(new ChromeOSEnv);
110 #endif
113 TestExtensionEnvironment::~TestExtensionEnvironment() {
116 TestingProfile* TestExtensionEnvironment::profile() const {
117 return profile_.get();
120 TestExtensionSystem* TestExtensionEnvironment::GetExtensionSystem() {
121 return static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile()));
124 ExtensionService* TestExtensionEnvironment::GetExtensionService() {
125 if (!extension_service_)
126 extension_service_ = CreateExtensionServiceForProfile(profile());
127 return extension_service_;
130 ExtensionPrefs* TestExtensionEnvironment::GetExtensionPrefs() {
131 return ExtensionPrefs::Get(profile_.get());
134 const Extension* TestExtensionEnvironment::MakeExtension(
135 const base::Value& manifest_extra) {
136 scoped_ptr<base::DictionaryValue> manifest =
137 MakeExtensionManifest(manifest_extra);
138 scoped_refptr<Extension> result =
139 ExtensionBuilder().SetManifest(manifest.Pass()).Build();
140 GetExtensionService()->AddExtension(result.get());
141 return result.get();
144 const Extension* TestExtensionEnvironment::MakeExtension(
145 const base::Value& manifest_extra,
146 const std::string& id) {
147 scoped_ptr<base::DictionaryValue> manifest =
148 MakeExtensionManifest(manifest_extra);
149 scoped_refptr<Extension> result =
150 ExtensionBuilder().SetManifest(manifest.Pass()).SetID(id).Build();
151 GetExtensionService()->AddExtension(result.get());
152 return result.get();
155 scoped_refptr<Extension> TestExtensionEnvironment::MakePackagedApp(
156 const std::string& id,
157 bool install) {
158 scoped_refptr<Extension> result = ExtensionBuilder()
159 .SetManifest(MakePackagedAppManifest())
160 .AddFlags(Extension::FROM_WEBSTORE)
161 .SetID(id)
162 .Build();
163 if (install)
164 GetExtensionService()->AddExtension(result.get());
165 return result;
168 scoped_ptr<content::WebContents> TestExtensionEnvironment::MakeTab() const {
169 scoped_ptr<content::WebContents> contents(
170 content::WebContentsTester::CreateTestWebContents(profile(), NULL));
171 // Create a tab id.
172 SessionTabHelper::CreateForWebContents(contents.get());
173 return contents.Pass();
176 void TestExtensionEnvironment::DeleteProfile() {
177 profile_.reset();
178 extension_service_ = nullptr;
181 } // namespace extensions