charcounter_test.js 7.8 KB

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