Add chromium_builder_lkgr_drmemory_win target for LKGR DrMemory build on ClusterFuzz
[chromium-blink-merge.git] / extensions / renderer / module_system_test.h
blobe72b25aff16f6955c5746f10b1db383a9755da56
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/scoped_persistent.h"
10 #include "extensions/renderer/script_context.h"
11 #include "gin/public/context_holder.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "v8/include/v8.h"
15 namespace extensions {
17 class ModuleSystemTestEnvironment {
18 public:
19 class AssertNatives;
20 class StringSourceMap;
22 explicit ModuleSystemTestEnvironment(v8::Isolate* isolate);
23 ~ModuleSystemTestEnvironment();
25 // Register a named JS module in the module system.
26 void RegisterModule(const std::string& name, const std::string& code);
28 // Register a named JS module with source retrieved from a ResourceBundle.
29 void RegisterModule(const std::string& name, int resource_id);
31 // Register a named JS module in the module system and tell the module system
32 // to use it to handle any requireNative() calls for native modules with that
33 // name.
34 void OverrideNativeHandler(const std::string& name, const std::string& code);
36 // Registers |file_name| from chrome/test/data/extensions as a module name
37 // |module_name|.
38 void RegisterTestFile(const std::string& module_name,
39 const std::string& file_name);
41 // Create an empty object in the global scope with name |name|.
42 v8::Handle<v8::Object> CreateGlobal(const std::string& name);
44 void ShutdownGin();
46 void ShutdownModuleSystem();
48 ModuleSystem* module_system() { return context_->module_system(); }
50 ScriptContext* context() { return context_.get(); }
52 v8::Isolate* isolate() { return isolate_; }
54 AssertNatives* assert_natives() { return assert_natives_; }
56 private:
57 v8::Isolate* isolate_;
58 scoped_ptr<gin::ContextHolder> context_holder_;
59 v8::HandleScope handle_scope_;
60 scoped_ptr<ScriptContext> context_;
61 AssertNatives* assert_natives_;
62 scoped_ptr<StringSourceMap> source_map_;
64 DISALLOW_COPY_AND_ASSIGN(ModuleSystemTestEnvironment);
67 // Test fixture for testing JS that makes use of the module system.
69 // Typically tests will look like:
71 // TEST_F(MyModuleSystemTest, TestStuff) {
72 // ModuleSystem::NativesEnabledScope natives_enabled(module_system_.get());
73 // RegisterModule("test", "requireNative('assert').AssertTrue(true);");
74 // module_system_->Require("test");
75 // }
77 // By default a test will fail if no method in the native module 'assert' is
78 // called. This behaviour can be overridden by calling ExpectNoAssertionsMade().
79 class ModuleSystemTest : public testing::Test {
80 public:
81 ModuleSystemTest();
82 virtual ~ModuleSystemTest();
84 virtual void TearDown() OVERRIDE;
86 protected:
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();
99 private:
100 v8::Isolate* isolate_;
101 scoped_ptr<ModuleSystemTestEnvironment> env_;
102 bool should_assertions_be_made_;
104 private:
105 DISALLOW_COPY_AND_ASSIGN(ModuleSystemTest);
108 } // namespace extensions
110 #endif // EXTENSIONS_RENDERER_MODULE_SYSTEM_TEST_H_