| 1 |
// Copyright 2008 The Closure Library Authors. All Rights Reserved. |
| 2 |
// Use of this source code is governed by the Apache License, Version 2.0. |
| 3 |
|
| 4 |
goog.provide('goog.demos.editor.HelloWorldDialogPluginTest'); |
| 5 |
goog.setTestOnly('goog.demos.editor.HelloWorldDialogPluginTest'); |
| 6 |
|
| 7 |
goog.require('goog.demos.editor.HelloWorldDialog'); |
| 8 |
goog.require('goog.demos.editor.HelloWorldDialog.OkEvent'); |
| 9 |
goog.require('goog.demos.editor.HelloWorldDialogPlugin'); |
| 10 |
goog.require('goog.demos.editor.HelloWorldDialogPlugin.Command'); |
| 11 |
goog.require('goog.dom'); |
| 12 |
goog.require('goog.dom.NodeType'); |
| 13 |
goog.require('goog.testing.ExpectedFailures'); |
| 14 |
goog.require('goog.testing.MockControl'); |
| 15 |
goog.require('goog.testing.MockRange'); |
| 16 |
goog.require('goog.testing.PropertyReplacer'); |
| 17 |
goog.require('goog.testing.editor.FieldMock'); |
| 18 |
goog.require('goog.testing.editor.TestHelper'); |
| 19 |
goog.require('goog.testing.editor.dom'); |
| 20 |
goog.require('goog.testing.events'); |
| 21 |
goog.require('goog.testing.jsunit'); |
| 22 |
goog.require('goog.testing.mockmatchers.ArgumentMatcher'); |
| 23 |
goog.require('goog.userAgent'); |
| 24 |
|
| 25 |
var plugin; |
| 26 |
var mockCtrl; |
| 27 |
var mockField; |
| 28 |
var mockRange; |
| 29 |
var mockPlaceCursorNextTo; |
| 30 |
var stubs = new goog.testing.PropertyReplacer(); |
| 31 |
|
| 32 |
var fieldObj; |
| 33 |
|
| 34 |
var CUSTOM_MESSAGE = 'Hello, cruel world...'; |
| 35 |
|
| 36 |
var expectedFailures; |
| 37 |
|
| 38 |
function setUpPage() { |
| 39 |
expectedFailures = new goog.testing.ExpectedFailures(); |
| 40 |
} |
| 41 |
|
| 42 |
function setUp() { |
| 43 |
mockCtrl = new goog.testing.MockControl(); |
| 44 |
|
| 45 |
mockRange = new goog.testing.MockRange(); |
| 46 |
mockCtrl.addMock(mockRange); |
| 47 |
|
| 48 |
mockField = |
| 49 |
new goog.testing.editor.FieldMock(undefined, undefined, mockRange); |
| 50 |
mockCtrl.addMock(mockField); |
| 51 |
|
| 52 |
mockPlaceCursorNextTo = mockCtrl.createFunctionMock('placeCursorNextTo'); |
| 53 |
} |
| 54 |
|
| 55 |
function tearDown() { |
| 56 |
plugin.dispose(); |
| 57 |
tearDownRealEditableField(); |
| 58 |
expectedFailures.handleTearDown(); |
| 59 |
stubs.reset(); |
| 60 |
goog.dom.removeChildren(goog.dom.getElement('myField')); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Tests that the plugin's dialog is properly created. |
| 65 |
*/ |
| 66 |
function testCreateDialog() { |
| 67 |
mockField.$replay(); |
| 68 |
|
| 69 |
plugin = new goog.demos.editor.HelloWorldDialogPlugin(); |
| 70 |
plugin.registerFieldObject(mockField); |
| 71 |
|
| 72 |
var dialog = plugin.createDialog(goog.dom.getDomHelper()); |
| 73 |
assertTrue('Dialog should be of type goog.demos.editor.HelloWorldDialog', |
| 74 |
dialog instanceof goog.demos.editor.HelloWorldDialog); |
| 75 |
|
| 76 |
mockField.$verify(); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Tests that when the OK event fires the editable field is properly updated. |
| 81 |
*/ |
| 82 |
function testOk() { |
| 83 |
mockField.focus(); |
| 84 |
mockField.dispatchBeforeChange(); |
| 85 |
mockRange.removeContents(); |
| 86 |
// Tests that an argument is a span with the custom message. |
| 87 |
var createdNodeMatcher = new goog.testing.mockmatchers.ArgumentMatcher( |
| 88 |
function(arg) { |
| 89 |
return arg.nodeType == goog.dom.NodeType.ELEMENT && |
| 90 |
arg.tagName == goog.dom.TagName.SPAN && |
| 91 |
goog.dom.getRawTextContent(arg) == CUSTOM_MESSAGE; |
| 92 |
}); |
| 93 |
mockRange.insertNode(createdNodeMatcher, false); |
| 94 |
mockRange.$does(function(node, before) { |
| 95 |
return node; |
| 96 |
}); |
| 97 |
mockPlaceCursorNextTo(createdNodeMatcher, false); |
| 98 |
stubs.set(goog.editor.range, 'placeCursorNextTo', mockPlaceCursorNextTo); |
| 99 |
mockField.dispatchSelectionChangeEvent(); |
| 100 |
mockField.dispatchChange(); |
| 101 |
mockCtrl.$replayAll(); |
| 102 |
|
| 103 |
plugin = new goog.demos.editor.HelloWorldDialogPlugin(); |
| 104 |
plugin.registerFieldObject(mockField); |
| 105 |
var dialog = plugin.createDialog(goog.dom.getDomHelper()); |
| 106 |
|
| 107 |
// Mock of execCommand + clicking OK without actually opening the dialog. |
| 108 |
dialog.dispatchEvent( |
| 109 |
new goog.demos.editor.HelloWorldDialog.OkEvent(CUSTOM_MESSAGE)); |
| 110 |
|
| 111 |
mockCtrl.$verifyAll(); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Setup a real editable field (instead of a mock) and register the plugin to |
| 116 |
* it. |
| 117 |
*/ |
| 118 |
function setUpRealEditableField() { |
| 119 |
fieldObj = new goog.editor.Field('myField', document); |
| 120 |
fieldObj.makeEditable(); |
| 121 |
// Register the plugin to that field. |
| 122 |
plugin = new goog.demos.editor.HelloWorldDialogPlugin(); |
| 123 |
fieldObj.registerPlugin(plugin); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Tear down the real editable field. |
| 128 |
*/ |
| 129 |
function tearDownRealEditableField() { |
| 130 |
if (fieldObj) { |
| 131 |
fieldObj.makeUneditable(); |
| 132 |
fieldObj.dispose(); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Tests that the selection is cleared when the dialog opens and is |
| 138 |
* correctly restored after ok is clicked. |
| 139 |
*/ |
| 140 |
function testRestoreSelectionOnOk() { |
| 141 |
setUpRealEditableField(); |
| 142 |
|
| 143 |
fieldObj.setHtml(false, '12345'); |
| 144 |
var elem = fieldObj.getElement(); |
| 145 |
var helper = new goog.testing.editor.TestHelper(elem); |
| 146 |
helper.select('12345', 1, '12345', 4); // Selects '234'. |
| 147 |
|
| 148 |
assertEquals('Incorrect text selected before dialog is opened', |
| 149 |
'234', |
| 150 |
fieldObj.getRange().getText()); |
| 151 |
plugin.execCommand( |
| 152 |
goog.demos.editor.HelloWorldDialogPlugin.Command.HELLO_WORLD_DIALOG); |
| 153 |
|
| 154 |
// TODO(user): IE returns some bogus range when field doesn't have |
| 155 |
// selection. Remove the expectedFailure when robbyw fixes the issue. |
| 156 |
// NOTE(user): You can't remove the selection from a field in Opera without |
| 157 |
// blurring it. |
| 158 |
elem.parentNode.blur(); |
| 159 |
expectedFailures.expectFailureFor(goog.userAgent.IE || |
| 160 |
goog.userAgent.OPERA); |
| 161 |
try { |
| 162 |
assertNull('There should be no selection while dialog is open', |
| 163 |
fieldObj.getRange()); |
| 164 |
} catch (e) { |
| 165 |
expectedFailures.handleException(e); |
| 166 |
} |
| 167 |
|
| 168 |
goog.testing.events.fireClickSequence( |
| 169 |
plugin.dialog_.getOkButtonElement()); |
| 170 |
assertEquals('No text should be selected after clicking ok', |
| 171 |
'', |
| 172 |
fieldObj.getRange().getText()); |
| 173 |
|
| 174 |
// Test that the caret is placed after the custom message. |
| 175 |
goog.testing.editor.dom.assertRangeBetweenText( |
| 176 |
'Hello, world!', '5', fieldObj.getRange()); |
| 177 |
} |