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