[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" |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 11 | #include "base/macros.h" |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 12 | #include "base/run_loop.h" |
[email protected] | e0ec3cb1 | 2013-04-03 17:35:30 | [diff] [blame] | 13 | #include "base/values.h" |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 14 | #include "build/build_config.h" |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 15 | #include "chrome/browser/extensions/extension_service.h" |
| 16 | #include "chrome/browser/extensions/test_extension_system.h" |
| 17 | #include "chrome/browser/sessions/session_tab_helper.h" |
[email protected] | e0ec3cb1 | 2013-04-03 17:35:30 | [diff] [blame] | 18 | #include "chrome/test/base/testing_profile.h" |
tapted | c7bd3ce | 2015-06-16 02:50:44 | [diff] [blame] | 19 | #include "content/public/test/test_browser_thread_bundle.h" |
[email protected] | e375b8a | 2013-11-14 07:45:00 | [diff] [blame] | 20 | #include "content/public/test/test_utils.h" |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 21 | #include "content/public/test/web_contents_tester.h" |
juncai | cf52333 | 2015-06-04 00:14:04 | [diff] [blame] | 22 | #include "extensions/browser/extension_prefs.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 23 | #include "extensions/common/extension.h" |
[email protected] | 22b7b2c | 2013-11-05 22:52:42 | [diff] [blame] | 24 | #include "extensions/common/extension_builder.h" |
| 25 | #include "extensions/common/value_builder.h" |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 26 | #include "testing/gtest/include/gtest/gtest.h" |
| 27 | |
tapted | dfd8eda | 2015-08-04 09:15:23 | [diff] [blame] | 28 | #if defined(OS_CHROMEOS) |
| 29 | #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h" |
| 30 | #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 31 | #include "chrome/browser/chromeos/settings/device_settings_service.h" |
| 32 | #endif |
| 33 | |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 34 | namespace extensions { |
| 35 | |
| 36 | using content::BrowserThread; |
| 37 | |
jackhou | c587f30 | 2015-04-13 08:16:39 | [diff] [blame] | 38 | namespace { |
| 39 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame^] | 40 | std::unique_ptr<base::DictionaryValue> MakeExtensionManifest( |
jackhou | c587f30 | 2015-04-13 08:16:39 | [diff] [blame] | 41 | const base::Value& manifest_extra) { |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame^] | 42 | std::unique_ptr<base::DictionaryValue> manifest = |
| 43 | DictionaryBuilder() |
| 44 | .Set("name", "Extension") |
| 45 | .Set("version", "1.0") |
| 46 | .Set("manifest_version", 2) |
| 47 | .Build(); |
jackhou | c587f30 | 2015-04-13 08:16:39 | [diff] [blame] | 48 | const base::DictionaryValue* manifest_extra_dict; |
| 49 | if (manifest_extra.GetAsDictionary(&manifest_extra_dict)) { |
| 50 | manifest->MergeDictionary(manifest_extra_dict); |
| 51 | } else { |
| 52 | std::string manifest_json; |
estade | 8d04646 | 2015-05-16 01:02:34 | [diff] [blame] | 53 | base::JSONWriter::Write(manifest_extra, &manifest_json); |
jackhou | c587f30 | 2015-04-13 08:16:39 | [diff] [blame] | 54 | ADD_FAILURE() << "Expected dictionary; got \"" << manifest_json << "\""; |
| 55 | } |
| 56 | return manifest; |
| 57 | } |
| 58 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame^] | 59 | std::unique_ptr<base::DictionaryValue> MakePackagedAppManifest() { |
tapted | c7bd3ce | 2015-06-16 02:50:44 | [diff] [blame] | 60 | return extensions::DictionaryBuilder() |
| 61 | .Set("name", "Test App Name") |
| 62 | .Set("version", "2.0") |
| 63 | .Set("manifest_version", 2) |
dcheng | 794d2bd | 2016-02-27 03:51:32 | [diff] [blame] | 64 | .Set("app", extensions::DictionaryBuilder() |
| 65 | .Set("background", |
| 66 | extensions::DictionaryBuilder() |
| 67 | .Set("scripts", extensions::ListBuilder() |
| 68 | .Append("background.js") |
| 69 | .Build()) |
| 70 | .Build()) |
| 71 | .Build()) |
tapted | c7bd3ce | 2015-06-16 02:50:44 | [diff] [blame] | 72 | .Build(); |
| 73 | } |
| 74 | |
jackhou | c587f30 | 2015-04-13 08:16:39 | [diff] [blame] | 75 | } // namespace |
| 76 | |
tapted | dfd8eda | 2015-08-04 09:15:23 | [diff] [blame] | 77 | // Extra environment state required for ChromeOS. |
| 78 | class TestExtensionEnvironment::ChromeOSEnv { |
| 79 | public: |
| 80 | ChromeOSEnv() {} |
| 81 | |
| 82 | private: |
| 83 | #if defined(OS_CHROMEOS) |
| 84 | chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; |
| 85 | chromeos::ScopedTestCrosSettings test_cros_settings_; |
| 86 | chromeos::ScopedTestUserManager test_user_manager_; |
| 87 | #endif |
| 88 | |
| 89 | DISALLOW_COPY_AND_ASSIGN(ChromeOSEnv); |
| 90 | }; |
| 91 | |
tapted | c7bd3ce | 2015-06-16 02:50:44 | [diff] [blame] | 92 | // static |
| 93 | ExtensionService* TestExtensionEnvironment::CreateExtensionServiceForProfile( |
| 94 | TestingProfile* profile) { |
| 95 | TestExtensionSystem* extension_system = |
| 96 | static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile)); |
| 97 | return extension_system->CreateExtensionService( |
| 98 | base::CommandLine::ForCurrentProcess(), base::FilePath(), false); |
| 99 | } |
| 100 | |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 101 | TestExtensionEnvironment::TestExtensionEnvironment() |
tapted | c7bd3ce | 2015-06-16 02:50:44 | [diff] [blame] | 102 | : thread_bundle_(new content::TestBrowserThreadBundle), |
tapted | c7bd3ce | 2015-06-16 02:50:44 | [diff] [blame] | 103 | extension_service_(nullptr) { |
tapted | dfd8eda | 2015-08-04 09:15:23 | [diff] [blame] | 104 | Init(); |
tapted | c7bd3ce | 2015-06-16 02:50:44 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | TestExtensionEnvironment::TestExtensionEnvironment( |
| 108 | base::MessageLoopForUI* message_loop) |
tapted | dfd8eda | 2015-08-04 09:15:23 | [diff] [blame] | 109 | : extension_service_(nullptr) { |
| 110 | Init(); |
| 111 | } |
| 112 | |
| 113 | void TestExtensionEnvironment::Init() { |
| 114 | profile_.reset(new TestingProfile); |
| 115 | #if defined(OS_CHROMEOS) |
| 116 | if (!chromeos::DeviceSettingsService::IsInitialized()) |
| 117 | chromeos_env_.reset(new ChromeOSEnv); |
| 118 | #endif |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | TestExtensionEnvironment::~TestExtensionEnvironment() { |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 122 | } |
| 123 | |
[email protected] | e0ec3cb1 | 2013-04-03 17:35:30 | [diff] [blame] | 124 | TestingProfile* TestExtensionEnvironment::profile() const { |
| 125 | return profile_.get(); |
| 126 | } |
| 127 | |
[email protected] | 94cf31c | 2013-12-07 03:58:32 | [diff] [blame] | 128 | TestExtensionSystem* TestExtensionEnvironment::GetExtensionSystem() { |
[email protected] | 5fbbea6 | 2014-02-26 20:07:33 | [diff] [blame] | 129 | return static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile())); |
[email protected] | 94cf31c | 2013-12-07 03:58:32 | [diff] [blame] | 130 | } |
| 131 | |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 132 | ExtensionService* TestExtensionEnvironment::GetExtensionService() { |
tapted | c7bd3ce | 2015-06-16 02:50:44 | [diff] [blame] | 133 | if (!extension_service_) |
| 134 | extension_service_ = CreateExtensionServiceForProfile(profile()); |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 135 | return extension_service_; |
| 136 | } |
| 137 | |
[email protected] | 94cf31c | 2013-12-07 03:58:32 | [diff] [blame] | 138 | ExtensionPrefs* TestExtensionEnvironment::GetExtensionPrefs() { |
tapted | c7bd3ce | 2015-06-16 02:50:44 | [diff] [blame] | 139 | return ExtensionPrefs::Get(profile_.get()); |
[email protected] | 94cf31c | 2013-12-07 03:58:32 | [diff] [blame] | 140 | } |
| 141 | |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 142 | const Extension* TestExtensionEnvironment::MakeExtension( |
[email protected] | e0ec3cb1 | 2013-04-03 17:35:30 | [diff] [blame] | 143 | const base::Value& manifest_extra) { |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame^] | 144 | std::unique_ptr<base::DictionaryValue> manifest = |
jackhou | c587f30 | 2015-04-13 08:16:39 | [diff] [blame] | 145 | MakeExtensionManifest(manifest_extra); |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 146 | scoped_refptr<Extension> result = |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 147 | ExtensionBuilder().SetManifest(std::move(manifest)).Build(); |
[email protected] | dc24976f | 2013-06-02 21:15:09 | [diff] [blame] | 148 | GetExtensionService()->AddExtension(result.get()); |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 149 | return result.get(); |
| 150 | } |
| 151 | |
jackhou | c587f30 | 2015-04-13 08:16:39 | [diff] [blame] | 152 | const Extension* TestExtensionEnvironment::MakeExtension( |
| 153 | const base::Value& manifest_extra, |
| 154 | const std::string& id) { |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame^] | 155 | std::unique_ptr<base::DictionaryValue> manifest = |
jackhou | c587f30 | 2015-04-13 08:16:39 | [diff] [blame] | 156 | MakeExtensionManifest(manifest_extra); |
| 157 | scoped_refptr<Extension> result = |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 158 | ExtensionBuilder().SetManifest(std::move(manifest)).SetID(id).Build(); |
jackhou | c587f30 | 2015-04-13 08:16:39 | [diff] [blame] | 159 | GetExtensionService()->AddExtension(result.get()); |
| 160 | return result.get(); |
| 161 | } |
| 162 | |
tapted | c7bd3ce | 2015-06-16 02:50:44 | [diff] [blame] | 163 | scoped_refptr<Extension> TestExtensionEnvironment::MakePackagedApp( |
| 164 | const std::string& id, |
| 165 | bool install) { |
| 166 | scoped_refptr<Extension> result = ExtensionBuilder() |
| 167 | .SetManifest(MakePackagedAppManifest()) |
tapted | dfd8eda | 2015-08-04 09:15:23 | [diff] [blame] | 168 | .AddFlags(Extension::FROM_WEBSTORE) |
tapted | c7bd3ce | 2015-06-16 02:50:44 | [diff] [blame] | 169 | .SetID(id) |
| 170 | .Build(); |
| 171 | if (install) |
| 172 | GetExtensionService()->AddExtension(result.get()); |
| 173 | return result; |
| 174 | } |
| 175 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame^] | 176 | std::unique_ptr<content::WebContents> TestExtensionEnvironment::MakeTab() |
| 177 | const { |
| 178 | std::unique_ptr<content::WebContents> contents( |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 179 | content::WebContentsTester::CreateTestWebContents(profile(), NULL)); |
| 180 | // Create a tab id. |
| 181 | SessionTabHelper::CreateForWebContents(contents.get()); |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 182 | return contents; |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 183 | } |
| 184 | |
tapted | c7bd3ce | 2015-06-16 02:50:44 | [diff] [blame] | 185 | void TestExtensionEnvironment::DeleteProfile() { |
| 186 | profile_.reset(); |
| 187 | extension_service_ = nullptr; |
| 188 | } |
| 189 | |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 190 | } // namespace extensions |