| 1 |
// Copyright 2010 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.ui.editor.LinkDialogTest'); |
| 16 |
goog.setTestOnly('goog.ui.editor.LinkDialogTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.dom.DomHelper'); |
| 20 |
goog.require('goog.dom.TagName'); |
| 21 |
goog.require('goog.editor.BrowserFeature'); |
| 22 |
goog.require('goog.editor.Link'); |
| 23 |
goog.require('goog.events'); |
| 24 |
goog.require('goog.events.EventHandler'); |
| 25 |
goog.require('goog.events.EventType'); |
| 26 |
goog.require('goog.events.KeyCodes'); |
| 27 |
goog.require('goog.style'); |
| 28 |
goog.require('goog.testing.MockControl'); |
| 29 |
goog.require('goog.testing.PropertyReplacer'); |
| 30 |
goog.require('goog.testing.dom'); |
| 31 |
goog.require('goog.testing.events'); |
| 32 |
goog.require('goog.testing.events.Event'); |
| 33 |
goog.require('goog.testing.jsunit'); |
| 34 |
goog.require('goog.testing.mockmatchers'); |
| 35 |
goog.require('goog.testing.mockmatchers.ArgumentMatcher'); |
| 36 |
goog.require('goog.ui.editor.AbstractDialog'); |
| 37 |
goog.require('goog.ui.editor.LinkDialog'); |
| 38 |
goog.require('goog.ui.editor.messages'); |
| 39 |
goog.require('goog.userAgent'); |
| 40 |
|
| 41 |
var dialog; |
| 42 |
var mockCtrl; |
| 43 |
var mockLink; |
| 44 |
var mockOkHandler; |
| 45 |
var mockGetViewportSize; |
| 46 |
var mockWindowOpen; |
| 47 |
var isNew; |
| 48 |
var anchorElem; |
| 49 |
var stubs = new goog.testing.PropertyReplacer(); |
| 50 |
|
| 51 |
var ANCHOR_TEXT = 'anchor text'; |
| 52 |
var ANCHOR_URL = 'http://www.google.com/'; |
| 53 |
var ANCHOR_EMAIL = '[email protected]'; |
| 54 |
var ANCHOR_MAILTO = 'mailto:' + ANCHOR_EMAIL; |
| 55 |
|
| 56 |
function setUp() { |
| 57 |
anchorElem = goog.dom.createElement(goog.dom.TagName.A); |
| 58 |
goog.dom.appendChild(goog.dom.getDocument().body, anchorElem); |
| 59 |
|
| 60 |
mockCtrl = new goog.testing.MockControl(); |
| 61 |
mockLink = mockCtrl.createLooseMock(goog.editor.Link); |
| 62 |
mockOkHandler = mockCtrl.createLooseMock(goog.events.EventHandler); |
| 63 |
|
| 64 |
isNew = false; |
| 65 |
mockLink.isNew(); |
| 66 |
mockLink.$anyTimes(); |
| 67 |
mockLink.$does(function() { return isNew; }); |
| 68 |
mockLink.getCurrentText(); |
| 69 |
mockLink.$anyTimes(); |
| 70 |
mockLink.$does(function() { return anchorElem.innerHTML; }); |
| 71 |
mockLink.setTextAndUrl( |
| 72 |
goog.testing.mockmatchers.isString, goog.testing.mockmatchers.isString); |
| 73 |
mockLink.$anyTimes(); |
| 74 |
mockLink.$does(function(text, url) { |
| 75 |
anchorElem.innerHTML = text; |
| 76 |
anchorElem.href = url; |
| 77 |
}); |
| 78 |
mockLink.$registerArgumentListVerifier( |
| 79 |
'placeCursorRightOf', function() { return true; }); |
| 80 |
mockLink.placeCursorRightOf(goog.testing.mockmatchers.iBoolean); |
| 81 |
mockLink.$anyTimes(); |
| 82 |
mockLink.getAnchor(); |
| 83 |
mockLink.$anyTimes(); |
| 84 |
mockLink.$returns(anchorElem); |
| 85 |
|
| 86 |
mockWindowOpen = mockCtrl.createFunctionMock('open'); |
| 87 |
stubs.set(window, 'open', mockWindowOpen); |
| 88 |
} |
| 89 |
|
| 90 |
function tearDown() { |
| 91 |
dialog.dispose(); |
| 92 |
goog.dom.removeNode(anchorElem); |
| 93 |
stubs.reset(); |
| 94 |
} |
| 95 |
|
| 96 |
function setUpAnchor(href, text, opt_isNew, opt_target, opt_rel) { |
| 97 |
anchorElem.href = href; |
| 98 |
anchorElem.innerHTML = text; |
| 99 |
isNew = !!opt_isNew; |
| 100 |
if (opt_target) { |
| 101 |
anchorElem.target = opt_target; |
| 102 |
} |
| 103 |
if (opt_rel) { |
| 104 |
anchorElem.rel = opt_rel; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
/** |
| 110 |
* Creates and shows the dialog to be tested. |
| 111 |
* @param {Document=} opt_document Document to render the dialog into. |
| 112 |
* Defaults to the main window's document. |
| 113 |
* @param {boolean=} opt_openInNewWindow Whether the open in new window |
| 114 |
* checkbox should be shown. |
| 115 |
* @param {boolean=} opt_noFollow Whether rel=nofollow checkbox should be |
| 116 |
* shown. |
| 117 |
*/ |
| 118 |
function createAndShow(opt_document, opt_openInNewWindow, opt_noFollow) { |
| 119 |
dialog = new goog.ui.editor.LinkDialog( |
| 120 |
new goog.dom.DomHelper(opt_document), mockLink); |
| 121 |
if (opt_openInNewWindow) { |
| 122 |
dialog.showOpenLinkInNewWindow(false); |
| 123 |
} |
| 124 |
if (opt_noFollow) { |
| 125 |
dialog.showRelNoFollow(); |
| 126 |
} |
| 127 |
dialog.addEventListener( |
| 128 |
goog.ui.editor.AbstractDialog.EventType.OK, mockOkHandler); |
| 129 |
dialog.show(); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
/** |
| 134 |
* Sets up the mock event handler to expect an OK event with the given text |
| 135 |
* and url. |
| 136 |
*/ |
| 137 |
function expectOk(linkText, linkUrl, opt_openInNewWindow, opt_noFollow) { |
| 138 |
mockOkHandler.handleEvent( |
| 139 |
new goog.testing.mockmatchers.ArgumentMatcher( |
| 140 |
function(arg) { |
| 141 |
return arg.type == goog.ui.editor.AbstractDialog.EventType.OK && |
| 142 |
arg.linkText == linkText && arg.linkUrl == linkUrl && |
| 143 |
arg.openInNewWindow == !!opt_openInNewWindow && |
| 144 |
arg.noFollow == !!opt_noFollow; |
| 145 |
}, |
| 146 |
'{linkText: ' + linkText + ', linkUrl: ' + linkUrl + |
| 147 |
', openInNewWindow: ' + opt_openInNewWindow + ', noFollow: ' + |
| 148 |
opt_noFollow + '}')); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
/** |
| 153 |
* Return true if we should use active element in our tests. |
| 154 |
* @return {boolean} . |
| 155 |
*/ |
| 156 |
function useActiveElement() { |
| 157 |
return goog.editor.BrowserFeature.HAS_ACTIVE_ELEMENT || |
| 158 |
goog.userAgent.WEBKIT && goog.userAgent.isVersionOrHigher(9); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
/** |
| 163 |
* Tests that when you show the dialog for a new link, you can switch |
| 164 |
* to the URL view. |
| 165 |
* @param {Document=} opt_document Document to render the dialog into. |
| 166 |
* Defaults to the main window's document. |
| 167 |
*/ |
| 168 |
function testShowNewLinkSwitchToUrl(opt_document) { |
| 169 |
mockCtrl.$replayAll(); |
| 170 |
setUpAnchor('', '', true); // Must be done before creating the dialog. |
| 171 |
createAndShow(opt_document); |
| 172 |
|
| 173 |
var webRadio = dialog.dom.getElement(goog.ui.editor.LinkDialog.Id_.ON_WEB_TAB) |
| 174 |
.firstChild; |
| 175 |
var emailRadio = |
| 176 |
dialog.dom.getElement(goog.ui.editor.LinkDialog.Id_.EMAIL_ADDRESS_TAB) |
| 177 |
.firstChild; |
| 178 |
assertTrue('Web Radio Button selected', webRadio.checked); |
| 179 |
assertFalse('Email Radio Button selected', emailRadio.checked); |
| 180 |
if (useActiveElement()) { |
| 181 |
assertEquals( |
| 182 |
'Focus should be on url input', getUrlInput(), |
| 183 |
dialog.dom.getActiveElement()); |
| 184 |
} |
| 185 |
|
| 186 |
emailRadio.click(); |
| 187 |
assertFalse('Web Radio Button selected', webRadio.checked); |
| 188 |
assertTrue('Email Radio Button selected', emailRadio.checked); |
| 189 |
if (useActiveElement()) { |
| 190 |
assertEquals( |
| 191 |
'Focus should be on url input', getEmailInput(), |
| 192 |
dialog.dom.getActiveElement()); |
| 193 |
} |
| 194 |
|
| 195 |
mockCtrl.$verifyAll(); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
/** |
| 200 |
* Tests that when you show the dialog for a new link, the input fields are |
| 201 |
* empty, the web tab is selected and focus is in the url input field. |
| 202 |
* @param {Document=} opt_document Document to render the dialog into. |
| 203 |
* Defaults to the main window's document. |
| 204 |
*/ |
| 205 |
function testShowForNewLink(opt_document) { |
| 206 |
mockCtrl.$replayAll(); |
| 207 |
setUpAnchor('', '', true); // Must be done before creating the dialog. |
| 208 |
createAndShow(opt_document); |
| 209 |
|
| 210 |
assertEquals( |
| 211 |
'Display text input field should be empty', '', getDisplayInputText()); |
| 212 |
assertEquals('Url input field should be empty', '', getUrlInputText()); |
| 213 |
assertEquals( |
| 214 |
'On the web tab should be selected', goog.ui.editor.LinkDialog.Id_.ON_WEB, |
| 215 |
dialog.curTabId_); |
| 216 |
if (useActiveElement()) { |
| 217 |
assertEquals( |
| 218 |
'Focus should be on url input', getUrlInput(), |
| 219 |
dialog.dom.getActiveElement()); |
| 220 |
} |
| 221 |
|
| 222 |
mockCtrl.$verifyAll(); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
/** |
| 227 |
* Fakes that the mock field is using an iframe and does the same test as |
| 228 |
* testShowForNewLink(). |
| 229 |
*/ |
| 230 |
function testShowForNewLinkWithDiffAppWindow() { |
| 231 |
testShowForNewLink(goog.dom.getElement('appWindowIframe').contentDocument); |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
/** |
| 236 |
* Tests that when you show the dialog for a url link, the input fields are |
| 237 |
* filled in, the web tab is selected and focus is in the url input field. |
| 238 |
*/ |
| 239 |
function testShowForUrlLink() { |
| 240 |
mockCtrl.$replayAll(); |
| 241 |
setUpAnchor(ANCHOR_URL, ANCHOR_TEXT); |
| 242 |
createAndShow(); |
| 243 |
|
| 244 |
assertEquals( |
| 245 |
'Display text input field should be filled in', ANCHOR_TEXT, |
| 246 |
getDisplayInputText()); |
| 247 |
assertEquals( |
| 248 |
'Url input field should be filled in', ANCHOR_URL, getUrlInputText()); |
| 249 |
assertEquals( |
| 250 |
'On the web tab should be selected', goog.ui.editor.LinkDialog.Id_.ON_WEB, |
| 251 |
dialog.curTabId_); |
| 252 |
if (useActiveElement()) { |
| 253 |
assertEquals( |
| 254 |
'Focus should be on url input', getUrlInput(), |
| 255 |
dialog.dom.getActiveElement()); |
| 256 |
} |
| 257 |
|
| 258 |
mockCtrl.$verifyAll(); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
/** |
| 263 |
* Tests that when you show the dialog for a mailto link, the input fields are |
| 264 |
* filled in, the email tab is selected and focus is in the email input field. |
| 265 |
*/ |
| 266 |
function testShowForMailtoLink() { |
| 267 |
mockCtrl.$replayAll(); |
| 268 |
setUpAnchor(ANCHOR_MAILTO, ANCHOR_TEXT); |
| 269 |
createAndShow(); |
| 270 |
|
| 271 |
assertEquals( |
| 272 |
'Display text input field should be filled in', ANCHOR_TEXT, |
| 273 |
getDisplayInputText()); |
| 274 |
assertEquals( |
| 275 |
'Email input field should be filled in', |
| 276 |
ANCHOR_EMAIL, // The 'mailto:' is not in the input! |
| 277 |
getEmailInputText()); |
| 278 |
assertEquals( |
| 279 |
'Email tab should be selected', |
| 280 |
goog.ui.editor.LinkDialog.Id_.EMAIL_ADDRESS, dialog.curTabId_); |
| 281 |
if (useActiveElement()) { |
| 282 |
assertEquals( |
| 283 |
'Focus should be on email input', getEmailInput(), |
| 284 |
dialog.dom.getActiveElement()); |
| 285 |
} |
| 286 |
|
| 287 |
mockCtrl.$verifyAll(); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
/** |
| 292 |
* Tests that the display text is autogenerated from the url input in the |
| 293 |
* right situations (and not generated when appropriate too). |
| 294 |
*/ |
| 295 |
function testAutogeneration() { |
| 296 |
mockCtrl.$replayAll(); |
| 297 |
setUpAnchor('', '', true); |
| 298 |
createAndShow(); |
| 299 |
|
| 300 |
// Simulate typing a url when everything is empty, should autogen. |
| 301 |
setUrlInputText(ANCHOR_URL); |
| 302 |
assertEquals( |
| 303 |
'Display text should have been autogenerated', ANCHOR_URL, |
| 304 |
getDisplayInputText()); |
| 305 |
|
| 306 |
// Simulate typing text when url is set, afterwards should not autogen. |
| 307 |
setDisplayInputText(ANCHOR_TEXT); |
| 308 |
setUrlInputText(ANCHOR_MAILTO); |
| 309 |
assertNotEquals( |
| 310 |
'Display text should not have been autogenerated', ANCHOR_MAILTO, |
| 311 |
getDisplayInputText()); |
| 312 |
assertEquals( |
| 313 |
'Display text should have remained the same', ANCHOR_TEXT, |
| 314 |
getDisplayInputText()); |
| 315 |
|
| 316 |
// Simulate typing text equal to existing url, afterwards should autogen. |
| 317 |
setDisplayInputText(ANCHOR_MAILTO); |
| 318 |
setUrlInputText(ANCHOR_URL); |
| 319 |
assertEquals( |
| 320 |
'Display text should have been autogenerated', ANCHOR_URL, |
| 321 |
getDisplayInputText()); |
| 322 |
|
| 323 |
mockCtrl.$verifyAll(); |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
/** |
| 328 |
* Tests that the display text is not autogenerated from the url input in all |
| 329 |
* situations when the autogeneration feature is turned off. |
| 330 |
*/ |
| 331 |
function testAutogenerationOff() { |
| 332 |
mockCtrl.$replayAll(); |
| 333 |
|
| 334 |
setUpAnchor('', '', true); |
| 335 |
createAndShow(); |
| 336 |
|
| 337 |
// Disable the autogen feature |
| 338 |
dialog.setAutogenFeatureEnabled(false); |
| 339 |
|
| 340 |
// Simulate typing a url when everything is empty, should not autogen. |
| 341 |
setUrlInputText(ANCHOR_URL); |
| 342 |
assertEquals( |
| 343 |
'Display text should not have been autogenerated', '', |
| 344 |
getDisplayInputText()); |
| 345 |
|
| 346 |
// Simulate typing text when url is set, afterwards should not autogen. |
| 347 |
setDisplayInputText(ANCHOR_TEXT); |
| 348 |
setUrlInputText(ANCHOR_MAILTO); |
| 349 |
assertNotEquals( |
| 350 |
'Display text should not have been autogenerated', ANCHOR_MAILTO, |
| 351 |
getDisplayInputText()); |
| 352 |
assertEquals( |
| 353 |
'Display text should have remained the same', ANCHOR_TEXT, |
| 354 |
getDisplayInputText()); |
| 355 |
|
| 356 |
// Simulate typing text equal to existing url, afterwards should not |
| 357 |
// autogen. |
| 358 |
setDisplayInputText(ANCHOR_MAILTO); |
| 359 |
setUrlInputText(ANCHOR_URL); |
| 360 |
assertEquals( |
| 361 |
'Display text should not have been autogenerated', ANCHOR_MAILTO, |
| 362 |
getDisplayInputText()); |
| 363 |
|
| 364 |
mockCtrl.$verifyAll(); |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
/** |
| 369 |
* Tests that clicking OK with the url tab selected dispatches an event with |
| 370 |
* the proper link data. |
| 371 |
*/ |
| 372 |
function testOkForUrl() { |
| 373 |
expectOk(ANCHOR_TEXT, ANCHOR_URL); |
| 374 |
mockCtrl.$replayAll(); |
| 375 |
setUpAnchor('', '', true); |
| 376 |
createAndShow(); |
| 377 |
|
| 378 |
dialog.tabPane_.setSelectedTabId(goog.ui.editor.LinkDialog.Id_.ON_WEB_TAB); |
| 379 |
setDisplayInputText(ANCHOR_TEXT); |
| 380 |
setUrlInputText(ANCHOR_URL); |
| 381 |
goog.testing.events.fireClickSequence(dialog.getOkButtonElement()); |
| 382 |
|
| 383 |
mockCtrl.$verifyAll(); |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
/** |
| 388 |
* Tests that clicking OK with the url tab selected but with an email address |
| 389 |
* in the url field dispatches an event with the proper link data. |
| 390 |
*/ |
| 391 |
function testOkForUrlWithEmail() { |
| 392 |
expectOk(ANCHOR_TEXT, ANCHOR_MAILTO); |
| 393 |
mockCtrl.$replayAll(); |
| 394 |
setUpAnchor('', '', true); |
| 395 |
createAndShow(); |
| 396 |
|
| 397 |
dialog.tabPane_.setSelectedTabId(goog.ui.editor.LinkDialog.Id_.ON_WEB_TAB); |
| 398 |
setDisplayInputText(ANCHOR_TEXT); |
| 399 |
setUrlInputText(ANCHOR_EMAIL); |
| 400 |
goog.testing.events.fireClickSequence(dialog.getOkButtonElement()); |
| 401 |
|
| 402 |
mockCtrl.$verifyAll(); |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
/** |
| 407 |
* Tests that clicking OK with the email tab selected dispatches an event with |
| 408 |
* the proper link data. |
| 409 |
*/ |
| 410 |
function testOkForEmail() { |
| 411 |
expectOk(ANCHOR_TEXT, ANCHOR_MAILTO); |
| 412 |
mockCtrl.$replayAll(); |
| 413 |
setUpAnchor('', '', true); |
| 414 |
createAndShow(); |
| 415 |
|
| 416 |
dialog.tabPane_.setSelectedTabId( |
| 417 |
goog.ui.editor.LinkDialog.Id_.EMAIL_ADDRESS_TAB); |
| 418 |
|
| 419 |
setDisplayInputText(ANCHOR_TEXT); |
| 420 |
setEmailInputText(ANCHOR_EMAIL); |
| 421 |
goog.testing.events.fireClickSequence(dialog.getOkButtonElement()); |
| 422 |
|
| 423 |
mockCtrl.$verifyAll(); |
| 424 |
} |
| 425 |
|
| 426 |
function testOpenLinkInNewWindowNewLink() { |
| 427 |
expectOk(ANCHOR_TEXT, ANCHOR_URL, true); |
| 428 |
expectOk(ANCHOR_TEXT, ANCHOR_URL, false); |
| 429 |
mockCtrl.$replayAll(); |
| 430 |
|
| 431 |
setUpAnchor('', '', true); |
| 432 |
createAndShow(undefined, true); |
| 433 |
dialog.tabPane_.setSelectedTabId(goog.ui.editor.LinkDialog.Id_.ON_WEB_TAB); |
| 434 |
setDisplayInputText(ANCHOR_TEXT); |
| 435 |
setUrlInputText(ANCHOR_URL); |
| 436 |
|
| 437 |
assertFalse( |
| 438 |
'"Open in new window" should start unchecked', |
| 439 |
getOpenInNewWindowCheckboxChecked()); |
| 440 |
setOpenInNewWindowCheckboxChecked(true); |
| 441 |
assertTrue( |
| 442 |
'"Open in new window" should have gotten checked', |
| 443 |
getOpenInNewWindowCheckboxChecked()); |
| 444 |
goog.testing.events.fireClickSequence(dialog.getOkButtonElement()); |
| 445 |
|
| 446 |
// Reopen same dialog |
| 447 |
dialog.show(); |
| 448 |
dialog.tabPane_.setSelectedTabId(goog.ui.editor.LinkDialog.Id_.ON_WEB_TAB); |
| 449 |
setDisplayInputText(ANCHOR_TEXT); |
| 450 |
setUrlInputText(ANCHOR_URL); |
| 451 |
|
| 452 |
assertTrue( |
| 453 |
'"Open in new window" should remember it was checked', |
| 454 |
getOpenInNewWindowCheckboxChecked()); |
| 455 |
setOpenInNewWindowCheckboxChecked(false); |
| 456 |
assertFalse( |
| 457 |
'"Open in new window" should have gotten unchecked', |
| 458 |
getOpenInNewWindowCheckboxChecked()); |
| 459 |
goog.testing.events.fireClickSequence(dialog.getOkButtonElement()); |
| 460 |
} |
| 461 |
|
| 462 |
function testOpenLinkInNewWindowExistingLink() { |
| 463 |
mockCtrl.$replayAll(); |
| 464 |
|
| 465 |
// Edit an existing link that already opens in a new window. |
| 466 |
setUpAnchor('', '', false, '_blank'); |
| 467 |
createAndShow(undefined, true); |
| 468 |
dialog.tabPane_.setSelectedTabId(goog.ui.editor.LinkDialog.Id_.ON_WEB_TAB); |
| 469 |
setDisplayInputText(ANCHOR_TEXT); |
| 470 |
setUrlInputText(ANCHOR_URL); |
| 471 |
|
| 472 |
assertTrue( |
| 473 |
'"Open in new window" should start checked for existing link', |
| 474 |
getOpenInNewWindowCheckboxChecked()); |
| 475 |
|
| 476 |
mockCtrl.$verifyAll(); |
| 477 |
} |
| 478 |
|
| 479 |
function testRelNoFollowNewLink() { |
| 480 |
expectOk(ANCHOR_TEXT, ANCHOR_URL, null, true); |
| 481 |
expectOk(ANCHOR_TEXT, ANCHOR_URL, null, false); |
| 482 |
mockCtrl.$replayAll(); |
| 483 |
|
| 484 |
setUpAnchor('', '', true, true); |
| 485 |
createAndShow(null, null, true); |
| 486 |
dialog.tabPane_.setSelectedTabId(goog.ui.editor.LinkDialog.Id_.ON_WEB_TAB); |
| 487 |
setDisplayInputText(ANCHOR_TEXT); |
| 488 |
setUrlInputText(ANCHOR_URL); |
| 489 |
assertFalse( |
| 490 |
'rel=nofollow should start unchecked', |
| 491 |
dialog.relNoFollowCheckbox_.checked); |
| 492 |
|
| 493 |
// Check rel=nofollow and close the dialog. |
| 494 |
dialog.relNoFollowCheckbox_.checked = true; |
| 495 |
goog.testing.events.fireClickSequence(dialog.getOkButtonElement()); |
| 496 |
|
| 497 |
// Reopen the same dialog. |
| 498 |
anchorElem.rel = 'foo nofollow bar'; |
| 499 |
dialog.show(); |
| 500 |
dialog.tabPane_.setSelectedTabId(goog.ui.editor.LinkDialog.Id_.ON_WEB_TAB); |
| 501 |
setDisplayInputText(ANCHOR_TEXT); |
| 502 |
setUrlInputText(ANCHOR_URL); |
| 503 |
assertTrue( |
| 504 |
'rel=nofollow should start checked when reopening the dialog', |
| 505 |
dialog.relNoFollowCheckbox_.checked); |
| 506 |
} |
| 507 |
|
| 508 |
function testRelNoFollowExistingLink() { |
| 509 |
mockCtrl.$replayAll(); |
| 510 |
|
| 511 |
setUpAnchor('', '', null, null, 'foo nofollow bar'); |
| 512 |
createAndShow(null, null, true); |
| 513 |
assertTrue( |
| 514 |
'rel=nofollow should start checked for existing link', |
| 515 |
dialog.relNoFollowCheckbox_.checked); |
| 516 |
|
| 517 |
mockCtrl.$verifyAll(); |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
/** |
| 522 |
* Test that clicking on the test button opens a new window with the correct |
| 523 |
* options. |
| 524 |
*/ |
| 525 |
function testWebTestButton() { |
| 526 |
if (goog.userAgent.GECKO) { |
| 527 |
// TODO(robbyw): Figure out why this is flaky and fix it. |
| 528 |
return; |
| 529 |
} |
| 530 |
|
| 531 |
var width, height; |
| 532 |
mockWindowOpen( |
| 533 |
ANCHOR_URL, '_blank', |
| 534 |
new goog.testing.mockmatchers.ArgumentMatcher(function(str) { |
| 535 |
return str == |
| 536 |
'width=' + width + ',height=' + height + |
| 537 |
',toolbar=1,scrollbars=1,location=1,statusbar=0,' + |
| 538 |
'menubar=1,resizable=1'; |
| 539 |
}, '3rd arg: (string) window.open() options')); |
| 540 |
|
| 541 |
mockCtrl.$replayAll(); |
| 542 |
setUpAnchor(ANCHOR_URL, ANCHOR_TEXT); |
| 543 |
createAndShow(); |
| 544 |
|
| 545 |
// Measure viewport after opening dialog because that might cause scrollbars |
| 546 |
// to appear and reduce the viewport size. |
| 547 |
var size = goog.dom.getViewportSize(window); |
| 548 |
width = Math.max(size.width - 50, 50); |
| 549 |
height = Math.max(size.height - 50, 50); |
| 550 |
|
| 551 |
var testLink = goog.testing.dom.findTextNode( |
| 552 |
goog.ui.editor.messages.MSG_TEST_THIS_LINK, |
| 553 |
dialog.dialogInternal_.getElement()); |
| 554 |
goog.testing.events.fireClickSequence(testLink.parentNode); |
| 555 |
|
| 556 |
mockCtrl.$verifyAll(); |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
/** |
| 561 |
* Test that clicking on the test button does not open a new window when |
| 562 |
* the event is canceled. |
| 563 |
*/ |
| 564 |
function testWebTestButtonPreventDefault() { |
| 565 |
mockCtrl.$replayAll(); |
| 566 |
setUpAnchor(ANCHOR_URL, ANCHOR_TEXT); |
| 567 |
createAndShow(); |
| 568 |
|
| 569 |
goog.events.listen( |
| 570 |
dialog, goog.ui.editor.LinkDialog.EventType.BEFORE_TEST_LINK, |
| 571 |
function(e) { |
| 572 |
assertEquals(e.url, ANCHOR_URL); |
| 573 |
e.preventDefault(); |
| 574 |
}); |
| 575 |
var testLink = goog.testing.dom.findTextNode( |
| 576 |
goog.ui.editor.messages.MSG_TEST_THIS_LINK, |
| 577 |
dialog.dialogInternal_.getElement()); |
| 578 |
goog.testing.events.fireClickSequence(testLink.parentNode); |
| 579 |
|
| 580 |
mockCtrl.$verifyAll(); |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
/** |
| 585 |
* Test that the setTextToDisplayVisible() correctly works. |
| 586 |
* options. |
| 587 |
*/ |
| 588 |
function testSetTextToDisplayVisible() { |
| 589 |
mockCtrl.$replayAll(); |
| 590 |
setUpAnchor('', '', true); |
| 591 |
createAndShow(); |
| 592 |
|
| 593 |
assertNotEquals( |
| 594 |
'none', goog.style.getStyle(dialog.textToDisplayDiv_, 'display')); |
| 595 |
dialog.setTextToDisplayVisible(false); |
| 596 |
assertEquals( |
| 597 |
'none', goog.style.getStyle(dialog.textToDisplayDiv_, 'display')); |
| 598 |
dialog.setTextToDisplayVisible(true); |
| 599 |
assertNotEquals( |
| 600 |
'none', goog.style.getStyle(dialog.textToDisplayDiv_, 'display')); |
| 601 |
|
| 602 |
mockCtrl.$verifyAll(); |
| 603 |
} |
| 604 |
|
| 605 |
function getDisplayInput() { |
| 606 |
return dialog.dom.getElement(goog.ui.editor.LinkDialog.Id_.TEXT_TO_DISPLAY); |
| 607 |
} |
| 608 |
|
| 609 |
function getDisplayInputText() { |
| 610 |
return getDisplayInput().value; |
| 611 |
} |
| 612 |
|
| 613 |
function setDisplayInputText(text) { |
| 614 |
var textInput = getDisplayInput(); |
| 615 |
textInput.value = text; |
| 616 |
// Fire event so that dialog behaves like when user types. |
| 617 |
fireInputEvent(textInput, goog.events.KeyCodes.M); |
| 618 |
} |
| 619 |
|
| 620 |
function getUrlInput() { |
| 621 |
var elt = dialog.dom.getElement(goog.ui.editor.LinkDialog.Id_.ON_WEB_INPUT); |
| 622 |
assertNotNullNorUndefined('UrlInput must be found', elt); |
| 623 |
return elt; |
| 624 |
} |
| 625 |
|
| 626 |
function getUrlInputText() { |
| 627 |
return getUrlInput().value; |
| 628 |
} |
| 629 |
|
| 630 |
function setUrlInputText(text) { |
| 631 |
var urlInput = getUrlInput(); |
| 632 |
urlInput.value = text; |
| 633 |
// Fire event so that dialog behaves like when user types. |
| 634 |
fireInputEvent(dialog.urlInputHandler_, goog.events.KeyCodes.M); |
| 635 |
} |
| 636 |
|
| 637 |
function getEmailInput() { |
| 638 |
var elt = |
| 639 |
dialog.dom.getElement(goog.ui.editor.LinkDialog.Id_.EMAIL_ADDRESS_INPUT); |
| 640 |
assertNotNullNorUndefined('EmailInput must be found', elt); |
| 641 |
return elt; |
| 642 |
} |
| 643 |
|
| 644 |
function getEmailInputText() { |
| 645 |
return getEmailInput().value; |
| 646 |
} |
| 647 |
|
| 648 |
function setEmailInputText(text) { |
| 649 |
var emailInput = getEmailInput(); |
| 650 |
emailInput.value = text; |
| 651 |
// Fire event so that dialog behaves like when user types. |
| 652 |
fireInputEvent(dialog.emailInputHandler_, goog.events.KeyCodes.M); |
| 653 |
} |
| 654 |
|
| 655 |
function getOpenInNewWindowCheckboxChecked() { |
| 656 |
return dialog.openInNewWindowCheckbox_.checked; |
| 657 |
} |
| 658 |
|
| 659 |
function setOpenInNewWindowCheckboxChecked(checked) { |
| 660 |
dialog.openInNewWindowCheckbox_.checked = checked; |
| 661 |
} |
| 662 |
|
| 663 |
function fireInputEvent(input, keyCode) { |
| 664 |
var inputEvent = |
| 665 |
new goog.testing.events.Event(goog.events.EventType.INPUT, input); |
| 666 |
inputEvent.keyCode = keyCode; |
| 667 |
inputEvent.charCode = keyCode; |
| 668 |
goog.testing.events.fireBrowserEvent(inputEvent); |
| 669 |
} |