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