Blame view

bower_components/iron-dropdown/test/iron-dropdown.html 13.2 KB
73bcce88   luigser   COMPONENTS
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
  <!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>
    <meta charset="UTF-8">
    <title>iron-dropdown basic tests</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
  
    <script src="../../webcomponentsjs/webcomponents-lite.js"></script>
    <script src="../../web-component-tester/browser.js"></script>
    <script src="../../test-fixture/test-fixture-mocha.js"></script>
    <script src="../../iron-test-helpers/mock-interactions.js"></script>
  
    <link rel="import" href="../iron-dropdown.html">
    <link rel="import" href="../../test-fixture/test-fixture.html">
  
  </head>
a1a3bc73   Luigi Serra   graphs updates
26
27
28
29
30
31
  <style>
    body {
      margin: 0;
      padding: 0;
    }
  </style>
73bcce88   luigser   COMPONENTS
32
33
34
35
36
37
38
39
40
41
  <body>
  
    <test-fixture id="TrivialDropdown">
      <template>
        <iron-dropdown>
          <div class="dropdown-content">Hello!</div>
        </iron-dropdown>
      </template>
    </test-fixture>
  
e619a3b0   Luigi Serra   Controllet cross ...
42
43
    <test-fixture id="NonLockingDropdown">
      <template>
a1a3bc73   Luigi Serra   graphs updates
44
45
        <iron-dropdown allow-outside-scroll>
          <div class="dropdown-content">I don't lock scroll!</div>
e619a3b0   Luigi Serra   Controllet cross ...
46
47
48
49
        </iron-dropdown>
      </template>
    </test-fixture>
  
73bcce88   luigser   COMPONENTS
50
51
52
53
54
55
56
57
58
59
    <test-fixture id="AlignedDropdown">
      <template>
        <div style="display: block; position: relative; width: 100px; height: 100px;">
          <iron-dropdown horizontal-align="right" vertical-align="top">
            <div class="dropdown-content">Hello!</div>
          </iron-dropdown>
        </div>
      </template>
    </test-fixture>
  
a1a3bc73   Luigi Serra   graphs updates
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    <!-- Absolutely position the dropdown so that it has enough space to move around -->
    <test-fixture id="OffsetDropdownTopLeft">
      <template>
        <div style="display: block; position: absolute; top: 40px; left: 40px; width: 100px; height: 100px;">
          <iron-dropdown>
            <div class="dropdown-content">Hello!</div>
          </iron-dropdown>
        </div>
      </template>
    </test-fixture>
  
    <test-fixture id="OffsetDropdownBottomRight">
      <template>
        <div style="display: block; position: absolute; top: 40px; left: 40px; width: 100px; height: 100px;">
          <iron-dropdown horizontal-align="right" vertical-align="bottom">
            <div class="dropdown-content">Hello!</div>
          </iron-dropdown>
        </div>
      </template>
    </test-fixture>
  
73bcce88   luigser   COMPONENTS
81
82
83
84
85
86
87
88
89
90
    <test-fixture id="FocusableContentDropdown">
      <template>
        <iron-dropdown>
          <div class="dropdown-content" tabindex="0">
            <div class="subcontent" tabindex="0"></div>
          </div>
        </iron-dropdown>
      </template>
    </test-fixture>
  
a1a3bc73   Luigi Serra   graphs updates
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
    <test-fixture id="RTLDropdownLeft">
      <template>
        <div dir="rtl" style="display: block; position: relative; width: 100px; height: 100px;">
          <iron-dropdown>
            <div class="dropdown-content">Hello!</div>
          </iron-dropdown>
        </div>
      </template>
    </test-fixture>
  
    <test-fixture id="RTLDropdownRight">
      <template>
        <div dir="rtl" style="display: block; position: relative; width: 100px; height: 100px;">
          <iron-dropdown horizontal-align="right">
            <div class="dropdown-content">Hello!</div>
          </iron-dropdown>
        </div>
      </template>
    </test-fixture>
  
73bcce88   luigser   COMPONENTS
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
    <script>
      function elementIsVisible(element) {
        var contentRect = element.getBoundingClientRect();
        var computedStyle = window.getComputedStyle(element);
  
        return computedStyle.display !== 'none' &&
          contentRect.width > 0 &&
          contentRect.height > 0;
      }
  
      suite('<iron-dropdown>', function() {
        var dropdown;
        var content;
  
        suite('basic', function() {
          setup(function() {
            dropdown = fixture('TrivialDropdown');
            content = Polymer.dom(dropdown).querySelector('.dropdown-content');
          });
  
          test('effectively hides the dropdown content', function() {
            expect(elementIsVisible(content)).to.be.equal(false);
          });
  
          test('shows dropdown content when opened', function(done) {
            dropdown.open();
  
            Polymer.Base.async(function() {
              expect(elementIsVisible(content)).to.be.equal(true);
              done();
            });
          });
  
          test('hides dropdown content when outside is clicked', function(done) {
            dropdown.open();
  
            Polymer.Base.async(function() {
              expect(elementIsVisible(content)).to.be.equal(true);
  
a1a3bc73   Luigi Serra   graphs updates
150
151
152
153
154
155
              // The document capture-click listeners are set async.
              // Note(noms): I think this bit in iron-overlay-behavior is pretty
              // brittle, so if the tests start failing in the future, make sure
              // _toggleListeners is getting called at the right time.
              Polymer.Base.async(function() {
                MockInteractions.tap(dropdown.parentNode);
73bcce88   luigser   COMPONENTS
156
157
158
159
                Polymer.Base.async(function() {
                  expect(elementIsVisible(content)).to.be.equal(false);
                  done();
                }, 100);
a1a3bc73   Luigi Serra   graphs updates
160
              }, 1);
73bcce88   luigser   COMPONENTS
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
            });
          });
  
          suite('when content is focusable', function() {
            setup(function() {
              dropdown = fixture('FocusableContentDropdown');
              content = Polymer.dom(dropdown).querySelector('.dropdown-content');
            });
            test('focuses the content when opened', function(done) {
              dropdown.open();
  
              Polymer.Base.async(function() {
                expect(document.activeElement).to.be.equal(content);
                done();
              });
            });
  
            test('focuses a configured focus target', function(done) {
              var focusableChild = Polymer.dom(content).querySelector('div[tabindex]');
              dropdown.focusTarget = focusableChild;
  
              dropdown.open();
  
              Polymer.Base.async(function() {
                expect(document.activeElement).to.not.be.equal(content);
                expect(document.activeElement).to.be.equal(focusableChild);
                done();
              });
            });
          });
        });
  
e619a3b0   Luigi Serra   Controllet cross ...
193
194
195
196
197
198
199
        suite('locking scroll', function() {
          var dropdown;
  
          setup(function() {
            dropdown = fixture('NonLockingDropdown');
          });
  
a1a3bc73   Luigi Serra   graphs updates
200
          test('can be disabled with `allowOutsideScroll`', function(done) {
e619a3b0   Luigi Serra   Controllet cross ...
201
202
            dropdown.open();
  
a1a3bc73   Luigi Serra   graphs updates
203
204
205
206
207
            Polymer.Base.async(function() {
              expect(Polymer.IronDropdownScrollManager.elementIsScrollLocked(document.body))
                .to.be.equal(false);
              done();
            });
e619a3b0   Luigi Serra   Controllet cross ...
208
209
210
          });
        });
  
73bcce88   luigser   COMPONENTS
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
244
245
246
247
248
249
250
251
252
253
        suite('aligned dropdown', function() {
          var parent;
          setup(function() {
            parent = fixture('AlignedDropdown');
            dropdown = parent.querySelector('iron-dropdown');
          });
  
          test('can be re-aligned to the right and the top', function(done) {
            var parentRect;
            var dropdownRect;
  
            dropdown.opened = true;
  
            Polymer.Base.async(function() {
              dropdownRect = dropdown.getBoundingClientRect();
              parentRect = parent.getBoundingClientRect();
  
              // NOTE(cdata): IE10 / 11 have minor rounding errors in this case,
              // so we assert with `closeTo` and a tight threshold:
              expect(dropdownRect.top).to.be.closeTo(parentRect.top, 0.1);
              expect(dropdownRect.right).to.be.closeTo(parentRect.right, 0.1);
              done();
            }, 1);
          });
  
          test('can be re-aligned to the bottom', function(done) {
            var parentRect;
            var dropdownRect;
  
            dropdown.verticalAlign = 'bottom';
            dropdown.opened = true;
  
            Polymer.Base.async(function() {
              parentRect = parent.getBoundingClientRect();
              dropdownRect = dropdown.getBoundingClientRect();
  
              // NOTE(cdata): IE10 / 11 have minor rounding errors in this case,
              // so we assert with `closeTo` and a tight threshold:
              expect(dropdownRect.bottom).to.be.closeTo(parentRect.bottom, 0.1);
              expect(dropdownRect.right).to.be.closeTo(parentRect.right, 0.1);
              done();
            }, 1);
          });
a1a3bc73   Luigi Serra   graphs updates
254
        });
73bcce88   luigser   COMPONENTS
255
  
a1a3bc73   Luigi Serra   graphs updates
256
257
258
259
260
261
262
263
        suite('when align is left/top, with an offset', function() {
          var dropdownRect;
          var offsetDropdownRect;
          var dropdown;
          setup(function() {
            var parent = fixture('OffsetDropdownTopLeft');
            dropdown = parent.querySelector('iron-dropdown');
          });
73bcce88   luigser   COMPONENTS
264
  
a1a3bc73   Luigi Serra   graphs updates
265
266
          test('can be offset towards the bottom right', function(done) {
            dropdown.opened = true;
73bcce88   luigser   COMPONENTS
267
  
a1a3bc73   Luigi Serra   graphs updates
268
269
            Polymer.Base.async(function() {
              dropdownRect = dropdown.getBoundingClientRect();
73bcce88   luigser   COMPONENTS
270
  
a1a3bc73   Luigi Serra   graphs updates
271
272
273
              dropdown.verticalOffset = 10;
              dropdown.horizontalOffset = 10;
              offsetDropdownRect = dropdown.getBoundingClientRect();
73bcce88   luigser   COMPONENTS
274
  
a1a3bc73   Luigi Serra   graphs updates
275
276
277
278
279
280
281
              // verticalAlign is top, so a positive offset moves down.
              expect(dropdownRect.top + 10).to.be.closeTo(offsetDropdownRect.top, 0.1);
              // horizontalAlign is left, so a positive offset moves to the right.
              expect(dropdownRect.left + 10).to.be.closeTo(offsetDropdownRect.left, 0.1);
              done();
            }, 1);
          });
73bcce88   luigser   COMPONENTS
282
  
a1a3bc73   Luigi Serra   graphs updates
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
          test('can be offset towards the top left', function(done) {
            dropdown.opened = true;
  
            Polymer.Base.async(function() {
              dropdownRect = dropdown.getBoundingClientRect();
  
              dropdown.verticalOffset = -10;
              dropdown.horizontalOffset = -10;
              offsetDropdownRect = dropdown.getBoundingClientRect();
  
              // verticalAlign is top, so a negative offset moves up.
              expect(dropdownRect.top - 10).to.be.closeTo(offsetDropdownRect.top, 0.1);
              // horizontalAlign is left, so a negative offset moves to the left.
              expect(dropdownRect.left - 10).to.be.closeTo(offsetDropdownRect.left, 0.1);
              done();
            }, 1);
          });
        });
  
        suite('when align is right/bottom, with an offset', function() {
          var dropdownRect;
          var offsetDropdownRect;
          var dropdown;
          setup(function() {
            var parent = fixture('OffsetDropdownBottomRight');
            dropdown = parent.querySelector('iron-dropdown');
          });
  
          test('can be offset towards the top left', function(done) {
            dropdown.opened = true;
  
            Polymer.Base.async(function() {
              dropdownRect = dropdown.getBoundingClientRect();
  
              dropdown.verticalOffset = 10;
              dropdown.horizontalOffset = 10;
              offsetDropdownRect = dropdown.getBoundingClientRect();
  
              // verticalAlign is bottom, so a positive offset moves up.
              expect(dropdownRect.bottom - 10).to.be.closeTo(offsetDropdownRect.bottom, 0.1);
              // horizontalAlign is right, so a positive offset moves to the left.
              expect(dropdownRect.right - 10).to.be.closeTo(offsetDropdownRect.right, 0.1);
              done();
            }, 1);
          });
  
          test('can be offset towards the bottom right', function(done) {
            dropdown.opened = true;
  
            Polymer.Base.async(function() {
              dropdownRect = dropdown.getBoundingClientRect();
  
              dropdown.verticalOffset = -10;
              dropdown.horizontalOffset = -10;
              offsetDropdownRect = dropdown.getBoundingClientRect();
  
              // verticalAlign is bottom, so a negative offset moves down.
              expect(dropdownRect.bottom + 10).to.be.closeTo(offsetDropdownRect.bottom, 0.1);
              // horizontalAlign is right, so a positive offset moves to the right.
              expect(dropdownRect.right + 10).to.be.closeTo(offsetDropdownRect.right, 0.1);
              done();
            }, 1);
          });
        });
  
        suite('RTL', function() {
          var dropdown;
          var dropdownRect;
  
          test('with horizontalAlign=left', function(done) {
            var parent = fixture('RTLDropdownLeft');
            dropdown = parent.querySelector('iron-dropdown');
            dropdown.open();
  
            Polymer.Base.async(function() {
              // In RTL, if `horizontalAlign` is "left", that's the same as
              // being right-aligned in LTR. So the dropdown should be in the top
              // right corner.
              dropdownRect = dropdown.getBoundingClientRect();
              expect(dropdownRect.top).to.be.equal(0);
              expect(dropdownRect.right).to.be.equal(100);
              done();
73bcce88   luigser   COMPONENTS
365
366
367
            });
          });
  
a1a3bc73   Luigi Serra   graphs updates
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
          test('with horizontalAlign=right', function(done) {
            var parent = fixture('RTLDropdownRight');
            dropdown = parent.querySelector('iron-dropdown');
            dropdown.open();
  
            Polymer.Base.async(function() {
              // In RTL, if `horizontalAlign` is "right", that's the same as
              // being left-aligned in LTR. So the dropdown should be in the top
              // left corner.
              dropdownRect = dropdown.getBoundingClientRect();
              expect(dropdownRect.top).to.be.equal(0);
              expect(dropdownRect.left).to.be.equal(0);
              done();
            });
          });
73bcce88   luigser   COMPONENTS
383
384
385
386
387
        });
      });
    </script>
  </body>
  </html>