aboutsummaryrefslogtreecommitdiff
path: root/catapult/third_party/polymer/components/iron-overlay-behavior/test/iron-overlay-behavior.html
blob: f6c3621a08ce9e01de1f704e0d136d8a3e9e8e46 (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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
<!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>iron-overlay-behavior tests</title>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1, user-scalable=yes">

    <script src="../../webcomponentsjs/webcomponents-lite.js"></script>

    <script src="../../web-component-tester/browser.js"></script>
    <link rel="import" href="../../iron-test-helpers/iron-test-helpers.html">
    <link rel="import" href="test-overlay.html">
    <link rel="import" href="test-overlay2.html">
    <link rel="import" href="test-buttons.html">
    <link rel="import" href="test-menu-button.html">

    <style is="custom-style">
      iron-overlay-backdrop {
        /* For quicker tests */
        --iron-overlay-backdrop: {
          transition: none;
        }
      }
    </style>

  </head>

  <body>

    <test-fixture id="basic">
      <template>
        <test-overlay>
          Basic Overlay
        </test-overlay>
      </template>
    </test-fixture>

    <test-fixture id="opened">
      <template>
        <test-overlay opened>
          Basic Overlay
        </test-overlay>
      </template>
    </test-fixture>

    <test-fixture id="autofocus">
      <template>
        <test-overlay>
          Autofocus
          <button autofocus>button</button>
        </test-overlay>
      </template>
    </test-fixture>

    <test-fixture id="focusables">
      <template>
        <test-overlay tabindex="-1">
          <h2>Focusables (no tabindex)</h2>
          <div>
            <input class="focusable1" placeholder="1 (nested)">
          </div>
          <button class="focusable2">1</button>
          <button disabled> disabled button</button>
          <div tabindex="-1">not focusable</div>
          <button class="focusable3">2</button>
        </test-overlay>
        <test-overlay tabindex="-1">
          <h2>Focusables (with tabindex)</h2>
          <div tabindex="-1">not focusable</div>
          <div tabindex="3" class="focusable3">3</div>
          <div tabindex="4" class="focusable4">4</div>
          <div tabindex="5" class="focusable5">5</div>
          <div>
            <div tabindex="1" class="focusable1">1 (nested)</div>
            <div tabindex="6" class="focusable6">6 (nested)</div>
          </div>
          <div tabindex="2" class="focusable2">2</div>
        </test-overlay>
        <test-overlay2>
          Overlay with optimized focusableNodes getter
          <button class="focusable1">1</button>
        </test-overlay2>
      </template>
    </test-fixture>

    <test-fixture id="backdrop">
      <template>
        <test-overlay with-backdrop>
          Overlay with backdrop
        </test-overlay>
      </template>
    </test-fixture>

    <test-fixture id="multiple">
      <template>
        <test-overlay class="overlay-1">
          Test overlay 1
        </test-overlay>
        <test-overlay class="overlay-2">
          Test overlay 2
          <button>Click</button>
        </test-overlay>
        <test-overlay2 class="overlay-3">
          Other overlay 3
        </test-overlay2>
      </template>
    </test-fixture>

    <test-fixture id="composed">
      <template>
        <test-menu-button></test-menu-button>
      </template>
    </test-fixture>

    <test-buttons id="buttons"></test-buttons>
    <input id="focusInput" placeholder="focus input">

    <script>

      function runAfterOpen(overlay, callback) {
        overlay.addEventListener('iron-overlay-opened', callback);
        overlay.open();
      }

      function runAfterClose(overlay, callback) {
        overlay.addEventListener('iron-overlay-closed', callback);
        overlay.close();
      }

      suite('basic overlay', function() {
        var overlay;

        setup(function() {
          overlay = fixture('basic');
        });

        test('overlay starts hidden', function() {
          assert.isFalse(overlay.opened, 'overlay starts closed');
          assert.equal(getComputedStyle(overlay).display, 'none', 'overlay starts hidden');
        });

        test('_renderOpened called only after is attached', function(done) {
          var overlay = document.createElement('test-overlay');
          // The overlay is ready at this point, but not yet attached.
          var spy = sinon.spy(overlay, '_renderOpened');
          // This triggers _openedChanged.
          overlay.opened = true;
          // Wait long enough for requestAnimationFrame callback.
          overlay.async(function() {
            assert.isFalse(spy.called, '_renderOpened not called');
            // Because not attached yet, overlay should not be the current overlay!
            assert.isNotOk(overlay._manager.currentOverlay(), 'currentOverlay not set');
            done();
          }, 100);
        });

        test('overlay open/close events', function(done) {
          var nevents = 0;

          overlay.addEventListener('iron-overlay-opened', function() {
            nevents += 1;
            overlay.opened = false;
          });

          overlay.addEventListener('iron-overlay-closed', function() {
            nevents += 1;
            assert.equal(nevents, 2, 'opened and closed events fired');
            done();
          });

          overlay.opened = true;
        });

        test('open() refits overlay only once', function(done) {
          var spy = sinon.spy(overlay, 'refit');
          runAfterOpen(overlay, function() {
            assert.equal(spy.callCount, 1, 'overlay did refit only once');
            done();
          });
        });

        test('open overlay refits on iron-resize', function(done) {
          runAfterOpen(overlay, function() {
            var spy = sinon.spy(overlay, 'refit');
            overlay.fire('iron-resize');
            Polymer.dom.flush();
            requestAnimationFrame(function() {
              assert.isTrue(spy.called, 'overlay did refit');
              done();
            });
          });
        });

        test('closed overlay does not refit on iron-resize', function(done) {
          var spy = sinon.spy(overlay, 'refit');
          overlay.fire('iron-resize');
          Polymer.dom.flush();
          requestAnimationFrame(function() {
            assert.isFalse(spy.called, 'overlay should not refit');
            done();
          });
        });

        test('open() triggers iron-resize', function(done) {
          var callCount = 0;
          // Ignore iron-resize triggered by window resize.
          window.addEventListener('resize', function() { callCount--; }, true);
          overlay.addEventListener('iron-resize', function() { callCount++; });
          runAfterOpen(overlay, function() {
            assert.equal(callCount, 1, 'iron-resize called once before iron-overlay-opened');
            done();
          });
        });

        test('close() triggers iron-resize', function(done) {
          runAfterOpen(overlay, function() {
            var spy = sinon.stub();
            overlay.addEventListener('iron-resize', spy);
            runAfterClose(overlay, function() {
              assert.equal(spy.callCount, 1, 'iron-resize called once before iron-overlay-closed');
              done();
            });
          });
        });

        test('closed overlay does not trigger iron-resize when its content changes', function() {
          // Ignore iron-resize triggered by window resize.
          var callCount = 0;
          window.addEventListener('resize', function() { callCount--; }, true);
          overlay.addEventListener('iron-resize', function() { callCount++; });
          Polymer.dom(overlay).appendChild(document.createElement('div'));
          Polymer.dom.flush();
          assert.equal(callCount, 0, 'iron-resize should not be called');
        });

        test('open overlay triggers iron-resize when its content changes', function(done) {
          runAfterOpen(overlay, function() {
            var spy = sinon.stub();
            overlay.addEventListener('iron-resize', spy);
            Polymer.dom(overlay).appendChild(document.createElement('div'));
            Polymer.dom.flush();
            assert.equal(spy.callCount, 1, 'iron-resize should be called once');
            done();
          });
        });

        test('close an overlay quickly after open', function(done) {
          // first, open the overlay
          overlay.open();
          overlay.async(function() {
            // during the opening transition, close the overlay
            this.close();
            // wait for any exceptions to be thrown until the transition is done
            this.async(function() {
              done();
            }, 300);
          });
        });

        test('clicking an overlay does not close it', function(done) {
          runAfterOpen(overlay, function() {
            var spy = sinon.stub();
            overlay.addEventListener('iron-overlay-closed', spy);
            MockInteractions.tap(overlay);
            overlay.async(function() {
              assert.isFalse(spy.called, 'iron-overlay-closed should not fire');
              done();
            }, 10);
          });
        });

        test('open overlay on mousedown does not close it', function(done) {
          var btn = document.createElement('button');
          btn.addEventListener('mousedown', overlay.open.bind(overlay));
          document.body.appendChild(btn);
          // It triggers mousedown, mouseup, and click.
          MockInteractions.tap(btn);
          document.body.removeChild(btn);

          assert.isTrue(overlay.opened, 'overlay opened');
          overlay.async(function() {
            assert.isTrue(overlay.opened, 'overlay is still open');
            done();
          }, 10);
        });

        test('clicking outside fires iron-overlay-canceled', function(done) {
          runAfterOpen(overlay, function() {
            overlay.addEventListener('iron-overlay-canceled', function(event) {
              assert.equal(event.detail.target, document.body, 'detail contains original click event');
              done();
            });
            MockInteractions.tap(document.body);
          });
        });

        test('clicking outside closes the overlay', function(done) {
          runAfterOpen(overlay, function() {
            overlay.addEventListener('iron-overlay-closed', function(event) {
              assert.isTrue(event.detail.canceled, 'overlay is canceled');
              done();
            });
            MockInteractions.tap(document.body);
          });
        });

        test('iron-overlay-canceled event can be prevented', function(done) {
          runAfterOpen(overlay, function() {
            overlay.addEventListener('iron-overlay-canceled', function(event) {
              event.preventDefault();
            });
            var spy = sinon.stub();
            overlay.addEventListener('iron-overlay-closed', spy);
            MockInteractions.tap(document.body);
            Polymer.Base.async(function() {
              assert.isTrue(overlay.opened, 'overlay is still open');
              assert.isFalse(spy.called, 'iron-overlay-closed not fired');
              done();
            }, 10);
          });
        });

        test('cancel an overlay with esc key', function(done) {
          runAfterOpen(overlay, function() {
            overlay.addEventListener('iron-overlay-canceled', function(event) {
              assert.equal(event.detail.type, 'keydown');
              done();
            });
            MockInteractions.pressAndReleaseKeyOn(document, 27);
          });
        });

        test('close an overlay with esc key', function(done) {
          runAfterOpen(overlay, function() {
            overlay.addEventListener('iron-overlay-closed', function(event) {
              assert.isTrue(event.detail.canceled, 'overlay is canceled');
              done();
            });
            MockInteractions.pressAndReleaseKeyOn(document, 27);
          });
        });

        test('no-cancel-on-outside-click property', function(done) {
          overlay.noCancelOnOutsideClick = true;
          runAfterOpen(overlay, function() {
            var spy = sinon.stub();
            overlay.addEventListener('iron-overlay-closed', spy);
            MockInteractions.tap(document.body);
            Polymer.Base.async(function() {
              assert.isFalse(spy.called, 'iron-overlay-closed should not fire');
              done();
            }, 10);
          });
        });

        test('no-cancel-on-esc-key property', function(done) {
          overlay.noCancelOnEscKey = true;
          runAfterOpen(overlay, function() {
            var spy = sinon.stub();
            overlay.addEventListener('iron-overlay-closed', spy);
            MockInteractions.pressAndReleaseKeyOn(document, 27);
            Polymer.Base.async(function() {
              assert.isFalse(spy.called, 'iron-overlay-cancel should not fire');
              done();
            }, 10);
          });
        });

        test('with-backdrop sets tabindex=-1 and removes it', function() {
          overlay.withBackdrop = true;
          assert.equal(overlay.getAttribute('tabindex'), '-1', 'tabindex is -1');
          overlay.withBackdrop = false;
          assert.isFalse(overlay.hasAttribute('tabindex'), 'tabindex removed');
        });

        test('with-backdrop does not override tabindex if already set', function() {
          overlay.setAttribute('tabindex', '1');
          overlay.withBackdrop = true;
          assert.equal(overlay.getAttribute('tabindex'), '1', 'tabindex is 1');
          overlay.withBackdrop = false;
          assert.equal(overlay.getAttribute('tabindex'), '1', 'tabindex is still 1');
        });

      });

      suite('keyboard event listener', function() {
        var overlay;

        var preventKeyDown = function(event) {
          event.preventDefault();
          event.stopPropagation();
        }

        suiteSetup(function() {
          // Worst case scenario: listener with useCapture = true that prevents & stops propagation
          // added before the overlay is initialized.
          document.addEventListener('keydown', preventKeyDown, true);
        });

        setup(function() {
          overlay = fixture('basic');
        });

        suiteTeardown(function() {
          document.removeEventListener('keydown', preventKeyDown, true);
        });

        test('cancel an overlay with esc key even if event is prevented by other listeners', function(done) {
          runAfterOpen(overlay, function() {
            overlay.addEventListener('iron-overlay-canceled', function(event) {
              done();
            });
            MockInteractions.pressAndReleaseKeyOn(document, 27);
          });
        });
      });

      suite('tap event listener', function() {
        var overlay;

        var preventTap = function(event) {
          event.preventDefault();
          event.stopPropagation();
        };

        suiteSetup(function() {
          // Worst case scenario: listener with useCapture = true that prevents & stops propagation
          // added before the overlay is initialized.
          document.body.addEventListener('tap', preventTap, true);
        });

        setup(function() {
          overlay = fixture('basic');
        });

        suiteTeardown(function() {
          document.body.removeEventListener('tap', preventTap, true);
        });

        test('cancel an overlay with tap outside even if event is prevented by other listeners', function(done) {
          runAfterOpen(overlay, function() {
            overlay.addEventListener('iron-overlay-canceled', function(event) {
              done();
            });
            MockInteractions.tap(document.body);
          });
        });
      });

      suite('opened overlay', function() {
        var overlay;

        setup(function() {
          overlay = fixture('opened');
        });

        test('overlay open by default', function(done) {
          overlay.addEventListener('iron-overlay-opened', function() {
            assert.isTrue(overlay.opened, 'overlay starts opened');
            assert.notEqual(getComputedStyle(overlay).display, 'none', 'overlay starts showing');
            done();
          });
        });

        test('overlay positioned & sized properly', function(done) {
          overlay.addEventListener('iron-overlay-opened', function() {
            var s = getComputedStyle(overlay);
            assert.closeTo(parseFloat(s.left), (window.innerWidth - overlay.offsetWidth) / 2, 1, 'centered horizontally');
            assert.closeTo(parseFloat(s.top), (window.innerHeight - overlay.offsetHeight) / 2, 1, 'centered vertically');
            done();
          });
        });
      });

      suite('focus handling', function() {
        var overlay;

        setup(function() {
          // Ensure focus is set to document.body
          document.body.focus();
          overlay = fixture('autofocus');
        });

        test('node with autofocus is focused', function(done) {
          runAfterOpen(overlay, function() {
            assert.equal(Polymer.dom(overlay).querySelector('[autofocus]'), document.activeElement, '<button autofocus> is focused');
            done();
          });
        });

        test('no-auto-focus will not focus node with autofocus', function(done) {
          overlay.noAutoFocus = true;
          runAfterOpen(overlay, function() {
            assert.notEqual(Polymer.dom(overlay).querySelector('[autofocus]'), document.activeElement, '<button autofocus> not focused after opened');
            done();
          });
          // In Safari the element with autofocus will immediately receive focus when displayed for the first time http://jsbin.com/woroci/2/
          // Ensure this is not the case for overlay.
          assert.notEqual(Polymer.dom(overlay).querySelector('[autofocus]'), document.activeElement, '<button autofocus> not immediately focused');
        });

        test('no-cancel-on-outside-click property; focus stays on overlay when click outside', function(done) {
          overlay.noCancelOnOutsideClick = true;
          runAfterOpen(overlay, function() {
            MockInteractions.tap(document.body);
            Polymer.Base.async(function() {
              assert.equal(Polymer.dom(overlay).querySelector('[autofocus]'), document.activeElement, '<button autofocus> is focused');
              done();
            }, 10);
          });
        });

        test('with-backdrop traps the focus within the overlay', function(done) {
          var focusSpy = sinon.stub();
          var button = document.createElement('button');
          document.body.appendChild(button);
          button.addEventListener('focus', focusSpy, true);

          overlay.withBackdrop = true;
          runAfterOpen(overlay, function() {
            // Try to steal the focus
            MockInteractions.focus(button);
            assert.equal(Polymer.dom(overlay).querySelector('[autofocus]'), document.activeElement, '<button autofocus> is focused');
            assert.equal(focusSpy.callCount, 0, 'button in body did not get the focus');
            document.body.removeChild(button);
            done();
          });
        });

        test('overlay with-backdrop and 1 focusable: prevent TAB and trap the focus', function(done) {
          overlay.withBackdrop = true;
          runAfterOpen(overlay, function() {
            // 1ms timeout needed by IE10 to have proper focus switching.
            Polymer.Base.async(function() {
              // Spy keydown.
              var tabSpy = sinon.spy();
              document.addEventListener('keydown', tabSpy);
              // Simulate TAB.
              MockInteractions.pressAndReleaseKeyOn(document, 9);
              assert.equal(Polymer.dom(overlay).querySelector('[autofocus]'), document.activeElement, 'focus stays on button');
              assert.isTrue(tabSpy.calledOnce, 'keydown spy called');
              assert.isTrue(tabSpy.getCall(0).args[0].defaultPrevented, 'keydown default prevented');
              // Cleanup.
              document.removeEventListener('keydown', tabSpy);
              done();
            }, 1);
          });
        });

        test('empty overlay with-backdrop: prevent TAB and trap the focus', function(done) {
          overlay = fixture('basic');
          overlay.withBackdrop = true;
          runAfterOpen(overlay, function() {
            // 1ms timeout needed by IE10 to have proper focus switching.
            Polymer.Base.async(function() {
              // Spy keydown.
              var tabSpy = sinon.spy();
              document.addEventListener('keydown', tabSpy);
              // Simulate TAB.
              MockInteractions.pressAndReleaseKeyOn(document, 9);
              assert.equal(overlay, document.activeElement, 'focus stays on overlay');
              assert.isTrue(tabSpy.calledOnce, 'keydown spy called');
              assert.isTrue(tabSpy.getCall(0).args[0].defaultPrevented, 'keydown default prevented');
              // Cleanup.
              document.removeEventListener('keydown', tabSpy);
              done();
            }, 1);
          });
        });

      });

      suite('focusable nodes', function() {
        var overlay, overlayWithTabIndex, overlayFocusableNodes;

        setup(function() {
          var f = fixture('focusables');
          overlay = f[0];
          overlayWithTabIndex = f[1];
          overlayFocusableNodes = f[2];
        });

        test('_focusableNodes returns nodes that are focusable', function(done) {
          runAfterOpen(overlay, function() {
            var focusableNodes = overlay._focusableNodes;
            assert.equal(focusableNodes.length, 3, '3 nodes are focusable');
            assert.equal(focusableNodes[0], Polymer.dom(overlay).querySelector('.focusable1'));
            assert.equal(focusableNodes[1], Polymer.dom(overlay).querySelector('.focusable2'));
            assert.equal(focusableNodes[2], Polymer.dom(overlay).querySelector('.focusable3'));
            done();
          });
        });

        test('_focusableNodes includes overlay if it has a valid tabindex', function(done) {
          runAfterOpen(overlay, function() {
            overlay.setAttribute('tabindex', '0');
            var focusableNodes = overlay._focusableNodes;
            assert.equal(focusableNodes.length, 4, '4 focusable nodes');
            assert.notEqual(focusableNodes.indexOf(overlay), -1, 'overlay is included');
            done();
          });
        });

        test('_focusableNodes respects the tabindex order', function(done) {
          runAfterOpen(overlayWithTabIndex, function() {
            var focusableNodes = overlayWithTabIndex._focusableNodes;
            assert.equal(focusableNodes.length, 6, '6 nodes are focusable');
            assert.equal(focusableNodes[0], Polymer.dom(overlayWithTabIndex).querySelector('.focusable1'));
            assert.equal(focusableNodes[1], Polymer.dom(overlayWithTabIndex).querySelector('.focusable2'));
            assert.equal(focusableNodes[2], Polymer.dom(overlayWithTabIndex).querySelector('.focusable3'));
            assert.equal(focusableNodes[3], Polymer.dom(overlayWithTabIndex).querySelector('.focusable4'));
            assert.equal(focusableNodes[4], Polymer.dom(overlayWithTabIndex).querySelector('.focusable5'));
            assert.equal(focusableNodes[5], Polymer.dom(overlayWithTabIndex).querySelector('.focusable6'));
            done();
          });
        });

        test('_focusableNodes can be overridden', function(done) {
          runAfterOpen(overlayFocusableNodes, function() {
            // It has 1 focusable in the light dom, and 2 in the shadow dom.
            var focusableNodes = overlayFocusableNodes._focusableNodes;
            assert.equal(focusableNodes.length, 2, 'length ok');
            assert.equal(focusableNodes[0], overlayFocusableNodes.$.first, 'first ok');
            assert.equal(focusableNodes[1], overlayFocusableNodes.$.last, 'last ok');
            done();
          });
        });

        test('with-backdrop: TAB & Shift+TAB wrap focus', function(done) {
          overlay.withBackdrop = true;
          runAfterOpen(overlay, function() {
            var focusableNodes = overlay._focusableNodes;
            // 1ms timeout needed by IE10 to have proper focus switching.
            Polymer.Base.async(function() {
              // Go to last element.
              focusableNodes[focusableNodes.length-1].focus();
              // Spy keydown.
              var tabSpy = sinon.spy();
              document.addEventListener('keydown', tabSpy);
              // Simulate TAB.
              MockInteractions.pressAndReleaseKeyOn(document, 9);
              assert.equal(focusableNodes[0], document.activeElement, 'focus wrapped to first focusable');
              assert.isTrue(tabSpy.calledOnce, 'keydown spy called');
              assert.isTrue(tabSpy.getCall(0).args[0].defaultPrevented, 'keydown default prevented');
              // Simulate Shift+TAB.
              MockInteractions.pressAndReleaseKeyOn(document, 9, ['shift']);
              assert.equal(focusableNodes[focusableNodes.length-1], document.activeElement, 'focus wrapped to last focusable');
              assert.isTrue(tabSpy.calledTwice, 'keydown spy called again');
              assert.isTrue(tabSpy.getCall(1).args[0].defaultPrevented, 'keydown default prevented again');
              // Cleanup.
              document.removeEventListener('keydown', tabSpy);
              done();
            }, 1);
          });
        });

        test('with-backdrop: TAB & Shift+TAB wrap focus respecting tabindex', function(done) {
          overlayWithTabIndex.withBackdrop = true;
          runAfterOpen(overlayWithTabIndex, function() {
            var focusableNodes = overlayWithTabIndex._focusableNodes;
            // 1ms timeout needed by IE10 to have proper focus switching.
            Polymer.Base.async(function() {
              // Go to last element.
              focusableNodes[focusableNodes.length-1].focus();
              // Simulate TAB.
              MockInteractions.pressAndReleaseKeyOn(document, 9);
              assert.equal(focusableNodes[0], document.activeElement, 'focus wrapped to first focusable');
              // Simulate Shift+TAB.
              MockInteractions.pressAndReleaseKeyOn(document, 9, ['shift']);
              assert.equal(focusableNodes[focusableNodes.length-1], document.activeElement, 'focus wrapped to last focusable');
              done();
            }, 1);
          });
        });

        test('with-backdrop: Shift+TAB after open wrap focus', function(done) {
          overlay.withBackdrop = true;
          runAfterOpen(overlay, function() {
            var focusableNodes = overlay._focusableNodes;
            // 1ms timeout needed by IE10 to have proper focus switching.
            Polymer.Base.async(function() {
              // Spy keydown.
              var tabSpy = sinon.spy();
              document.addEventListener('keydown', tabSpy);
              // Simulate Shift+TAB.
              MockInteractions.pressAndReleaseKeyOn(document, 9, ['shift']);
              assert.equal(focusableNodes[focusableNodes.length-1], document.activeElement, 'focus wrapped to last focusable');
              assert.isTrue(tabSpy.calledOnce, 'keydown spy called');
              assert.isTrue(tabSpy.getCall(0).args[0].defaultPrevented, 'keydown default prevented');
              // Cleanup.
              document.removeEventListener('keydown', tabSpy);
              done();
            }, 1);
          });
        });

        test('with-backdrop: after open, update last focusable node and then Shift+TAB', function(done) {
          overlay.withBackdrop = true;
          runAfterOpen(overlay, function() {
            var focusableNodes = overlay._focusableNodes;
            // 1ms timeout needed by IE10 to have proper focus switching.
            Polymer.Base.async(function() {
              // Before tabbing, make lastFocusable non-tabbable. This will make
              // the one before it (focusableNodes.length - 2), the new last focusable node.
              focusableNodes[focusableNodes.length-1].setAttribute('tabindex', '-1');
              overlay.invalidateTabbables();
              // Simulate Shift+TAB.
              MockInteractions.pressAndReleaseKeyOn(document, 9, ['shift']);
              assert.equal(focusableNodes[focusableNodes.length-2], document.activeElement, 'focus wrapped correctly');
              done();
            }, 1);
          });
        });

        test('with-backdrop: Shift+TAB wrap focus in shadowDOM', function(done) {
          overlayFocusableNodes.withBackdrop = true;
          runAfterOpen(overlayFocusableNodes, function() {
            // 1ms timeout needed by IE10 to have proper focus switching.
            Polymer.Base.async(function() {
              // Spy keydown.
              var tabSpy = sinon.spy();
              document.addEventListener('keydown', tabSpy);
              // Simulate Shift+TAB.
              MockInteractions.pressAndReleaseKeyOn(document, 9, ['shift']);
              assert.equal(overlayFocusableNodes.$.last, Polymer.IronOverlayManager.deepActiveElement, 'focus wrapped to last focusable in the shadowDOM');
              assert.isTrue(tabSpy.calledOnce, 'keydown spy called');
              assert.isTrue(tabSpy.getCall(0).args[0].defaultPrevented, 'keydown default prevented');
              // Cleanup.
              document.removeEventListener('keydown', tabSpy);
              done();
            }, 1);
          });
        });

      });

      suite('Polymer.IronOverlayManager.deepActiveElement', function() {

        test('handles document.body', function () {
          document.body.focus();
          assert.equal(Polymer.IronOverlayManager.deepActiveElement, document.body);
        });

        test('handles light dom', function () {
          var focusable = document.getElementById('focusInput');
          focusable.focus();
          assert.equal(Polymer.IronOverlayManager.deepActiveElement, focusable, 'input is handled');
          focusable.blur();
        });

        test('handles shadow dom', function () {
          var focusable = document.getElementById('buttons').$.button0;
          focusable.focus();
          assert.equal(Polymer.IronOverlayManager.deepActiveElement, focusable);
          focusable.blur();
        });

      });

      suite('restore-focus-on-close', function() {

        var overlay;
        setup(function () {
          overlay = fixture('autofocus');
          overlay.restoreFocusOnClose = true;
        });

        teardown(function () {
          // No matter what, return the focus to body!
          document.body.focus();
        });

        test('does not return focus on close by default (restore-focus-on-close=false)', function(done) {
          overlay.restoreFocusOnClose = false;
          var focusable = document.getElementById('focusInput');
          focusable.focus();
          runAfterOpen(overlay, function() {
            runAfterClose(overlay, function() {
              assert.notEqual(Polymer.IronOverlayManager.deepActiveElement, focusable, 'focus is not restored to focusable');
              done();
            });
          });
        });

        test('overlay returns focus on close', function(done) {
          var focusable = document.getElementById('focusInput');
          focusable.focus();
          runAfterOpen(overlay, function() {
            runAfterClose(overlay, function() {
              assert.equal(Polymer.IronOverlayManager.deepActiveElement, focusable, 'focus restored to focusable');
              done();
            });
          });
        });

        test('overlay returns focus on close (ShadowDOM)', function(done) {
          var focusable = document.getElementById('buttons').$.button0;
          focusable.focus();
          runAfterOpen(overlay, function() {
            runAfterClose(overlay, function() {
              assert.equal(Polymer.IronOverlayManager.deepActiveElement, focusable, 'focus restored to focusable');
              done();
            });
          });
        });

      });

      suite('overlay with backdrop', function() {
        var overlay;

        setup(function() {
          overlay = fixture('backdrop');
        });

        test('backdrop is opened when overlay is opened', function(done) {
          assert.isOk(overlay.backdropElement, 'backdrop is defined');
          runAfterOpen(overlay, function() {
            assert.isTrue(overlay.backdropElement.opened, 'backdrop is opened');
            assert.isOk(overlay.backdropElement.parentNode, 'backdrop is inserted in the DOM');
            done();
          });
        });

        test('backdrop appears behind the overlay', function(done) {
          runAfterOpen(overlay, function() {
            styleZ = parseInt(window.getComputedStyle(overlay).zIndex, 10);
            backdropStyleZ = parseInt(window.getComputedStyle(overlay.backdropElement).zIndex, 10);
            assert.isTrue(styleZ > backdropStyleZ, 'overlay has higher z-index than backdrop');
            done();
          });
        });

        test('backdrop is removed when overlay is closed', function(done) {
          runAfterOpen(overlay, function() {
            runAfterClose(overlay, function() {
              assert.isFalse(overlay.backdropElement.opened, 'backdrop is closed');
              assert.isNotOk(overlay.backdropElement.parentNode, 'backdrop is removed from the DOM');
              assert.lengthOf(document.querySelectorAll('iron-overlay-backdrop'), 0, 'no backdrop elements on body');
              done();
            });
          });
        });

        test('backdrop is removed when the element is removed from DOM', function(done) {
          runAfterOpen(overlay, function() {
            Polymer.dom(overlay).parentNode.removeChild(overlay);
            // Ensure detached is executed.
            Polymer.dom.flush();
            assert.isFalse(overlay.backdropElement.opened, 'backdrop is closed');
            assert.isNotOk(overlay.backdropElement.parentNode, 'backdrop is removed from the DOM');
            assert.lengthOf(document.querySelectorAll('iron-overlay-backdrop'), 0, 'no backdrop elements on body');
            assert.isNotOk(overlay._manager.currentOverlay(), 'currentOverlay ok');
            done();
          });
        });

        test('manager.getBackdrops() updated on opened changes', function(done) {
          runAfterOpen(overlay, function() {
            assert.equal(Polymer.IronOverlayManager.getBackdrops().length, 1, 'overlay added to manager backdrops');
            runAfterClose(overlay, function() {
              assert.equal(Polymer.IronOverlayManager.getBackdrops().length, 0, 'overlay removed from manager backdrops');
              done();
            });
          });
        });

        test('updating with-backdrop to false closes backdrop', function(done) {
          runAfterOpen(overlay, function() {
            overlay.withBackdrop = false;
            assert.isFalse(overlay.backdropElement.opened, 'backdrop is closed');
            assert.isNotObject(overlay.backdropElement.parentNode, 'backdrop is removed from document');
            done();
          });
        });

        test('backdrop is removed when toggling overlay opened', function(done) {
          overlay.open();
          runAfterClose(overlay, function() {
            assert.isFalse(overlay.backdropElement.opened, 'backdrop is closed');
            assert.isNotOk(overlay.backdropElement.parentNode, 'backdrop is removed from document');
            done();
          });
        });

        test('withBackdrop = false does not prevent click outside event', function(done) {
          overlay.withBackdrop = false;
          runAfterOpen(overlay, function() {
            overlay.addEventListener('iron-overlay-canceled', function(event) {
              assert.isFalse(event.detail.defaultPrevented, 'click event not prevented');
              done();
            });
            MockInteractions.tap(document.body);
          });
        });
      });

      suite('multiple overlays', function() {
        var overlay1, overlay2;

        setup(function() {
          var f = fixture('multiple');
          overlay1 = f[0];
          overlay2 = f[1];
        });

        test('new overlays appear on top', function(done) {
          runAfterOpen(overlay1, function() {
            runAfterOpen(overlay2, function() {
              var styleZ = parseInt(window.getComputedStyle(overlay1).zIndex, 10);
              var styleZ1 = parseInt(window.getComputedStyle(overlay2).zIndex, 10);
              assert.isTrue(styleZ1 > styleZ, 'overlay2 has higher z-index than overlay1');
              done();
            });
          });
        });

        test('ESC closes only the top overlay', function(done) {
          runAfterOpen(overlay1, function() {
            runAfterOpen(overlay2, function() {
              MockInteractions.pressAndReleaseKeyOn(document, 27);
              assert.isFalse(overlay2.opened, 'overlay2 was closed');
              assert.isTrue(overlay1.opened, 'overlay1 is still opened');
              done();
            });
          });
        });

        test('close an overlay in proximity to another overlay', function(done) {
          // Open and close a separate overlay.
          overlay1.open();
          overlay1.close();

          // Open the overlay we care about.
          overlay2.open();

          // Immediately close the first overlay.
          // Wait for infinite recursion, otherwise we win.
          runAfterClose(overlay2, function() {
            done();
          })
        });

      });

      suite('Manager overlays in sync', function() {
        var overlay1, overlay2;
        var overlays;

        setup(function() {
          var f = fixture('multiple');
          overlay1 = f[0];
          overlay2 = f[1];
          overlays = Polymer.IronOverlayManager._overlays;
        });

        test('no duplicates after attached', function(done) {
          overlay1 = document.createElement('test-overlay');
          runAfterOpen(overlay1, function() {
            assert.equal(overlays.length, 1, 'correct count after open and attached');
            document.body.removeChild(overlay1);
            done();
          });
          document.body.appendChild(overlay1);
        });

        test('call open multiple times handled', function(done) {
          overlay1.open();
          overlay1.open();
          runAfterOpen(overlay1, function() {
            assert.equal(overlays.length, 1, '1 overlay after open');
            done();
          })
        });

        test('close handled', function(done) {
          runAfterOpen(overlay1, function() {
            runAfterClose(overlay1, function() {
              assert.equal(overlays.length, 0, '0 overlays after close');
              done();
            });
          });
        });

        test('open/close brings overlay on top', function(done) {
          overlay1.open();
          runAfterOpen(overlay2, function() {
            assert.equal(overlays.indexOf(overlay1), 0, 'overlay1 at index 0');
            assert.equal(overlays.indexOf(overlay2), 1, 'overlay2 at index 1');
            overlay1.close();
            runAfterOpen(overlay1, function() {
              assert.equal(overlays.indexOf(overlay1), 1, 'overlay1 moved at index 1');
              assert.isAbove(parseInt(overlay1.style.zIndex), parseInt(overlay2.style.zIndex), 'overlay1 on top of overlay2');
              done();
            });
          });
        });
      });

      suite('z-ordering', function() {

        var originalMinimumZ;
        var overlay1, overlay2;

        setup(function() {
          var f = fixture('multiple');
          overlay1 = f[0];
          overlay2 = f[1];
          originalMinimumZ = Polymer.IronOverlayManager._minimumZ;
        });

        teardown(function() {
          Polymer.IronOverlayManager._minimumZ = originalMinimumZ;
        });

        // for iframes
        test('default z-index is greater than 100', function(done) {
          runAfterOpen(overlay1, function() {
            var styleZ = parseInt(window.getComputedStyle(overlay1).zIndex, 10);
            assert.isTrue(styleZ > 100, 'overlay1 z-index is <= 100');
            done();
          });
        });

        test('ensureMinimumZ() effects z-index', function(done) {
          Polymer.IronOverlayManager.ensureMinimumZ(1000);

          runAfterOpen(overlay1, function() {
            var styleZ = parseInt(window.getComputedStyle(overlay1).zIndex, 10);
            assert.isTrue(styleZ > 1000, 'overlay1 z-index is <= 1000');
            done();
          });
        });

        test('ensureMinimumZ() never decreases the minimum z-index', function(done) {
          Polymer.IronOverlayManager.ensureMinimumZ(1000);
          Polymer.IronOverlayManager.ensureMinimumZ(500);

          runAfterOpen(overlay1, function() {
            var styleZ = parseInt(window.getComputedStyle(overlay1).zIndex, 10);
            assert.isTrue(styleZ > 1000, 'overlay1 z-index is <= 1000');
            done();
          });
        });

      });

      suite('multiple overlays with backdrop', function() {
        var overlay1, overlay2, overlay3;

        setup(function() {
          var f = fixture('multiple');
          overlay1 = f[0];
          overlay2 = f[1];
          overlay3 = f[2];
          overlay1.withBackdrop = overlay2.withBackdrop = overlay3.withBackdrop = true;
        });

        test('multiple overlays share the same backdrop', function() {
          assert.isTrue(overlay1.backdropElement === overlay2.backdropElement, 'overlay1 and overlay2 have the same backdrop element');
          assert.isTrue(overlay1.backdropElement === overlay3.backdropElement, 'overlay1 and overlay3 have the same backdrop element');
        });

        test('only one iron-overlay-backdrop in the DOM', function(done) {
          // Open them all.
          overlay1.opened = true;
          overlay2.opened = true;
          runAfterOpen(overlay3, function() {
            assert.lengthOf(document.querySelectorAll('iron-overlay-backdrop'), 1, 'only one backdrop element in the DOM');
            done();
          });
        });

        test('iron-overlay-backdrop is removed from the DOM when all overlays with backdrop are closed', function(done) {
          // Open & close them all.
          overlay1.opened = true;
          overlay2.opened = true;
          runAfterOpen(overlay3, function() {
            overlay1.opened = overlay2.opened = false;
            runAfterClose(overlay3, function() {
              assert.lengthOf(document.querySelectorAll('iron-overlay-backdrop'), 0, 'backdrop element removed from the DOM');
              done();
            });
          });
        });

        test('newest overlay appear on top', function(done) {
          runAfterOpen(overlay1, function() {
            runAfterOpen(overlay2, function() {
              var styleZ = parseInt(window.getComputedStyle(overlay1).zIndex, 10);
              var style1Z = parseInt(window.getComputedStyle(overlay2).zIndex, 10);
              var bgStyleZ = parseInt(window.getComputedStyle(overlay1.backdropElement).zIndex, 10);
              assert.isTrue(style1Z > styleZ, 'overlay2 has higher z-index than overlay1');
              assert.isTrue(styleZ > bgStyleZ, 'overlay1 has higher z-index than backdrop');
              done();
            });
          });
        });

        var clickEvents = ['click', 'tap'];
        for (var i = 0; i < clickEvents.length; i++) {
          var eventName = clickEvents[i];
          test(eventName + ' event handled only by top overlay', function(done) {
            runAfterOpen(overlay1, function() {
              runAfterOpen(overlay2, function() {
                var btn = Polymer.dom(overlay2).querySelector('button');
                btn.addEventListener(eventName, overlay2.close.bind(overlay2));
                MockInteractions.tap(btn);
                assert.isFalse(overlay2.opened, 'overlay2 closed');
                assert.isTrue(overlay1.opened, 'overlay1 opened');
                overlay2.addEventListener('iron-overlay-closed', function() {
                  assert.isTrue(overlay1.opened, 'overlay1 still opened');
                  done();
                });
              });
            });
          });
        }

        test('updating with-backdrop updates z-index', function(done) {
          runAfterOpen(overlay1, function() {
            runAfterOpen(overlay2, function() {
              overlay1.withBackdrop = false;
              var styleZ = parseInt(window.getComputedStyle(overlay1).zIndex, 10);
              var style1Z = parseInt(window.getComputedStyle(overlay2).zIndex, 10);
              var bgStyleZ = parseInt(window.getComputedStyle(overlay1.backdropElement).zIndex, 10);
              assert.isTrue(style1Z > bgStyleZ, 'overlay2 has higher z-index than backdrop');
              assert.isTrue(styleZ < bgStyleZ, 'overlay1 has lower z-index than backdrop');
              done();
            });
          });
        });

      });

      suite('overlay in composed tree', function() {
        var composed, overlay, trigger;
        setup(function(done) {
          composed = fixture('composed');
          overlay = composed.$.overlay;
          trigger = composed.$.trigger;
          overlay.withBackdrop = true;
          overlay.addEventListener('iron-overlay-opened', function() {
            done();
          });
          // Opens the overlay.
          MockInteractions.tap(trigger);
        });

        test('click on overlay content does not close it', function(done) {
          // Tap on button inside overlay.
          MockInteractions.tap(Polymer.dom(overlay).querySelector('button'));
          Polymer.Base.async(function(){
            assert.isTrue(overlay.opened, 'overlay still opened');
            done();
          }, 1);
        });

        test('with-backdrop wraps the focus within the overlay', function(done) {
          // 1ms timeout needed by IE10 to have proper focus switching.
          Polymer.Base.async(function(){
            var buttons = Polymer.dom(overlay).querySelectorAll('button');
            // Go to last element.
            buttons[buttons.length-1].focus();
            // Spy keydown.
            var tabSpy = sinon.spy();
            document.addEventListener('keydown', tabSpy);
            // Simulate TAB.
            MockInteractions.pressAndReleaseKeyOn(document, 9);
            assert.equal(buttons[0], Polymer.IronOverlayManager.deepActiveElement, 'focus wrapped to first focusable');
            assert.isTrue(tabSpy.calledOnce, 'keydown spy called');
            assert.isTrue(tabSpy.getCall(0).args[0].defaultPrevented, 'keydown default prevented');
            // Simulate Shift+TAB.
            MockInteractions.pressAndReleaseKeyOn(document, 9, ['shift']);
            assert.equal(buttons[buttons.length-1], Polymer.IronOverlayManager.deepActiveElement, 'focus wrapped to last focusable');
            assert.isTrue(tabSpy.calledTwice, 'keydown spy called again');
            assert.isTrue(tabSpy.getCall(1).args[0].defaultPrevented, 'keydown default prevented again');
            // Cleanup.
            document.removeEventListener('keydown', tabSpy);
            done();
          }, 1);
        });

      });

      suite('always-on-top', function() {
        var overlay1, overlay2;

        setup(function() {
          var f = fixture('multiple');
          overlay1 = f[0];
          overlay2 = f[1];
          overlay1.alwaysOnTop = true;
        });

        test('stays on top', function(done) {
          runAfterOpen(overlay1, function() {
            runAfterOpen(overlay2, function() {
              var zIndex1 = parseInt(window.getComputedStyle(overlay1).zIndex, 10);
              var zIndex2 = parseInt(window.getComputedStyle(overlay2).zIndex, 10);
              assert.isAbove(zIndex1, zIndex2, 'overlay1 on top');
              assert.equal(Polymer.IronOverlayManager.currentOverlay(), overlay1, 'currentOverlay ok');
              done();
            });
          });
        });

        test('stays on top also if another overlay is with-backdrop', function(done) {
          overlay2.withBackdrop = true;
          runAfterOpen(overlay1, function() {
            runAfterOpen(overlay2, function() {
              var zIndex1 = parseInt(window.getComputedStyle(overlay1).zIndex, 10);
              var zIndex2 = parseInt(window.getComputedStyle(overlay2).zIndex, 10);
              assert.isAbove(zIndex1, zIndex2, 'overlay1 on top');
              assert.equal(Polymer.IronOverlayManager.currentOverlay(), overlay1, 'currentOverlay ok');
              done();
            });
          });
        });

        test('last overlay with always-on-top wins', function(done) {
          overlay2.alwaysOnTop = true;
          runAfterOpen(overlay1, function() {
            runAfterOpen(overlay2, function() {
              var zIndex1 = parseInt(window.getComputedStyle(overlay1).zIndex, 10);
              var zIndex2 = parseInt(window.getComputedStyle(overlay2).zIndex, 10);
              assert.isAbove(zIndex2, zIndex1, 'overlay2 on top');
              assert.equal(Polymer.IronOverlayManager.currentOverlay(), overlay2, 'currentOverlay ok');
              done();
            });
          });
        });

      });

      suite('animations', function() {

        test('overlay animations correctly triggered', function(done) {
          var overlay = fixture('basic');
          overlay.animated = true;
          overlay.open();
          overlay.addEventListener('simple-overlay-open-animation-start', function() {
            // Since animated overlay will transition center + 300px to center,
            // we should not find the element at the center when the open animation starts.
            var centerElement = document.elementFromPoint(window.innerWidth/2, window.innerHeight/2);
            assert.notEqual(centerElement, overlay, 'overlay should not be centered already');
            done();
          });
        });

      });

      suite('a11y', function() {

        test('overlay has aria-hidden=true when opened', function() {
          var overlay = fixture('basic');
          assert.equal(overlay.getAttribute('aria-hidden'), 'true', 'overlay has aria-hidden="true"');
          overlay.open();
          assert.isFalse(overlay.hasAttribute('aria-hidden'), 'overlay does not have aria-hidden attribute');
          overlay.close();
          assert.equal(overlay.getAttribute('aria-hidden'), 'true', 'overlay has aria-hidden="true"');
        });

      });
    </script>

  </body>

</html>