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