blob: ef593c02eed78d040cce24563d0c6f86c7b746c1 [file] [log] [blame]
Viktar Donichd8286292016-11-11 16:33:03 -08001<!DOCTYPE html>
2<!--
Dave Borowitz8cdc76b2018-03-26 10:04:27 -04003@license
Viktar Donichd8286292016-11-11 16:33:03 -08004Copyright (C) 2016 The Android Open Source Project
5
6Licensed under the Apache License, Version 2.0 (the "License");
7you may not use this file except in compliance with the License.
8You may obtain a copy of the License at
9
10http://www.apache.org/licenses/LICENSE-2.0
11
12Unless required by applicable law or agreed to in writing, software
13distributed under the License is distributed on an "AS IS" BASIS,
14WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15See the License for the specific language governing permissions and
16limitations 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 Rehmsen62909352019-05-16 16:10:33 +020021<script src="/test/common-test-setup.js"></script>
22<script src="/bower_components/webcomponentsjs/custom-elements-es5-adapter.js"></script>
Viktar Donichd8286292016-11-11 16:33:03 -080023
Ole Rehmsenecf0b782019-05-16 11:29:39 +020024<script src="/bower_components/webcomponentsjs/webcomponents-lite.js"></script>
Ole Rehmsen31640742019-05-16 11:24:47 +020025<script src="/bower_components/web-component-tester/browser.js"></script>
Mike Samuele07c4b22017-06-02 13:08:19 -040026<link rel="import" href="../../../test/common-test-setup.html"/>
Viktar Donichd8286292016-11-11 16:33:03 -080027<link rel="import" href="gr-button.html">
28
Viktar Donich29e1ce52017-03-28 17:02:44 -070029<script>void(0);</script>
30
Viktar Donichd8286292016-11-11 16:33:03 -080031<test-fixture id="basic">
32 <template>
33 <gr-button></gr-button>
34 </template>
35</test-fixture>
36
37<script>
Kasper Nilssond41ecff2017-05-15 16:10:56 -070038 suite('gr-select tests', () => {
39 let element;
40 let sandbox;
Viktar Donichd8286292016-11-11 16:33:03 -080041
Kasper Nilssond41ecff2017-05-15 16:10:56 -070042 const addSpyOn = function(eventName) {
43 const spy = sandbox.spy();
Milutin Kristofic081f6702019-10-15 15:51:38 +020044 if (eventName == 'tap') {
45 Polymer.Gestures.addListener(element, eventName, spy);
46 } else {
47 element.addEventListener(eventName, spy);
48 }
Viktar Donichd8286292016-11-11 16:33:03 -080049 return spy;
50 };
51
Kasper Nilssond41ecff2017-05-15 16:10:56 -070052 setup(() => {
Viktar Donichd8286292016-11-11 16:33:03 -080053 element = fixture('basic');
54 sandbox = sinon.sandbox.create();
55 });
56
Kasper Nilssond41ecff2017-05-15 16:10:56 -070057 teardown(() => {
Viktar Donichd8286292016-11-11 16:33:03 -080058 sandbox.restore();
59 });
60
Becky Siegele662f672017-11-03 12:22:24 -070061 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 Zhou59e60c92019-11-21 15:12:17 -080071 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 Kristofic081f6702019-10-15 15:51:38 +020086 // '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 Nilssond41ecff2017-05-15 16:10:56 -070088 for (const eventName of ['tap', 'click']) {
89 test('dispatches ' + eventName + ' event', () => {
90 const spy = addSpyOn(eventName);
Viktar Donichd8286292016-11-11 16:33:03 -080091 MockInteractions.tap(element);
92 assert.isTrue(spy.calledOnce);
93 });
Kasper Nilssond41ecff2017-05-15 16:10:56 -070094 }
Viktar Donichd8286292016-11-11 16:33:03 -080095
96 // Keycodes: 32 for Space, 13 for Enter.
Kasper Nilssond41ecff2017-05-15 16:10:56 -070097 for (const key of [32, 13]) {
Milutin Kristofic081f6702019-10-15 15:51:38 +020098 test('dispatches click event on keycode ' + key, () => {
Kasper Nilssond41ecff2017-05-15 16:10:56 -070099 const tapSpy = sandbox.spy();
Milutin Kristofic081f6702019-10-15 15:51:38 +0200100 element.addEventListener('click', tapSpy);
Viktar Donichd8286292016-11-11 16:33:03 -0800101 MockInteractions.pressAndReleaseKeyOn(element, key);
102 assert.isTrue(tapSpy.calledOnce);
Kasper Nilssond41ecff2017-05-15 16:10:56 -0700103 });
Kasper Nilsson2de67492017-10-24 11:03:07 -0700104
Milutin Kristofic081f6702019-10-15 15:51:38 +0200105 test('dispatches no click event with modifier on keycode ' + key, () => {
Kasper Nilsson2de67492017-10-24 11:03:07 -0700106 const tapSpy = sandbox.spy();
Milutin Kristofic081f6702019-10-15 15:51:38 +0200107 element.addEventListener('click', tapSpy);
Kasper Nilsson2de67492017-10-24 11:03:07 -0700108 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 Nilssond41ecff2017-05-15 16:10:56 -0700114 }
Viktar Donichd8286292016-11-11 16:33:03 -0800115
Kasper Nilssond41ecff2017-05-15 16:10:56 -0700116 suite('disabled', () => {
117 setup(() => {
Viktar Donichd8286292016-11-11 16:33:03 -0800118 element.disabled = true;
119 });
120
Kasper Nilssond41ecff2017-05-15 16:10:56 -0700121 for (const eventName of ['tap', 'click']) {
122 test('stops ' + eventName + ' event', () => {
123 const spy = addSpyOn(eventName);
Viktar Donichd8286292016-11-11 16:33:03 -0800124 MockInteractions.tap(element);
125 assert.isFalse(spy.called);
126 });
Kasper Nilssond41ecff2017-05-15 16:10:56 -0700127 }
Viktar Donichd8286292016-11-11 16:33:03 -0800128
129 // Keycodes: 32 for Space, 13 for Enter.
Kasper Nilssond41ecff2017-05-15 16:10:56 -0700130 for (const key of [32, 13]) {
Milutin Kristofic081f6702019-10-15 15:51:38 +0200131 test('stops click event on keycode ' + key, () => {
Kasper Nilssond41ecff2017-05-15 16:10:56 -0700132 const tapSpy = sandbox.spy();
Milutin Kristofic081f6702019-10-15 15:51:38 +0200133 element.addEventListener('click', tapSpy);
Viktar Donichd8286292016-11-11 16:33:03 -0800134 MockInteractions.pressAndReleaseKeyOn(element, key);
135 assert.isFalse(tapSpy.called);
Kasper Nilssond41ecff2017-05-15 16:10:56 -0700136 });
137 }
Viktar Donichd8286292016-11-11 16:33:03 -0800138 });
139 });
140</script>