OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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. | |
4 | |
5 #ifndef CHROME_TEST_BASE_MODULE_SYSTEM_TEST_H_ | |
6 #define CHROME_TEST_BASE_MODULE_SYSTEM_TEST_H_ | |
7 | |
8 #include "chrome/renderer/extensions/chrome_v8_context.h" | |
9 #include "extensions/renderer/module_system.h" | |
10 #include "extensions/renderer/scoped_persistent.h" | |
11 #include "gin/public/context_holder.h" | |
12 #include "gin/public/isolate_holder.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 #include "v8/include/v8.h" | |
15 | |
16 class ModuleSystemTestEnvironment { | |
17 public: | |
18 class AssertNatives; | |
19 class StringSourceMap; | |
20 | |
21 explicit ModuleSystemTestEnvironment(gin::IsolateHolder* isolate_holder); | |
22 ~ModuleSystemTestEnvironment(); | |
23 | |
24 // Register a named JS module in the module system. | |
25 void RegisterModule(const std::string& name, const std::string& code); | |
26 | |
27 // Register a named JS module with source retrieved from a ResourceBundle. | |
28 void RegisterModule(const std::string& name, int resource_id); | |
29 | |
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 | |
32 // name. | |
33 void OverrideNativeHandler(const std::string& name, const std::string& code); | |
34 | |
35 // Registers |file_name| from chrome/test/data/extensions as a module name | |
36 // |module_name|. | |
37 void RegisterTestFile(const std::string& module_name, | |
38 const std::string& file_name); | |
39 | |
40 // Create an empty object in the global scope with name |name|. | |
41 v8::Handle<v8::Object> CreateGlobal(const std::string& name); | |
42 | |
43 void ShutdownGin(); | |
44 | |
45 void ShutdownModuleSystem(); | |
46 | |
47 extensions::ModuleSystem* module_system() { | |
48 return context_->module_system(); | |
49 } | |
50 | |
51 extensions::ChromeV8Context* context() { return context_.get(); } | |
52 | |
53 v8::Isolate* isolate() { return isolate_holder_->isolate(); } | |
54 | |
55 AssertNatives* assert_natives() { return assert_natives_; } | |
56 | |
57 private: | |
58 gin::IsolateHolder* isolate_holder_; | |
59 scoped_ptr<gin::ContextHolder> context_holder_; | |
60 v8::HandleScope handle_scope_; | |
61 scoped_ptr<extensions::ChromeV8Context> context_; | |
62 AssertNatives* assert_natives_; | |
63 scoped_ptr<StringSourceMap> source_map_; | |
64 | |
65 DISALLOW_COPY_AND_ASSIGN(ModuleSystemTestEnvironment); | |
66 }; | |
67 | |
68 // Test fixture for testing JS that makes use of the module system. | |
69 // | |
70 // Typically tests will look like: | |
71 // | |
72 // TEST_F(MyModuleSystemTest, TestStuff) { | |
73 // ModuleSystem::NativesEnabledScope natives_enabled(module_system_.get()); | |
74 // RegisterModule("test", "requireNative('assert').AssertTrue(true);"); | |
75 // module_system_->Require("test"); | |
76 // } | |
77 // | |
78 // By default a test will fail if no method in the native module 'assert' is | |
79 // called. This behaviour can be overridden by calling ExpectNoAssertionsMade(). | |
80 // | |
81 // TODO(kalman): move this back into chrome/renderer/extensions. | |
82 class ModuleSystemTest : public testing::Test { | |
83 public: | |
84 ModuleSystemTest(); | |
85 virtual ~ModuleSystemTest(); | |
86 | |
87 virtual void TearDown() OVERRIDE; | |
88 | |
89 protected: | |
90 ModuleSystemTestEnvironment* env() { return env_.get(); } | |
91 | |
92 scoped_ptr<ModuleSystemTestEnvironment> CreateEnvironment(); | |
93 | |
94 // Make the test fail if any asserts are called. By default a test will fail | |
95 // if no asserts are called. | |
96 void ExpectNoAssertionsMade(); | |
97 | |
98 // Runs promises that have been resolved. Resolved promises will not run | |
99 // until this is called. | |
100 void RunResolvedPromises(); | |
101 | |
102 private: | |
103 gin::IsolateHolder isolate_holder_; | |
104 scoped_ptr<ModuleSystemTestEnvironment> env_; | |
105 bool should_assertions_be_made_; | |
106 | |
107 private: | |
108 DISALLOW_COPY_AND_ASSIGN(ModuleSystemTest); | |
109 }; | |
110 | |
111 #endif // CHROME_TEST_BASE_MODULE_SYSTEM_TEST_H_ | |
OLD | NEW |