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