blob: 2e1c8192059ff6ca7a86a8117e849965f28fd15f [file] [log] [blame]
[email protected]11d42c82013-02-01 00:14:321// 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.
4
5#include "chrome/browser/extensions/test_extension_environment.h"
6
limasdf3d102542015-12-09 03:58:457#include <utility>
8
[email protected]11d42c82013-02-01 00:14:329#include "base/command_line.h"
10#include "base/json/json_writer.h"
[email protected]e0ec3cb12013-04-03 17:35:3011#include "base/values.h"
[email protected]11d42c82013-02-01 00:14:3212#include "chrome/browser/extensions/extension_service.h"
13#include "chrome/browser/extensions/test_extension_system.h"
14#include "chrome/browser/sessions/session_tab_helper.h"
[email protected]e0ec3cb12013-04-03 17:35:3015#include "chrome/test/base/testing_profile.h"
taptedc7bd3ce2015-06-16 02:50:4416#include "content/public/test/test_browser_thread_bundle.h"
[email protected]e375b8a2013-11-14 07:45:0017#include "content/public/test/test_utils.h"
[email protected]11d42c82013-02-01 00:14:3218#include "content/public/test/web_contents_tester.h"
juncaicf523332015-06-04 00:14:0419#include "extensions/browser/extension_prefs.h"
[email protected]22b7b2c2013-11-05 22:52:4220#include "extensions/common/extension_builder.h"
21#include "extensions/common/value_builder.h"
[email protected]11d42c82013-02-01 00:14:3222#include "testing/gtest/include/gtest/gtest.h"
23
tapteddfd8eda2015-08-04 09:15:2324#if defined(OS_CHROMEOS)
25#include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
tapteddfd8eda2015-08-04 09:15:2326#include "chrome/browser/chromeos/settings/device_settings_service.h"
A Olsenc63621c2018-08-13 15:32:1727#include "chrome/browser/chromeos/settings/scoped_cros_settings_test_helper.h"
tapteddfd8eda2015-08-04 09:15:2328#endif
29
[email protected]11d42c82013-02-01 00:14:3230namespace extensions {
31
32using content::BrowserThread;
33
jackhouc587f302015-04-13 08:16:3934namespace {
35
dchengc963c7142016-04-08 03:55:2236std::unique_ptr<base::DictionaryValue> MakeExtensionManifest(
jackhouc587f302015-04-13 08:16:3937 const base::Value& manifest_extra) {
dchengc963c7142016-04-08 03:55:2238 std::unique_ptr<base::DictionaryValue> manifest =
39 DictionaryBuilder()
40 .Set("name", "Extension")
41 .Set("version", "1.0")
42 .Set("manifest_version", 2)
43 .Build();
jackhouc587f302015-04-13 08:16:3944 const base::DictionaryValue* manifest_extra_dict;
45 if (manifest_extra.GetAsDictionary(&manifest_extra_dict)) {
46 manifest->MergeDictionary(manifest_extra_dict);
47 } else {
48 std::string manifest_json;
estade8d046462015-05-16 01:02:3449 base::JSONWriter::Write(manifest_extra, &manifest_json);
jackhouc587f302015-04-13 08:16:3950 ADD_FAILURE() << "Expected dictionary; got \"" << manifest_json << "\"";
51 }
52 return manifest;
53}
54
dchengc963c7142016-04-08 03:55:2255std::unique_ptr<base::DictionaryValue> MakePackagedAppManifest() {
taptedc7bd3ce2015-06-16 02:50:4456 return extensions::DictionaryBuilder()
57 .Set("name", "Test App Name")
58 .Set("version", "2.0")
59 .Set("manifest_version", 2)
dcheng794d2bd2016-02-27 03:51:3260 .Set("app", extensions::DictionaryBuilder()
61 .Set("background",
62 extensions::DictionaryBuilder()
63 .Set("scripts", extensions::ListBuilder()
64 .Append("background.js")
65 .Build())
66 .Build())
67 .Build())
taptedc7bd3ce2015-06-16 02:50:4468 .Build();
69}
70
jackhouc587f302015-04-13 08:16:3971} // namespace
72
Gabriel Charette0601da42018-03-29 14:46:3473#if defined(OS_CHROMEOS)
tapteddfd8eda2015-08-04 09:15:2374// Extra environment state required for ChromeOS.
75class TestExtensionEnvironment::ChromeOSEnv {
76 public:
77 ChromeOSEnv() {}
78
79 private:
A Olsenc63621c2018-08-13 15:32:1780 chromeos::ScopedCrosSettingsTestHelper cros_settings_test_helper_;
tapteddfd8eda2015-08-04 09:15:2381 chromeos::ScopedTestUserManager test_user_manager_;
tapteddfd8eda2015-08-04 09:15:2382
83 DISALLOW_COPY_AND_ASSIGN(ChromeOSEnv);
84};
Gabriel Charette0601da42018-03-29 14:46:3485#endif // defined(OS_CHROMEOS)
tapteddfd8eda2015-08-04 09:15:2386
taptedc7bd3ce2015-06-16 02:50:4487// static
88ExtensionService* TestExtensionEnvironment::CreateExtensionServiceForProfile(
89 TestingProfile* profile) {
90 TestExtensionSystem* extension_system =
91 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile));
92 return extension_system->CreateExtensionService(
93 base::CommandLine::ForCurrentProcess(), base::FilePath(), false);
94}
95
Gabriel Charette0601da42018-03-29 14:46:3496TestExtensionEnvironment::TestExtensionEnvironment(Type type)
97 : thread_bundle_(type == Type::kWithTaskEnvironment
98 ? std::make_unique<content::TestBrowserThreadBundle>()
99 : nullptr),
tapteddfd8eda2015-08-04 09:15:23100#if defined(OS_CHROMEOS)
Gabriel Charette0601da42018-03-29 14:46:34101 chromeos_env_(chromeos::DeviceSettingsService::IsInitialized()
102 ? nullptr
103 : std::make_unique<ChromeOSEnv>()),
tapteddfd8eda2015-08-04 09:15:23104#endif
Gabriel Charette0601da42018-03-29 14:46:34105 profile_(std::make_unique<TestingProfile>()) {
[email protected]11d42c82013-02-01 00:14:32106}
107
108TestExtensionEnvironment::~TestExtensionEnvironment() {
[email protected]11d42c82013-02-01 00:14:32109}
110
[email protected]e0ec3cb12013-04-03 17:35:30111TestingProfile* TestExtensionEnvironment::profile() const {
112 return profile_.get();
113}
114
[email protected]94cf31c2013-12-07 03:58:32115TestExtensionSystem* TestExtensionEnvironment::GetExtensionSystem() {
[email protected]5fbbea62014-02-26 20:07:33116 return static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile()));
[email protected]94cf31c2013-12-07 03:58:32117}
118
[email protected]11d42c82013-02-01 00:14:32119ExtensionService* TestExtensionEnvironment::GetExtensionService() {
taptedc7bd3ce2015-06-16 02:50:44120 if (!extension_service_)
121 extension_service_ = CreateExtensionServiceForProfile(profile());
[email protected]11d42c82013-02-01 00:14:32122 return extension_service_;
123}
124
[email protected]94cf31c2013-12-07 03:58:32125ExtensionPrefs* TestExtensionEnvironment::GetExtensionPrefs() {
taptedc7bd3ce2015-06-16 02:50:44126 return ExtensionPrefs::Get(profile_.get());
[email protected]94cf31c2013-12-07 03:58:32127}
128
[email protected]11d42c82013-02-01 00:14:32129const Extension* TestExtensionEnvironment::MakeExtension(
[email protected]e0ec3cb12013-04-03 17:35:30130 const base::Value& manifest_extra) {
dchengc963c7142016-04-08 03:55:22131 std::unique_ptr<base::DictionaryValue> manifest =
jackhouc587f302015-04-13 08:16:39132 MakeExtensionManifest(manifest_extra);
Devlin Cronin8e5892f2018-10-04 00:13:43133 scoped_refptr<const Extension> result =
dcheng1fc00f12015-12-26 22:18:03134 ExtensionBuilder().SetManifest(std::move(manifest)).Build();
[email protected]dc24976f2013-06-02 21:15:09135 GetExtensionService()->AddExtension(result.get());
[email protected]11d42c82013-02-01 00:14:32136 return result.get();
137}
138
jackhouc587f302015-04-13 08:16:39139const Extension* TestExtensionEnvironment::MakeExtension(
140 const base::Value& manifest_extra,
141 const std::string& id) {
dchengc963c7142016-04-08 03:55:22142 std::unique_ptr<base::DictionaryValue> manifest =
jackhouc587f302015-04-13 08:16:39143 MakeExtensionManifest(manifest_extra);
Devlin Cronin8e5892f2018-10-04 00:13:43144 scoped_refptr<const Extension> result =
dcheng1fc00f12015-12-26 22:18:03145 ExtensionBuilder().SetManifest(std::move(manifest)).SetID(id).Build();
jackhouc587f302015-04-13 08:16:39146 GetExtensionService()->AddExtension(result.get());
147 return result.get();
148}
149
Devlin Cronin8e5892f2018-10-04 00:13:43150scoped_refptr<const Extension> TestExtensionEnvironment::MakePackagedApp(
taptedc7bd3ce2015-06-16 02:50:44151 const std::string& id,
152 bool install) {
Devlin Cronin8e5892f2018-10-04 00:13:43153 scoped_refptr<const Extension> result =
154 ExtensionBuilder()
155 .SetManifest(MakePackagedAppManifest())
156 .AddFlags(Extension::FROM_WEBSTORE)
157 .SetID(id)
158 .Build();
taptedc7bd3ce2015-06-16 02:50:44159 if (install)
160 GetExtensionService()->AddExtension(result.get());
161 return result;
162}
163
dchengc963c7142016-04-08 03:55:22164std::unique_ptr<content::WebContents> TestExtensionEnvironment::MakeTab()
165 const {
166 std::unique_ptr<content::WebContents> contents(
[email protected]11d42c82013-02-01 00:14:32167 content::WebContentsTester::CreateTestWebContents(profile(), NULL));
168 // Create a tab id.
169 SessionTabHelper::CreateForWebContents(contents.get());
dcheng1fc00f12015-12-26 22:18:03170 return contents;
[email protected]11d42c82013-02-01 00:14:32171}
172
taptedc7bd3ce2015-06-16 02:50:44173void TestExtensionEnvironment::DeleteProfile() {
174 profile_.reset();
175 extension_service_ = nullptr;
176}
177
[email protected]11d42c82013-02-01 00:14:32178} // namespace extensions