| 1 |
// Copyright 2017 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.dom.assertsTest'); |
| 16 |
goog.setTestOnly('goog.dom.assertsTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom.asserts'); |
| 19 |
goog.require('goog.testing.StrictMock'); |
| 20 |
goog.require('goog.testing.jsunit'); |
| 21 |
goog.require('goog.userAgent'); |
| 22 |
|
| 23 |
|
| 24 |
function testAssertIsLocation() { |
| 25 |
assertNotThrows(function() { |
| 26 |
goog.dom.asserts.assertIsLocation(window.location); |
| 27 |
}); |
| 28 |
|
| 29 |
// Ad-hoc mock objects are allowed. |
| 30 |
var o = {foo: 'bar'}; |
| 31 |
assertNotThrows(function() { |
| 32 |
goog.dom.asserts.assertIsLocation(o); |
| 33 |
}); |
| 34 |
|
| 35 |
// So are fancy mocks. |
| 36 |
var mock = new goog.testing.StrictMock(window.location); |
| 37 |
assertNotThrows(function() { |
| 38 |
goog.dom.asserts.assertIsLocation(mock); |
| 39 |
}); |
| 40 |
|
| 41 |
if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) { |
| 42 |
var linkElement = document.createElement('LINK'); |
| 43 |
var ex = assertThrows(function() { |
| 44 |
goog.dom.asserts.assertIsLocation(linkElement); |
| 45 |
}); |
| 46 |
assertContains('Argument is not a Location', ex.message); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
function testAssertIsHtmlAnchorElement() { |
| 51 |
var anchorElement = document.createElement('A'); |
| 52 |
assertNotThrows(function() { |
| 53 |
goog.dom.asserts.assertIsHTMLAnchorElement(anchorElement); |
| 54 |
}); |
| 55 |
|
| 56 |
// Ad-hoc mock objects are allowed. |
| 57 |
var o = {foo: 'bar'}; |
| 58 |
assertNotThrows(function() { |
| 59 |
goog.dom.asserts.assertIsHTMLAnchorElement(o); |
| 60 |
}); |
| 61 |
// So are fancy mocks. |
| 62 |
var mock = new goog.testing.StrictMock(anchorElement); |
| 63 |
assertNotThrows(function() { |
| 64 |
goog.dom.asserts.assertIsHTMLAnchorElement(mock); |
| 65 |
}); |
| 66 |
|
| 67 |
if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) { |
| 68 |
var otherElement = document.createElement('LINK'); |
| 69 |
var ex = assertThrows(function() { |
| 70 |
goog.dom.asserts.assertIsHTMLAnchorElement(otherElement); |
| 71 |
}); |
| 72 |
assertContains('Argument is not a HTMLAnchorElement', ex.message); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
function testAssertIsHtmlButtonElement() { |
| 77 |
var buttonElement = document.createElement('BUTTON'); |
| 78 |
assertNotThrows(function() { |
| 79 |
goog.dom.asserts.assertIsHTMLButtonElement(buttonElement); |
| 80 |
}); |
| 81 |
|
| 82 |
// Ad-hoc mock objects are allowed. |
| 83 |
var o = {foo: 'bar'}; |
| 84 |
assertNotThrows(function() { |
| 85 |
goog.dom.asserts.assertIsHTMLButtonElement(o); |
| 86 |
}); |
| 87 |
// So are fancy mocks. |
| 88 |
var mock = new goog.testing.StrictMock(buttonElement); |
| 89 |
assertNotThrows(function() { |
| 90 |
goog.dom.asserts.assertIsHTMLButtonElement(mock); |
| 91 |
}); |
| 92 |
|
| 93 |
if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) { |
| 94 |
var otherElement = document.createElement('LINK'); |
| 95 |
var ex = assertThrows(function() { |
| 96 |
goog.dom.asserts.assertIsHTMLButtonElement(otherElement); |
| 97 |
}); |
| 98 |
assertContains('Argument is not a HTMLButtonElement', ex.message); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
function testAssertIsHtmlLinkElement() { |
| 104 |
var linkElement = document.createElement('LINK'); |
| 105 |
assertNotThrows(function() { |
| 106 |
goog.dom.asserts.assertIsHTMLLinkElement(linkElement); |
| 107 |
}); |
| 108 |
|
| 109 |
// Ad-hoc mock objects are allowed. |
| 110 |
var o = {foo: 'bar'}; |
| 111 |
assertNotThrows(function() { |
| 112 |
goog.dom.asserts.assertIsHTMLLinkElement(o); |
| 113 |
}); |
| 114 |
|
| 115 |
// So are fancy mocks. |
| 116 |
var mock = new goog.testing.StrictMock(linkElement); |
| 117 |
assertNotThrows(function() { |
| 118 |
goog.dom.asserts.assertIsHTMLLinkElement(mock); |
| 119 |
}); |
| 120 |
|
| 121 |
if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) { |
| 122 |
var otherElement = document.createElement('A'); |
| 123 |
var ex = assertThrows(function() { |
| 124 |
goog.dom.asserts.assertIsHTMLLinkElement(otherElement); |
| 125 |
}); |
| 126 |
assertContains('Argument is not a HTMLLinkElement', ex.message); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
function testAssertIsHtmlImageElement() { |
| 131 |
var imgElement = document.createElement('IMG'); |
| 132 |
assertNotThrows(function() { |
| 133 |
goog.dom.asserts.assertIsHTMLImageElement(imgElement); |
| 134 |
}); |
| 135 |
|
| 136 |
// Ad-hoc mock objects are allowed. |
| 137 |
var o = {foo: 'bar'}; |
| 138 |
assertNotThrows(function() { |
| 139 |
goog.dom.asserts.assertIsHTMLImageElement(o); |
| 140 |
}); |
| 141 |
|
| 142 |
// So are fancy mocks. |
| 143 |
var mock = new goog.testing.StrictMock(imgElement); |
| 144 |
assertNotThrows(function() { |
| 145 |
goog.dom.asserts.assertIsHTMLImageElement(mock); |
| 146 |
}); |
| 147 |
|
| 148 |
if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) { |
| 149 |
var otherElement = document.createElement('SCRIPT'); |
| 150 |
var ex = assertThrows(function() { |
| 151 |
goog.dom.asserts.assertIsHTMLImageElement(otherElement); |
| 152 |
}); |
| 153 |
assertContains('Argument is not a HTMLImageElement', ex.message); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
function testAssertIsHtmlInputElement() { |
| 158 |
var inputElement = document.createElement('INPUT'); |
| 159 |
assertNotThrows(function() { |
| 160 |
goog.dom.asserts.assertIsHTMLInputElement(inputElement); |
| 161 |
}); |
| 162 |
|
| 163 |
// Ad-hoc mock objects are allowed. |
| 164 |
var o = {foo: 'bar'}; |
| 165 |
assertNotThrows(function() { |
| 166 |
goog.dom.asserts.assertIsHTMLInputElement(o); |
| 167 |
}); |
| 168 |
// So are fancy mocks. |
| 169 |
var mock = new goog.testing.StrictMock(inputElement); |
| 170 |
assertNotThrows(function() { |
| 171 |
goog.dom.asserts.assertIsHTMLInputElement(mock); |
| 172 |
}); |
| 173 |
|
| 174 |
if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) { |
| 175 |
var otherElement = document.createElement('LINK'); |
| 176 |
var ex = assertThrows(function() { |
| 177 |
goog.dom.asserts.assertIsHTMLInputElement(otherElement); |
| 178 |
}); |
| 179 |
assertContains('Argument is not a HTMLInputElement', ex.message); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
function testAssertIsHtmlFormElement() { |
| 185 |
var formElement = document.createElement('FORM'); |
| 186 |
assertNotThrows(function() { |
| 187 |
goog.dom.asserts.assertIsHTMLFormElement(formElement); |
| 188 |
}); |
| 189 |
|
| 190 |
// Ad-hoc mock objects are allowed. |
| 191 |
var o = {foo: 'bar'}; |
| 192 |
assertNotThrows(function() { |
| 193 |
goog.dom.asserts.assertIsHTMLFormElement(o); |
| 194 |
}); |
| 195 |
// So are fancy mocks. |
| 196 |
var mock = new goog.testing.StrictMock(formElement); |
| 197 |
assertNotThrows(function() { |
| 198 |
goog.dom.asserts.assertIsHTMLFormElement(mock); |
| 199 |
}); |
| 200 |
|
| 201 |
if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) { |
| 202 |
var otherElement = document.createElement('LINK'); |
| 203 |
var ex = assertThrows(function() { |
| 204 |
goog.dom.asserts.assertIsHTMLFormElement(otherElement); |
| 205 |
}); |
| 206 |
assertContains('Argument is not a HTMLFormElement', ex.message); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
function testAssertIsHtmlEmbedElement() { |
| 212 |
var el = document.createElement('EMBED'); |
| 213 |
assertNotThrows(function() { |
| 214 |
goog.dom.asserts.assertIsHTMLEmbedElement(el); |
| 215 |
}); |
| 216 |
|
| 217 |
if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) { |
| 218 |
var otherElement = document.createElement('SCRIPT'); |
| 219 |
var ex = assertThrows(function() { |
| 220 |
goog.dom.asserts.assertIsHTMLEmbedElement(otherElement); |
| 221 |
}); |
| 222 |
assertContains('Argument is not a HTMLEmbedElement', ex.message); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
function testAssertIsHtmlFrameElement() { |
| 227 |
var el = document.createElement('FRAME'); |
| 228 |
assertNotThrows(function() { |
| 229 |
goog.dom.asserts.assertIsHTMLFrameElement(el); |
| 230 |
}); |
| 231 |
|
| 232 |
if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) { |
| 233 |
var otherElement = document.createElement('SCRIPT'); |
| 234 |
var ex = assertThrows(function() { |
| 235 |
goog.dom.asserts.assertIsHTMLFrameElement(otherElement); |
| 236 |
}); |
| 237 |
assertContains('Argument is not a HTMLFrameElement', ex.message); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
function testAssertIsHtmlIFrameElement() { |
| 242 |
var el = document.createElement('IFRAME'); |
| 243 |
assertNotThrows(function() { |
| 244 |
goog.dom.asserts.assertIsHTMLIFrameElement(el); |
| 245 |
}); |
| 246 |
|
| 247 |
if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) { |
| 248 |
var otherElement = document.createElement('SCRIPT'); |
| 249 |
var ex = assertThrows(function() { |
| 250 |
goog.dom.asserts.assertIsHTMLIFrameElement(otherElement); |
| 251 |
}); |
| 252 |
assertContains('Argument is not a HTMLIFrameElement', ex.message); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
function testAssertIsHtmlObjectElement() { |
| 257 |
var el = document.createElement('OBJECT'); |
| 258 |
assertNotThrows(function() { |
| 259 |
goog.dom.asserts.assertIsHTMLObjectElement(el); |
| 260 |
}); |
| 261 |
|
| 262 |
if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) { |
| 263 |
var otherElement = document.createElement('SCRIPT'); |
| 264 |
var ex = assertThrows(function() { |
| 265 |
goog.dom.asserts.assertIsHTMLObjectElement(otherElement); |
| 266 |
}); |
| 267 |
assertContains('Argument is not a HTMLObjectElement', ex.message); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
function testAssertIsHtmlScriptElement() { |
| 272 |
var el = document.createElement('SCRIPT'); |
| 273 |
assertNotThrows(function() { |
| 274 |
goog.dom.asserts.assertIsHTMLScriptElement(el); |
| 275 |
}); |
| 276 |
|
| 277 |
if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) { |
| 278 |
var otherElement = document.createElement('IMG'); |
| 279 |
var ex = assertThrows(function() { |
| 280 |
goog.dom.asserts.assertIsHTMLScriptElement(otherElement); |
| 281 |
}); |
| 282 |
assertContains('Argument is not a HTMLScriptElement', ex.message); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
function testInOtherWindow() { |
| 287 |
var iframe = document.createElement('IFRAME'); |
| 288 |
document.body.appendChild(iframe); |
| 289 |
var el = iframe.contentWindow.document.createElement('SCRIPT'); |
| 290 |
assertNotThrows(function() { |
| 291 |
goog.dom.asserts.assertIsHTMLScriptElement(el); |
| 292 |
}); |
| 293 |
|
| 294 |
if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) { |
| 295 |
var ex = assertThrows(function() { |
| 296 |
goog.dom.asserts.assertIsHTMLImageElement(el); |
| 297 |
}); |
| 298 |
assertContains('Argument is not a HTMLImageElement', ex.message); |
| 299 |
} |
| 300 |
|
| 301 |
document.body.removeChild(iframe); |
| 302 |
} |