[email protected] | 582f6e9 | 2014-07-16 23:39:15 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 582f6e9 | 2014-07-16 23:39:15 | [diff] [blame] | 5 | #include "extensions/renderer/module_system_test.h" |
| 6 | |
avi | 2d124c0 | 2015-12-23 06:36:42 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
[email protected] | 582f6e9 | 2014-07-16 23:39:15 | [diff] [blame] | 9 | #include <map> |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 10 | #include <memory> |
[email protected] | 582f6e9 | 2014-07-16 23:39:15 | [diff] [blame] | 11 | #include <string> |
dcheng | e59eca160 | 2015-12-18 17:48:00 | [diff] [blame] | 12 | #include <utility> |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 13 | |
Devlin Cronin | cc02a0c | 2019-01-03 22:15:07 | [diff] [blame] | 14 | #include "base/bind.h" |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 15 | #include "base/callback.h" |
rdevlin.cronin | 585b125 | 2016-04-19 23:29:26 | [diff] [blame] | 16 | #include "base/command_line.h" |
Findit | 59c7f1e | 2019-03-12 07:00:56 | [diff] [blame] | 17 | #include "base/feature_list.h" |
[email protected] | f8d87d3 | 2013-06-06 02:51:29 | [diff] [blame] | 18 | #include "base/files/file_path.h" |
thestig | 9471270 | 2014-09-10 07:46:59 | [diff] [blame] | 19 | #include "base/files/file_util.h" |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 20 | #include "base/lazy_instance.h" |
[email protected] | f8d87d3 | 2013-06-06 02:51:29 | [diff] [blame] | 21 | #include "base/path_service.h" |
Gabriel Charette | 078e366 | 2017-08-28 22:59:04 | [diff] [blame] | 22 | #include "base/run_loop.h" |
[email protected] | 4570a25 | 2013-03-31 00:35:43 | [diff] [blame] | 23 | #include "base/strings/string_piece.h" |
Devlin Cronin | 2db58e3 | 2017-08-15 21:29:29 | [diff] [blame] | 24 | #include "extensions/common/extension_builder.h" |
Findit | 59c7f1e | 2019-03-12 07:00:56 | [diff] [blame] | 25 | #include "extensions/common/extension_features.h" |
[email protected] | 582f6e9 | 2014-07-16 23:39:15 | [diff] [blame] | 26 | #include "extensions/common/extension_paths.h" |
Devlin Cronin | 2db58e3 | 2017-08-15 21:29:29 | [diff] [blame] | 27 | #include "extensions/common/value_builder.h" |
Devlin Cronin | 8cade48 | 2017-07-20 03:09:31 | [diff] [blame] | 28 | #include "extensions/renderer/ipc_message_sender.h" |
[email protected] | 701a94e | 2014-04-17 04:37:37 | [diff] [blame] | 29 | #include "extensions/renderer/logging_native_handler.h" |
Devlin Cronin | 8cade48 | 2017-07-20 03:09:31 | [diff] [blame] | 30 | #include "extensions/renderer/native_extension_bindings_system.h" |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 31 | #include "extensions/renderer/object_backed_native_handler.h" |
kalman | 33076cb | 2015-08-11 19:12:07 | [diff] [blame] | 32 | #include "extensions/renderer/safe_builtins.h" |
Devlin Cronin | 8cade48 | 2017-07-20 03:09:31 | [diff] [blame] | 33 | #include "extensions/renderer/script_context_set.h" |
rdevlin.cronin | 892cc67 | 2016-12-19 20:00:18 | [diff] [blame] | 34 | #include "extensions/renderer/string_source_map.h" |
| 35 | #include "extensions/renderer/test_v8_extension_configuration.h" |
[email protected] | 701a94e | 2014-04-17 04:37:37 | [diff] [blame] | 36 | #include "extensions/renderer/utils_native_handler.h" |
Devlin Cronin | 8cade48 | 2017-07-20 03:09:31 | [diff] [blame] | 37 | #include "gin/converter.h" |
[email protected] | 11844fa | 2012-05-10 00:35:59 | [diff] [blame] | 38 | #include "ui/base/resource/resource_bundle.h" |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 39 | |
[email protected] | 582f6e9 | 2014-07-16 23:39:15 | [diff] [blame] | 40 | namespace extensions { |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 41 | namespace { |
| 42 | |
| 43 | class FailsOnException : public ModuleSystem::ExceptionHandler { |
| 44 | public: |
bashi | 6a4854f | 2015-06-19 00:51:51 | [diff] [blame] | 45 | FailsOnException() : ModuleSystem::ExceptionHandler(nullptr) {} |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 46 | void HandleUncaughtException(const v8::TryCatch& try_catch) override { |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 47 | FAIL() << "Uncaught exception: " << CreateExceptionString(try_catch); |
| 48 | } |
| 49 | }; |
| 50 | |
Devlin Cronin | 8cade48 | 2017-07-20 03:09:31 | [diff] [blame] | 51 | class GetAPINatives : public ObjectBackedNativeHandler { |
| 52 | public: |
| 53 | GetAPINatives(ScriptContext* context, |
| 54 | NativeExtensionBindingsSystem* bindings_system) |
Devlin Cronin | d9ea834 | 2018-01-27 06:00:04 | [diff] [blame] | 55 | : ObjectBackedNativeHandler(context), bindings_system_(bindings_system) { |
Findit | 59c7f1e | 2019-03-12 07:00:56 | [diff] [blame] | 56 | DCHECK_EQ( |
| 57 | base::FeatureList::IsEnabled(extensions_features::kNativeCrxBindings), |
| 58 | !!bindings_system); |
Devlin Cronin | d9ea834 | 2018-01-27 06:00:04 | [diff] [blame] | 59 | } |
| 60 | ~GetAPINatives() override {} |
Devlin Cronin | 8cade48 | 2017-07-20 03:09:31 | [diff] [blame] | 61 | |
Devlin Cronin | d9ea834 | 2018-01-27 06:00:04 | [diff] [blame] | 62 | // ObjectBackedNativeHandler: |
| 63 | void AddRoutes() override { |
Devlin Cronin | 8cade48 | 2017-07-20 03:09:31 | [diff] [blame] | 64 | auto get_api = [](ScriptContext* context, |
| 65 | NativeExtensionBindingsSystem* bindings_system, |
| 66 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 67 | CHECK_EQ(1, args.Length()); |
| 68 | CHECK(args[0]->IsString()); |
Dan Elphick | 38a50805 | 2018-07-23 22:19:53 | [diff] [blame] | 69 | std::string api_name = gin::V8ToString(context->isolate(), args[0]); |
Devlin Cronin | 8cade48 | 2017-07-20 03:09:31 | [diff] [blame] | 70 | v8::Local<v8::Object> api; |
| 71 | if (bindings_system) { |
| 72 | api = bindings_system->GetAPIObjectForTesting(context, api_name); |
| 73 | } else { |
| 74 | v8::Local<v8::Object> full_binding; |
| 75 | CHECK( |
| 76 | context->module_system()->Require(api_name).ToLocal(&full_binding)) |
| 77 | << "Failed to get: " << api_name; |
| 78 | v8::Local<v8::Value> api_value; |
| 79 | CHECK(full_binding |
| 80 | ->Get(context->v8_context(), |
| 81 | gin::StringToSymbol(context->isolate(), "binding")) |
| 82 | .ToLocal(&api_value)) |
| 83 | << "Failed to get: " << api_name; |
| 84 | CHECK(api_value->IsObject()) << "Failed to get: " << api_name; |
| 85 | api = api_value.As<v8::Object>(); |
| 86 | } |
| 87 | args.GetReturnValue().Set(api); |
| 88 | }; |
| 89 | |
Devlin Cronin | cc02a0c | 2019-01-03 22:15:07 | [diff] [blame] | 90 | RouteHandlerFunction( |
| 91 | "get", base::BindRepeating(get_api, context(), bindings_system_)); |
Devlin Cronin | 8cade48 | 2017-07-20 03:09:31 | [diff] [blame] | 92 | } |
Devlin Cronin | d9ea834 | 2018-01-27 06:00:04 | [diff] [blame] | 93 | |
| 94 | private: |
| 95 | NativeExtensionBindingsSystem* bindings_system_ = nullptr; |
| 96 | |
| 97 | DISALLOW_COPY_AND_ASSIGN(GetAPINatives); |
Devlin Cronin | 8cade48 | 2017-07-20 03:09:31 | [diff] [blame] | 98 | }; |
| 99 | |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 100 | } // namespace |
| 101 | |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 102 | // Native JS functions for doing asserts. |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 103 | class ModuleSystemTestEnvironment::AssertNatives |
| 104 | : public ObjectBackedNativeHandler { |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 105 | public: |
[email protected] | 582f6e9 | 2014-07-16 23:39:15 | [diff] [blame] | 106 | explicit AssertNatives(ScriptContext* context) |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 107 | : ObjectBackedNativeHandler(context), |
[email protected] | 2e0e0bc | 2013-02-04 10:30:34 | [diff] [blame] | 108 | assertion_made_(false), |
Devlin Cronin | d9ea834 | 2018-01-27 06:00:04 | [diff] [blame] | 109 | failed_(false) {} |
| 110 | |
| 111 | // ObjectBackedNativeHandler: |
| 112 | void AddRoutes() override { |
Devlin Cronin | cc02a0c | 2019-01-03 22:15:07 | [diff] [blame] | 113 | RouteHandlerFunction("AssertTrue", |
| 114 | base::BindRepeating(&AssertNatives::AssertTrue, |
| 115 | base::Unretained(this))); |
| 116 | RouteHandlerFunction("AssertFalse", |
| 117 | base::BindRepeating(&AssertNatives::AssertFalse, |
| 118 | base::Unretained(this))); |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | bool assertion_made() { return assertion_made_; } |
| 122 | bool failed() { return failed_; } |
| 123 | |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 124 | void AssertTrue(const v8::FunctionCallbackInfo<v8::Value>& args) { |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 125 | CHECK_EQ(1, args.Length()); |
| 126 | assertion_made_ = true; |
dcarney | 04cd964 | 2014-11-21 13:58:11 | [diff] [blame] | 127 | failed_ = failed_ || !args[0]->ToBoolean(args.GetIsolate())->Value(); |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 128 | } |
| 129 | |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 130 | void AssertFalse(const v8::FunctionCallbackInfo<v8::Value>& args) { |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 131 | CHECK_EQ(1, args.Length()); |
| 132 | assertion_made_ = true; |
dcarney | 04cd964 | 2014-11-21 13:58:11 | [diff] [blame] | 133 | failed_ = failed_ || args[0]->ToBoolean(args.GetIsolate())->Value(); |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | private: |
| 137 | bool assertion_made_; |
| 138 | bool failed_; |
| 139 | }; |
| 140 | |
Devlin Cronin | 8cade48 | 2017-07-20 03:09:31 | [diff] [blame] | 141 | ModuleSystemTestEnvironment::ModuleSystemTestEnvironment( |
| 142 | v8::Isolate* isolate, |
Devlin Cronin | 2db58e3 | 2017-08-15 21:29:29 | [diff] [blame] | 143 | ScriptContextSet* context_set, |
| 144 | scoped_refptr<const Extension> extension) |
[email protected] | 99ea16b | 2014-07-17 22:15:56 | [diff] [blame] | 145 | : isolate_(isolate), |
| 146 | context_holder_(new gin::ContextHolder(isolate_)), |
| 147 | handle_scope_(isolate_), |
Devlin Cronin | 2db58e3 | 2017-08-15 21:29:29 | [diff] [blame] | 148 | extension_(extension), |
Devlin Cronin | 8cade48 | 2017-07-20 03:09:31 | [diff] [blame] | 149 | context_set_(context_set), |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 150 | source_map_(new StringSourceMap()) { |
kalman | 33076cb | 2015-08-11 19:12:07 | [diff] [blame] | 151 | context_holder_->SetContext(v8::Context::New( |
rdevlin.cronin | 892cc67 | 2016-12-19 20:00:18 | [diff] [blame] | 152 | isolate, TestV8ExtensionConfiguration::GetConfiguration())); |
Devlin Cronin | 8cade48 | 2017-07-20 03:09:31 | [diff] [blame] | 153 | |
| 154 | { |
Jeremy Roman | 16529d0e | 2017-08-24 18:13:47 | [diff] [blame] | 155 | auto context = std::make_unique<ScriptContext>( |
Devlin Cronin | 2db58e3 | 2017-08-15 21:29:29 | [diff] [blame] | 156 | context_holder_->context(), |
| 157 | nullptr, // WebFrame |
| 158 | extension_.get(), Feature::BLESSED_EXTENSION_CONTEXT, extension_.get(), |
| 159 | Feature::BLESSED_EXTENSION_CONTEXT); |
Devlin Cronin | 8cade48 | 2017-07-20 03:09:31 | [diff] [blame] | 160 | context_ = context.get(); |
| 161 | context_set_->AddForTesting(std::move(context)); |
| 162 | } |
| 163 | |
[email protected] | 9a59844 | 2013-06-04 16:39:12 | [diff] [blame] | 164 | context_->v8_context()->Enter(); |
Devlin Cronin | 8cade48 | 2017-07-20 03:09:31 | [diff] [blame] | 165 | assert_natives_ = new AssertNatives(context_); |
| 166 | |
Findit | 59c7f1e | 2019-03-12 07:00:56 | [diff] [blame] | 167 | if (base::FeatureList::IsEnabled(extensions_features::kNativeCrxBindings)) |
| 168 | bindings_system_ = std::make_unique<NativeExtensionBindingsSystem>(nullptr); |
[email protected] | 2a35687 | 2014-02-21 23:18:52 | [diff] [blame] | 169 | |
| 170 | { |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 171 | std::unique_ptr<ModuleSystem> module_system( |
Devlin Cronin | 8cade48 | 2017-07-20 03:09:31 | [diff] [blame] | 172 | new ModuleSystem(context_, source_map_.get())); |
Devlin Cronin | d9ea834 | 2018-01-27 06:00:04 | [diff] [blame] | 173 | context_->SetModuleSystem(std::move(module_system)); |
[email protected] | 2a35687 | 2014-02-21 23:18:52 | [diff] [blame] | 174 | } |
| 175 | ModuleSystem* module_system = context_->module_system(); |
[email protected] | 582f6e9 | 2014-07-16 23:39:15 | [diff] [blame] | 176 | module_system->RegisterNativeHandler( |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 177 | "assert", std::unique_ptr<NativeHandler>(assert_natives_)); |
[email protected] | 582f6e9 | 2014-07-16 23:39:15 | [diff] [blame] | 178 | module_system->RegisterNativeHandler( |
| 179 | "logging", |
Devlin Cronin | 8cade48 | 2017-07-20 03:09:31 | [diff] [blame] | 180 | std::unique_ptr<NativeHandler>(new LoggingNativeHandler(context_))); |
[email protected] | 582f6e9 | 2014-07-16 23:39:15 | [diff] [blame] | 181 | module_system->RegisterNativeHandler( |
| 182 | "utils", |
Devlin Cronin | 8cade48 | 2017-07-20 03:09:31 | [diff] [blame] | 183 | std::unique_ptr<NativeHandler>(new UtilsNativeHandler(context_))); |
| 184 | module_system->RegisterNativeHandler( |
| 185 | "apiGetter", |
Jeremy Roman | 16529d0e | 2017-08-24 18:13:47 | [diff] [blame] | 186 | std::make_unique<GetAPINatives>(context_, bindings_system_.get())); |
[email protected] | 2a35687 | 2014-02-21 23:18:52 | [diff] [blame] | 187 | module_system->SetExceptionHandlerForTest( |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 188 | std::unique_ptr<ModuleSystem::ExceptionHandler>(new FailsOnException)); |
Devlin Cronin | 8cade48 | 2017-07-20 03:09:31 | [diff] [blame] | 189 | |
Findit | 59c7f1e | 2019-03-12 07:00:56 | [diff] [blame] | 190 | if (bindings_system_) { |
| 191 | bindings_system_->DidCreateScriptContext(context_); |
| 192 | bindings_system_->UpdateBindingsForContext(context_); |
| 193 | } |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 194 | } |
| 195 | |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 196 | ModuleSystemTestEnvironment::~ModuleSystemTestEnvironment() { |
Devlin Cronin | 8cade48 | 2017-07-20 03:09:31 | [diff] [blame] | 197 | if (context_) |
kalman | b0c1c50 | 2015-04-15 00:25:06 | [diff] [blame] | 198 | ShutdownModuleSystem(); |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 199 | } |
| 200 | |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 201 | void ModuleSystemTestEnvironment::RegisterModule(const std::string& name, |
| 202 | const std::string& code) { |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 203 | source_map_->RegisterModule(name, code); |
| 204 | } |
| 205 | |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 206 | void ModuleSystemTestEnvironment::RegisterModule(const std::string& name, |
Yuzhu Shen | bcd734d | 2017-11-15 20:40:58 | [diff] [blame] | 207 | int resource_id, |
| 208 | bool gzipped) { |
Lei Zhang | cf30efc | 2017-10-04 21:31:24 | [diff] [blame] | 209 | const std::string& code = ui::ResourceBundle::GetSharedInstance() |
[email protected] | 582f6e9 | 2014-07-16 23:39:15 | [diff] [blame] | 210 | .GetRawDataResource(resource_id) |
| 211 | .as_string(); |
Yuzhu Shen | bcd734d | 2017-11-15 20:40:58 | [diff] [blame] | 212 | source_map_->RegisterModule(name, code, gzipped); |
[email protected] | 11844fa | 2012-05-10 00:35:59 | [diff] [blame] | 213 | } |
| 214 | |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 215 | void ModuleSystemTestEnvironment::OverrideNativeHandler( |
| 216 | const std::string& name, |
| 217 | const std::string& code) { |
[email protected] | 11844fa | 2012-05-10 00:35:59 | [diff] [blame] | 218 | RegisterModule(name, code); |
[email protected] | 2a35687 | 2014-02-21 23:18:52 | [diff] [blame] | 219 | context_->module_system()->OverrideNativeHandlerForTest(name); |
[email protected] | 11844fa | 2012-05-10 00:35:59 | [diff] [blame] | 220 | } |
| 221 | |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 222 | void ModuleSystemTestEnvironment::RegisterTestFile( |
| 223 | const std::string& module_name, |
| 224 | const std::string& file_name) { |
[email protected] | f8d87d3 | 2013-06-06 02:51:29 | [diff] [blame] | 225 | base::FilePath test_js_file_path; |
Avi Drissman | 210441b7 | 2018-05-01 15:51:00 | [diff] [blame] | 226 | ASSERT_TRUE(base::PathService::Get(DIR_TEST_DATA, &test_js_file_path)); |
[email protected] | 582f6e9 | 2014-07-16 23:39:15 | [diff] [blame] | 227 | test_js_file_path = test_js_file_path.AppendASCII(file_name); |
[email protected] | f8d87d3 | 2013-06-06 02:51:29 | [diff] [blame] | 228 | std::string test_js; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 229 | ASSERT_TRUE(base::ReadFileToString(test_js_file_path, &test_js)); |
[email protected] | f8d87d3 | 2013-06-06 02:51:29 | [diff] [blame] | 230 | source_map_->RegisterModule(module_name, test_js); |
| 231 | } |
| 232 | |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 233 | void ModuleSystemTestEnvironment::ShutdownGin() { |
| 234 | context_holder_.reset(); |
| 235 | } |
| 236 | |
| 237 | void ModuleSystemTestEnvironment::ShutdownModuleSystem() { |
kalman | b0c1c50 | 2015-04-15 00:25:06 | [diff] [blame] | 238 | CHECK(context_->is_valid()); |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 239 | context_->v8_context()->Exit(); |
Devlin Cronin | 8cade48 | 2017-07-20 03:09:31 | [diff] [blame] | 240 | context_set_->Remove(context_); |
| 241 | base::RunLoop().RunUntilIdle(); |
| 242 | context_ = nullptr; |
| 243 | assert_natives_ = nullptr; |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 244 | } |
| 245 | |
tfarina | f85316f | 2015-04-29 17:03:40 | [diff] [blame] | 246 | v8::Local<v8::Object> ModuleSystemTestEnvironment::CreateGlobal( |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 247 | const std::string& name) { |
[email protected] | 99ea16b | 2014-07-17 22:15:56 | [diff] [blame] | 248 | v8::EscapableHandleScope handle_scope(isolate_); |
| 249 | v8::Local<v8::Object> object = v8::Object::New(isolate_); |
Dan Elphick | 19cdae8 | 2018-12-11 16:03:51 | [diff] [blame] | 250 | isolate_->GetCurrentContext() |
| 251 | ->Global() |
| 252 | ->Set(context_->v8_context(), |
| 253 | v8::String::NewFromUtf8(isolate_, name.c_str(), |
| 254 | v8::NewStringType::kInternalized) |
| 255 | .ToLocalChecked(), |
| 256 | object) |
| 257 | .ToChecked(); |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 258 | return handle_scope.Escape(object); |
| 259 | } |
| 260 | |
| 261 | ModuleSystemTest::ModuleSystemTest() |
[email protected] | 99ea16b | 2014-07-17 22:15:56 | [diff] [blame] | 262 | : isolate_(v8::Isolate::GetCurrent()), |
Devlin Cronin | 8cade48 | 2017-07-20 03:09:31 | [diff] [blame] | 263 | context_set_(&extension_ids_), |
| 264 | should_assertions_be_made_(true) {} |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 265 | |
| 266 | ModuleSystemTest::~ModuleSystemTest() { |
| 267 | } |
| 268 | |
sammc | 32a6fc7d | 2014-09-15 02:47:31 | [diff] [blame] | 269 | void ModuleSystemTest::SetUp() { |
Devlin Cronin | 2db58e3 | 2017-08-15 21:29:29 | [diff] [blame] | 270 | extension_ = CreateExtension(); |
sammc | 32a6fc7d | 2014-09-15 02:47:31 | [diff] [blame] | 271 | env_ = CreateEnvironment(); |
rdevlin.cronin | 585b125 | 2016-04-19 23:29:26 | [diff] [blame] | 272 | base::CommandLine::ForCurrentProcess()->AppendSwitch("test-type"); |
sammc | 32a6fc7d | 2014-09-15 02:47:31 | [diff] [blame] | 273 | } |
| 274 | |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 275 | void ModuleSystemTest::TearDown() { |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 276 | // All tests must assert at least once unless otherwise specified. |
Devlin Cronin | 8cade48 | 2017-07-20 03:09:31 | [diff] [blame] | 277 | if (env_->assert_natives()) { // The context may have already been shutdown. |
| 278 | EXPECT_EQ(should_assertions_be_made_, |
| 279 | env_->assert_natives()->assertion_made()); |
| 280 | EXPECT_FALSE(env_->assert_natives()->failed()); |
| 281 | } else { |
| 282 | EXPECT_FALSE(should_assertions_be_made_); |
| 283 | } |
sammc | 32a6fc7d | 2014-09-15 02:47:31 | [diff] [blame] | 284 | env_.reset(); |
| 285 | v8::HeapStatistics stats; |
| 286 | isolate_->GetHeapStatistics(&stats); |
| 287 | size_t old_heap_size = 0; |
| 288 | // Run the GC until the heap size reaches a steady state to ensure that |
| 289 | // all the garbage is collected. |
| 290 | while (stats.used_heap_size() != old_heap_size) { |
| 291 | old_heap_size = stats.used_heap_size(); |
| 292 | isolate_->RequestGarbageCollectionForTesting( |
| 293 | v8::Isolate::kFullGarbageCollection); |
| 294 | isolate_->GetHeapStatistics(&stats); |
| 295 | } |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 296 | } |
| 297 | |
Devlin Cronin | 2db58e3 | 2017-08-15 21:29:29 | [diff] [blame] | 298 | scoped_refptr<const Extension> ModuleSystemTest::CreateExtension() { |
| 299 | std::unique_ptr<base::DictionaryValue> manifest = |
| 300 | DictionaryBuilder() |
| 301 | .Set("name", "test") |
| 302 | .Set("version", "1.0") |
| 303 | .Set("manifest_version", 2) |
| 304 | .Build(); |
| 305 | return ExtensionBuilder().SetManifest(std::move(manifest)).Build(); |
| 306 | } |
| 307 | |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 308 | std::unique_ptr<ModuleSystemTestEnvironment> |
| 309 | ModuleSystemTest::CreateEnvironment() { |
Jeremy Roman | 16529d0e | 2017-08-24 18:13:47 | [diff] [blame] | 310 | return std::make_unique<ModuleSystemTestEnvironment>(isolate_, &context_set_, |
Devlin Cronin | 2db58e3 | 2017-08-15 21:29:29 | [diff] [blame] | 311 | extension_); |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | void ModuleSystemTest::ExpectNoAssertionsMade() { |
| 315 | should_assertions_be_made_ = false; |
| 316 | } |
| 317 | |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 318 | void ModuleSystemTest::RunResolvedPromises() { |
dgozman | fdfd5d1 | 2016-03-11 07:50:47 | [diff] [blame] | 319 | v8::MicrotasksScope::PerformCheckpoint(isolate_); |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 320 | } |
[email protected] | 582f6e9 | 2014-07-16 23:39:15 | [diff] [blame] | 321 | |
| 322 | } // namespace extensions |