bidiinput_test.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2012 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.BidiInputTest');
  15. goog.setTestOnly('goog.ui.BidiInputTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.testing.jsunit');
  18. goog.require('goog.ui.BidiInput');
  19. function setUp() {
  20. document.body.focus();
  21. }
  22. function tearDown() {
  23. document.getElementById('emptyText').value = '';
  24. document.getElementById('bidiText').value = 'hello, world!';
  25. }
  26. function testEmptyInput() {
  27. var bidiInput = new goog.ui.BidiInput();
  28. var emptyText = goog.dom.getElement('emptyText');
  29. bidiInput.decorate(emptyText);
  30. assertEquals('', bidiInput.getValue());
  31. bidiInput.setValue('hello!');
  32. assertEquals('hello!', bidiInput.getValue());
  33. }
  34. function testSetDirection() {
  35. var shalomInHebrew = '\u05e9\u05dc\u05d5\u05dd';
  36. var isAGoodLanguageInHebrew =
  37. '\u05d4\u05d9\u05d0 \u05e9\u05e4\u05d4 \u05d8\u05d5\u05d1\u05d4';
  38. var learnInHebrew = '\u05dc\u05de\u05d3';
  39. var bidiInput = new goog.ui.BidiInput();
  40. var bidiText = goog.dom.getElement('bidiText');
  41. bidiInput.decorate(bidiText);
  42. assertEquals('ltr', bidiInput.getDirection());
  43. bidiInput.setValue(shalomInHebrew);
  44. assertEquals('rtl', bidiInput.getDirection());
  45. bidiInput.setValue('hello!');
  46. assertEquals('ltr', bidiInput.getDirection());
  47. bidiInput.setValue(':) , ? ! ' + shalomInHebrew);
  48. assertEquals('rtl', bidiInput.getDirection());
  49. bidiInput.setValue(':) , ? ! hello!');
  50. assertEquals('ltr', bidiInput.getDirection());
  51. bidiInput.setValue(' ;) ');
  52. assertEquals(null, bidiInput.getDirection());
  53. bidiInput.setValue(shalomInHebrew + ', how are you today?');
  54. assertEquals('ltr', bidiInput.getDirection());
  55. bidiInput.setValue('Hello is ' + shalomInHebrew + ' in Hebrew');
  56. assertEquals('ltr', bidiInput.getDirection());
  57. bidiInput.setValue('JavaScript ' + isAGoodLanguageInHebrew);
  58. assertEquals('rtl', bidiInput.getDirection());
  59. bidiInput.setValue(learnInHebrew + ' JavaScript');
  60. assertEquals('rtl', bidiInput.getDirection());
  61. bidiInput.setValue('');
  62. assertEquals(null, bidiInput.getDirection());
  63. }
  64. function testSetDirection_inContenteditableDiv() {
  65. var shalomInHebrew = '\u05e9\u05dc\u05d5\u05dd';
  66. var isAGoodLanguageInHebrew =
  67. '\u05d4\u05d9\u05d0 \u05e9\u05e4\u05d4 \u05d8\u05d5\u05d1\u05d4';
  68. var learnInHebrew = '\u05dc\u05de\u05d3';
  69. var bidiInput = new goog.ui.BidiInput();
  70. var bidiTextDiv = goog.dom.getElement('bidiTextDiv');
  71. bidiInput.decorate(bidiTextDiv);
  72. assertEquals('ltr', bidiInput.getDirection());
  73. bidiInput.setValue(shalomInHebrew);
  74. assertEquals('rtl', bidiInput.getDirection());
  75. bidiInput.setValue('hello!');
  76. assertEquals('ltr', bidiInput.getDirection());
  77. bidiInput.setValue(':) , ? ! ' + shalomInHebrew);
  78. assertEquals('rtl', bidiInput.getDirection());
  79. bidiInput.setValue(':) , ? ! hello!');
  80. assertEquals('ltr', bidiInput.getDirection());
  81. bidiInput.setValue(' ;) ');
  82. assertEquals(null, bidiInput.getDirection());
  83. bidiInput.setValue(shalomInHebrew + ', how are you today?');
  84. assertEquals('ltr', bidiInput.getDirection());
  85. bidiInput.setValue('Hello is ' + shalomInHebrew + ' in Hebrew');
  86. assertEquals('ltr', bidiInput.getDirection());
  87. bidiInput.setValue('JavaScript ' + isAGoodLanguageInHebrew);
  88. assertEquals('rtl', bidiInput.getDirection());
  89. bidiInput.setValue(learnInHebrew + ' JavaScript');
  90. assertEquals('rtl', bidiInput.getDirection());
  91. bidiInput.setValue('');
  92. assertEquals(null, bidiInput.getDirection());
  93. }