| 1 |
// Copyright 2008 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.editor.plugins.AbstractBubblePluginTest'); |
| 16 |
goog.setTestOnly('goog.editor.plugins.AbstractBubblePluginTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.dom.TagName'); |
| 20 |
goog.require('goog.editor.plugins.AbstractBubblePlugin'); |
| 21 |
goog.require('goog.events.BrowserEvent'); |
| 22 |
goog.require('goog.events.EventType'); |
| 23 |
goog.require('goog.events.KeyCodes'); |
| 24 |
goog.require('goog.functions'); |
| 25 |
goog.require('goog.style'); |
| 26 |
goog.require('goog.testing.editor.FieldMock'); |
| 27 |
goog.require('goog.testing.editor.TestHelper'); |
| 28 |
goog.require('goog.testing.events'); |
| 29 |
goog.require('goog.testing.events.Event'); |
| 30 |
goog.require('goog.testing.jsunit'); |
| 31 |
goog.require('goog.ui.editor.Bubble'); |
| 32 |
goog.require('goog.userAgent'); |
| 33 |
|
| 34 |
var testHelper; |
| 35 |
var fieldDiv; |
| 36 |
var COMMAND = 'base'; |
| 37 |
var fieldMock; |
| 38 |
var bubblePlugin; |
| 39 |
var link; |
| 40 |
var link2; |
| 41 |
|
| 42 |
function setUpPage() { |
| 43 |
fieldDiv = goog.dom.getElement('field'); |
| 44 |
var viewportSize = goog.dom.getViewportSize(); |
| 45 |
// Some tests depends on enough size of viewport. |
| 46 |
if (viewportSize.width < 600 || viewportSize.height < 440) { |
| 47 |
window.moveTo(0, 0); |
| 48 |
window.resizeTo(640, 480); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
function setUp() { |
| 53 |
testHelper = new goog.testing.editor.TestHelper(fieldDiv); |
| 54 |
testHelper.setUpEditableElement(); |
| 55 |
fieldMock = new goog.testing.editor.FieldMock(); |
| 56 |
|
| 57 |
bubblePlugin = new goog.editor.plugins.AbstractBubblePlugin(COMMAND); |
| 58 |
bubblePlugin.fieldObject = fieldMock; |
| 59 |
|
| 60 |
fieldDiv.innerHTML = '<a href="http://www.google.com">Google</a>' + |
| 61 |
'<a href="http://www.google.com">Google2</a>'; |
| 62 |
link = fieldDiv.firstChild; |
| 63 |
link2 = fieldDiv.lastChild; |
| 64 |
|
| 65 |
window.scrollTo(0, 0); |
| 66 |
goog.style.setStyle(document.body, 'direction', 'ltr'); |
| 67 |
goog.style.setStyle(document.getElementById('field'), 'position', 'static'); |
| 68 |
} |
| 69 |
|
| 70 |
function tearDown() { |
| 71 |
bubblePlugin.closeBubble(); |
| 72 |
testHelper.tearDownEditableElement(); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
/** |
| 77 |
* This is a helper function for setting up the targetElement with a |
| 78 |
* given direction. |
| 79 |
* |
| 80 |
* @param {string} dir The direction of the targetElement, 'ltr' or 'rtl'. |
| 81 |
*/ |
| 82 |
function prepareTargetWithGivenDirection(dir) { |
| 83 |
goog.style.setStyle(document.body, 'direction', dir); |
| 84 |
|
| 85 |
fieldDiv.style.direction = dir; |
| 86 |
fieldDiv.innerHTML = '<a href="http://www.google.com">Google</a>'; |
| 87 |
link = fieldDiv.firstChild; |
| 88 |
|
| 89 |
fieldMock.$replay(); |
| 90 |
bubblePlugin.createBubbleContents = function(bubbleContainer) { |
| 91 |
bubbleContainer.innerHTML = '<div style="border:1px solid blue;">B</div>'; |
| 92 |
goog.style.setStyle(bubbleContainer, 'border', '1px solid white'); |
| 93 |
}; |
| 94 |
bubblePlugin.registerFieldObject(fieldMock); |
| 95 |
bubblePlugin.enable(fieldMock); |
| 96 |
bubblePlugin.createBubble(link); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
/** |
| 101 |
* Similar in intent to mock reset, but implemented by recreating the mock |
| 102 |
* variable. $reset() can't work because it will reset general any-time |
| 103 |
* expectations done in the fieldMock constructor. |
| 104 |
*/ |
| 105 |
function resetFieldMock() { |
| 106 |
fieldMock = new goog.testing.editor.FieldMock(); |
| 107 |
bubblePlugin.fieldObject = fieldMock; |
| 108 |
} |
| 109 |
|
| 110 |
function helpTestCreateBubble(opt_fn) { |
| 111 |
fieldMock.$replay(); |
| 112 |
var numCalled = 0; |
| 113 |
bubblePlugin.createBubbleContents = function(bubbleContainer) { |
| 114 |
numCalled++; |
| 115 |
assertNotNull('bubbleContainer should not be null', bubbleContainer); |
| 116 |
}; |
| 117 |
if (opt_fn) { |
| 118 |
opt_fn(); |
| 119 |
} |
| 120 |
bubblePlugin.createBubble(link); |
| 121 |
assertEquals('createBubbleContents should be called', 1, numCalled); |
| 122 |
fieldMock.$verify(); |
| 123 |
} |
| 124 |
|
| 125 |
function testCreateBubble(opt_fn) { |
| 126 |
helpTestCreateBubble(opt_fn); |
| 127 |
assertTrue(bubblePlugin.getSharedBubble_() instanceof goog.ui.editor.Bubble); |
| 128 |
|
| 129 |
assertTrue('Bubble should be visible', bubblePlugin.isVisible()); |
| 130 |
} |
| 131 |
|
| 132 |
function testOpeningBubbleCallsOnShow() { |
| 133 |
var numCalled = 0; |
| 134 |
testCreateBubble(function() { |
| 135 |
bubblePlugin.onShow = function() { numCalled++; }; |
| 136 |
}); |
| 137 |
|
| 138 |
assertEquals('onShow should be called', 1, numCalled); |
| 139 |
fieldMock.$verify(); |
| 140 |
} |
| 141 |
|
| 142 |
function testCloseBubble() { |
| 143 |
testCreateBubble(); |
| 144 |
|
| 145 |
bubblePlugin.closeBubble(); |
| 146 |
assertFalse('Bubble should not be visible', bubblePlugin.isVisible()); |
| 147 |
fieldMock.$verify(); |
| 148 |
} |
| 149 |
|
| 150 |
function testZindexBehavior() { |
| 151 |
// Don't use the default return values. |
| 152 |
fieldMock.$reset(); |
| 153 |
fieldMock.getAppWindow().$anyTimes().$returns(window); |
| 154 |
fieldMock.getEditableDomHelper().$anyTimes().$returns( |
| 155 |
goog.dom.getDomHelper(document)); |
| 156 |
fieldMock.getBaseZindex().$returns(2); |
| 157 |
bubblePlugin.createBubbleContents = goog.nullFunction; |
| 158 |
fieldMock.$replay(); |
| 159 |
|
| 160 |
bubblePlugin.createBubble(link); |
| 161 |
assertEquals( |
| 162 |
'2', '' + bubblePlugin.getSharedBubble_().bubbleContainer_.style.zIndex); |
| 163 |
|
| 164 |
fieldMock.$verify(); |
| 165 |
} |
| 166 |
|
| 167 |
function testNoTwoBubblesOpenAtSameTime() { |
| 168 |
fieldMock.$replay(); |
| 169 |
var origClose = goog.bind(bubblePlugin.closeBubble, bubblePlugin); |
| 170 |
var numTimesCloseCalled = 0; |
| 171 |
bubblePlugin.closeBubble = function() { |
| 172 |
numTimesCloseCalled++; |
| 173 |
origClose(); |
| 174 |
}; |
| 175 |
bubblePlugin.getBubbleTargetFromSelection = goog.functions.identity; |
| 176 |
bubblePlugin.createBubbleContents = goog.nullFunction; |
| 177 |
|
| 178 |
bubblePlugin.handleSelectionChangeInternal(link); |
| 179 |
assertEquals(0, numTimesCloseCalled); |
| 180 |
assertEquals(link, bubblePlugin.targetElement_); |
| 181 |
fieldMock.$verify(); |
| 182 |
|
| 183 |
bubblePlugin.handleSelectionChangeInternal(link2); |
| 184 |
assertEquals(1, numTimesCloseCalled); |
| 185 |
assertEquals(link2, bubblePlugin.targetElement_); |
| 186 |
fieldMock.$verify(); |
| 187 |
} |
| 188 |
|
| 189 |
function testHandleSelectionChangeWithEvent() { |
| 190 |
fieldMock.$replay(); |
| 191 |
var fakeEvent = new goog.events.BrowserEvent({type: 'mouseup', target: link}); |
| 192 |
bubblePlugin.getBubbleTargetFromSelection = goog.functions.identity; |
| 193 |
bubblePlugin.createBubbleContents = goog.nullFunction; |
| 194 |
bubblePlugin.handleSelectionChange(fakeEvent); |
| 195 |
assertTrue('Bubble should have been opened', bubblePlugin.isVisible()); |
| 196 |
assertEquals( |
| 197 |
'Bubble target should be provided event\'s target', link, |
| 198 |
bubblePlugin.targetElement_); |
| 199 |
} |
| 200 |
|
| 201 |
function testHandleSelectionChangeWithTarget() { |
| 202 |
fieldMock.$replay(); |
| 203 |
bubblePlugin.getBubbleTargetFromSelection = goog.functions.identity; |
| 204 |
bubblePlugin.createBubbleContents = goog.nullFunction; |
| 205 |
bubblePlugin.handleSelectionChange(undefined, link2); |
| 206 |
assertTrue('Bubble should have been opened', bubblePlugin.isVisible()); |
| 207 |
assertEquals( |
| 208 |
'Bubble target should be provided target', link2, |
| 209 |
bubblePlugin.targetElement_); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
/** |
| 214 |
* Regression test for @bug 2945341 |
| 215 |
*/ |
| 216 |
function testSelectOneTextCharacterNoError() { |
| 217 |
fieldMock.$replay(); |
| 218 |
bubblePlugin.getBubbleTargetFromSelection = goog.functions.identity; |
| 219 |
bubblePlugin.createBubbleContents = goog.nullFunction; |
| 220 |
// Select first char of first link's text node. |
| 221 |
testHelper.select(link.firstChild, 0, link.firstChild, 1); |
| 222 |
// This should execute without js errors. |
| 223 |
bubblePlugin.handleSelectionChange(); |
| 224 |
assertTrue('Bubble should have been opened', bubblePlugin.isVisible()); |
| 225 |
fieldMock.$verify(); |
| 226 |
} |
| 227 |
|
| 228 |
function testTabKeyEvents() { |
| 229 |
fieldMock.$replay(); |
| 230 |
bubblePlugin.enableKeyboardNavigation(true); |
| 231 |
bubblePlugin.getBubbleTargetFromSelection = goog.functions.identity; |
| 232 |
var nonTabbable1, tabbable1, tabbable2, nonTabbable2; |
| 233 |
bubblePlugin.createBubbleContents = function(container) { |
| 234 |
nonTabbable1 = goog.dom.createDom(goog.dom.TagName.DIV); |
| 235 |
tabbable1 = goog.dom.createDom(goog.dom.TagName.DIV); |
| 236 |
tabbable2 = goog.dom.createDom(goog.dom.TagName.DIV); |
| 237 |
nonTabbable2 = goog.dom.createDom(goog.dom.TagName.DIV); |
| 238 |
goog.dom.append( |
| 239 |
container, nonTabbable1, tabbable1, tabbable2, nonTabbable2); |
| 240 |
bubblePlugin.setTabbable(tabbable1); |
| 241 |
bubblePlugin.setTabbable(tabbable2); |
| 242 |
}; |
| 243 |
bubblePlugin.handleSelectionChangeInternal(link); |
| 244 |
assertTrue('Bubble should be visible', bubblePlugin.isVisible()); |
| 245 |
|
| 246 |
var tabHandledByBubble = simulateTabKeyOnBubble(); |
| 247 |
assertTrue('The action should be handled by the plugin', tabHandledByBubble); |
| 248 |
assertFocused(tabbable1); |
| 249 |
|
| 250 |
// Tab on the first tabbable. The test framework doesn't easily let us verify |
| 251 |
// the desired behavior - namely, that the second tabbable gets focused - but |
| 252 |
// we verify that the field doesn't get the focus. |
| 253 |
goog.testing.events.fireKeySequence(tabbable1, goog.events.KeyCodes.TAB); |
| 254 |
|
| 255 |
fieldMock.$verify(); |
| 256 |
|
| 257 |
// Tabbing on the last tabbable should trigger focus() of the target field. |
| 258 |
resetFieldMock(); |
| 259 |
fieldMock.focus(); |
| 260 |
fieldMock.$replay(); |
| 261 |
goog.testing.events.fireKeySequence(tabbable2, goog.events.KeyCodes.TAB); |
| 262 |
fieldMock.$verify(); |
| 263 |
} |
| 264 |
|
| 265 |
function testTabKeyEventsWithShiftKey() { |
| 266 |
fieldMock.$replay(); |
| 267 |
bubblePlugin.enableKeyboardNavigation(true); |
| 268 |
bubblePlugin.getBubbleTargetFromSelection = goog.functions.identity; |
| 269 |
var nonTabbable, tabbable1, tabbable2; |
| 270 |
bubblePlugin.createBubbleContents = function(container) { |
| 271 |
nonTabbable = goog.dom.createDom(goog.dom.TagName.DIV); |
| 272 |
tabbable1 = goog.dom.createDom(goog.dom.TagName.DIV); |
| 273 |
// The test acts only on one tabbable, but we give another one to make sure |
| 274 |
// that the tabbable we act on is not also the last. |
| 275 |
tabbable2 = goog.dom.createDom(goog.dom.TagName.DIV); |
| 276 |
goog.dom.append(container, nonTabbable, tabbable1, tabbable2); |
| 277 |
bubblePlugin.setTabbable(tabbable1); |
| 278 |
bubblePlugin.setTabbable(tabbable2); |
| 279 |
}; |
| 280 |
bubblePlugin.handleSelectionChangeInternal(link); |
| 281 |
|
| 282 |
assertTrue('Bubble should be visible', bubblePlugin.isVisible()); |
| 283 |
|
| 284 |
var tabHandledByBubble = simulateTabKeyOnBubble(); |
| 285 |
assertTrue('The action should be handled by the plugin', tabHandledByBubble); |
| 286 |
assertFocused(tabbable1); |
| 287 |
fieldMock.$verify(); |
| 288 |
|
| 289 |
// Shift-tabbing on the first tabbable should trigger focus() of the target |
| 290 |
// field. |
| 291 |
resetFieldMock(); |
| 292 |
fieldMock.focus(); |
| 293 |
fieldMock.$replay(); |
| 294 |
goog.testing.events.fireKeySequence( |
| 295 |
tabbable1, goog.events.KeyCodes.TAB, {shiftKey: true}); |
| 296 |
fieldMock.$verify(); |
| 297 |
} |
| 298 |
|
| 299 |
function testLinksAreTabbable() { |
| 300 |
fieldMock.$replay(); |
| 301 |
bubblePlugin.enableKeyboardNavigation(true); |
| 302 |
bubblePlugin.getBubbleTargetFromSelection = goog.functions.identity; |
| 303 |
var nonTabbable1, link1, link2, nonTabbable2; |
| 304 |
bubblePlugin.createBubbleContents = function(container) { |
| 305 |
nonTabbable1 = goog.dom.createDom(goog.dom.TagName.DIV); |
| 306 |
goog.dom.appendChild(container, nonTabbable1); |
| 307 |
bubbleLink1 = this.createLink('linkInBubble1', 'Foo', false, container); |
| 308 |
bubbleLink2 = this.createLink('linkInBubble2', 'Bar', false, container); |
| 309 |
nonTabbable2 = goog.dom.createDom(goog.dom.TagName.DIV); |
| 310 |
goog.dom.appendChild(container, nonTabbable2); |
| 311 |
}; |
| 312 |
bubblePlugin.handleSelectionChangeInternal(link); |
| 313 |
assertTrue('Bubble should be visible', bubblePlugin.isVisible()); |
| 314 |
|
| 315 |
var tabHandledByBubble = simulateTabKeyOnBubble(); |
| 316 |
assertTrue('The action should be handled by the plugin', tabHandledByBubble); |
| 317 |
assertFocused(bubbleLink1); |
| 318 |
|
| 319 |
fieldMock.$verify(); |
| 320 |
|
| 321 |
// Tabbing on the last link should trigger focus() of the target field. |
| 322 |
resetFieldMock(); |
| 323 |
fieldMock.focus(); |
| 324 |
fieldMock.$replay(); |
| 325 |
goog.testing.events.fireKeySequence(bubbleLink2, goog.events.KeyCodes.TAB); |
| 326 |
fieldMock.$verify(); |
| 327 |
} |
| 328 |
|
| 329 |
function testTabKeyNoEffectKeyboardNavDisabled() { |
| 330 |
fieldMock.$replay(); |
| 331 |
bubblePlugin.getBubbleTargetFromSelection = goog.functions.identity; |
| 332 |
var bubbleLink; |
| 333 |
bubblePlugin.createBubbleContents = function(container) { |
| 334 |
bubbleLink = this.createLink('linkInBubble', 'Foo', false, container); |
| 335 |
}; |
| 336 |
bubblePlugin.handleSelectionChangeInternal(link); |
| 337 |
|
| 338 |
assertTrue('Bubble should be visible', bubblePlugin.isVisible()); |
| 339 |
|
| 340 |
var tabHandledByBubble = simulateTabKeyOnBubble(); |
| 341 |
assertFalse( |
| 342 |
'The action should not be handled by the plugin', tabHandledByBubble); |
| 343 |
assertNotFocused(bubbleLink); |
| 344 |
|
| 345 |
// Verify that tabbing the link doesn't cause focus of the field. |
| 346 |
goog.testing.events.fireKeySequence(bubbleLink, goog.events.KeyCodes.TAB); |
| 347 |
|
| 348 |
fieldMock.$verify(); |
| 349 |
} |
| 350 |
|
| 351 |
function testOtherKeyEventNoEffectKeyboardNavEnabled() { |
| 352 |
fieldMock.$replay(); |
| 353 |
bubblePlugin.enableKeyboardNavigation(true); |
| 354 |
bubblePlugin.getBubbleTargetFromSelection = goog.functions.identity; |
| 355 |
var bubbleLink; |
| 356 |
bubblePlugin.createBubbleContents = function(container) { |
| 357 |
bubbleLink = this.createLink('linkInBubble', 'Foo', false, container); |
| 358 |
}; |
| 359 |
bubblePlugin.handleSelectionChangeInternal(link); |
| 360 |
|
| 361 |
assertTrue('Bubble should be visible', bubblePlugin.isVisible()); |
| 362 |
|
| 363 |
// Test pressing CTRL + B: this should not have any effect. |
| 364 |
var keyHandledByBubble = |
| 365 |
simulateKeyDownOnBubble(goog.events.KeyCodes.B, true); |
| 366 |
|
| 367 |
assertFalse( |
| 368 |
'The action should not be handled by the plugin', keyHandledByBubble); |
| 369 |
assertNotFocused(bubbleLink); |
| 370 |
|
| 371 |
fieldMock.$verify(); |
| 372 |
} |
| 373 |
|
| 374 |
function testSetTabbableSetsTabIndex() { |
| 375 |
var element1 = goog.dom.createDom(goog.dom.TagName.DIV); |
| 376 |
var element2 = goog.dom.createDom(goog.dom.TagName.DIV); |
| 377 |
element1.setAttribute('tabIndex', '1'); |
| 378 |
|
| 379 |
bubblePlugin.setTabbable(element1); |
| 380 |
bubblePlugin.setTabbable(element2); |
| 381 |
|
| 382 |
assertEquals('1', element1.getAttribute('tabIndex')); |
| 383 |
assertEquals('0', element2.getAttribute('tabIndex')); |
| 384 |
} |
| 385 |
|
| 386 |
function testDisable() { |
| 387 |
testCreateBubble(); |
| 388 |
fieldMock.setUneditable(true); |
| 389 |
bubblePlugin.disable(fieldMock); |
| 390 |
bubblePlugin.closeBubble(); |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
/** |
| 395 |
* Sends a tab key event to the bubble. |
| 396 |
* @return {boolean} whether the bubble hanlded the event. |
| 397 |
*/ |
| 398 |
function simulateTabKeyOnBubble() { |
| 399 |
return simulateKeyDownOnBubble(goog.events.KeyCodes.TAB, false); |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
/** |
| 404 |
* Sends a key event to the bubble. |
| 405 |
* @param {number} keyCode |
| 406 |
* @param {boolean} isCtrl |
| 407 |
* @return {boolean} whether the bubble hanlded the event. |
| 408 |
*/ |
| 409 |
function simulateKeyDownOnBubble(keyCode, isCtrl) { |
| 410 |
// In some browsers (e.g. FireFox) the editable field is marked with |
| 411 |
// designMode on. In the test setting (and not in production setting), the |
| 412 |
// bubble element shares the same window and hence the designMode. In this |
| 413 |
// mode, activeElement remains the <body> and isn't changed along with the |
| 414 |
// focus as a result of tab key. |
| 415 |
if (goog.userAgent.GECKO) { |
| 416 |
bubblePlugin.getSharedBubble_() |
| 417 |
.getContentElement() |
| 418 |
.ownerDocument.designMode = 'off'; |
| 419 |
} |
| 420 |
|
| 421 |
var event = |
| 422 |
new goog.testing.events.Event(goog.events.EventType.KEYDOWN, null); |
| 423 |
event.keyCode = keyCode; |
| 424 |
event.ctrlKey = isCtrl; |
| 425 |
return bubblePlugin.handleKeyDown(event); |
| 426 |
} |
| 427 |
|
| 428 |
function assertFocused(element) { |
| 429 |
// The activeElement assertion below doesn't work in IE7. At this time IE7 is |
| 430 |
// no longer supported by any client product, so we don't care. |
| 431 |
if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher(8)) { |
| 432 |
return; |
| 433 |
} |
| 434 |
assertEquals('unexpected focus', element, document.activeElement); |
| 435 |
} |
| 436 |
|
| 437 |
function assertNotFocused(element) { |
| 438 |
assertNotEquals('unexpected focus', element, document.activeElement); |
| 439 |
} |