Debian Codegraph @

closure-library/closure/goog/ui/charcounter_test.js

raw
1 // Copyright 2014 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.ui.CharCounterTest');
16 goog.setTestOnly('goog.ui.CharCounterTest');
17
18 goog.require('goog.dom');
19 goog.require('goog.testing.asserts');
20 goog.require('goog.testing.jsunit');
21 goog.require('goog.ui.CharCounter');
22 goog.require('goog.userAgent');
23
24 var countElement, charCounter, inputElement;
25 var incremental = goog.ui.CharCounter.Display.INCREMENTAL;
26 var remaining = goog.ui.CharCounter.Display.REMAINING;
27 var maxLength = 25;
28
29 function setUp() {
30 inputElement = goog.dom.getElement('test-textarea-id');
31 inputElement.value = '';
32 countElement = goog.dom.getElementByClass('char-count');
33 goog.dom.setTextContent(countElement, '');
34 charCounter = new goog.ui.CharCounter(inputElement, countElement, maxLength);
35 }
36
37 function tearDown() {
38 charCounter.dispose();
39 }
40
41 function setupCheckLength(content, mode) {
42 inputElement.value = content;
43 charCounter.setDisplayMode(mode);
44 charCounter.checkLength();
45 }
46
47 function testConstructor() {
48 assertNotNull('Character counter can not be null', charCounter);
49 assertEquals(maxLength.toString(), goog.dom.getTextContent(countElement));
50 }
51
52 function testSetMaxLength() {
53 charCounter.setMaxLength(10);
54 assertEquals('10', goog.dom.getTextContent(countElement));
55
56 var tooLongContent = 'This is too long text content';
57 inputElement.value = tooLongContent;
58 charCounter.setMaxLength(10);
59 assertEquals('0', goog.dom.getTextContent(countElement));
60 assertEquals('This is to', inputElement.value);
61 }
62
63 function testGetMaxLength() {
64 assertEquals(maxLength, charCounter.getMaxLength());
65 }
66
67 function testSetDisplayMode() {
68 // Test counter to be in incremental mode
69 charCounter.setDisplayMode(incremental);
70 assertEquals('0', goog.dom.getTextContent(countElement));
71
72 // Test counter to be in remaining mode
73 charCounter.setDisplayMode(remaining);
74 assertEquals(maxLength.toString(), goog.dom.getTextContent(countElement));
75 }
76
77 function testGetDisplayMode() {
78 assertEquals(remaining, charCounter.getDisplayMode());
79
80 var incrementalCharCounter = new goog.ui.CharCounter(
81 inputElement, countElement, maxLength, incremental);
82 assertEquals(incremental, incrementalCharCounter.getDisplayMode());
83 }
84
85 function testCheckLength() {
86 // Test the characters remaining in DOM
87 setupCheckLength('', remaining);
88 assertEquals(maxLength.toString(), goog.dom.getTextContent(countElement));
89
90 // Test the characters incremental in DOM
91 setupCheckLength('', incremental);
92 assertEquals('0', goog.dom.getTextContent(countElement));
93 }
94
95 function testCheckLength_limitedContent() {
96 var limitedContent = 'Limited text content';
97 var limitedContentLength = limitedContent.length;
98 var remainingLimitedContentLength = maxLength - limitedContentLength;
99
100 // Set some content and test the characters remaining in DOM
101 setupCheckLength(limitedContent, remaining);
102
103 assertEquals(limitedContent, inputElement.value);
104 assertEquals(
105 remainingLimitedContentLength.toString(),
106 goog.dom.getTextContent(countElement));
107
108 // Test the characters incremented in DOM with limited content
109 charCounter.setDisplayMode(incremental);
110 charCounter.checkLength();
111
112 assertEquals(limitedContent, inputElement.value);
113 assertEquals(
114 limitedContentLength.toString(), goog.dom.getTextContent(countElement));
115 }
116
117 function testCheckLength_overflowContent() {
118 var tooLongContent = 'This is too long text content';
119 var truncatedContent = 'This is too long text con';
120
121 // Set content longer than the maxLength and test the characters remaining
122 // in DOM with overflowing content
123 setupCheckLength(tooLongContent, remaining);
124
125 assertEquals(truncatedContent, inputElement.value);
126 assertEquals('0', goog.dom.getTextContent(countElement));
127
128 // Set content longer than the maxLength and test the characters
129 // incremented in DOM with overflowing content
130 setupCheckLength(tooLongContent, incremental);
131
132 assertEquals(truncatedContent, inputElement.value);
133 assertEquals(maxLength.toString(), goog.dom.getTextContent(countElement));
134 }
135
136 function testCheckLength_newLineContent() {
137 var newLineContent = 'New\nline';
138 var newLineContentLength = newLineContent.length;
139 var remainingNewLineContentLength = maxLength - newLineContentLength;
140
141 var carriageReturnContent = 'New\r\nline';
142 var carriageReturnContentLength = carriageReturnContent.length;
143 var remainingCarriageReturnContentLength =
144 maxLength - carriageReturnContentLength;
145
146 // Set some content with new line characters and test the characters
147 // remaining in DOM
148 setupCheckLength(newLineContent, remaining);
149
150 // Test for IE 7,8 which appends \r to \n
151 if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('9.0')) {
152 assertEquals(carriageReturnContent, inputElement.value);
153 assertEquals(
154 remainingCarriageReturnContentLength.toString(),
155 goog.dom.getTextContent(countElement));
156 } else {
157 assertEquals(newLineContent, inputElement.value);
158 assertEquals(
159 remainingNewLineContentLength.toString(),
160 goog.dom.getTextContent(countElement));
161 }
162
163 // Set some content with new line characters and test the characters
164 // incremental in DOM
165 setupCheckLength(newLineContent, incremental);
166
167 // Test for IE 7,8 which appends \r to \n
168 if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('9.0')) {
169 assertEquals(carriageReturnContent, inputElement.value);
170 assertEquals(
171 carriageReturnContentLength.toString(),
172 goog.dom.getTextContent(countElement));
173 } else {
174 assertEquals(newLineContent, inputElement.value);
175 assertEquals(
176 newLineContentLength.toString(), goog.dom.getTextContent(countElement));
177 }
178 }
179
180 function testCheckLength_carriageReturnContent() {
181 var newLineContent = 'New\nline';
182 var newLineContentLength = newLineContent.length;
183 var remainingNewLineContentLength = maxLength - newLineContentLength;
184
185 var carriageReturnContent = 'New\r\nline';
186 var carriageReturnContentLength = carriageReturnContent.length;
187 var remainingCarriageReturnContentLength =
188 maxLength - carriageReturnContentLength;
189
190 // Set some content with carriage return characters and test the
191 // characters remaining in DOM
192 setupCheckLength(carriageReturnContent, remaining);
193
194 // Test for IE 7,8
195 if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('9.0')) {
196 assertEquals(carriageReturnContent, inputElement.value);
197 assertEquals(
198 remainingCarriageReturnContentLength.toString(),
199 goog.dom.getTextContent(countElement));
200 } else {
201 // Others replace \r\n with \n
202 assertEquals(newLineContent, inputElement.value);
203 assertEquals(
204 remainingNewLineContentLength.toString(),
205 goog.dom.getTextContent(countElement));
206 }
207
208 // Set some content with carriage return characters and test the
209 // characters incremental in DOM
210 setupCheckLength(carriageReturnContent, incremental);
211
212 // Test for IE 7,8
213 if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('9.0')) {
214 assertEquals(carriageReturnContent, inputElement.value);
215 assertEquals(
216 carriageReturnContentLength.toString(),
217 goog.dom.getTextContent(countElement));
218 } else {
219 // Others replace \r\n with \n
220 assertEquals(newLineContent, inputElement.value);
221 assertEquals(
222 newLineContentLength.toString(), goog.dom.getTextContent(countElement));
223 }
224 }