blob: ed0da2e4ca866885f358cb1cc8d91bb8b0da84a9 [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>
21
Viktar Donich29e1ce52017-03-28 17:02:44 -070022<script src="../../../bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
Viktar Donichd8286292016-11-11 16:33:03 -080023<script src="../../../bower_components/web-component-tester/browser.js"></script>
Mike Samuele07c4b22017-06-02 13:08:19 -040024<link rel="import" href="../../../test/common-test-setup.html"/>
Viktar Donichd8286292016-11-11 16:33:03 -080025<link rel="import" href="gr-button.html">
26
Viktar Donich29e1ce52017-03-28 17:02:44 -070027<script>void(0);</script>
28
Viktar Donichd8286292016-11-11 16:33:03 -080029<test-fixture id="basic">
30 <template>
31 <gr-button></gr-button>
32 </template>
33</test-fixture>
34
35<script>
Kasper Nilssond41ecff2017-05-15 16:10:56 -070036 suite('gr-select tests', () => {
37 let element;
38 let sandbox;
Viktar Donichd8286292016-11-11 16:33:03 -080039
Kasper Nilssond41ecff2017-05-15 16:10:56 -070040 const addSpyOn = function(eventName) {
41 const spy = sandbox.spy();
Viktar Donichd8286292016-11-11 16:33:03 -080042 element.addEventListener(eventName, spy);
43 return spy;
44 };
45
Kasper Nilssond41ecff2017-05-15 16:10:56 -070046 setup(() => {
Viktar Donichd8286292016-11-11 16:33:03 -080047 element = fixture('basic');
48 sandbox = sinon.sandbox.create();
49 });
50
Kasper Nilssond41ecff2017-05-15 16:10:56 -070051 teardown(() => {
Viktar Donichd8286292016-11-11 16:33:03 -080052 sandbox.restore();
53 });
54
Becky Siegele662f672017-11-03 12:22:24 -070055 test('disabled is set by disabled or loading', () => {
56 assert.isFalse(element.$$('paper-button').disabled);
57 element.disabled = true;
58 assert.isTrue(element.$$('paper-button').disabled);
59 element.disabled = false;
60 assert.isFalse(element.$$('paper-button').disabled);
61 element.loading = true;
62 assert.isTrue(element.$$('paper-button').disabled);
63 });
64
Kasper Nilssond41ecff2017-05-15 16:10:56 -070065 for (const eventName of ['tap', 'click']) {
66 test('dispatches ' + eventName + ' event', () => {
67 const spy = addSpyOn(eventName);
Viktar Donichd8286292016-11-11 16:33:03 -080068 MockInteractions.tap(element);
69 assert.isTrue(spy.calledOnce);
70 });
Kasper Nilssond41ecff2017-05-15 16:10:56 -070071 }
Viktar Donichd8286292016-11-11 16:33:03 -080072
73 // Keycodes: 32 for Space, 13 for Enter.
Kasper Nilssond41ecff2017-05-15 16:10:56 -070074 for (const key of [32, 13]) {
75 test('dispatches tap event on keycode ' + key, () => {
76 const tapSpy = sandbox.spy();
Viktar Donichd8286292016-11-11 16:33:03 -080077 element.addEventListener('tap', tapSpy);
78 MockInteractions.pressAndReleaseKeyOn(element, key);
79 assert.isTrue(tapSpy.calledOnce);
Kasper Nilssond41ecff2017-05-15 16:10:56 -070080 });
Kasper Nilsson2de67492017-10-24 11:03:07 -070081
82 test('dispatches no tap event with modifier on keycode ' + key, () => {
83 const tapSpy = sandbox.spy();
84 element.addEventListener('tap', tapSpy);
85 MockInteractions.pressAndReleaseKeyOn(element, key, 'shift');
86 MockInteractions.pressAndReleaseKeyOn(element, key, 'ctrl');
87 MockInteractions.pressAndReleaseKeyOn(element, key, 'meta');
88 MockInteractions.pressAndReleaseKeyOn(element, key, 'alt');
89 assert.isFalse(tapSpy.calledOnce);
90 });
Kasper Nilssond41ecff2017-05-15 16:10:56 -070091 }
Viktar Donichd8286292016-11-11 16:33:03 -080092
Kasper Nilssond41ecff2017-05-15 16:10:56 -070093 suite('disabled', () => {
94 setup(() => {
Viktar Donichd8286292016-11-11 16:33:03 -080095 element.disabled = true;
96 });
97
Kasper Nilssond41ecff2017-05-15 16:10:56 -070098 for (const eventName of ['tap', 'click']) {
99 test('stops ' + eventName + ' event', () => {
100 const spy = addSpyOn(eventName);
Viktar Donichd8286292016-11-11 16:33:03 -0800101 MockInteractions.tap(element);
102 assert.isFalse(spy.called);
103 });
Kasper Nilssond41ecff2017-05-15 16:10:56 -0700104 }
Viktar Donichd8286292016-11-11 16:33:03 -0800105
106 // Keycodes: 32 for Space, 13 for Enter.
Kasper Nilssond41ecff2017-05-15 16:10:56 -0700107 for (const key of [32, 13]) {
108 test('stops tap event on keycode ' + key, () => {
109 const tapSpy = sandbox.spy();
Viktar Donichd8286292016-11-11 16:33:03 -0800110 element.addEventListener('tap', tapSpy);
111 MockInteractions.pressAndReleaseKeyOn(element, key);
112 assert.isFalse(tapSpy.called);
Kasper Nilssond41ecff2017-05-15 16:10:56 -0700113 });
114 }
Viktar Donichd8286292016-11-11 16:33:03 -0800115 });
116 });
117</script>