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"
25 #include "ui/aura/env.h"
28 namespace extensions
{
30 using content::BrowserThread
;
34 scoped_ptr
<base::DictionaryValue
> MakeExtensionManifest(
35 const base::Value
& manifest_extra
) {
36 scoped_ptr
<base::DictionaryValue
> manifest
= DictionaryBuilder()
37 .Set("name", "Extension")
38 .Set("version", "1.0")
39 .Set("manifest_version", 2)
41 const base::DictionaryValue
* manifest_extra_dict
;
42 if (manifest_extra
.GetAsDictionary(&manifest_extra_dict
)) {
43 manifest
->MergeDictionary(manifest_extra_dict
);
45 std::string manifest_json
;
46 base::JSONWriter::Write(manifest_extra
, &manifest_json
);
47 ADD_FAILURE() << "Expected dictionary; got \"" << manifest_json
<< "\"";
52 scoped_ptr
<base::DictionaryValue
> MakePackagedAppManifest() {
53 return extensions::DictionaryBuilder()
54 .Set("name", "Test App Name")
55 .Set("version", "2.0")
56 .Set("manifest_version", 2)
57 .Set("app", extensions::DictionaryBuilder().Set(
59 extensions::DictionaryBuilder().Set(
61 extensions::ListBuilder().Append("background.js"))))
68 ExtensionService
* TestExtensionEnvironment::CreateExtensionServiceForProfile(
69 TestingProfile
* profile
) {
70 TestExtensionSystem
* extension_system
=
71 static_cast<TestExtensionSystem
*>(ExtensionSystem::Get(profile
));
72 return extension_system
->CreateExtensionService(
73 base::CommandLine::ForCurrentProcess(), base::FilePath(), false);
76 TestExtensionEnvironment::TestExtensionEnvironment()
77 : thread_bundle_(new content::TestBrowserThreadBundle
),
78 profile_(new TestingProfile
),
79 extension_service_(nullptr) {
81 aura::Env::CreateInstance(true);
85 TestExtensionEnvironment::TestExtensionEnvironment(
86 base::MessageLoopForUI
* message_loop
)
87 : profile_(new TestingProfile
), extension_service_(nullptr) {
89 aura::Env::CreateInstance(true);
93 TestExtensionEnvironment::~TestExtensionEnvironment() {
95 aura::Env::DeleteInstance();
99 TestingProfile
* TestExtensionEnvironment::profile() const {
100 return profile_
.get();
103 TestExtensionSystem
* TestExtensionEnvironment::GetExtensionSystem() {
104 return static_cast<TestExtensionSystem
*>(ExtensionSystem::Get(profile()));
107 ExtensionService
* TestExtensionEnvironment::GetExtensionService() {
108 if (!extension_service_
)
109 extension_service_
= CreateExtensionServiceForProfile(profile());
110 return extension_service_
;
113 ExtensionPrefs
* TestExtensionEnvironment::GetExtensionPrefs() {
114 return ExtensionPrefs::Get(profile_
.get());
117 const Extension
* TestExtensionEnvironment::MakeExtension(
118 const base::Value
& manifest_extra
) {
119 scoped_ptr
<base::DictionaryValue
> manifest
=
120 MakeExtensionManifest(manifest_extra
);
121 scoped_refptr
<Extension
> result
=
122 ExtensionBuilder().SetManifest(manifest
.Pass()).Build();
123 GetExtensionService()->AddExtension(result
.get());
127 const Extension
* TestExtensionEnvironment::MakeExtension(
128 const base::Value
& manifest_extra
,
129 const std::string
& id
) {
130 scoped_ptr
<base::DictionaryValue
> manifest
=
131 MakeExtensionManifest(manifest_extra
);
132 scoped_refptr
<Extension
> result
=
133 ExtensionBuilder().SetManifest(manifest
.Pass()).SetID(id
).Build();
134 GetExtensionService()->AddExtension(result
.get());
138 scoped_refptr
<Extension
> TestExtensionEnvironment::MakePackagedApp(
139 const std::string
& id
,
141 scoped_refptr
<Extension
> result
= ExtensionBuilder()
142 .SetManifest(MakePackagedAppManifest())
146 GetExtensionService()->AddExtension(result
.get());
150 scoped_ptr
<content::WebContents
> TestExtensionEnvironment::MakeTab() const {
151 scoped_ptr
<content::WebContents
> contents(
152 content::WebContentsTester::CreateTestWebContents(profile(), NULL
));
154 SessionTabHelper::CreateForWebContents(contents
.get());
155 return contents
.Pass();
158 void TestExtensionEnvironment::DeleteProfile() {
160 extension_service_
= nullptr;
163 } // namespace extensions