123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- // Copyright 2008 The Closure Library Authors. All Rights Reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS-IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- goog.provide('goog.ui.ButtonTest');
- goog.setTestOnly('goog.ui.ButtonTest');
- goog.require('goog.dom');
- goog.require('goog.dom.classlist');
- goog.require('goog.events');
- goog.require('goog.events.Event');
- goog.require('goog.events.EventType');
- goog.require('goog.events.KeyCodes');
- goog.require('goog.events.KeyHandler');
- goog.require('goog.testing.events');
- goog.require('goog.testing.jsunit');
- goog.require('goog.ui.Button');
- goog.require('goog.ui.ButtonRenderer');
- goog.require('goog.ui.ButtonSide');
- goog.require('goog.ui.Component');
- goog.require('goog.ui.NativeButtonRenderer');
- var sandbox;
- var button;
- var clonedButtonDom;
- var demoButtonElement;
- function setUp() {
- sandbox = goog.dom.getElement('sandbox');
- button = new goog.ui.Button();
- demoButtonElement = goog.dom.getElement('demoButton');
- clonedButtonDom = demoButtonElement.cloneNode(true);
- }
- function tearDown() {
- button.dispose();
- demoButtonElement.parentNode.replaceChild(clonedButtonDom, demoButtonElement);
- goog.dom.removeChildren(sandbox);
- }
- function testConstructor() {
- assertNotNull('Button must not be null', button);
- assertEquals(
- 'Renderer must default to expected value',
- goog.ui.NativeButtonRenderer.getInstance(), button.getRenderer());
- var fakeDomHelper = {};
- var testButton = new goog.ui.Button(
- 'Hello', goog.ui.ButtonRenderer.getInstance(), fakeDomHelper);
- assertEquals(
- 'Content must have expected value', 'Hello', testButton.getContent());
- assertEquals(
- 'Renderer must have expected value', goog.ui.ButtonRenderer.getInstance(),
- testButton.getRenderer());
- assertEquals(
- 'DOM helper must have expected value', fakeDomHelper,
- testButton.getDomHelper());
- testButton.dispose();
- }
- function testGetSetValue() {
- assertUndefined(
- 'Button\'s value must default to undefined', button.getValue());
- button.setValue(17);
- assertEquals('Button must have expected value', 17, button.getValue());
- button.render(sandbox);
- assertEquals(
- 'Button element must have expected value', '17',
- button.getElement().value);
- button.setValue('foo');
- assertEquals(
- 'Button element must have updated value', 'foo',
- button.getElement().value);
- button.setValueInternal('bar');
- assertEquals('Button must have new internal value', 'bar', button.getValue());
- assertEquals(
- 'Button element must be unchanged', 'foo', button.getElement().value);
- }
- function testGetSetTooltip() {
- assertUndefined(
- 'Button\'s tooltip must default to undefined', button.getTooltip());
- button.setTooltip('Hello');
- assertEquals(
- 'Button must have expected tooltip', 'Hello', button.getTooltip());
- button.render(sandbox);
- assertEquals(
- 'Button element must have expected title', 'Hello',
- button.getElement().title);
- button.setTooltip('Goodbye');
- assertEquals(
- 'Button element must have updated title', 'Goodbye',
- button.getElement().title);
- button.setTooltipInternal('World');
- assertEquals(
- 'Button must have new internal tooltip', 'World', button.getTooltip());
- assertEquals(
- 'Button element must be unchanged', 'Goodbye', button.getElement().title);
- }
- function testSetCollapsed() {
- assertNull(
- 'Button must not have any collapsed styling by default',
- button.getExtraClassNames());
- button.setCollapsed(goog.ui.ButtonSide.START);
- assertSameElements(
- 'Button must have the start side collapsed',
- ['goog-button-collapse-left'], button.getExtraClassNames());
- button.render(sandbox);
- assertSameElements(
- 'Button element must have the start side collapsed',
- ['goog-button', 'goog-button-collapse-left'],
- goog.dom.classlist.get(button.getElement()));
- button.setCollapsed(goog.ui.ButtonSide.BOTH);
- assertSameElements(
- 'Button must have both sides collapsed',
- ['goog-button-collapse-left', 'goog-button-collapse-right'],
- button.getExtraClassNames());
- assertSameElements(
- 'Button element must have both sides collapsed',
- [
- 'goog-button', 'goog-button-collapse-left', 'goog-button-collapse-right'
- ],
- goog.dom.classlist.get(button.getElement()));
- }
- function testDispose() {
- assertFalse('Button must not have been disposed of', button.isDisposed());
- button.render(sandbox);
- button.setValue('foo');
- button.setTooltip('bar');
- button.dispose();
- assertTrue('Button must have been disposed of', button.isDisposed());
- assertUndefined('Button\'s value must have been deleted', button.getValue());
- assertUndefined(
- 'Button\'s tooltip must have been deleted', button.getTooltip());
- }
- function testBasicButtonBehavior() {
- var dispatchedActionCount = 0;
- var handleAction = function() { dispatchedActionCount++; };
- goog.events.listen(button, goog.ui.Component.EventType.ACTION, handleAction);
- button.decorate(demoButtonElement);
- goog.testing.events.fireClickSequence(demoButtonElement);
- assertEquals(
- 'Button must have dispatched ACTION on click', 1, dispatchedActionCount);
- dispatchedActionCount = 0;
- var e = new goog.events.Event(goog.events.KeyHandler.EventType.KEY, button);
- e.keyCode = goog.events.KeyCodes.ENTER;
- button.handleKeyEvent(e);
- assertEquals(
- 'Enabled button must have dispatched ACTION on Enter key', 1,
- dispatchedActionCount);
- dispatchedActionCount = 0;
- e = new goog.events.Event(goog.events.EventType.KEYUP, button);
- e.keyCode = goog.events.KeyCodes.SPACE;
- button.handleKeyEvent(e);
- assertEquals(
- 'Enabled button must have dispatched ACTION on Space key', 1,
- dispatchedActionCount);
- goog.events.unlisten(
- button, goog.ui.Component.EventType.ACTION, handleAction);
- }
- function testDisabledButtonBehavior() {
- var dispatchedActionCount = 0;
- var handleAction = function() { dispatchedActionCount++; };
- goog.events.listen(button, goog.ui.Component.EventType.ACTION, handleAction);
- button.setEnabled(false);
- dispatchedActionCount = 0;
- button.handleKeyEvent({keyCode: goog.events.KeyCodes.ENTER});
- assertEquals(
- 'Disabled button must not dispatch ACTION on Enter key', 0,
- dispatchedActionCount);
- dispatchedActionCount = 0;
- button.handleKeyEvent(
- {keyCode: goog.events.KeyCodes.SPACE, type: goog.events.EventType.KEYUP});
- assertEquals(
- 'Disabled button must not have dispatched ACTION on Space', 0,
- dispatchedActionCount);
- goog.events.unlisten(
- button, goog.ui.Component.EventType.ACTION, handleAction);
- }
- function testSpaceFireActionOnKeyUp() {
- var dispatchedActionCount = 0;
- var handleAction = function() { dispatchedActionCount++; };
- goog.events.listen(button, goog.ui.Component.EventType.ACTION, handleAction);
- dispatchedActionCount = 0;
- e = new goog.events.Event(goog.events.KeyHandler.EventType.KEY, button);
- e.keyCode = goog.events.KeyCodes.SPACE;
- button.handleKeyEvent(e);
- assertEquals(
- 'Button must not have dispatched ACTION on Space keypress', 0,
- dispatchedActionCount);
- assertEquals(
- 'The default action (scrolling) must have been prevented ' +
- 'for Space keypress',
- false, e.returnValue_);
- dispatchedActionCount = 0;
- e = new goog.events.Event(goog.events.EventType.KEYUP, button);
- e.keyCode = goog.events.KeyCodes.SPACE;
- button.handleKeyEvent(e);
- assertEquals(
- 'Button must have dispatched ACTION on Space keyup', 1,
- dispatchedActionCount);
- goog.events.unlisten(
- button, goog.ui.Component.EventType.ACTION, handleAction);
- }
- function testEnterFireActionOnKeyPress() {
- var dispatchedActionCount = 0;
- var handleAction = function() { dispatchedActionCount++; };
- goog.events.listen(button, goog.ui.Component.EventType.ACTION, handleAction);
- dispatchedActionCount = 0;
- e = new goog.events.Event(goog.events.KeyHandler.EventType.KEY, button);
- e.keyCode = goog.events.KeyCodes.ENTER;
- button.handleKeyEvent(e);
- assertEquals(
- 'Button must have dispatched ACTION on Enter keypress', 1,
- dispatchedActionCount);
- dispatchedActionCount = 0;
- e = new goog.events.Event(goog.events.EventType.KEYUP, button);
- e.keyCode = goog.events.KeyCodes.ENTER;
- button.handleKeyEvent(e);
- assertEquals(
- 'Button must not have dispatched ACTION on Enter keyup', 0,
- dispatchedActionCount);
- goog.events.unlisten(
- button, goog.ui.Component.EventType.ACTION, handleAction);
- }
- function testSetAriaLabel() {
- assertNull(
- 'Button must not have aria label by default', button.getAriaLabel());
- button.setAriaLabel('Button 1');
- button.render();
- assertEquals(
- 'Button element must have expected aria-label', 'Button 1',
- button.getElement().getAttribute('aria-label'));
- button.setAriaLabel('Button 2');
- assertEquals(
- 'Button element must have updated aria-label', 'Button 2',
- button.getElement().getAttribute('aria-label'));
- }
- function testSetAriaLabel_decorate() {
- assertNull(
- 'Button must not have aria label by default', button.getAriaLabel());
- button.setAriaLabel('Button 1');
- button.decorate(demoButtonElement);
- var el = button.getElementStrict();
- assertEquals(
- 'Button element must have expected aria-label', 'Button 1',
- el.getAttribute('aria-label'));
- assertEquals(
- 'Button element must have expected aria-role', 'button',
- el.getAttribute('role'));
- button.setAriaLabel('Button 2');
- assertEquals(
- 'Button element must have updated aria-label', 'Button 2',
- el.getAttribute('aria-label'));
- assertEquals(
- 'Button element must have expected aria-role', 'button',
- el.getAttribute('role'));
- }
|