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