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