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