aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/editor/plugins/loremipsum_test.html
blob: f2e60afd11b88810f27ac7fe345131f8d012b0f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<!DOCTYPE html>

<!--
All Rights Reserved.

@author nicksantos@google.com (Nick Santos)
-->

<html>
<!--
Copyright 2008 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>goog.editor.plugins.LoremIpsum Tests</title>
<script type="text/javascript" src="../../base.js"></script>
<script type="text/javascript">
  goog.require('goog.dom');
  goog.require('goog.editor.Command');
  goog.require('goog.editor.Field');
  goog.require('goog.editor.plugins.LoremIpsum');
  goog.require('goog.testing.jsunit');
  goog.require('goog.userAgent');
</script>
</head>
<body>

<div id='root'><div id='field'></div></div>

<script type="text/javascript">

var FIELD;
var PLUGIN;
var HTML;
var UPPERCASE_CONTENTS = '<P>THE OWLS ARE NOT WHAT THEY SEEM.</P>';

function setUp() {
  HTML = goog.dom.getElement('root').innerHTML;

  FIELD = new goog.editor.Field('field');

  PLUGIN = new goog.editor.plugins.LoremIpsum(
      'The owls are not what they seem.');
  FIELD.registerPlugin(PLUGIN);
}

function tearDown() {
  FIELD.dispose();
  goog.dom.getElement('root').innerHTML = HTML;
}

function testQueryUsingLorem() {
  FIELD.makeEditable();

  assertTrue(FIELD.queryCommandValue(goog.editor.Command.USING_LOREM));
  FIELD.setHtml(true, 'fresh content', false, true);
  assertFalse(FIELD.queryCommandValue(goog.editor.Command.USING_LOREM));
}

function testUpdateLoremIpsum() {
  goog.dom.getElement('field').innerHTML = 'stuff';

  var loremPlugin = FIELD.getPluginByClassId('LoremIpsum');
  FIELD.makeEditable();
  var content = '<div>foo</div>';

  FIELD.setHtml(false, '', false, /* Don't update lorem */ false);
  assertFalse('Field started with content, lorem must not be enabled.',
      FIELD.queryCommandValue(goog.editor.Command.USING_LOREM));
  FIELD.execCommand(goog.editor.Command.UPDATE_LOREM);
  assertTrue('Field was set to empty, update must turn on lorem ipsum',
      FIELD.queryCommandValue(goog.editor.Command.USING_LOREM));

  FIELD.unregisterPlugin(loremPlugin);
  FIELD.setHtml(false, content, false,
      /* Update (turn off) lorem */ true);
  FIELD.setHtml(false, '', false, /* Don't update lorem */ false);
  FIELD.execCommand(goog.editor.Command.UPDATE_LOREM);
  assertFalse('Field with no lorem message must not use lorem ipsum',
      FIELD.queryCommandValue(goog.editor.Command.USING_LOREM));
  FIELD.registerPlugin(loremPlugin);

  FIELD.setHtml(false, content, false, true);
  FIELD.setHtml(false, '', false, false);
  goog.editor.Field.setActiveFieldId(FIELD.id);
  FIELD.execCommand(goog.editor.Command.UPDATE_LOREM);
  assertFalse('Active field must not use lorem ipsum',
      FIELD.queryCommandValue(goog.editor.Command.USING_LOREM));
  goog.editor.Field.setActiveFieldId(null);

  FIELD.setHtml(false, content, false, true);
  FIELD.setHtml(false, '', false, false);
  FIELD.setModalMode(true);
  FIELD.execCommand(goog.editor.Command.UPDATE_LOREM);
  assertFalse('Must not turn on lorem ipsum while a dialog is open.',
      FIELD.queryCommandValue(goog.editor.Command.USING_LOREM));
  FIELD.setModalMode(true);

  FIELD.dispose();
}

function testLoremIpsumAndGetCleanContents() {
  goog.dom.getElement('field').innerHTML = 'This is a field';
  FIELD.makeEditable();

  // test direct getCleanContents
  assertEquals('field reported wrong contents', 'This is a field',
      FIELD.getCleanContents());

  // test indirect getCleanContents
  var contents = FIELD.getCleanContents();
  assertEquals('field reported wrong contents', 'This is a field', contents);

  // set field html, but explicitly forbid converting to lorem ipsum text
  FIELD.setHtml(false, '&nbsp;', true, false /* no lorem */);
  assertEquals('field contains unexpected contents', getNbsp(),
      FIELD.getElement().innerHTML);
  assertEquals('field reported wrong contents', getNbsp(),
      FIELD.getCleanContents());

  // now set field html allowing lorem
  FIELD.setHtml(false, '&nbsp;', true, true /* lorem */);
  assertEquals('field reported wrong contents', goog.string.Unicode.NBSP,
      FIELD.getCleanContents());
  assertEquals('field contains unexpected contents', UPPERCASE_CONTENTS,
      FIELD.getElement().innerHTML.toUpperCase());
}

function testLoremIpsumAndGetCleanContents2() {
  // make a field blank before we make it editable, and then check
  // that making it editable activates lorem.
  assert('field is editable', FIELD.isUneditable());
  goog.dom.getElement('field').innerHTML = '   ';

  FIELD.makeEditable();
  assertEquals('field contains unexpected contents',
      UPPERCASE_CONTENTS, FIELD.getElement().innerHTML.toUpperCase());

  FIELD.makeUneditable();
  assertEquals('field contains unexpected contents',
      UPPERCASE_CONTENTS, goog.dom.getElement('field').innerHTML.toUpperCase());
}

function testLoremIpsumInClickToEditMode() {
  // in click-to-edit mode, trogedit manages the editable state of the editor,
  // so we must manage lorem ipsum in uneditable mode too.
  FIELD.makeEditable();

  assertEquals('field contains unexpected contents',
      UPPERCASE_CONTENTS, FIELD.getElement().innerHTML.toUpperCase());

  FIELD.makeUneditable();
  assertEquals('field contains unexpected contents',
      UPPERCASE_CONTENTS, goog.dom.getElement('field').innerHTML.toUpperCase());
}

function getNbsp() {
  // On WebKit (pre-528) and Opera, &nbsp; shows up as its unicode character in
  // innerHTML under some circumstances.
  return (goog.userAgent.WEBKIT && !goog.userAgent.isVersion('528')) ||
         goog.userAgent.OPERA ? '\u00a0' : '&nbsp;';
}

</script>
</body>
</html>