Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / extensions / renderer / messaging_utils_unittest.cc
blobfbb2dec93c8adc5e19538b861f68c90b83716e8b
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
3 // found in the LICENSE file.
5 #include "base/strings/stringprintf.h"
6 #include "extensions/renderer/module_system_test.h"
7 #include "grit/extensions_renderer_resources.h"
9 namespace extensions {
10 namespace {
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));
26 private:
27 void SetUp() override {
28 ModuleSystemTest::SetUp();
30 env()->RegisterModule("messaging_utils", IDR_MESSAGING_UTILS_JS);
34 TEST_F(MessagingUtilsUnittest, TestNothing) {
35 ExpectNoAssertionsMade();
38 TEST_F(MessagingUtilsUnittest, NoArguments) {
39 ModuleSystem::NativesEnabledScope natives_enabled_scope(
40 env()->module_system());
41 RegisterTestModule(
42 "var args = messagingUtils.alignSendMessageArguments();\n"
43 "AssertTrue(args === null);");
44 env()->module_system()->Require("test");
47 TEST_F(MessagingUtilsUnittest, ZeroArguments) {
48 ModuleSystem::NativesEnabledScope natives_enabled_scope(
49 env()->module_system());
50 RegisterTestModule(
51 "var args = messagingUtils.alignSendMessageArguments([]);"
52 "AssertTrue(args === null);");
53 env()->module_system()->Require("test");
56 TEST_F(MessagingUtilsUnittest, TooManyArgumentsNoOptions) {
57 ModuleSystem::NativesEnabledScope natives_enabled_scope(
58 env()->module_system());
59 RegisterTestModule(
60 "var args = messagingUtils.alignSendMessageArguments(\n"
61 " ['a', 'b', 'c', 'd']);\n"
62 "AssertTrue(args === null);");
63 env()->module_system()->Require("test");
66 TEST_F(MessagingUtilsUnittest, TooManyArgumentsWithOptions) {
67 ModuleSystem::NativesEnabledScope natives_enabled_scope(
68 env()->module_system());
69 RegisterTestModule(
70 "var args = messagingUtils.alignSendMessageArguments(\n"
71 " ['a', 'b', 'c', 'd', 'e'], true);\n"
72 "AssertTrue(args === null);");
73 env()->module_system()->Require("test");
76 TEST_F(MessagingUtilsUnittest, FinalArgumentIsNotAFunctionNoOptions) {
77 ModuleSystem::NativesEnabledScope natives_enabled_scope(
78 env()->module_system());
79 RegisterTestModule(
80 "var args = messagingUtils.alignSendMessageArguments(\n"
81 " ['a', 'b', 'c']);\n"
82 "AssertTrue(args === null);");
83 env()->module_system()->Require("test");
86 TEST_F(MessagingUtilsUnittest, FinalArgumentIsNotAFunctionWithOptions) {
87 ModuleSystem::NativesEnabledScope natives_enabled_scope(
88 env()->module_system());
89 RegisterTestModule(
90 "var args = messagingUtils.alignSendMessageArguments(\n"
91 " ['a', 'b', 'c', 'd'], true);\n"
92 "AssertTrue(args === null);");
93 env()->module_system()->Require("test");
96 TEST_F(MessagingUtilsUnittest, OneStringArgument) {
97 ModuleSystem::NativesEnabledScope natives_enabled_scope(
98 env()->module_system());
99 // Because the request argument is required, a single argument must get
100 // mapped to it rather than to the optional targetId argument.
101 RegisterTestModule(
102 "var args = messagingUtils.alignSendMessageArguments(['a']);\n"
103 "AssertTrue(args.length == 3);\n"
104 "AssertTrue(args[0] === null);\n"
105 "AssertTrue(args[1] == 'a');\n"
106 "AssertTrue(args[2] === null);");
107 env()->module_system()->Require("test");
110 TEST_F(MessagingUtilsUnittest, OneStringAndOneNullArgument) {
111 ModuleSystem::NativesEnabledScope natives_enabled_scope(
112 env()->module_system());
113 // Explicitly specifying null as the request is allowed.
114 RegisterTestModule(
115 "var args = messagingUtils.alignSendMessageArguments(['a', null]);\n"
116 "AssertTrue(args.length == 3);\n"
117 "AssertTrue(args[0] == 'a');\n"
118 "AssertTrue(args[1] === null);\n"
119 "AssertTrue(args[2] === null);");
120 env()->module_system()->Require("test");
123 TEST_F(MessagingUtilsUnittest, OneNullAndOneStringArgument) {
124 ModuleSystem::NativesEnabledScope natives_enabled_scope(
125 env()->module_system());
126 RegisterTestModule(
127 "var args = messagingUtils.alignSendMessageArguments([null, 'a']);\n"
128 "AssertTrue(args.length == 3);\n"
129 "AssertTrue(args[0] === null);\n"
130 "AssertTrue(args[1] == 'a');\n"
131 "AssertTrue(args[2] === null);");
132 env()->module_system()->Require("test");
135 TEST_F(MessagingUtilsUnittest, OneStringAndOneFunctionArgument) {
136 ModuleSystem::NativesEnabledScope natives_enabled_scope(
137 env()->module_system());
138 // When the arguments are a string and a function, the function is
139 // unambiguously the responseCallback. Because the request argument is
140 // required, the remaining argument must get mapped to it rather than to the
141 // optional targetId argument.
142 RegisterTestModule(
143 "var cb = function() {};\n"
144 "var args = messagingUtils.alignSendMessageArguments(['a', cb]);\n"
145 "AssertTrue(args.length == 3);\n"
146 "AssertTrue(args[0] === null);\n"
147 "AssertTrue(args[1] == 'a');\n"
148 "AssertTrue(args[2] == cb);");
149 env()->module_system()->Require("test");
152 TEST_F(MessagingUtilsUnittest, OneStringAndOneObjectArgument) {
153 ModuleSystem::NativesEnabledScope natives_enabled_scope(
154 env()->module_system());
155 // This tests an ambiguous set of arguments when options are present:
156 // chrome.runtime.sendMessage('target', {'msg': 'this is a message'});
157 // vs.
158 // chrome.runtime.sendMessage('request', {'includeTlsChannelId': true});
160 // The question is whether the string should map to the target and the
161 // dictionary to the message, or whether the string should map to the message
162 // and the dictionary to the options. Because the target and message arguments
163 // predate the options argument, we bind the string in this case to the
164 // targetId.
165 RegisterTestModule(
166 "var obj = {'b': true};\n"
167 "var args = messagingUtils.alignSendMessageArguments(['a', obj], true);\n"
168 "AssertTrue(args.length == 4);\n"
169 "AssertTrue(args[0] == 'a');\n"
170 "AssertTrue(args[1] == obj);\n"
171 "AssertTrue(args[2] === null);\n"
172 "AssertTrue(args[3] === null);");
173 env()->module_system()->Require("test");
176 TEST_F(MessagingUtilsUnittest, TwoObjectArguments) {
177 ModuleSystem::NativesEnabledScope natives_enabled_scope(
178 env()->module_system());
179 // When two non-string arguments are provided and options are present, the
180 // two arguments must match request and options, respectively, because
181 // targetId must be a string.
182 RegisterTestModule(
183 "var obj1 = {'a': 'foo'};\n"
184 "var obj2 = {'b': 'bar'};\n"
185 "var args = messagingUtils.alignSendMessageArguments(\n"
186 " [obj1, obj2], true);\n"
187 "AssertTrue(args.length == 4);\n"
188 "AssertTrue(args[0] === null);\n"
189 "AssertTrue(args[1] == obj1);\n"
190 "AssertTrue(args[2] == obj2);\n"
191 "AssertTrue(args[3] === null);");
192 env()->module_system()->Require("test");
195 } // namespace
196 } // namespace extensions