Viktar Donich | d828629 | 2016-11-11 16:33:03 -0800 | [diff] [blame] | 1 | <!DOCTYPE html> |
| 2 | <!-- |
Dave Borowitz | 8cdc76b | 2018-03-26 10:04:27 -0400 | [diff] [blame] | 3 | @license |
Viktar Donich | d828629 | 2016-11-11 16:33:03 -0800 | [diff] [blame] | 4 | Copyright (C) 2016 The Android Open Source Project |
| 5 | |
| 6 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | you may not use this file except in compliance with the License. |
| 8 | You may obtain a copy of the License at |
| 9 | |
| 10 | http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | |
| 12 | Unless required by applicable law or agreed to in writing, software |
| 13 | distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | See the License for the specific language governing permissions and |
| 16 | limitations under the License. |
| 17 | --> |
| 18 | |
| 19 | <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes"> |
| 20 | <title>gr-button</title> |
Ole Rehmsen | 6290935 | 2019-05-16 16:10:33 +0200 | [diff] [blame] | 21 | <script src="/test/common-test-setup.js"></script> |
| 22 | <script src="/bower_components/webcomponentsjs/custom-elements-es5-adapter.js"></script> |
Viktar Donich | d828629 | 2016-11-11 16:33:03 -0800 | [diff] [blame] | 23 | |
Ole Rehmsen | ecf0b78 | 2019-05-16 11:29:39 +0200 | [diff] [blame] | 24 | <script src="/bower_components/webcomponentsjs/webcomponents-lite.js"></script> |
Ole Rehmsen | 3164074 | 2019-05-16 11:24:47 +0200 | [diff] [blame] | 25 | <script src="/bower_components/web-component-tester/browser.js"></script> |
Mike Samuel | e07c4b2 | 2017-06-02 13:08:19 -0400 | [diff] [blame] | 26 | <link rel="import" href="../../../test/common-test-setup.html"/> |
Viktar Donich | d828629 | 2016-11-11 16:33:03 -0800 | [diff] [blame] | 27 | <link rel="import" href="gr-button.html"> |
| 28 | |
Viktar Donich | 29e1ce5 | 2017-03-28 17:02:44 -0700 | [diff] [blame] | 29 | <script>void(0);</script> |
| 30 | |
Viktar Donich | d828629 | 2016-11-11 16:33:03 -0800 | [diff] [blame] | 31 | <test-fixture id="basic"> |
| 32 | <template> |
| 33 | <gr-button></gr-button> |
| 34 | </template> |
| 35 | </test-fixture> |
| 36 | |
| 37 | <script> |
Kasper Nilsson | d41ecff | 2017-05-15 16:10:56 -0700 | [diff] [blame] | 38 | suite('gr-select tests', () => { |
| 39 | let element; |
| 40 | let sandbox; |
Viktar Donich | d828629 | 2016-11-11 16:33:03 -0800 | [diff] [blame] | 41 | |
Kasper Nilsson | d41ecff | 2017-05-15 16:10:56 -0700 | [diff] [blame] | 42 | const addSpyOn = function(eventName) { |
| 43 | const spy = sandbox.spy(); |
Milutin Kristofic | 081f670 | 2019-10-15 15:51:38 +0200 | [diff] [blame] | 44 | if (eventName == 'tap') { |
| 45 | Polymer.Gestures.addListener(element, eventName, spy); |
| 46 | } else { |
| 47 | element.addEventListener(eventName, spy); |
| 48 | } |
Viktar Donich | d828629 | 2016-11-11 16:33:03 -0800 | [diff] [blame] | 49 | return spy; |
| 50 | }; |
| 51 | |
Kasper Nilsson | d41ecff | 2017-05-15 16:10:56 -0700 | [diff] [blame] | 52 | setup(() => { |
Viktar Donich | d828629 | 2016-11-11 16:33:03 -0800 | [diff] [blame] | 53 | element = fixture('basic'); |
| 54 | sandbox = sinon.sandbox.create(); |
| 55 | }); |
| 56 | |
Kasper Nilsson | d41ecff | 2017-05-15 16:10:56 -0700 | [diff] [blame] | 57 | teardown(() => { |
Viktar Donich | d828629 | 2016-11-11 16:33:03 -0800 | [diff] [blame] | 58 | sandbox.restore(); |
| 59 | }); |
| 60 | |
Becky Siegel | e662f67 | 2017-11-03 12:22:24 -0700 | [diff] [blame] | 61 | test('disabled is set by disabled or loading', () => { |
| 62 | assert.isFalse(element.$$('paper-button').disabled); |
| 63 | element.disabled = true; |
| 64 | assert.isTrue(element.$$('paper-button').disabled); |
| 65 | element.disabled = false; |
| 66 | assert.isFalse(element.$$('paper-button').disabled); |
| 67 | element.loading = true; |
| 68 | assert.isTrue(element.$$('paper-button').disabled); |
| 69 | }); |
| 70 | |
Tao Zhou | 59e60c9 | 2019-11-21 15:12:17 -0800 | [diff] [blame] | 71 | test('tabindex should be -1 if disabled', () => { |
| 72 | element.disabled = true; |
| 73 | assert.isTrue(element.getAttribute('tabindex') === '-1'); |
| 74 | }); |
| 75 | |
| 76 | // Regression tests for Issue: 11969 |
| 77 | test('tabindex should be reset to 0 if enabled', () => { |
| 78 | element.disabled = false; |
| 79 | assert.isTrue(element.getAttribute('tabindex') === '0'); |
| 80 | element.disabled = true; |
| 81 | assert.isTrue(element.getAttribute('tabindex') === '-1'); |
| 82 | element.disabled = false; |
| 83 | assert.isTrue(element.getAttribute('tabindex') === '0'); |
| 84 | }); |
| 85 | |
Milutin Kristofic | 081f670 | 2019-10-15 15:51:38 +0200 | [diff] [blame] | 86 | // 'tap' event is tested so we don't loose backward compatibility with older |
| 87 | // plugins who didn't move to on-click which is faster and well supported. |
Kasper Nilsson | d41ecff | 2017-05-15 16:10:56 -0700 | [diff] [blame] | 88 | for (const eventName of ['tap', 'click']) { |
| 89 | test('dispatches ' + eventName + ' event', () => { |
| 90 | const spy = addSpyOn(eventName); |
Viktar Donich | d828629 | 2016-11-11 16:33:03 -0800 | [diff] [blame] | 91 | MockInteractions.tap(element); |
| 92 | assert.isTrue(spy.calledOnce); |
| 93 | }); |
Kasper Nilsson | d41ecff | 2017-05-15 16:10:56 -0700 | [diff] [blame] | 94 | } |
Viktar Donich | d828629 | 2016-11-11 16:33:03 -0800 | [diff] [blame] | 95 | |
| 96 | // Keycodes: 32 for Space, 13 for Enter. |
Kasper Nilsson | d41ecff | 2017-05-15 16:10:56 -0700 | [diff] [blame] | 97 | for (const key of [32, 13]) { |
Milutin Kristofic | 081f670 | 2019-10-15 15:51:38 +0200 | [diff] [blame] | 98 | test('dispatches click event on keycode ' + key, () => { |
Kasper Nilsson | d41ecff | 2017-05-15 16:10:56 -0700 | [diff] [blame] | 99 | const tapSpy = sandbox.spy(); |
Milutin Kristofic | 081f670 | 2019-10-15 15:51:38 +0200 | [diff] [blame] | 100 | element.addEventListener('click', tapSpy); |
Viktar Donich | d828629 | 2016-11-11 16:33:03 -0800 | [diff] [blame] | 101 | MockInteractions.pressAndReleaseKeyOn(element, key); |
| 102 | assert.isTrue(tapSpy.calledOnce); |
Kasper Nilsson | d41ecff | 2017-05-15 16:10:56 -0700 | [diff] [blame] | 103 | }); |
Kasper Nilsson | 2de6749 | 2017-10-24 11:03:07 -0700 | [diff] [blame] | 104 | |
Milutin Kristofic | 081f670 | 2019-10-15 15:51:38 +0200 | [diff] [blame] | 105 | test('dispatches no click event with modifier on keycode ' + key, () => { |
Kasper Nilsson | 2de6749 | 2017-10-24 11:03:07 -0700 | [diff] [blame] | 106 | const tapSpy = sandbox.spy(); |
Milutin Kristofic | 081f670 | 2019-10-15 15:51:38 +0200 | [diff] [blame] | 107 | element.addEventListener('click', tapSpy); |
Kasper Nilsson | 2de6749 | 2017-10-24 11:03:07 -0700 | [diff] [blame] | 108 | MockInteractions.pressAndReleaseKeyOn(element, key, 'shift'); |
| 109 | MockInteractions.pressAndReleaseKeyOn(element, key, 'ctrl'); |
| 110 | MockInteractions.pressAndReleaseKeyOn(element, key, 'meta'); |
| 111 | MockInteractions.pressAndReleaseKeyOn(element, key, 'alt'); |
| 112 | assert.isFalse(tapSpy.calledOnce); |
| 113 | }); |
Kasper Nilsson | d41ecff | 2017-05-15 16:10:56 -0700 | [diff] [blame] | 114 | } |
Viktar Donich | d828629 | 2016-11-11 16:33:03 -0800 | [diff] [blame] | 115 | |
Kasper Nilsson | d41ecff | 2017-05-15 16:10:56 -0700 | [diff] [blame] | 116 | suite('disabled', () => { |
| 117 | setup(() => { |
Viktar Donich | d828629 | 2016-11-11 16:33:03 -0800 | [diff] [blame] | 118 | element.disabled = true; |
| 119 | }); |
| 120 | |
Kasper Nilsson | d41ecff | 2017-05-15 16:10:56 -0700 | [diff] [blame] | 121 | for (const eventName of ['tap', 'click']) { |
| 122 | test('stops ' + eventName + ' event', () => { |
| 123 | const spy = addSpyOn(eventName); |
Viktar Donich | d828629 | 2016-11-11 16:33:03 -0800 | [diff] [blame] | 124 | MockInteractions.tap(element); |
| 125 | assert.isFalse(spy.called); |
| 126 | }); |
Kasper Nilsson | d41ecff | 2017-05-15 16:10:56 -0700 | [diff] [blame] | 127 | } |
Viktar Donich | d828629 | 2016-11-11 16:33:03 -0800 | [diff] [blame] | 128 | |
| 129 | // Keycodes: 32 for Space, 13 for Enter. |
Kasper Nilsson | d41ecff | 2017-05-15 16:10:56 -0700 | [diff] [blame] | 130 | for (const key of [32, 13]) { |
Milutin Kristofic | 081f670 | 2019-10-15 15:51:38 +0200 | [diff] [blame] | 131 | test('stops click event on keycode ' + key, () => { |
Kasper Nilsson | d41ecff | 2017-05-15 16:10:56 -0700 | [diff] [blame] | 132 | const tapSpy = sandbox.spy(); |
Milutin Kristofic | 081f670 | 2019-10-15 15:51:38 +0200 | [diff] [blame] | 133 | element.addEventListener('click', tapSpy); |
Viktar Donich | d828629 | 2016-11-11 16:33:03 -0800 | [diff] [blame] | 134 | MockInteractions.pressAndReleaseKeyOn(element, key); |
| 135 | assert.isFalse(tapSpy.called); |
Kasper Nilsson | d41ecff | 2017-05-15 16:10:56 -0700 | [diff] [blame] | 136 | }); |
| 137 | } |
Viktar Donich | d828629 | 2016-11-11 16:33:03 -0800 | [diff] [blame] | 138 | }); |
| 139 | }); |
| 140 | </script> |