1 // Copyright 2014 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.
5 #ifndef EXTENSIONS_RENDERER_MODULE_SYSTEM_TEST_H_
6 #define EXTENSIONS_RENDERER_MODULE_SYSTEM_TEST_H_
8 #include "extensions/renderer/module_system.h"
9 #include "extensions/renderer/script_context.h"
10 #include "gin/public/context_holder.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "v8/include/v8.h"
14 namespace extensions
{
16 class ModuleSystemTestEnvironment
{
19 class StringSourceMap
;
21 explicit ModuleSystemTestEnvironment(v8::Isolate
* isolate
);
22 ~ModuleSystemTestEnvironment();
24 // Register a named JS module in the module system.
25 void RegisterModule(const std::string
& name
, const std::string
& code
);
27 // Register a named JS module with source retrieved from a ResourceBundle.
28 void RegisterModule(const std::string
& name
, int resource_id
);
30 // Register a named JS module in the module system and tell the module system
31 // to use it to handle any requireNative() calls for native modules with that
33 void OverrideNativeHandler(const std::string
& name
, const std::string
& code
);
35 // Registers |file_name| from chrome/test/data/extensions as a module name
37 void RegisterTestFile(const std::string
& module_name
,
38 const std::string
& file_name
);
40 // Create an empty object in the global scope with name |name|.
41 v8::Handle
<v8::Object
> CreateGlobal(const std::string
& name
);
45 void ShutdownModuleSystem();
47 ModuleSystem
* module_system() { return context_
->module_system(); }
49 ScriptContext
* context() { return context_
.get(); }
51 v8::Isolate
* isolate() { return isolate_
; }
53 AssertNatives
* assert_natives() { return assert_natives_
; }
56 v8::Isolate
* isolate_
;
57 scoped_ptr
<gin::ContextHolder
> context_holder_
;
58 v8::HandleScope handle_scope_
;
59 scoped_ptr
<ScriptContext
> context_
;
60 AssertNatives
* assert_natives_
;
61 scoped_ptr
<StringSourceMap
> source_map_
;
63 DISALLOW_COPY_AND_ASSIGN(ModuleSystemTestEnvironment
);
66 // Test fixture for testing JS that makes use of the module system.
68 // Typically tests will look like:
70 // TEST_F(MyModuleSystemTest, TestStuff) {
71 // ModuleSystem::NativesEnabledScope natives_enabled(module_system_.get());
72 // RegisterModule("test", "requireNative('assert').AssertTrue(true);");
73 // module_system_->Require("test");
76 // By default a test will fail if no method in the native module 'assert' is
77 // called. This behaviour can be overridden by calling ExpectNoAssertionsMade().
78 class ModuleSystemTest
: public testing::Test
{
81 ~ModuleSystemTest() override
;
83 void SetUp() override
;
84 void TearDown() override
;
87 ModuleSystemTestEnvironment
* env() { return env_
.get(); }
89 scoped_ptr
<ModuleSystemTestEnvironment
> CreateEnvironment();
91 // Make the test fail if any asserts are called. By default a test will fail
92 // if no asserts are called.
93 void ExpectNoAssertionsMade();
95 // Runs promises that have been resolved. Resolved promises will not run
96 // until this is called.
97 void RunResolvedPromises();
100 v8::Isolate
* isolate_
;
101 scoped_ptr
<ModuleSystemTestEnvironment
> env_
;
102 bool should_assertions_be_made_
;
105 DISALLOW_COPY_AND_ASSIGN(ModuleSystemTest
);
108 } // namespace extensions
110 #endif // EXTENSIONS_RENDERER_MODULE_SYSTEM_TEST_H_