| 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.ClickToEditWrapperTest'); |
| 16 |
goog.setTestOnly('goog.editor.ClickToEditWrapperTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.dom.Range'); |
| 20 |
goog.require('goog.dom.TagName'); |
| 21 |
goog.require('goog.editor.ClickToEditWrapper'); |
| 22 |
goog.require('goog.editor.SeamlessField'); |
| 23 |
goog.require('goog.testing.MockClock'); |
| 24 |
goog.require('goog.testing.events'); |
| 25 |
goog.require('goog.testing.jsunit'); |
| 26 |
|
| 27 |
var FIELD; |
| 28 |
var CLOCK; |
| 29 |
var HTML; |
| 30 |
|
| 31 |
function setUp() { |
| 32 |
HTML = goog.dom.getElement('root').innerHTML; |
| 33 |
CLOCK = new goog.testing.MockClock(true); |
| 34 |
|
| 35 |
// The following 3 lines are to get around an IE bug where it says |
| 36 |
// 'Incompatible markup pointers for this operation'. |
| 37 |
// Must be done in the setup, not teardown, or else it won't take effect for |
| 38 |
// the first test that is run, or any test that runs immediately after a |
| 39 |
// "breaking async" message from the jsunit framework. |
| 40 |
goog.dom.Range.clearSelection(); |
| 41 |
window.blur(); |
| 42 |
window.focus(); |
| 43 |
} |
| 44 |
|
| 45 |
/** @param {boolean=} opt_isBlended */ |
| 46 |
function setUpField(opt_isBlended) { |
| 47 |
FIELD = opt_isBlended ? new goog.editor.SeamlessField('testField') : |
| 48 |
new goog.editor.SeamlessField('testField'); |
| 49 |
|
| 50 |
(new goog.editor.ClickToEditWrapper(FIELD)); |
| 51 |
|
| 52 |
goog.dom.Range.clearSelection(); |
| 53 |
} |
| 54 |
|
| 55 |
function tearDown() { |
| 56 |
if (FIELD) { |
| 57 |
FIELD.dispose(); |
| 58 |
} |
| 59 |
|
| 60 |
CLOCK.dispose(); |
| 61 |
|
| 62 |
goog.dom.getElement('root').innerHTML = HTML; |
| 63 |
} |
| 64 |
|
| 65 |
function testClickToEdit(opt_isBlended) { |
| 66 |
setUpField(opt_isBlended); |
| 67 |
|
| 68 |
var text = goog.dom.getElement('testField').firstChild; |
| 69 |
goog.dom.Range.createFromNodes(text, 4, text, 8).select(); |
| 70 |
|
| 71 |
goog.testing.events.fireClickSequence(text.parentNode); |
| 72 |
|
| 73 |
assertFalse( |
| 74 |
'Field should not be made editable immediately after clicking', |
| 75 |
FIELD.isLoaded()); |
| 76 |
CLOCK.tick(1); |
| 77 |
assertTrue('Field should be editable', FIELD.isLoaded()); |
| 78 |
|
| 79 |
var dom = FIELD.getEditableDomHelper(); |
| 80 |
var selection = goog.dom.Range.createFromWindow(dom.getWindow()); |
| 81 |
|
| 82 |
var body = FIELD.getElement(); |
| 83 |
text = body.firstChild; |
| 84 |
|
| 85 |
assertEquals('Wrong start node', text, selection.getStartNode()); |
| 86 |
assertEquals('Wrong end node', text, selection.getEndNode()); |
| 87 |
assertEquals('Wrong start offset', 4, selection.getStartOffset()); |
| 88 |
assertEquals('Wrong end offset', 8, selection.getEndOffset()); |
| 89 |
} |
| 90 |
|
| 91 |
function testBlendedClickToEdit() { |
| 92 |
testClickToEdit(true); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
function testClickToEditWithAnchor(opt_isBlended) { |
| 97 |
setUpField(opt_isBlended); |
| 98 |
|
| 99 |
goog.dom.getElement('testAnchor').focus(); |
| 100 |
goog.testing.events.fireClickSequence(goog.dom.getElement('testAnchor')); |
| 101 |
|
| 102 |
CLOCK.tick(1); |
| 103 |
assertTrue('Field should be editable', FIELD.isLoaded()); |
| 104 |
|
| 105 |
var dom = FIELD.getEditableDomHelper(); |
| 106 |
var selection = goog.dom.Range.createFromWindow(dom.getWindow()); |
| 107 |
|
| 108 |
// TODO(brndn): the location of the cursor is not yet specified by the W3C |
| 109 |
// Editing APIs (https://dvcs.w3.org/hg/editing/raw-file/tip/editing.html). |
| 110 |
// See b/15678403. IE and some webkit (all Safari, and up to Chrome 57) |
| 111 |
// return the end of the previous text node, while other browsers return |
| 112 |
// the start of the next node. |
| 113 |
var body = FIELD.getElement(); |
| 114 |
var text = body.firstChild; |
| 115 |
var link = dom.getElementsByTagNameAndClass(goog.dom.TagName.A, null, body)[0] |
| 116 |
.firstChild; |
| 117 |
if (selection.getStartNode() == text) { |
| 118 |
assertEquals('Wrong start node', text, selection.getStartNode()); |
| 119 |
assertEquals('Wrong start offset', 17, selection.getStartOffset()); |
| 120 |
assertEquals('Wrong end node', text, selection.getEndNode()); |
| 121 |
assertEquals('Wrong end offset', 17, selection.getEndOffset()); |
| 122 |
} else { |
| 123 |
assertEquals('Wrong start node', link, selection.getStartNode()); |
| 124 |
assertEquals('Wrong start offset', 0, selection.getStartOffset()); |
| 125 |
assertEquals('Wrong end node', link, selection.getEndNode()); |
| 126 |
assertEquals('Wrong end offset', 0, selection.getEndOffset()); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
function testBlendedClickToEditWithAnchor() { |
| 131 |
testClickToEditWithAnchor(true); |
| 132 |
} |