aboutsummaryrefslogtreecommitdiff
path: root/catapult/third_party/polymer/components/paper-toast/test/basic.html
blob: 154015d78374c932e352f560f29e9ebd24533a19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<!doctype html>
<!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->

<html>

<head>

  <title>paper-toast-basic</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <script src="../../webcomponentsjs/webcomponents-lite.js"></script>
  <script src="../../web-component-tester/browser.js"></script>

  <link rel="import" href="../paper-toast.html">

  <style>
    body {
      margin: 0;
      padding: 0;
    }
  </style>
</head>

<body>

  <test-fixture id="basic">
    <template>
      <paper-toast></paper-toast>
    </template>
  </test-fixture>

  <test-fixture id="show">
    <template>
      <paper-toast opened></paper-toast>
    </template>
  </test-fixture>

  <test-fixture id="contained">
    <template>
      <paper-toast class="fit-bottom"></paper-toast>
      <div style="margin: 50px; width: 100px; height: 100px; background-color: orange;"></div>
    </template>
  </test-fixture>

  <script>

    suite('basic', function() {

      var toast;

      test('is hidden', function() {
        toast = fixture('basic');
        assert.isFalse(toast.opened, '`opened` is false');
      });

      test('is visible', function() {
        toast = fixture('show');
        assert.isTrue(toast.opened, '`opened` is true');
      });

      test('show() will open toast', function() {
        toast = fixture('basic');
        toast.show();
        assert.isTrue(toast.opened, '`opened` is true');
      });

      test('hide() will close toast', function() {
        toast = fixture('show');
        toast.hide();
        assert.isFalse(toast.opened, '`opened` is false');
      });

      test('toast auto-close after 10ms', function(done) {
        toast = fixture('basic');
        toast.duration = 10;
        toast.show();
        setTimeout(function() {
          assert.isFalse(toast.opened, '`opened` is false');
          done();
        }, 12);
      });

      test('toast fires opened event', function(done) {
        toast = fixture('show');
        toast.addEventListener('iron-overlay-opened', function() {
          done();
        });
      });

      test('toast does not get focused', function(done) {
        toast = fixture('show');
        var spy = sinon.spy(toast, 'focus');
        assert.isTrue(toast.noAutoFocus, 'no-auto-focus is true');
        toast.addEventListener('iron-overlay-opened', function() {
          assert.isFalse(spy.called, 'toast is not focused');
          done();
        });
      });

      test('toast fires closed event', function(done) {
        toast = fixture('basic');
        toast.show({duration: 350});
        toast.addEventListener('iron-overlay-closed', function() {
          done();
        });
      });

      test('show() accepts valid properties', function() {
        toast = fixture('basic');
        toast.show({text: 'hello world', duration: 20});
        assert.isTrue(toast.opened, '`opened` is true');
        assert.equal(toast.text, 'hello world', '`text` is correct');
        assert.equal(toast.duration, 20, '`duration` is correct');
      });

      test('show() does not accept invalid properties', function() {
        toast = fixture('basic');
        toast.show({foo: 'bar'});
        assert.isUndefined(toast.foo, '`foo` is not a valid property and will not be set');
        assert.isTrue(toast.opened, '`opened` is true');
      });

      test('show() does not accept private properties', function() {
        toast = fixture('basic');
        var temp = toast._manager;
        toast.show({_manager: 'bar'});
        assert.equal(toast._manager, temp, '`_manager` is a private property and will not be set');
        assert.isTrue(toast.opened, '`opened` is true');
      });

      test('show() accepts a string argument as the text parameter', function() {
        toast = fixture('basic');
        toast.show('hello world 2');
        assert.equal(toast.text, 'hello world 2', '`text is correct`');
      });

      suite('disable auto-close', function() {
        var spy;
        setup(function() {
          toast = fixture('basic');
          spy = sinon.spy(toast, 'async');
        });
        test('duration = Infinity', function() {
          toast.duration = Infinity;
          toast.show();
          assert.isFalse(spy.calledWith(toast.close), '`async` was not called with `close()`');
          assert.isFalse(spy.calledWith(toast.hide), '`async` was not called with `hide()`');
        });

        test('duration = 0', function() {
          toast.duration = 0;
          toast.show();
          assert.isFalse(spy.calledWith(toast.close), '`async` was not called with `close()`');
          assert.isFalse(spy.calledWith(toast.hide), '`async` was not called with `hide()`');
        });

        test('duration = -10', function() {
          toast.duration = -10;
          toast.show();
          assert.isFalse(spy.calledWith(toast.close), '`async` was not called with `close()`');
          assert.isFalse(spy.calledWith(toast.hide), '`async` was not called with `hide()`');
        });
      });

      test('there is only 1 toast opened', function() {
        var toast1 = fixture('basic');
        var toast2 = fixture('show');
        toast2.open();
        toast1.open();
        assert.isTrue(toast1.opened, 'toast1 is opened');
        assert.isFalse(toast2.opened, 'toast2 is not opened');
        toast2.open();
        assert.isFalse(toast1.opened, 'toast1 is now not opened');
        assert.isTrue(toast2.opened, 'toast2 is now opened');
      });

      test('auto-close is correctly reset', function(done) {
        toast = fixture('basic');
        toast.duration = 10;
        toast.show();
        // a bit later (before the auto-close), toast is reset
        setTimeout(function() {
          toast.hide();
          // keep toast opened
          toast.duration = 0;
          toast.show();
          setTimeout(function() {
            assert.isTrue(toast.opened, 'toast is still open');
            done();
          }, 10);
        }, 5);
      });

      test('toast is positioned according at the bottom left of its fitInto', function(done) {
        var f = fixture('contained');
        var toast = f[0];
        var container = f[1];
        toast.fitInto = container;
        toast.open();
        // Wait for it to be opened, so it will be sized correctly.
        toast.addEventListener('iron-overlay-opened', function() {
          var rect = toast.getBoundingClientRect();
          assert.equal(rect.left, 50, 'left ok');
          // 150px from top, (100px of height + 50px of margin-top)
          assert.equal(rect.bottom, 150, 'bottom');
          done();
        });
      });

      suite('a11y', function() {
        test('show() will announce text', function() {
          toast = fixture('basic');
          var spy = sinon.spy(toast, 'fire');
          toast.text = 'announce!';
          toast.show();
          assert.isTrue(spy.calledWith('iron-announce', {
            text: 'announce!'
          }), 'text announced');
        });

        test('hide() will not announce text', function() {
          toast = fixture('show');
          var spy = sinon.spy(toast, 'fire');
          toast.hide();
          assert.isFalse(spy.calledWith('iron-announce'), 'text not announced');
        });
      });

    });
  </script>

</body>

</html>