[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "chrome/browser/extensions/test_extension_environment.h" |
| 6 | |
limasdf | 3d10254 | 2015-12-09 03:58:45 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 9 | #include "base/command_line.h" |
| 10 | #include "base/json/json_writer.h" |
[email protected] | e0ec3cb1 | 2013-04-03 17:35:30 | [diff] [blame] | 11 | #include "base/values.h" |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 12 | #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] | e0ec3cb1 | 2013-04-03 17:35:30 | [diff] [blame] | 15 | #include "chrome/test/base/testing_profile.h" |
tapted | c7bd3ce | 2015-06-16 02:50:44 | [diff] [blame] | 16 | #include "content/public/test/test_browser_thread_bundle.h" |
[email protected] | e375b8a | 2013-11-14 07:45:00 | [diff] [blame] | 17 | #include "content/public/test/test_utils.h" |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 18 | #include "content/public/test/web_contents_tester.h" |
juncai | cf52333 | 2015-06-04 00:14:04 | [diff] [blame] | 19 | #include "extensions/browser/extension_prefs.h" |
[email protected] | 22b7b2c | 2013-11-05 22:52:42 | [diff] [blame] | 20 | #include "extensions/common/extension_builder.h" |
| 21 | #include "extensions/common/value_builder.h" |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 22 | #include "testing/gtest/include/gtest/gtest.h" |
| 23 | |
tapted | dfd8eda | 2015-08-04 09:15:23 | [diff] [blame] | 24 | #if defined(OS_CHROMEOS) |
| 25 | #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h" |
tapted | dfd8eda | 2015-08-04 09:15:23 | [diff] [blame] | 26 | #include "chrome/browser/chromeos/settings/device_settings_service.h" |
A Olsen | c63621c | 2018-08-13 15:32:17 | [diff] [blame] | 27 | #include "chrome/browser/chromeos/settings/scoped_cros_settings_test_helper.h" |
tapted | dfd8eda | 2015-08-04 09:15:23 | [diff] [blame] | 28 | #endif |
| 29 | |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 30 | namespace extensions { |
| 31 | |
| 32 | using content::BrowserThread; |
| 33 | |
jackhou | c587f30 | 2015-04-13 08:16:39 | [diff] [blame] | 34 | namespace { |
| 35 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 36 | std::unique_ptr<base::DictionaryValue> MakeExtensionManifest( |
jackhou | c587f30 | 2015-04-13 08:16:39 | [diff] [blame] | 37 | const base::Value& manifest_extra) { |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 38 | std::unique_ptr<base::DictionaryValue> manifest = |
| 39 | DictionaryBuilder() |
| 40 | .Set("name", "Extension") |
| 41 | .Set("version", "1.0") |
| 42 | .Set("manifest_version", 2) |
| 43 | .Build(); |
jackhou | c587f30 | 2015-04-13 08:16:39 | [diff] [blame] | 44 | 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; |
estade | 8d04646 | 2015-05-16 01:02:34 | [diff] [blame] | 49 | base::JSONWriter::Write(manifest_extra, &manifest_json); |
jackhou | c587f30 | 2015-04-13 08:16:39 | [diff] [blame] | 50 | ADD_FAILURE() << "Expected dictionary; got \"" << manifest_json << "\""; |
| 51 | } |
| 52 | return manifest; |
| 53 | } |
| 54 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 55 | std::unique_ptr<base::DictionaryValue> MakePackagedAppManifest() { |
tapted | c7bd3ce | 2015-06-16 02:50:44 | [diff] [blame] | 56 | return extensions::DictionaryBuilder() |
| 57 | .Set("name", "Test App Name") |
| 58 | .Set("version", "2.0") |
| 59 | .Set("manifest_version", 2) |
dcheng | 794d2bd | 2016-02-27 03:51:32 | [diff] [blame] | 60 | .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()) |
tapted | c7bd3ce | 2015-06-16 02:50:44 | [diff] [blame] | 68 | .Build(); |
| 69 | } |
| 70 | |
jackhou | c587f30 | 2015-04-13 08:16:39 | [diff] [blame] | 71 | } // namespace |
| 72 | |
Gabriel Charette | 0601da4 | 2018-03-29 14:46:34 | [diff] [blame] | 73 | #if defined(OS_CHROMEOS) |
tapted | dfd8eda | 2015-08-04 09:15:23 | [diff] [blame] | 74 | // Extra environment state required for ChromeOS. |
| 75 | class TestExtensionEnvironment::ChromeOSEnv { |
| 76 | public: |
| 77 | ChromeOSEnv() {} |
| 78 | |
| 79 | private: |
A Olsen | c63621c | 2018-08-13 15:32:17 | [diff] [blame] | 80 | chromeos::ScopedCrosSettingsTestHelper cros_settings_test_helper_; |
tapted | dfd8eda | 2015-08-04 09:15:23 | [diff] [blame] | 81 | chromeos::ScopedTestUserManager test_user_manager_; |
tapted | dfd8eda | 2015-08-04 09:15:23 | [diff] [blame] | 82 | |
| 83 | DISALLOW_COPY_AND_ASSIGN(ChromeOSEnv); |
| 84 | }; |
Gabriel Charette | 0601da4 | 2018-03-29 14:46:34 | [diff] [blame] | 85 | #endif // defined(OS_CHROMEOS) |
tapted | dfd8eda | 2015-08-04 09:15:23 | [diff] [blame] | 86 | |
tapted | c7bd3ce | 2015-06-16 02:50:44 | [diff] [blame] | 87 | // static |
| 88 | ExtensionService* 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 Charette | 0601da4 | 2018-03-29 14:46:34 | [diff] [blame] | 96 | TestExtensionEnvironment::TestExtensionEnvironment(Type type) |
| 97 | : thread_bundle_(type == Type::kWithTaskEnvironment |
| 98 | ? std::make_unique<content::TestBrowserThreadBundle>() |
| 99 | : nullptr), |
tapted | dfd8eda | 2015-08-04 09:15:23 | [diff] [blame] | 100 | #if defined(OS_CHROMEOS) |
Gabriel Charette | 0601da4 | 2018-03-29 14:46:34 | [diff] [blame] | 101 | chromeos_env_(chromeos::DeviceSettingsService::IsInitialized() |
| 102 | ? nullptr |
| 103 | : std::make_unique<ChromeOSEnv>()), |
tapted | dfd8eda | 2015-08-04 09:15:23 | [diff] [blame] | 104 | #endif |
Gabriel Charette | 0601da4 | 2018-03-29 14:46:34 | [diff] [blame] | 105 | profile_(std::make_unique<TestingProfile>()) { |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | TestExtensionEnvironment::~TestExtensionEnvironment() { |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 109 | } |
| 110 | |
[email protected] | e0ec3cb1 | 2013-04-03 17:35:30 | [diff] [blame] | 111 | TestingProfile* TestExtensionEnvironment::profile() const { |
| 112 | return profile_.get(); |
| 113 | } |
| 114 | |
[email protected] | 94cf31c | 2013-12-07 03:58:32 | [diff] [blame] | 115 | TestExtensionSystem* TestExtensionEnvironment::GetExtensionSystem() { |
[email protected] | 5fbbea6 | 2014-02-26 20:07:33 | [diff] [blame] | 116 | return static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile())); |
[email protected] | 94cf31c | 2013-12-07 03:58:32 | [diff] [blame] | 117 | } |
| 118 | |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 119 | ExtensionService* TestExtensionEnvironment::GetExtensionService() { |
tapted | c7bd3ce | 2015-06-16 02:50:44 | [diff] [blame] | 120 | if (!extension_service_) |
| 121 | extension_service_ = CreateExtensionServiceForProfile(profile()); |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 122 | return extension_service_; |
| 123 | } |
| 124 | |
[email protected] | 94cf31c | 2013-12-07 03:58:32 | [diff] [blame] | 125 | ExtensionPrefs* TestExtensionEnvironment::GetExtensionPrefs() { |
tapted | c7bd3ce | 2015-06-16 02:50:44 | [diff] [blame] | 126 | return ExtensionPrefs::Get(profile_.get()); |
[email protected] | 94cf31c | 2013-12-07 03:58:32 | [diff] [blame] | 127 | } |
| 128 | |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 129 | const Extension* TestExtensionEnvironment::MakeExtension( |
[email protected] | e0ec3cb1 | 2013-04-03 17:35:30 | [diff] [blame] | 130 | const base::Value& manifest_extra) { |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 131 | std::unique_ptr<base::DictionaryValue> manifest = |
jackhou | c587f30 | 2015-04-13 08:16:39 | [diff] [blame] | 132 | MakeExtensionManifest(manifest_extra); |
Devlin Cronin | 8e5892f | 2018-10-04 00:13:43 | [diff] [blame] | 133 | scoped_refptr<const Extension> result = |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 134 | ExtensionBuilder().SetManifest(std::move(manifest)).Build(); |
[email protected] | dc24976f | 2013-06-02 21:15:09 | [diff] [blame] | 135 | GetExtensionService()->AddExtension(result.get()); |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 136 | return result.get(); |
| 137 | } |
| 138 | |
jackhou | c587f30 | 2015-04-13 08:16:39 | [diff] [blame] | 139 | const Extension* TestExtensionEnvironment::MakeExtension( |
| 140 | const base::Value& manifest_extra, |
| 141 | const std::string& id) { |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 142 | std::unique_ptr<base::DictionaryValue> manifest = |
jackhou | c587f30 | 2015-04-13 08:16:39 | [diff] [blame] | 143 | MakeExtensionManifest(manifest_extra); |
Devlin Cronin | 8e5892f | 2018-10-04 00:13:43 | [diff] [blame] | 144 | scoped_refptr<const Extension> result = |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 145 | ExtensionBuilder().SetManifest(std::move(manifest)).SetID(id).Build(); |
jackhou | c587f30 | 2015-04-13 08:16:39 | [diff] [blame] | 146 | GetExtensionService()->AddExtension(result.get()); |
| 147 | return result.get(); |
| 148 | } |
| 149 | |
Devlin Cronin | 8e5892f | 2018-10-04 00:13:43 | [diff] [blame] | 150 | scoped_refptr<const Extension> TestExtensionEnvironment::MakePackagedApp( |
tapted | c7bd3ce | 2015-06-16 02:50:44 | [diff] [blame] | 151 | const std::string& id, |
| 152 | bool install) { |
Devlin Cronin | 8e5892f | 2018-10-04 00:13:43 | [diff] [blame] | 153 | scoped_refptr<const Extension> result = |
| 154 | ExtensionBuilder() |
| 155 | .SetManifest(MakePackagedAppManifest()) |
| 156 | .AddFlags(Extension::FROM_WEBSTORE) |
| 157 | .SetID(id) |
| 158 | .Build(); |
tapted | c7bd3ce | 2015-06-16 02:50:44 | [diff] [blame] | 159 | if (install) |
| 160 | GetExtensionService()->AddExtension(result.get()); |
| 161 | return result; |
| 162 | } |
| 163 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 164 | std::unique_ptr<content::WebContents> TestExtensionEnvironment::MakeTab() |
| 165 | const { |
| 166 | std::unique_ptr<content::WebContents> contents( |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 167 | content::WebContentsTester::CreateTestWebContents(profile(), NULL)); |
| 168 | // Create a tab id. |
| 169 | SessionTabHelper::CreateForWebContents(contents.get()); |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 170 | return contents; |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 171 | } |
| 172 | |
tapted | c7bd3ce | 2015-06-16 02:50:44 | [diff] [blame] | 173 | void TestExtensionEnvironment::DeleteProfile() { |
| 174 | profile_.reset(); |
| 175 | extension_service_ = nullptr; |
| 176 | } |
| 177 | |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 178 | } // namespace extensions |