| 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.editor.plugins.EmoticonsTest'); |
| 16 |
goog.setTestOnly('goog.editor.plugins.EmoticonsTest'); |
| 17 |
|
| 18 |
goog.require('goog.Uri'); |
| 19 |
goog.require('goog.array'); |
| 20 |
goog.require('goog.dom'); |
| 21 |
goog.require('goog.dom.TagName'); |
| 22 |
goog.require('goog.editor.Field'); |
| 23 |
goog.require('goog.editor.plugins.Emoticons'); |
| 24 |
goog.require('goog.testing.jsunit'); |
| 25 |
goog.require('goog.ui.emoji.Emoji'); |
| 26 |
goog.require('goog.userAgent'); |
| 27 |
|
| 28 |
var HTML; |
| 29 |
|
| 30 |
function setUp() { |
| 31 |
HTML = goog.dom.getElement('parent').innerHTML; |
| 32 |
} |
| 33 |
|
| 34 |
function tearDown() { |
| 35 |
goog.dom.getElement('parent').innerHTML = HTML; |
| 36 |
} |
| 37 |
|
| 38 |
function testEmojiWithEmoticonsPlugin() { |
| 39 |
var plugin = new goog.editor.plugins.Emoticons(); |
| 40 |
var field = new goog.editor.Field('testField'); |
| 41 |
field.registerPlugin(plugin); |
| 42 |
field.makeEditable(); |
| 43 |
field.focusAndPlaceCursorAtStart(); |
| 44 |
|
| 45 |
var src = 'testdata/emoji/4F4.gif'; |
| 46 |
var id = '4F4'; |
| 47 |
var emoji = new goog.ui.emoji.Emoji(src, id); |
| 48 |
field.execCommand(goog.editor.plugins.Emoticons.COMMAND, emoji); |
| 49 |
|
| 50 |
// The url may be relative or absolute. |
| 51 |
var imgs = field.getEditableDomHelper().getElementsByTagNameAndClass( |
| 52 |
goog.dom.TagName.IMG); |
| 53 |
assertEquals(1, imgs.length); |
| 54 |
|
| 55 |
var img = imgs[0]; |
| 56 |
assertUriEquals(src, img.getAttribute('src')); |
| 57 |
assertEquals(id, img.getAttribute(goog.ui.emoji.Emoji.ATTRIBUTE)); |
| 58 |
assertEquals(id, img.getAttribute(goog.ui.emoji.Emoji.DATA_ATTRIBUTE)); |
| 59 |
|
| 60 |
var range = field.getRange(); |
| 61 |
assertNotNull('must have a selection', range); |
| 62 |
assertTrue('range must be a cursor', range.isCollapsed()); |
| 63 |
|
| 64 |
if (!goog.userAgent.IE) { |
| 65 |
var webkitValid = (2 == range.getStartOffset()); |
| 66 |
var otherValid = |
| 67 |
(2 == |
| 68 |
goog.array.indexOf( |
| 69 |
range.getContainerElement().childNodes, range.getStartNode())); |
| 70 |
|
| 71 |
assertTrue('range starts after image', webkitValid || otherValid); |
| 72 |
} |
| 73 |
|
| 74 |
assertEquals( |
| 75 |
'range must be around image', img.parentElement, |
| 76 |
range.getContainerElement()); |
| 77 |
} |
| 78 |
|
| 79 |
function assertUriEquals(expected, actual) { |
| 80 |
var winUri = new goog.Uri(window.location); |
| 81 |
assertEquals( |
| 82 |
winUri.resolve(new goog.Uri(expected)).toString(), |
| 83 |
winUri.resolve(new goog.Uri(actual)).toString()); |
| 84 |
} |