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_utils.h"
16 #include "content/public/test/web_contents_tester.h"
17 #include "extensions/common/extension.h"
18 #include "extensions/common/extension_builder.h"
19 #include "extensions/common/value_builder.h"
20 #include "testing/gtest/include/gtest/gtest.h"
23 #include "ui/aura/env.h"
26 namespace extensions
{
28 using content::BrowserThread
;
30 TestExtensionEnvironment::TestExtensionEnvironment()
31 : profile_(new TestingProfile
),
32 extension_service_(NULL
),
33 extension_prefs_(NULL
) {
35 aura::Env::CreateInstance(true);
39 TestExtensionEnvironment::~TestExtensionEnvironment() {
41 aura::Env::DeleteInstance();
45 TestingProfile
* TestExtensionEnvironment::profile() const {
46 return profile_
.get();
49 TestExtensionSystem
* TestExtensionEnvironment::GetExtensionSystem() {
50 return static_cast<TestExtensionSystem
*>(ExtensionSystem::Get(profile()));
53 ExtensionService
* TestExtensionEnvironment::GetExtensionService() {
54 if (extension_service_
== NULL
) {
55 extension_service_
= GetExtensionSystem()->CreateExtensionService(
56 base::CommandLine::ForCurrentProcess(), base::FilePath(), false);
58 return extension_service_
;
61 ExtensionPrefs
* TestExtensionEnvironment::GetExtensionPrefs() {
62 if (extension_prefs_
== NULL
) {
63 extension_prefs_
= GetExtensionSystem()->CreateExtensionPrefs(
64 base::CommandLine::ForCurrentProcess(), base::FilePath());
66 return extension_prefs_
;
69 const Extension
* TestExtensionEnvironment::MakeExtension(
70 const base::Value
& manifest_extra
) {
71 scoped_ptr
<base::DictionaryValue
> manifest
= DictionaryBuilder()
72 .Set("name", "Extension")
73 .Set("version", "1.0")
74 .Set("manifest_version", 2)
76 const base::DictionaryValue
* manifest_extra_dict
;
77 if (manifest_extra
.GetAsDictionary(&manifest_extra_dict
)) {
78 manifest
->MergeDictionary(manifest_extra_dict
);
80 std::string manifest_json
;
81 base::JSONWriter::Write(&manifest_extra
, &manifest_json
);
82 ADD_FAILURE() << "Expected dictionary; got \"" << manifest_json
<< "\"";
85 scoped_refptr
<Extension
> result
=
86 ExtensionBuilder().SetManifest(manifest
.Pass()).Build();
87 GetExtensionService()->AddExtension(result
.get());
91 scoped_ptr
<content::WebContents
> TestExtensionEnvironment::MakeTab() const {
92 scoped_ptr
<content::WebContents
> contents(
93 content::WebContentsTester::CreateTestWebContents(profile(), NULL
));
95 SessionTabHelper::CreateForWebContents(contents
.get());
96 return contents
.Pass();
99 } // namespace extensions