loremipsum_test.js 5.9 KB

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