prompt_test.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2010 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.PromptTest');
  15. goog.setTestOnly('goog.ui.PromptTest');
  16. goog.require('goog.dom.selection');
  17. goog.require('goog.events.InputHandler');
  18. goog.require('goog.events.KeyCodes');
  19. goog.require('goog.functions');
  20. goog.require('goog.string');
  21. goog.require('goog.testing.events');
  22. goog.require('goog.testing.jsunit');
  23. goog.require('goog.ui.BidiInput');
  24. goog.require('goog.ui.Dialog');
  25. goog.require('goog.ui.Prompt');
  26. goog.require('goog.userAgent');
  27. goog.require('goog.userAgent.product');
  28. var prompt;
  29. function setUp() {
  30. document.body.focus();
  31. }
  32. function tearDown() {
  33. goog.dispose(prompt);
  34. }
  35. function testFocusOnInputElement() {
  36. // FF does not perform focus if the window is not active in the first place.
  37. if (goog.userAgent.GECKO && document.hasFocus && !document.hasFocus()) {
  38. return;
  39. }
  40. var promptResult = undefined;
  41. prompt = new goog.ui.Prompt('title', 'Prompt:', function(result) {
  42. promptResult = result;
  43. }, 'defaultValue');
  44. prompt.setVisible(true);
  45. if (goog.userAgent.product.CHROME) {
  46. // For some reason, this test fails non-deterministically on Chrome,
  47. // but only on the test farm.
  48. return;
  49. }
  50. assertEquals('defaultValue', goog.dom.selection.getText(prompt.userInputEl_));
  51. }
  52. function testValidationFunction() {
  53. var promptResult = undefined;
  54. prompt = new goog.ui.Prompt(
  55. 'title', 'Prompt:', function(result) { promptResult = result; }, '');
  56. prompt.setValidationFunction(
  57. goog.functions.not(goog.string.isEmptyOrWhitespace));
  58. prompt.setVisible(true);
  59. var buttonSet = prompt.getButtonSet();
  60. var okButton = buttonSet.getButton(goog.ui.Dialog.DefaultButtonKeys.OK);
  61. assertTrue(okButton.disabled);
  62. prompt.userInputEl_.value = '';
  63. goog.testing.events.fireKeySequence(
  64. prompt.userInputEl_, goog.events.KeyCodes.SPACE);
  65. assertTrue(okButton.disabled);
  66. prompt.userInputEl_.value = 'foo';
  67. goog.testing.events.fireKeySequence(
  68. prompt.userInputEl_, goog.events.KeyCodes.X);
  69. assertFalse(okButton.disabled);
  70. }
  71. function testBidiInput() {
  72. var shalomInHebrew = '\u05e9\u05dc\u05d5\u05dd';
  73. var promptResult = undefined;
  74. prompt = new goog.ui.Prompt('title', 'Prompt:', goog.functions.NULL, '');
  75. var bidiInput = new goog.ui.BidiInput();
  76. prompt.setInputDecoratorFn(goog.bind(bidiInput.decorate, bidiInput));
  77. prompt.setVisible(true);
  78. prompt.userInputEl_.value = shalomInHebrew;
  79. goog.testing.events.fireKeySequence(
  80. prompt.userInputEl_, goog.events.KeyCodes.SPACE);
  81. goog.testing.events.fireBrowserEvent(
  82. {'target': prompt.userInputEl_, 'type': 'input'});
  83. bidiInput.inputHandler_.dispatchEvent(
  84. goog.events.InputHandler.EventType.INPUT);
  85. assertEquals('rtl', prompt.userInputEl_.dir);
  86. prompt.userInputEl_.value = 'shalomInEnglish';
  87. goog.testing.events.fireKeySequence(
  88. prompt.userInputEl_, goog.events.KeyCodes.SPACE);
  89. goog.testing.events.fireBrowserEvent(
  90. {'target': prompt.userInputEl_, 'type': 'input'});
  91. bidiInput.inputHandler_.dispatchEvent(
  92. goog.events.InputHandler.EventType.INPUT);
  93. assertEquals('ltr', prompt.userInputEl_.dir);
  94. goog.dispose(bidiInput);
  95. }
  96. function testBidiInput_off() {
  97. var shalomInHebrew = '\u05e9\u05dc\u05d5\u05dd';
  98. var promptResult = undefined;
  99. prompt = new goog.ui.Prompt('title', 'Prompt:', goog.functions.NULL, '');
  100. prompt.setVisible(true);
  101. prompt.userInputEl_.value = shalomInHebrew;
  102. goog.testing.events.fireKeySequence(
  103. prompt.userInputEl_, goog.events.KeyCodes.SPACE);
  104. goog.testing.events.fireBrowserEvent(
  105. {'target': prompt.userInputEl_, 'type': 'input'});
  106. assertEquals('', prompt.userInputEl_.dir);
  107. prompt.userInputEl_.value = 'shalomInEnglish';
  108. goog.testing.events.fireKeySequence(
  109. prompt.userInputEl_, goog.events.KeyCodes.SPACE);
  110. assertEquals('', prompt.userInputEl_.dir);
  111. }
  112. // An interactive test so we can manually see what it looks like.
  113. function newPrompt() {
  114. prompt = new goog.ui.Prompt('title', 'Prompt:', function(result) {
  115. alert('Result: ' + result);
  116. goog.dispose(prompt);
  117. }, 'defaultValue');
  118. prompt.setVisible(true);
  119. }