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|. | |
not at google - send to devlin
2014/07/07 23:18:50
heh, we should use this more. those embedded strin
| |
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 DeleteContextHolder(); | |
not at google - send to devlin
2014/07/07 23:18:50
I don't know what ContextHolder is. would it be ac
Sam McNally
2014/07/08 00:07:04
Done.
| |
44 | |
45 void DeleteModuleSystem(); | |
46 | |
47 extensions::ModuleSystem* module_system() { | |
48 return context_->module_system(); | |
49 } | |
50 | |
51 extensions::ChromeV8Context* context() { return context_.get(); } | |
52 | |
53 AssertNatives* assert_natives() { return assert_natives_; } | |
54 | |
55 private: | |
56 scoped_ptr<gin::ContextHolder> context_holder_; | |
57 v8::HandleScope handle_scope_; | |
58 scoped_ptr<extensions::ChromeV8Context> context_; | |
59 AssertNatives* assert_natives_; | |
60 scoped_ptr<StringSourceMap> source_map_; | |
61 | |
62 DISALLOW_COPY_AND_ASSIGN(ModuleSystemTestEnvironment); | |
63 }; | |
64 | |
14 // Test fixture for testing JS that makes use of the module system. | 65 // Test fixture for testing JS that makes use of the module system. |
15 // | 66 // |
16 // Typically tests will look like: | 67 // Typically tests will look like: |
17 // | 68 // |
18 // TEST_F(MyModuleSystemTest, TestStuff) { | 69 // TEST_F(MyModuleSystemTest, TestStuff) { |
19 // ModuleSystem::NativesEnabledScope natives_enabled(module_system_.get()); | 70 // ModuleSystem::NativesEnabledScope natives_enabled(module_system_.get()); |
20 // RegisterModule("test", "requireNative('assert').AssertTrue(true);"); | 71 // RegisterModule("test", "requireNative('assert').AssertTrue(true);"); |
21 // module_system_->Require("test"); | 72 // module_system_->Require("test"); |
22 // } | 73 // } |
23 // | 74 // |
24 // By default a test will fail if no method in the native module 'assert' is | 75 // 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(). | 76 // called. This behaviour can be overridden by calling ExpectNoAssertionsMade(). |
26 // | 77 // |
27 // TODO(kalman): move this back into chrome/renderer/extensions. | 78 // TODO(kalman): move this back into chrome/renderer/extensions. |
28 class ModuleSystemTest : public testing::Test { | 79 class ModuleSystemTest : public testing::Test { |
29 public: | 80 public: |
30 ModuleSystemTest(); | 81 ModuleSystemTest(); |
31 virtual ~ModuleSystemTest(); | 82 virtual ~ModuleSystemTest(); |
32 | 83 |
33 virtual void TearDown() OVERRIDE; | 84 virtual void TearDown() OVERRIDE; |
34 | 85 |
35 protected: | 86 protected: |
36 // Register a named JS module in the module system. | |
37 void RegisterModule(const std::string& name, const std::string& code); | |
38 | |
39 // Register a named JS module with source retrieved from a ResourceBundle. | |
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 | |
52 // Make the test fail if any asserts are called. By default a test will fail | 87 // Make the test fail if any asserts are called. By default a test will fail |
53 // if no asserts are called. | 88 // if no asserts are called. |
54 void ExpectNoAssertionsMade(); | 89 void ExpectNoAssertionsMade(); |
55 | 90 |
56 // Create an empty object in the global scope with name |name|. | 91 // Runs promises that have been resolved. Resolved promises will not run |
57 v8::Handle<v8::Object> CreateGlobal(const std::string& name); | 92 // until this is called. |
93 void RunResolvedPromises(); | |
58 | 94 |
59 v8::Isolate* isolate_; | 95 gin::IsolateHolder isolate_holder_; |
not at google - send to devlin
2014/07/07 23:18:50
could you expose a CreateEnvironment() and make th
Sam McNally
2014/07/08 00:07:04
Done.
| |
60 v8::HandleScope handle_scope_; | 96 ModuleSystemTestEnvironment env_; |
not at google - send to devlin
2014/07/07 23:18:50
it would be nicer if all of these were private, an
Sam McNally
2014/07/08 00:07:04
Done.
| |
61 scoped_ptr<extensions::ChromeV8Context> context_; | |
62 class AssertNatives; | |
63 AssertNatives* assert_natives_; | |
64 class StringSourceMap; | |
65 scoped_ptr<StringSourceMap> source_map_; | |
66 bool should_assertions_be_made_; | 97 bool should_assertions_be_made_; |
67 | 98 |
68 private: | 99 private: |
69 DISALLOW_COPY_AND_ASSIGN(ModuleSystemTest); | 100 DISALLOW_COPY_AND_ASSIGN(ModuleSystemTest); |
70 }; | 101 }; |
71 | 102 |
72 #endif // CHROME_TEST_BASE_MODULE_SYSTEM_TEST_H_ | 103 #endif // CHROME_TEST_BASE_MODULE_SYSTEM_TEST_H_ |
OLD | NEW |