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