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"
30 namespace extensions
{
32 using content::BrowserThread
;
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)
43 const base::DictionaryValue
* manifest_extra_dict
;
44 if (manifest_extra
.GetAsDictionary(&manifest_extra_dict
)) {
45 manifest
->MergeDictionary(manifest_extra_dict
);
47 std::string manifest_json
;
48 base::JSONWriter::Write(manifest_extra
, &manifest_json
);
49 ADD_FAILURE() << "Expected dictionary; got \"" << manifest_json
<< "\"";
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(
61 extensions::DictionaryBuilder().Set(
63 extensions::ListBuilder().Append("background.js"))))
69 // Extra environment state required for ChromeOS.
70 class TestExtensionEnvironment::ChromeOSEnv
{
75 #if defined(OS_CHROMEOS)
76 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_
;
77 chromeos::ScopedTestCrosSettings test_cros_settings_
;
78 chromeos::ScopedTestUserManager test_user_manager_
;
81 DISALLOW_COPY_AND_ASSIGN(ChromeOSEnv
);
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) {
99 TestExtensionEnvironment::TestExtensionEnvironment(
100 base::MessageLoopForUI
* message_loop
)
101 : extension_service_(nullptr) {
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
);
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());
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());
155 scoped_refptr
<Extension
> TestExtensionEnvironment::MakePackagedApp(
156 const std::string
& id
,
158 scoped_refptr
<Extension
> result
= ExtensionBuilder()
159 .SetManifest(MakePackagedAppManifest())
160 .AddFlags(Extension::FROM_WEBSTORE
)
164 GetExtensionService()->AddExtension(result
.get());
168 scoped_ptr
<content::WebContents
> TestExtensionEnvironment::MakeTab() const {
169 scoped_ptr
<content::WebContents
> contents(
170 content::WebContentsTester::CreateTestWebContents(profile(), NULL
));
172 SessionTabHelper::CreateForWebContents(contents
.get());
173 return contents
.Pass();
176 void TestExtensionEnvironment::DeleteProfile() {
178 extension_service_
= nullptr;
181 } // namespace extensions