OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/test/base/module_system_test.h" | 6 #include "chrome/test/base/module_system_test.h" |
7 #include "grit/extensions_renderer_resources.h" | 7 #include "grit/extensions_renderer_resources.h" |
8 | 8 |
9 namespace extensions { | 9 namespace extensions { |
10 namespace { | 10 namespace { |
11 | 11 |
12 class MessagingUtilsUnittest : public ModuleSystemTest { | 12 class MessagingUtilsUnittest : public ModuleSystemTest { |
13 protected: | 13 protected: |
14 void RegisterTestModule(const char* code) { | 14 void RegisterTestModule(const char* code) { |
15 RegisterModule("test", base::StringPrintf( | 15 env_.RegisterModule("test", |
16 "var assert = requireNative('assert');\n" | 16 base::StringPrintf( |
17 "var AssertTrue = assert.AssertTrue;\n" | 17 "var assert = requireNative('assert');\n" |
18 "var AssertFalse = assert.AssertFalse;\n" | 18 "var AssertTrue = assert.AssertTrue;\n" |
19 "var messagingUtils = require('messaging_utils');\n" | 19 "var AssertFalse = assert.AssertFalse;\n" |
20 "%s", code)); | 20 "var messagingUtils = require('messaging_utils');\n" |
| 21 "%s", |
| 22 code)); |
21 } | 23 } |
22 | 24 |
23 private: | 25 private: |
24 virtual void SetUp() OVERRIDE { | 26 virtual void SetUp() OVERRIDE { |
25 ModuleSystemTest::SetUp(); | 27 ModuleSystemTest::SetUp(); |
26 | 28 |
27 RegisterModule("messaging_utils", IDR_MESSAGING_UTILS_JS); | 29 env_.RegisterModule("messaging_utils", IDR_MESSAGING_UTILS_JS); |
28 } | 30 } |
29 | 31 |
30 }; | 32 }; |
31 | 33 |
32 TEST_F(MessagingUtilsUnittest, TestNothing) { | 34 TEST_F(MessagingUtilsUnittest, TestNothing) { |
33 ExpectNoAssertionsMade(); | 35 ExpectNoAssertionsMade(); |
34 } | 36 } |
35 | 37 |
36 TEST_F(MessagingUtilsUnittest, NoArguments) { | 38 TEST_F(MessagingUtilsUnittest, NoArguments) { |
37 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 39 ModuleSystem::NativesEnabledScope natives_enabled_scope(env_.module_system()); |
38 context_->module_system()); | |
39 RegisterTestModule( | 40 RegisterTestModule( |
40 "var args = messagingUtils.alignSendMessageArguments();\n" | 41 "var args = messagingUtils.alignSendMessageArguments();\n" |
41 "AssertTrue(args === null);"); | 42 "AssertTrue(args === null);"); |
42 context_->module_system()->Require("test"); | 43 env_.module_system()->Require("test"); |
43 } | 44 } |
44 | 45 |
45 TEST_F(MessagingUtilsUnittest, ZeroArguments) { | 46 TEST_F(MessagingUtilsUnittest, ZeroArguments) { |
46 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 47 ModuleSystem::NativesEnabledScope natives_enabled_scope(env_.module_system()); |
47 context_->module_system()); | |
48 RegisterTestModule( | 48 RegisterTestModule( |
49 "var args = messagingUtils.alignSendMessageArguments([]);" | 49 "var args = messagingUtils.alignSendMessageArguments([]);" |
50 "AssertTrue(args === null);"); | 50 "AssertTrue(args === null);"); |
51 context_->module_system()->Require("test"); | 51 env_.module_system()->Require("test"); |
52 } | 52 } |
53 | 53 |
54 TEST_F(MessagingUtilsUnittest, TooManyArgumentsNoOptions) { | 54 TEST_F(MessagingUtilsUnittest, TooManyArgumentsNoOptions) { |
55 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 55 ModuleSystem::NativesEnabledScope natives_enabled_scope(env_.module_system()); |
56 context_->module_system()); | |
57 RegisterTestModule( | 56 RegisterTestModule( |
58 "var args = messagingUtils.alignSendMessageArguments(\n" | 57 "var args = messagingUtils.alignSendMessageArguments(\n" |
59 " ['a', 'b', 'c', 'd']);\n" | 58 " ['a', 'b', 'c', 'd']);\n" |
60 "AssertTrue(args === null);"); | 59 "AssertTrue(args === null);"); |
61 context_->module_system()->Require("test"); | 60 env_.module_system()->Require("test"); |
62 } | 61 } |
63 | 62 |
64 TEST_F(MessagingUtilsUnittest, TooManyArgumentsWithOptions) { | 63 TEST_F(MessagingUtilsUnittest, TooManyArgumentsWithOptions) { |
65 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 64 ModuleSystem::NativesEnabledScope natives_enabled_scope(env_.module_system()); |
66 context_->module_system()); | |
67 RegisterTestModule( | 65 RegisterTestModule( |
68 "var args = messagingUtils.alignSendMessageArguments(\n" | 66 "var args = messagingUtils.alignSendMessageArguments(\n" |
69 " ['a', 'b', 'c', 'd', 'e'], true);\n" | 67 " ['a', 'b', 'c', 'd', 'e'], true);\n" |
70 "AssertTrue(args === null);"); | 68 "AssertTrue(args === null);"); |
71 context_->module_system()->Require("test"); | 69 env_.module_system()->Require("test"); |
72 } | 70 } |
73 | 71 |
74 TEST_F(MessagingUtilsUnittest, FinalArgumentIsNotAFunctionNoOptions) { | 72 TEST_F(MessagingUtilsUnittest, FinalArgumentIsNotAFunctionNoOptions) { |
75 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 73 ModuleSystem::NativesEnabledScope natives_enabled_scope(env_.module_system()); |
76 context_->module_system()); | |
77 RegisterTestModule( | 74 RegisterTestModule( |
78 "var args = messagingUtils.alignSendMessageArguments(\n" | 75 "var args = messagingUtils.alignSendMessageArguments(\n" |
79 " ['a', 'b', 'c']);\n" | 76 " ['a', 'b', 'c']);\n" |
80 "AssertTrue(args === null);"); | 77 "AssertTrue(args === null);"); |
81 context_->module_system()->Require("test"); | 78 env_.module_system()->Require("test"); |
82 } | 79 } |
83 | 80 |
84 TEST_F(MessagingUtilsUnittest, FinalArgumentIsNotAFunctionWithOptions) { | 81 TEST_F(MessagingUtilsUnittest, FinalArgumentIsNotAFunctionWithOptions) { |
85 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 82 ModuleSystem::NativesEnabledScope natives_enabled_scope(env_.module_system()); |
86 context_->module_system()); | |
87 RegisterTestModule( | 83 RegisterTestModule( |
88 "var args = messagingUtils.alignSendMessageArguments(\n" | 84 "var args = messagingUtils.alignSendMessageArguments(\n" |
89 " ['a', 'b', 'c', 'd'], true);\n" | 85 " ['a', 'b', 'c', 'd'], true);\n" |
90 "AssertTrue(args === null);"); | 86 "AssertTrue(args === null);"); |
91 context_->module_system()->Require("test"); | 87 env_.module_system()->Require("test"); |
92 } | 88 } |
93 | 89 |
94 TEST_F(MessagingUtilsUnittest, OneStringArgument) { | 90 TEST_F(MessagingUtilsUnittest, OneStringArgument) { |
95 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 91 ModuleSystem::NativesEnabledScope natives_enabled_scope(env_.module_system()); |
96 context_->module_system()); | |
97 // Because the request argument is required, a single argument must get | 92 // Because the request argument is required, a single argument must get |
98 // mapped to it rather than to the optional targetId argument. | 93 // mapped to it rather than to the optional targetId argument. |
99 RegisterTestModule( | 94 RegisterTestModule( |
100 "var args = messagingUtils.alignSendMessageArguments(['a']);\n" | 95 "var args = messagingUtils.alignSendMessageArguments(['a']);\n" |
101 "AssertTrue(args.length == 3);\n" | 96 "AssertTrue(args.length == 3);\n" |
102 "AssertTrue(args[0] === null);\n" | 97 "AssertTrue(args[0] === null);\n" |
103 "AssertTrue(args[1] == 'a');\n" | 98 "AssertTrue(args[1] == 'a');\n" |
104 "AssertTrue(args[2] === null);"); | 99 "AssertTrue(args[2] === null);"); |
105 context_->module_system()->Require("test"); | 100 env_.module_system()->Require("test"); |
106 } | 101 } |
107 | 102 |
108 TEST_F(MessagingUtilsUnittest, OneStringAndOneNullArgument) { | 103 TEST_F(MessagingUtilsUnittest, OneStringAndOneNullArgument) { |
109 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 104 ModuleSystem::NativesEnabledScope natives_enabled_scope(env_.module_system()); |
110 context_->module_system()); | |
111 // Explicitly specifying null as the request is allowed. | 105 // Explicitly specifying null as the request is allowed. |
112 RegisterTestModule( | 106 RegisterTestModule( |
113 "var args = messagingUtils.alignSendMessageArguments(['a', null]);\n" | 107 "var args = messagingUtils.alignSendMessageArguments(['a', null]);\n" |
114 "AssertTrue(args.length == 3);\n" | 108 "AssertTrue(args.length == 3);\n" |
115 "AssertTrue(args[0] == 'a');\n" | 109 "AssertTrue(args[0] == 'a');\n" |
116 "AssertTrue(args[1] === null);\n" | 110 "AssertTrue(args[1] === null);\n" |
117 "AssertTrue(args[2] === null);"); | 111 "AssertTrue(args[2] === null);"); |
118 context_->module_system()->Require("test"); | 112 env_.module_system()->Require("test"); |
119 } | 113 } |
120 | 114 |
121 TEST_F(MessagingUtilsUnittest, OneNullAndOneStringArgument) { | 115 TEST_F(MessagingUtilsUnittest, OneNullAndOneStringArgument) { |
122 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 116 ModuleSystem::NativesEnabledScope natives_enabled_scope(env_.module_system()); |
123 context_->module_system()); | |
124 RegisterTestModule( | 117 RegisterTestModule( |
125 "var args = messagingUtils.alignSendMessageArguments([null, 'a']);\n" | 118 "var args = messagingUtils.alignSendMessageArguments([null, 'a']);\n" |
126 "AssertTrue(args.length == 3);\n" | 119 "AssertTrue(args.length == 3);\n" |
127 "AssertTrue(args[0] === null);\n" | 120 "AssertTrue(args[0] === null);\n" |
128 "AssertTrue(args[1] == 'a');\n" | 121 "AssertTrue(args[1] == 'a');\n" |
129 "AssertTrue(args[2] === null);"); | 122 "AssertTrue(args[2] === null);"); |
130 context_->module_system()->Require("test"); | 123 env_.module_system()->Require("test"); |
131 } | 124 } |
132 | 125 |
133 TEST_F(MessagingUtilsUnittest, OneStringAndOneFunctionArgument) { | 126 TEST_F(MessagingUtilsUnittest, OneStringAndOneFunctionArgument) { |
134 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 127 ModuleSystem::NativesEnabledScope natives_enabled_scope(env_.module_system()); |
135 context_->module_system()); | |
136 // When the arguments are a string and a function, the function is | 128 // When the arguments are a string and a function, the function is |
137 // unambiguously the responseCallback. Because the request argument is | 129 // unambiguously the responseCallback. Because the request argument is |
138 // required, the remaining argument must get mapped to it rather than to the | 130 // required, the remaining argument must get mapped to it rather than to the |
139 // optional targetId argument. | 131 // optional targetId argument. |
140 RegisterTestModule( | 132 RegisterTestModule( |
141 "var cb = function() {};\n" | 133 "var cb = function() {};\n" |
142 "var args = messagingUtils.alignSendMessageArguments(['a', cb]);\n" | 134 "var args = messagingUtils.alignSendMessageArguments(['a', cb]);\n" |
143 "AssertTrue(args.length == 3);\n" | 135 "AssertTrue(args.length == 3);\n" |
144 "AssertTrue(args[0] === null);\n" | 136 "AssertTrue(args[0] === null);\n" |
145 "AssertTrue(args[1] == 'a');\n" | 137 "AssertTrue(args[1] == 'a');\n" |
146 "AssertTrue(args[2] == cb);"); | 138 "AssertTrue(args[2] == cb);"); |
147 context_->module_system()->Require("test"); | 139 env_.module_system()->Require("test"); |
148 } | 140 } |
149 | 141 |
150 TEST_F(MessagingUtilsUnittest, OneStringAndOneObjectArgument) { | 142 TEST_F(MessagingUtilsUnittest, OneStringAndOneObjectArgument) { |
151 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 143 ModuleSystem::NativesEnabledScope natives_enabled_scope(env_.module_system()); |
152 context_->module_system()); | |
153 // This tests an ambiguous set of arguments when options are present: | 144 // This tests an ambiguous set of arguments when options are present: |
154 // chrome.runtime.sendMessage('target', {'msg': 'this is a message'}); | 145 // chrome.runtime.sendMessage('target', {'msg': 'this is a message'}); |
155 // vs. | 146 // vs. |
156 // chrome.runtime.sendMessage('request', {'includeTlsChannelId': true}); | 147 // chrome.runtime.sendMessage('request', {'includeTlsChannelId': true}); |
157 // | 148 // |
158 // The question is whether the string should map to the target and the | 149 // The question is whether the string should map to the target and the |
159 // dictionary to the message, or whether the string should map to the message | 150 // dictionary to the message, or whether the string should map to the message |
160 // and the dictionary to the options. Because the target and message arguments | 151 // and the dictionary to the options. Because the target and message arguments |
161 // predate the options argument, we bind the string in this case to the | 152 // predate the options argument, we bind the string in this case to the |
162 // targetId. | 153 // targetId. |
163 RegisterTestModule( | 154 RegisterTestModule( |
164 "var obj = {'b': true};\n" | 155 "var obj = {'b': true};\n" |
165 "var args = messagingUtils.alignSendMessageArguments(['a', obj], true);\n" | 156 "var args = messagingUtils.alignSendMessageArguments(['a', obj], true);\n" |
166 "AssertTrue(args.length == 4);\n" | 157 "AssertTrue(args.length == 4);\n" |
167 "AssertTrue(args[0] == 'a');\n" | 158 "AssertTrue(args[0] == 'a');\n" |
168 "AssertTrue(args[1] == obj);\n" | 159 "AssertTrue(args[1] == obj);\n" |
169 "AssertTrue(args[2] === null);\n" | 160 "AssertTrue(args[2] === null);\n" |
170 "AssertTrue(args[3] === null);"); | 161 "AssertTrue(args[3] === null);"); |
171 context_->module_system()->Require("test"); | 162 env_.module_system()->Require("test"); |
172 } | 163 } |
173 | 164 |
174 TEST_F(MessagingUtilsUnittest, TwoObjectArguments) { | 165 TEST_F(MessagingUtilsUnittest, TwoObjectArguments) { |
175 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 166 ModuleSystem::NativesEnabledScope natives_enabled_scope(env_.module_system()); |
176 context_->module_system()); | |
177 // When two non-string arguments are provided and options are present, the | 167 // When two non-string arguments are provided and options are present, the |
178 // two arguments must match request and options, respectively, because | 168 // two arguments must match request and options, respectively, because |
179 // targetId must be a string. | 169 // targetId must be a string. |
180 RegisterTestModule( | 170 RegisterTestModule( |
181 "var obj1 = {'a': 'foo'};\n" | 171 "var obj1 = {'a': 'foo'};\n" |
182 "var obj2 = {'b': 'bar'};\n" | 172 "var obj2 = {'b': 'bar'};\n" |
183 "var args = messagingUtils.alignSendMessageArguments(\n" | 173 "var args = messagingUtils.alignSendMessageArguments(\n" |
184 " [obj1, obj2], true);\n" | 174 " [obj1, obj2], true);\n" |
185 "AssertTrue(args.length == 4);\n" | 175 "AssertTrue(args.length == 4);\n" |
186 "AssertTrue(args[0] === null);\n" | 176 "AssertTrue(args[0] === null);\n" |
187 "AssertTrue(args[1] == obj1);\n" | 177 "AssertTrue(args[1] == obj1);\n" |
188 "AssertTrue(args[2] == obj2);\n" | 178 "AssertTrue(args[2] == obj2);\n" |
189 "AssertTrue(args[3] === null);"); | 179 "AssertTrue(args[3] === null);"); |
190 context_->module_system()->Require("test"); | 180 env_.module_system()->Require("test"); |
191 } | 181 } |
192 | 182 |
193 } // namespace | 183 } // namespace |
194 } // namespace extensions | 184 } // namespace extensions |
OLD | NEW |