OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #include "base/strings/stringprintf.h" | 5 #include "base/strings/stringprintf.h" |
6 #include "extensions/renderer/module_system_test.h" | 6 #include "extensions/renderer/module_system_test.h" |
| 7 #include "gin/dictionary.h" |
7 #include "grit/extensions_renderer_resources.h" | 8 #include "grit/extensions_renderer_resources.h" |
8 | 9 |
9 namespace extensions { | 10 namespace extensions { |
10 namespace { | 11 namespace { |
11 | 12 |
12 class UtilsUnittest : public ModuleSystemTest { | 13 class UtilsUnittest : public ModuleSystemTest { |
13 protected: | |
14 void RegisterTestModule(const char* code) { | |
15 env()->RegisterModule("test", | |
16 base::StringPrintf( | |
17 "var assert = requireNative('assert');\n" | |
18 "var AssertTrue = assert.AssertTrue;\n" | |
19 "var AssertFalse = assert.AssertFalse;\n" | |
20 "var utils = require('utils');\n" | |
21 "%s", | |
22 code)); | |
23 } | |
24 | |
25 private: | 14 private: |
26 virtual void SetUp() OVERRIDE { | 15 virtual void SetUp() OVERRIDE { |
27 ModuleSystemTest::SetUp(); | 16 ModuleSystemTest::SetUp(); |
28 | 17 |
29 env()->RegisterModule("utils", IDR_UTILS_JS); | 18 env()->RegisterModule("utils", IDR_UTILS_JS); |
| 19 env()->RegisterTestFile("utils_unittest", "utils_unittest.js"); |
30 env()->OverrideNativeHandler("schema_registry", | 20 env()->OverrideNativeHandler("schema_registry", |
31 "exports.GetSchema = function() {};"); | 21 "exports.GetSchema = function() {};"); |
32 env()->OverrideNativeHandler("logging", | 22 env()->OverrideNativeHandler("logging", |
33 "exports.CHECK = function() {};\n" | 23 "exports.CHECK = function() {};\n" |
34 "exports.WARNING = function() {};"); | 24 "exports.WARNING = function() {};"); |
| 25 env()->OverrideNativeHandler("v8_context", ""); |
| 26 gin::Dictionary chrome(env()->isolate(), env()->CreateGlobal("chrome")); |
| 27 gin::Dictionary chrome_runtime( |
| 28 gin::Dictionary::CreateEmpty(env()->isolate())); |
| 29 chrome.Set("runtime", chrome_runtime); |
35 } | 30 } |
36 }; | 31 }; |
37 | 32 |
38 TEST_F(UtilsUnittest, TestNothing) { | 33 TEST_F(UtilsUnittest, TestNothing) { |
39 ExpectNoAssertionsMade(); | 34 ExpectNoAssertionsMade(); |
40 } | 35 } |
41 | 36 |
42 TEST_F(UtilsUnittest, SuperClass) { | 37 TEST_F(UtilsUnittest, SuperClass) { |
43 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 38 ModuleSystem::NativesEnabledScope natives_enabled_scope( |
44 env()->module_system()); | 39 env()->module_system()); |
45 RegisterTestModule( | 40 env()->module_system()->CallModuleMethod("utils_unittest", "testSuperClass"); |
46 "function SuperClassImpl() {}\n" | 41 } |
47 "\n" | |
48 "SuperClassImpl.prototype = {\n" | |
49 " attrA: 'aSuper',\n" | |
50 " attrB: 'bSuper',\n" | |
51 " func: function() { return 'func'; },\n" | |
52 " superFunc: function() { return 'superFunc'; }\n" | |
53 "};\n" | |
54 "\n" | |
55 "function SubClassImpl() {\n" | |
56 " SuperClassImpl.call(this);\n" | |
57 "}\n" | |
58 "\n" | |
59 "SubClassImpl.prototype = {\n" | |
60 " __proto__: SuperClassImpl.prototype,\n" | |
61 " attrA: 'aSub',\n" | |
62 " attrC: 'cSub',\n" | |
63 " func: function() { return 'overridden'; },\n" | |
64 " subFunc: function() { return 'subFunc'; }\n" | |
65 "};\n" | |
66 "\n" | |
67 "var SuperClass = utils.expose('SuperClass',\n" | |
68 " SuperClassImpl,\n" | |
69 " { functions: ['func', 'superFunc'],\n" | |
70 " properties: ['attrA', 'attrB'] });\n" | |
71 "\n" | |
72 "var SubClass = utils.expose('SubClass',\n" | |
73 " SubClassImpl,\n" | |
74 " { superclass: SuperClass,\n" | |
75 " functions: ['subFunc'],\n" | |
76 " properties: ['attrC'] });\n" | |
77 "\n" | |
78 "var supe = new SuperClass();\n" | |
79 "AssertTrue(supe.attrA == 'aSuper');\n" | |
80 "AssertTrue(supe.attrB == 'bSuper');\n" | |
81 "AssertFalse('attrC' in supe);\n" | |
82 "AssertTrue(supe.func() == 'func');\n" | |
83 "AssertTrue('superFunc' in supe);\n" | |
84 "AssertTrue(supe.superFunc() == 'superFunc');\n" | |
85 "AssertFalse('subFunc' in supe);\n" | |
86 "AssertTrue(supe instanceof SuperClass);\n" | |
87 "\n" | |
88 "var sub = new SubClass();\n" | |
89 "AssertTrue(sub.attrA == 'aSub');\n" | |
90 "AssertTrue(sub.attrB == 'bSuper');\n" | |
91 "AssertTrue(sub.attrC == 'cSub');\n" | |
92 "AssertTrue(sub.func() == 'overridden');\n" | |
93 "AssertTrue(sub.superFunc() == 'superFunc');\n" | |
94 "AssertTrue('subFunc' in sub);\n" | |
95 "AssertTrue(sub.subFunc() == 'subFunc');\n" | |
96 "AssertTrue(sub instanceof SuperClass);\n" | |
97 "AssertTrue(sub instanceof SubClass);\n" | |
98 "\n" | |
99 "function SubSubClassImpl() {}\n" | |
100 "SubSubClassImpl.prototype = Object.create(SubClassImpl.prototype);\n" | |
101 "SubSubClassImpl.prototype.subSubFunc = function() { return 'subsub'; }\n" | |
102 "\n" | |
103 "var SubSubClass = utils.expose('SubSubClass',\n" | |
104 " SubSubClassImpl,\n" | |
105 " { superclass: SubClass,\n" | |
106 " functions: ['subSubFunc'] });\n" | |
107 "var subsub = new SubSubClass();\n" | |
108 "AssertTrue(subsub.attrA == 'aSub');\n" | |
109 "AssertTrue(subsub.attrB == 'bSuper');\n" | |
110 "AssertTrue(subsub.attrC == 'cSub');\n" | |
111 "AssertTrue(subsub.func() == 'overridden');\n" | |
112 "AssertTrue(subsub.superFunc() == 'superFunc');\n" | |
113 "AssertTrue(subsub.subFunc() == 'subFunc');\n" | |
114 "AssertTrue(subsub.subSubFunc() == 'subsub');\n" | |
115 "AssertTrue(subsub instanceof SuperClass);\n" | |
116 "AssertTrue(subsub instanceof SubClass);\n" | |
117 "AssertTrue(subsub instanceof SubSubClass);\n"); | |
118 | 42 |
119 env()->module_system()->Require("test"); | 43 TEST_F(UtilsUnittest, PromiseNoResult) { |
| 44 ModuleSystem::NativesEnabledScope natives_enabled_scope( |
| 45 env()->module_system()); |
| 46 env()->module_system()->CallModuleMethod("utils_unittest", |
| 47 "testPromiseNoResult"); |
| 48 RunResolvedPromises(); |
| 49 } |
| 50 |
| 51 TEST_F(UtilsUnittest, PromiseOneResult) { |
| 52 ModuleSystem::NativesEnabledScope natives_enabled_scope( |
| 53 env()->module_system()); |
| 54 env()->module_system()->CallModuleMethod("utils_unittest", |
| 55 "testPromiseOneResult"); |
| 56 RunResolvedPromises(); |
| 57 } |
| 58 |
| 59 TEST_F(UtilsUnittest, PromiseTwoResults) { |
| 60 ModuleSystem::NativesEnabledScope natives_enabled_scope( |
| 61 env()->module_system()); |
| 62 env()->module_system()->CallModuleMethod("utils_unittest", |
| 63 "testPromiseTwoResults"); |
| 64 RunResolvedPromises(); |
| 65 } |
| 66 |
| 67 TEST_F(UtilsUnittest, PromiseError) { |
| 68 ModuleSystem::NativesEnabledScope natives_enabled_scope( |
| 69 env()->module_system()); |
| 70 env()->module_system()->CallModuleMethod("utils_unittest", |
| 71 "testPromiseError"); |
| 72 RunResolvedPromises(); |
120 } | 73 } |
121 | 74 |
122 } // namespace | 75 } // namespace |
123 } // namespace extensions | 76 } // namespace extensions |
OLD | NEW |