emoticons_test.js 2.7 KB

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