Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / extensions / test_extension_environment.cc
blob6ff01bde2fe8ed8735286b159499296e27992391
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(USE_AURA)
25 #include "ui/aura/env.h"
26 #endif
28 namespace extensions {
30 using content::BrowserThread;
32 namespace {
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)
40 .Build();
41 const base::DictionaryValue* manifest_extra_dict;
42 if (manifest_extra.GetAsDictionary(&manifest_extra_dict)) {
43 manifest->MergeDictionary(manifest_extra_dict);
44 } else {
45 std::string manifest_json;
46 base::JSONWriter::Write(manifest_extra, &manifest_json);
47 ADD_FAILURE() << "Expected dictionary; got \"" << manifest_json << "\"";
49 return manifest;
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(
58 "background",
59 extensions::DictionaryBuilder().Set(
60 "scripts",
61 extensions::ListBuilder().Append("background.js"))))
62 .Build();
65 } // namespace
67 // static
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) {
80 #if defined(USE_AURA)
81 aura::Env::CreateInstance(true);
82 #endif
85 TestExtensionEnvironment::TestExtensionEnvironment(
86 base::MessageLoopForUI* message_loop)
87 : profile_(new TestingProfile), extension_service_(nullptr) {
88 #if defined(USE_AURA)
89 aura::Env::CreateInstance(true);
90 #endif
93 TestExtensionEnvironment::~TestExtensionEnvironment() {
94 #if defined(USE_AURA)
95 aura::Env::DeleteInstance();
96 #endif
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());
124 return 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());
135 return result.get();
138 scoped_refptr<Extension> TestExtensionEnvironment::MakePackagedApp(
139 const std::string& id,
140 bool install) {
141 scoped_refptr<Extension> result = ExtensionBuilder()
142 .SetManifest(MakePackagedAppManifest())
143 .SetID(id)
144 .Build();
145 if (install)
146 GetExtensionService()->AddExtension(result.get());
147 return result;
150 scoped_ptr<content::WebContents> TestExtensionEnvironment::MakeTab() const {
151 scoped_ptr<content::WebContents> contents(
152 content::WebContentsTester::CreateTestWebContents(profile(), NULL));
153 // Create a tab id.
154 SessionTabHelper::CreateForWebContents(contents.get());
155 return contents.Pass();
158 void TestExtensionEnvironment::DeleteProfile() {
159 profile_.reset();
160 extension_service_ = nullptr;
163 } // namespace extensions