| 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.LoremIpsumTest'); |
| 16 |
goog.setTestOnly('goog.editor.plugins.LoremIpsumTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.editor.Command'); |
| 20 |
goog.require('goog.editor.Field'); |
| 21 |
goog.require('goog.editor.plugins.LoremIpsum'); |
| 22 |
goog.require('goog.html.SafeHtml'); |
| 23 |
goog.require('goog.string.Unicode'); |
| 24 |
goog.require('goog.testing.jsunit'); |
| 25 |
goog.require('goog.userAgent'); |
| 26 |
|
| 27 |
var FIELD; |
| 28 |
var PLUGIN; |
| 29 |
var HTML; |
| 30 |
var UPPERCASE_CONTENTS = '<P>THE OWLS ARE NOT WHAT THEY SEEM.</P>'; |
| 31 |
|
| 32 |
function setUp() { |
| 33 |
HTML = goog.dom.getElement('root').innerHTML; |
| 34 |
|
| 35 |
FIELD = new goog.editor.Field('field'); |
| 36 |
|
| 37 |
PLUGIN = |
| 38 |
new goog.editor.plugins.LoremIpsum('The owls are not what they seem.'); |
| 39 |
FIELD.registerPlugin(PLUGIN); |
| 40 |
} |
| 41 |
|
| 42 |
function tearDown() { |
| 43 |
FIELD.dispose(); |
| 44 |
goog.dom.getElement('root').innerHTML = HTML; |
| 45 |
} |
| 46 |
|
| 47 |
function testQueryUsingLorem() { |
| 48 |
FIELD.makeEditable(); |
| 49 |
|
| 50 |
assertTrue(FIELD.queryCommandValue(goog.editor.Command.USING_LOREM)); |
| 51 |
FIELD.setSafeHtml( |
| 52 |
true, goog.html.SafeHtml.htmlEscape('fresh content'), false, true); |
| 53 |
assertFalse(FIELD.queryCommandValue(goog.editor.Command.USING_LOREM)); |
| 54 |
} |
| 55 |
|
| 56 |
function testUpdateLoremIpsum() { |
| 57 |
goog.dom.setTextContent(goog.dom.getElement('field'), 'stuff'); |
| 58 |
|
| 59 |
var loremPlugin = FIELD.getPluginByClassId('LoremIpsum'); |
| 60 |
FIELD.makeEditable(); |
| 61 |
var content = goog.html.SafeHtml.create('div', {}, 'foo'); |
| 62 |
|
| 63 |
FIELD.setSafeHtml( |
| 64 |
false, goog.html.SafeHtml.EMPTY, false, /* Don't update lorem */ false); |
| 65 |
assertFalse( |
| 66 |
'Field started with content, lorem must not be enabled.', |
| 67 |
FIELD.queryCommandValue(goog.editor.Command.USING_LOREM)); |
| 68 |
FIELD.execCommand(goog.editor.Command.UPDATE_LOREM); |
| 69 |
assertTrue( |
| 70 |
'Field was set to empty, update must turn on lorem ipsum', |
| 71 |
FIELD.queryCommandValue(goog.editor.Command.USING_LOREM)); |
| 72 |
|
| 73 |
FIELD.unregisterPlugin(loremPlugin); |
| 74 |
FIELD.setSafeHtml( |
| 75 |
false, content, false, |
| 76 |
/* Update (turn off) lorem */ true); |
| 77 |
FIELD.setSafeHtml( |
| 78 |
false, goog.html.SafeHtml.EMPTY, false, /* Don't update lorem */ false); |
| 79 |
FIELD.execCommand(goog.editor.Command.UPDATE_LOREM); |
| 80 |
assertFalse( |
| 81 |
'Field with no lorem message must not use lorem ipsum', |
| 82 |
FIELD.queryCommandValue(goog.editor.Command.USING_LOREM)); |
| 83 |
FIELD.registerPlugin(loremPlugin); |
| 84 |
|
| 85 |
FIELD.setSafeHtml(false, content, false, true); |
| 86 |
FIELD.setSafeHtml(false, goog.html.SafeHtml.EMPTY, false, false); |
| 87 |
goog.editor.Field.setActiveFieldId(FIELD.id); |
| 88 |
FIELD.execCommand(goog.editor.Command.UPDATE_LOREM); |
| 89 |
assertFalse( |
| 90 |
'Active field must not use lorem ipsum', |
| 91 |
FIELD.queryCommandValue(goog.editor.Command.USING_LOREM)); |
| 92 |
goog.editor.Field.setActiveFieldId(null); |
| 93 |
|
| 94 |
FIELD.setSafeHtml(false, content, false, true); |
| 95 |
FIELD.setSafeHtml(false, goog.html.SafeHtml.EMPTY, false, false); |
| 96 |
FIELD.setModalMode(true); |
| 97 |
FIELD.execCommand(goog.editor.Command.UPDATE_LOREM); |
| 98 |
assertFalse( |
| 99 |
'Must not turn on lorem ipsum while a dialog is open.', |
| 100 |
FIELD.queryCommandValue(goog.editor.Command.USING_LOREM)); |
| 101 |
FIELD.setModalMode(true); |
| 102 |
|
| 103 |
FIELD.dispose(); |
| 104 |
} |
| 105 |
|
| 106 |
function testLoremIpsumAndGetCleanContents() { |
| 107 |
goog.dom.setTextContent(goog.dom.getElement('field'), 'This is a field'); |
| 108 |
FIELD.makeEditable(); |
| 109 |
|
| 110 |
// test direct getCleanContents |
| 111 |
assertEquals( |
| 112 |
'field reported wrong contents', 'This is a field', |
| 113 |
FIELD.getCleanContents()); |
| 114 |
|
| 115 |
// test indirect getCleanContents |
| 116 |
var contents = FIELD.getCleanContents(); |
| 117 |
assertEquals('field reported wrong contents', 'This is a field', contents); |
| 118 |
|
| 119 |
// set field html, but explicitly forbid converting to lorem ipsum text |
| 120 |
FIELD.setSafeHtml( |
| 121 |
false, goog.html.SafeHtml.htmlEscape(goog.string.Unicode.NBSP), true, |
| 122 |
false /* no lorem */); |
| 123 |
assertEquals( |
| 124 |
'field contains unexpected contents', getNbsp(), |
| 125 |
FIELD.getElement().innerHTML); |
| 126 |
assertEquals( |
| 127 |
'field reported wrong contents', getNbsp(), FIELD.getCleanContents()); |
| 128 |
|
| 129 |
// now set field html allowing lorem |
| 130 |
FIELD.setSafeHtml( |
| 131 |
false, goog.html.SafeHtml.htmlEscape(goog.string.Unicode.NBSP), true, |
| 132 |
true /* lorem */); |
| 133 |
assertEquals( |
| 134 |
'field reported wrong contents', goog.string.Unicode.NBSP, |
| 135 |
FIELD.getCleanContents()); |
| 136 |
assertEquals( |
| 137 |
'field contains unexpected contents', UPPERCASE_CONTENTS, |
| 138 |
FIELD.getElement().innerHTML.toUpperCase()); |
| 139 |
} |
| 140 |
|
| 141 |
function testLoremIpsumAndGetCleanContents2() { |
| 142 |
// make a field blank before we make it editable, and then check |
| 143 |
// that making it editable activates lorem. |
| 144 |
assert('field is editable', FIELD.isUneditable()); |
| 145 |
goog.dom.setTextContent(goog.dom.getElement('field'), ' '); |
| 146 |
|
| 147 |
FIELD.makeEditable(); |
| 148 |
assertEquals( |
| 149 |
'field contains unexpected contents', UPPERCASE_CONTENTS, |
| 150 |
FIELD.getElement().innerHTML.toUpperCase()); |
| 151 |
|
| 152 |
FIELD.makeUneditable(); |
| 153 |
assertEquals( |
| 154 |
'field contains unexpected contents', UPPERCASE_CONTENTS, |
| 155 |
goog.dom.getElement('field').innerHTML.toUpperCase()); |
| 156 |
} |
| 157 |
|
| 158 |
function testLoremIpsumInClickToEditMode() { |
| 159 |
// in click-to-edit mode, trogedit manages the editable state of the editor, |
| 160 |
// so we must manage lorem ipsum in uneditable mode too. |
| 161 |
FIELD.makeEditable(); |
| 162 |
|
| 163 |
assertEquals( |
| 164 |
'field contains unexpected contents', UPPERCASE_CONTENTS, |
| 165 |
FIELD.getElement().innerHTML.toUpperCase()); |
| 166 |
|
| 167 |
FIELD.makeUneditable(); |
| 168 |
assertEquals( |
| 169 |
'field contains unexpected contents', UPPERCASE_CONTENTS, |
| 170 |
goog.dom.getElement('field').innerHTML.toUpperCase()); |
| 171 |
} |
| 172 |
|
| 173 |
function getNbsp() { |
| 174 |
// On WebKit (pre-528) and Opera, shows up as its unicode character in |
| 175 |
// innerHTML under some circumstances. |
| 176 |
return (goog.userAgent.WEBKIT && !goog.userAgent.isVersionOrHigher('528')) || |
| 177 |
goog.userAgent.OPERA ? |
| 178 |
'\u00a0' : |
| 179 |
' '; |
| 180 |
} |