aboutsummaryrefslogtreecommitdiff
path: root/catapult/third_party/polymer/components/iron-fit-behavior/test/iron-fit-behavior.html
blob: 7dcc15dbfd163e691f75b1fa2d1430493f9a6ea6 (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
<!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-fit-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="test-fit.html">

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

      .absolute {
        position: absolute;
        top: 0;
        left: 0;
      }

      .scrolling {
        overflow: auto;
      }

      .sized-x {
        width: 100px;
      }

      .sized-y {
        height: 100px;
      }

      .positioned-left {
        position: absolute;
        left: 100px;
      }

      .positioned-right {
        position: absolute;
        right: 100px;
      }

      .positioned-top {
        position: absolute;
        top: 100px;
      }

      .positioned-bottom {
        position: absolute;
        bottom: 100px;
      }

      .with-max-width {
        max-width: 500px;
      }

      .with-max-height {
        max-height: 500px;
      }

      .with-margin {
        margin: 20px;
      }

      .constrain {
        position: fixed;
        top: 20px;
        left: 20px;
        width: 150px;
        height: 150px;
        border: 1px solid black;
        box-sizing: border-box;
      }

      .sizer {
        width: 9999px;
        height: 9999px;
      }

    </style>

  </head>
  <body>

    <div class="constrain"></div>

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

    <test-fixture id="absolute">
      <template>
        <test-fit auto-fit-on-attach class="absolute">
          Absolutely positioned
        </test-fit>
      </template>
    </test-fixture>

    <test-fixture id="sized-xy">
      <template>
        <test-fit auto-fit-on-attach class="sized-x sized-y">
          Sized (x/y), auto center/center
        </test-fit>
      </template>
    </test-fixture>

    <test-fixture id="sized-x">
      <template>
        <test-fit auto-fit-on-attach class="sized-x">
          Sized (x), auto center/center
        </test-fit>
      </template>
    </test-fixture>

    <test-fixture id="positioned-xy">
      <template>
        <test-fit auto-fit-on-attach class="sized-x positioned-left positioned-top">
          Sized (x/y), positioned/positioned
        </test-fit>
      </template>
    </test-fixture>

    <test-fixture id="inline-positioned-xy">
      <template>
        <test-fit auto-fit-on-attach class="sized-x sized-y" style="position:absolute;left:100px;top:100px;">
          Sized (x/y), positioned/positioned
        </test-fit>
      </template>
    </test-fixture>

    <test-fixture id="sectioned">
      <template>
        <test-fit auto-fit-on-attach class="sized-x">
          <div>
            Sized (x), auto center/center with scrolling section
          </div>
          <div class="internal"></div>
        </test-fit>
      </template>
    </test-fixture>

    <test-fixture id="constrain-target">
      <template>
        <div class="constrain">
          <test-fit auto-fit-on-attach class="el sized-x sized-y">
            <div>
              Auto center/center to parent element
            </div>
          </test-fit>
        </div>
      </template>
    </test-fixture>

    <test-fixture id="offscreen-container">
      <template>
        <div style="position: fixed; top: -1px; left: 0;">
          <test-fit auto-fit-on-attach class="el sized-x">
            <div>
              Sized (x), auto center/center, container is offscreen
            </div>
          </test-fit>
        </div>
      </template>
    </test-fixture>

    <test-fixture id="scrollable">
      <template>
        <test-fit auto-fit-on-attach class="scrolling">
          scrollable
          <div class="sizer"></div>
        </test-fit>
      </template>
    </test-fixture>

    <template id="ipsum">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    </template>

    <script>

      function makeScrolling(el) {
        el.classList.add('scrolling');
        var template = document.getElementById('ipsum');
        for (var i = 0; i < 20; i++) {
          el.appendChild(template.content.cloneNode(true));
        }
      }

      function intersects(r1, r2) {
        return !(r2.left >= r1.right || r2.right <= r1.left || r2.top >= r1.bottom || r2.bottom <= r1.top);
      }

      suite('basic', function() {

        var el;
        setup(function() {
          el = fixture('basic');
        });

        test('position() works without autoFitOnAttach', function() {
          el.verticalAlign = 'top';
          el.horizontalAlign = 'left';
          el.position();
          var rect = el.getBoundingClientRect();
          assert.equal(rect.top, 0, 'top ok');
          assert.equal(rect.left, 0, 'left ok');
        });

        test('constrain() works without autoFitOnAttach', function() {
          el.constrain();
          var style = getComputedStyle(el);
          assert.equal(style.maxWidth, window.innerWidth + 'px', 'maxWidth ok');
          assert.equal(style.maxHeight, window.innerHeight + 'px', 'maxHeight ok');
        });

        test('center() works without autoFitOnAttach', function() {
          el.center();
          var rect = el.getBoundingClientRect();
          assert.closeTo(rect.left - (window.innerWidth - rect.right), 0, 5, 'centered horizontally');
          assert.closeTo(rect.top - (window.innerHeight - rect.bottom), 0, 5, 'centered vertically');
        });

      });

      suite('manual positioning', function() {

        test('css positioned element is not re-positioned', function() {
          var el = fixture('positioned-xy');
          var rect = el.getBoundingClientRect();
          assert.equal(rect.top, 100, 'top is unset');
          assert.equal(rect.left, 100, 'left is unset');

        });

        test('inline positioned element is not re-positioned', function() {
          var el = fixture('inline-positioned-xy');
          var rect = el.getBoundingClientRect();
          // need to measure document.body here because mocha sets a min-width on html,body, and
          // the element is positioned wrt to that by css
          var bodyRect = document.body.getBoundingClientRect();
          assert.equal(rect.top, 100, 'top is unset');
          assert.equal(rect.left, 100, 'left is unset');

          el.refit();

          rect = el.getBoundingClientRect();
          assert.equal(rect.top, 100, 'top is unset after refit');
          assert.equal(rect.left, 100, 'left is unset after refit');

        });

        test('position property is preserved after', function() {
          var el = fixture('absolute');
          assert.equal(getComputedStyle(el).position, 'absolute', 'position:absolute is preserved');
        });
      });

      suite('fit to window', function() {

        test('sized element is centered in viewport', function() {
          var el = fixture('sized-xy');
          var rect = el.getBoundingClientRect();
          assert.closeTo(rect.left - (window.innerWidth - rect.right), 0, 5, 'centered horizontally');
          assert.closeTo(rect.top - (window.innerHeight - rect.bottom), 0, 5, 'centered vertically');
        });

        test('sized element with margin is centered in viewport', function() {
          var el = fixture('sized-xy');
          el.classList.add('with-margin');
          el.refit();
          var rect = el.getBoundingClientRect();
          assert.closeTo(rect.left - (window.innerWidth - rect.right), 0, 5, 'centered horizontally');
          assert.closeTo(rect.top - (window.innerHeight - rect.bottom), 0, 5, 'centered vertically');
        });

        test('sized element with transformed parent is centered in viewport', function() {
          var constrain = fixture('constrain-target');
          var el = Polymer.dom(constrain).querySelector('.el');
          var rectBefore = el.getBoundingClientRect();
          constrain.style.transform = 'translate3d(5px, 5px, 0)';
          el.center();
          var rectAfter = el.getBoundingClientRect();
          assert.equal(rectBefore.top, rectAfter.top, 'top ok');
          assert.equal(rectBefore.bottom, rectAfter.bottom, 'bottom ok');
          assert.equal(rectBefore.left, rectAfter.left, 'left ok');
          assert.equal(rectBefore.right, rectAfter.right, 'right ok');
        });

        test('scrolling element is centered in viewport', function() {
          var el = fixture('sized-x');
          makeScrolling(el);
          el.refit();
          var rect = el.getBoundingClientRect();
          assert.closeTo(rect.left - (window.innerWidth - rect.right), 0, 5, 'centered horizontally');
          assert.closeTo(rect.top - (window.innerHeight - rect.bottom), 0, 5, 'centered vertically');
        });

        test('scrolling element is constrained to viewport height', function() {
          var el = fixture('sized-x');
          makeScrolling(el);
          el.refit();
          var rect = el.getBoundingClientRect();
          assert.isTrue(rect.height <= window.innerHeight, 'height is less than or equal to viewport height');
        });

        test('scrolling element with offscreen container is constrained to viewport height', function() {
          var container = fixture('offscreen-container');
          var el = Polymer.dom(container).querySelector('.el')
          makeScrolling(el);
          el.refit();
          var rect = el.getBoundingClientRect();
          assert.isTrue(rect.height <= window.innerHeight, 'height is less than or equal to viewport height');
        });

        test('scrolling element with max-height is centered in viewport', function() {
          var el = fixture('sized-x');
          el.classList.add('with-max-height');
          makeScrolling(el);
          el.refit();
          var rect = el.getBoundingClientRect();
          assert.closeTo(rect.left - (window.innerWidth - rect.right), 0, 5, 'centered horizontally');
          assert.closeTo(rect.top - (window.innerHeight - rect.bottom), 0, 5, 'centered vertically');
        });

        test('scrolling element with max-height respects max-height', function() {
          var el = fixture('sized-x');
          el.classList.add('with-max-height');
          makeScrolling(el);
          el.refit();
          var rect = el.getBoundingClientRect();
          assert.isTrue(rect.height <= 500, 'height is less than or equal to max-height');
        });

        test('css positioned, scrolling element is constrained to viewport height (top,left)', function() {
          var el = fixture('positioned-xy');
          makeScrolling(el);
          el.refit();
          var rect = el.getBoundingClientRect();
          assert.isTrue(rect.height <= window.innerHeight - 100, 'height is less than or equal to viewport height');
        });

        test('css positioned, scrolling element is constrained to viewport height (bottom, right)', function() {
          var el = fixture('sized-x');
          el.classList.add('positioned-bottom');
          el.classList.add('positioned-right');
          el.refit();
          var rect = el.getBoundingClientRect();
          assert.isTrue(rect.height <= window.innerHeight - 100, 'height is less than or equal to viewport height');
        });

        test('sized, scrolling element with margin is centered in viewport', function() {
          var el = fixture('sized-x');
          el.classList.add('with-margin');
          makeScrolling(el);
          el.refit();
          var rect = el.getBoundingClientRect();
          assert.closeTo(rect.left - (window.innerWidth - rect.right), 0, 5, 'centered horizontally');
          assert.closeTo(rect.top - (window.innerHeight - rect.bottom), 0, 5, 'centered vertically');
        });

        test('sized, scrolling element is constrained to viewport height', function() {
          var el = fixture('sized-x');
          el.classList.add('with-margin');
          makeScrolling(el);
          el.refit();
          var rect = el.getBoundingClientRect();
          assert.isTrue(rect.height <= window.innerHeight - 20 * 2, 'height is less than or equal to viewport height');
        });

        test('css positioned, scrolling element with margin is constrained to viewport height (top, left)', function() {
          var el = fixture('positioned-xy');
          el.classList.add('with-margin');
          makeScrolling(el);
          el.refit();
          var rect = el.getBoundingClientRect();
          assert.isTrue(rect.height <= window.innerHeight - 100 - 20 * 2, 'height is less than or equal to viewport height');
        });

        test('css positioned, scrolling element with margin is constrained to viewport height (bottom, right)', function() {
          var el = fixture('sized-x');
          el.classList.add('positioned-bottom');
          el.classList.add('positioned-right');
          el.classList.add('with-margin')
          el.refit();
          var rect = el.getBoundingClientRect();
          assert.isTrue(rect.height <= window.innerHeight - 100 - 20 * 2, 'height is less than or equal to viewport height');
        });

        test('scrolling sizingTarget is constrained to viewport height', function() {
          el = fixture('sectioned');
          var internal = Polymer.dom(el).querySelector('.internal');
          el.sizingTarget = internal;
          makeScrolling(internal);
          el.refit();
          var rect = el.getBoundingClientRect();
          assert.isTrue(rect.height <= window.innerHeight, 'height is less than or equal to viewport height');
        });

        test('scrolling sizingTarget preserves scrolling position', function() {
          el = fixture('scrollable');
          el.scrollTop = 20;
          el.scrollLeft = 20;
          el.refit();
          assert.equal(el.scrollTop, 20, 'scrollTop ok');
          assert.equal(el.scrollLeft, 20, 'scrollLeft ok');
        });

      });

      suite('fit to element', function() {

        test('element fits in another element', function() {
          var constrain = fixture('constrain-target');
          var el = Polymer.dom(constrain).querySelector('.el')
          makeScrolling(el);
          el.fitInto = constrain;
          el.refit();
          var rect = el.getBoundingClientRect();
          var crect = constrain.getBoundingClientRect();
          assert.isTrue(rect.height <= crect.height, 'width is less than or equal to fitInto width');
          assert.isTrue(rect.height <= crect.height, 'height is less than or equal to fitInto height');
        });

        test('element centers in another element', function() {
          var constrain = fixture('constrain-target');
          var el = Polymer.dom(constrain).querySelector('.el');
          makeScrolling(el);
          el.fitInto = constrain;
          el.refit();
          var rect = el.getBoundingClientRect();
          var crect = constrain.getBoundingClientRect();
          assert.closeTo(rect.left - crect.left - (crect.right - rect.right), 0, 5, 'centered horizontally in fitInto');
          assert.closeTo(rect.top - crect.top - (crect.bottom - rect.bottom), 0, 5, 'centered vertically in fitInto');
        });

        test('element with max-width centers in another element', function() {
          var constrain = document.querySelector('.constrain');
          var el = fixture('sized-xy');
          el.classList.add('with-max-width');
          el.fitInto = constrain;
          el.refit();
          var rect = el.getBoundingClientRect();
          var crect = constrain.getBoundingClientRect();
          assert.closeTo(rect.left - crect.left - (crect.right - rect.right), 0, 5, 'centered horizontally in fitInto');
          assert.closeTo(rect.top - crect.top - (crect.bottom - rect.bottom), 0, 5, 'centered vertically in fitInto');
        });

        test('positioned element fits in another element', function() {
          var constrain = document.querySelector('.constrain');
          // element's positionTarget is `body`, and fitInto is `constrain`.
          var el = fixture('sized-xy');
          el.verticalAlign = 'top';
          el.horizontalAlign = 'left';
          el.fitInto = constrain;
          el.refit();
          var rect = el.getBoundingClientRect();
          var crect = constrain.getBoundingClientRect();
          assert.equal(rect.top, crect.top, 'top ok');
          assert.equal(rect.left, crect.left, 'left ok');
        });

      });

      suite('horizontal/vertical align', function() {
        var parent, parentRect;
        var el, elRect;
        var fitRect = {
          left: 0,
          top: 0,
          right: window.innerWidth,
          bottom: window.innerHeight,
          width: window.innerWidth,
          height: window.innerHeight
        };

        setup(function() {
          parent = fixture('constrain-target');
          parentRect = parent.getBoundingClientRect();
          el = Polymer.dom(parent).querySelector('.el');
          elRect = el.getBoundingClientRect();
        });

        test('intersects works', function() {
          var base = {top: 0, bottom: 1, left:0, right: 1};
          assert.isTrue(intersects(base, base), 'intersects itself');
          assert.isFalse(intersects(base, {top:1, bottom: 2, left: 0, right: 1}), 'no intersect on edge');
          assert.isFalse(intersects(base, {top:-2, bottom: -1, left: 0, right: 1}), 'no intersect on edge (negative values)');
          assert.isFalse(intersects(base, {top:2, bottom: 3, left: 0, right: 1}), 'no intersect');
        });

        suite('when verticalAlign is top', function() {
          test('element is aligned to the positionTarget top', function() {
            el.verticalAlign = 'top';
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.top, parentRect.top, 'top ok');
            assert.equal(rect.height, elRect.height, 'no cropping');
          });

          test('element is aligned to the positionTarget top without overlapping it', function() {
            // Allow enough space on the parent's bottom & right.
            parent.style.width = '10px';
            parent.style.height = '10px';
            parentRect = parent.getBoundingClientRect();
            el.verticalAlign = 'top';
            el.noOverlap = true;
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.isFalse(intersects(rect, parentRect), 'no overlap');
            assert.equal(rect.height, elRect.height, 'no cropping');
          });

          test('element margin is considered as offset', function() {
            el.verticalAlign = 'top';
            el.style.marginTop = '10px';
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.top, parentRect.top + 10, 'top ok');
            assert.equal(rect.height, elRect.height, 'no cropping');

            el.style.marginTop = '-10px';
            el.refit();
            rect = el.getBoundingClientRect();
            assert.equal(rect.top, parentRect.top - 10, 'top ok');
            assert.equal(rect.height, elRect.height, 'no cropping');
          });

          test('verticalOffset is applied', function() {
            el.verticalAlign = 'top';
            el.verticalOffset = 10;
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.top, parentRect.top + 10, 'top ok');
            assert.equal(rect.height, elRect.height, 'no cropping');
          });

          test('element is kept in viewport', function() {
            el.verticalAlign = 'top';
            // Make it go out of screen
            el.verticalOffset = -1000;
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.top, 0, 'top in viewport');
            assert.isTrue(rect.height < elRect.height, 'reduced size');
          });

          test('negative verticalOffset does not crop element', function() {
            // Push to the bottom of the screen.
            parent.style.top = (window.innerHeight - 50) +'px';
            el.verticalAlign = 'top';
            el.verticalOffset = -10;
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.top, window.innerHeight - 60, 'top ok');
            assert.equal(rect.bottom, window.innerHeight, 'bottom ok');
          });

          test('max-height is updated', function() {
            parent.style.top = '-10px';
            el.verticalAlign = 'top';
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.top, 0, 'top ok');
            assert.isBelow(rect.height, elRect.height, 'height ok');
          });

          test('min-height is preserved: element is displayed even if partially', function() {
            parent.style.top = '-10px';
            el.verticalAlign = 'top';
            el.style.minHeight = elRect.height + 'px';
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.top, 0, 'top ok');
            assert.equal(rect.height, elRect.height, 'min-height ok');
            assert.isTrue(intersects(rect, fitRect), 'partially visible');
          });

          test('dynamicAlign will prefer bottom align if it minimizes the cropping', function() {
            parent.style.top = '-10px';
            parentRect = parent.getBoundingClientRect();
            el.verticalAlign = 'top';
            el.dynamicAlign = true;
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.bottom, parentRect.bottom, 'bottom ok');
            assert.equal(rect.height, elRect.height, 'no cropping');
          });
        });

        suite('when verticalAlign is bottom', function() {
          test('element is aligned to the positionTarget bottom', function() {
            el.verticalAlign = 'bottom';
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.bottom, parentRect.bottom, 'bottom ok');
            assert.equal(rect.height, elRect.height, 'no cropping');
          });

          test('element is aligned to the positionTarget bottom without overlapping it', function() {
            el.verticalAlign = 'bottom';
            el.noOverlap = true;
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.isFalse(intersects(rect, parentRect), 'no overlap');
            assert.equal(rect.height, elRect.height, 'no cropping');
          });

          test('element margin is considered as offset', function() {
            el.verticalAlign = 'bottom';
            el.style.marginBottom = '10px';
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.bottom, parentRect.bottom - 10, 'bottom ok');
            assert.equal(rect.height, elRect.height, 'no cropping');

            el.style.marginBottom = '-10px';
            el.refit();
            rect = el.getBoundingClientRect();
            assert.equal(rect.bottom, parentRect.bottom + 10, 'bottom ok');
            assert.equal(rect.height, elRect.height, 'no cropping');
          });

          test('verticalOffset is applied', function() {
            el.verticalAlign = 'bottom';
            el.verticalOffset = 10;
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.bottom, parentRect.bottom - 10, 'bottom ok');
            assert.equal(rect.height, elRect.height, 'no cropping');
          });

          test('element is kept in viewport', function() {
            el.verticalAlign = 'bottom';
            // Make it go out of screen
            el.verticalOffset = 1000;
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.top, 0, 'top in viewport');
            assert.isTrue(rect.height < elRect.height, 'reduced size');
          });

          test('element max-height is updated', function() {
            parent.style.top = (100 - parentRect.height) + 'px';
            el.verticalAlign = 'bottom';
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.bottom, 100, 'bottom ok');
            assert.equal(rect.height, 100, 'height ok');
          });

          test('min-height is preserved: element is displayed even if partially', function() {
            parent.style.top = (elRect.height - 10 - parentRect.height) + 'px';
            el.verticalAlign = 'bottom';
            el.style.minHeight = elRect.height + 'px';
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.top, 0, 'top ok');
            assert.equal(rect.height, elRect.height, 'min-height ok');
            assert.isTrue(intersects(rect, fitRect), 'partially visible');
          });

          test('dynamicAlign will prefer top align if it minimizes the cropping', function() {
            parent.style.top = (window.innerHeight - elRect.height) + 'px';
            parentRect = parent.getBoundingClientRect();
            el.verticalAlign = 'bottom';
            el.dynamicAlign = true;
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.top, parentRect.top, 'top ok');
            assert.equal(rect.height, elRect.height, 'no cropping');
          });
        });

        suite('when verticalAlign is auto', function() {
          test('element is aligned to the positionTarget top', function() {
            el.verticalAlign = 'auto';
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.top, parentRect.top, 'auto aligned to top');
            assert.equal(rect.height, elRect.height, 'no cropping');
          });

          test('element is aligned to the positionTarget top without overlapping it', function() {
            // Allow enough space on the parent's bottom & right.
            parent.style.width = '10px';
            parent.style.height = '10px';
            parentRect = parent.getBoundingClientRect();
            el.verticalAlign = 'auto';
            el.noOverlap = true;
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.height, elRect.height, 'no cropping');
            assert.isFalse(intersects(rect, parentRect), 'no overlap');
          });

          test('bottom is preferred to top if it diminishes the cropped area', function() {
            // This would cause a cropping of the element, so it should automatically
            // align to the bottom to avoid it.
            parent.style.top = '-10px';
            parentRect = parent.getBoundingClientRect();
            el.verticalAlign = 'auto';
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.bottom, parentRect.bottom, 'auto aligned to bottom');
            assert.equal(rect.height, elRect.height, 'no cropping');
          });

          test('bottom is preferred to top if it diminishes the cropped area, without overlapping positionTarget', function() {
            // This would cause a cropping of the element, so it should automatically
            // align to the bottom to avoid it.
            parent.style.top = '-10px';
            parentRect = parent.getBoundingClientRect();
            el.verticalAlign = 'auto';
            el.noOverlap = true;
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.height, elRect.height, 'no cropping');
            assert.isFalse(intersects(rect, parentRect), 'no overlap');
          });
        });

        suite('when horizontalAlign is left', function() {
          test('element is aligned to the positionTarget left', function() {
            el.horizontalAlign = 'left';
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.left, parentRect.left, 'left ok');
            assert.equal(rect.width, elRect.width, 'no cropping');
          });

          test('element is aligned to the positionTarget left without overlapping it', function() {
            // Make space at the parent's right.
            parent.style.width = '10px';
            parentRect = parent.getBoundingClientRect();
            el.horizontalAlign = 'left';
            el.noOverlap = true;
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.isFalse(intersects(rect, parentRect), 'no overlap');
            assert.equal(rect.width, elRect.width, 'no cropping');
          });

          test('element margin is considered as offset', function() {
            el.horizontalAlign = 'left';
            el.style.marginLeft = '10px';
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.left, parentRect.left + 10, 'left ok');
            assert.equal(rect.width, elRect.width, 'no cropping');

            el.style.marginLeft = '-10px';
            el.refit();
            rect = el.getBoundingClientRect();
            assert.equal(rect.left, parentRect.left - 10, 'left ok');
            assert.equal(rect.width, elRect.width, 'no cropping');
          });

          test('horizontalOffset is applied', function() {
            el.horizontalAlign = 'left';
            el.horizontalOffset = 10;
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.left, parentRect.left + 10, 'left ok');
            assert.equal(rect.width, elRect.width, 'no cropping');
          });

          test('element is kept in viewport', function() {
            el.horizontalAlign = 'left';
            // Make it go out of screen.
            el.horizontalOffset = -1000;
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.left, 0, 'left in viewport');
            assert.isTrue(rect.width < elRect.width, 'reduced size');
          });

          test('negative horizontalOffset does not crop element', function() {
            // Push to the bottom of the screen.
            parent.style.left = (window.innerWidth - 50) +'px';
            el.horizontalAlign = 'left';
            el.horizontalOffset = -10;
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.left, window.innerWidth - 60, 'left ok');
            assert.equal(rect.right, window.innerWidth, 'right ok');
          });

          test('element max-width is updated', function() {
            parent.style.left = '-10px';
            el.horizontalAlign = 'left';
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.left, 0, 'left ok');
            assert.isBelow(rect.width, elRect.width, 'width ok');
          });

          test('min-width is preserved: element is displayed even if partially', function() {
            parent.style.left = '-10px';
            el.style.minWidth = elRect.width + 'px';
            el.horizontalAlign = 'left';
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.left, 0, 'left ok');
            assert.equal(rect.width, elRect.width, 'min-width ok');
            assert.isTrue(intersects(rect, fitRect), 'partially visible');
          });

          test('dynamicAlign will prefer right align if it minimizes the cropping', function() {
            parent.style.left = '-10px';
            parentRect = parent.getBoundingClientRect();
            el.horizontalAlign = 'left';
            el.dynamicAlign = true;
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.right, parentRect.right, 'right ok');
            assert.equal(rect.height, elRect.height, 'no cropping');
          });

        });

        suite('when horizontalAlign is right', function() {
          test('element is aligned to the positionTarget right', function() {
            el.horizontalAlign = 'right';
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.right, parentRect.right, 'right ok');
            assert.equal(rect.width, elRect.width, 'no cropping');
          });

          test('element is aligned to the positionTarget right without overlapping it', function() {
            // Make space at the parent's left.
            parent.style.left = elRect.width + 'px';
            parentRect = parent.getBoundingClientRect();
            el.horizontalAlign = 'right';
            el.noOverlap = true;
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.isFalse(intersects(rect, parentRect), 'no overlap');
            assert.equal(rect.width, elRect.width, 'no cropping');
          });

          test('element margin is considered as offset', function() {
            el.horizontalAlign = 'right';
            el.style.marginRight = '10px';
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.right, parentRect.right - 10, 'right ok');
            assert.equal(rect.width, elRect.width, 'no cropping');

            el.style.marginRight = '-10px';
            el.refit();
            rect = el.getBoundingClientRect();
            assert.equal(rect.right, parentRect.right + 10, 'right ok');
            assert.equal(rect.width, elRect.width, 'no cropping');
          });

          test('horizontalOffset is applied', function() {
            el.horizontalAlign = 'right';
            el.horizontalOffset = 10;
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.right, parentRect.right - 10, 'right ok');
            assert.equal(rect.width, elRect.width, 'no cropping');
          });

          test('element is kept in viewport', function() {
            el.horizontalAlign = 'right';
            // Make it go out of screen.
            el.horizontalOffset = 1000;
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.left, 0, 'left in viewport');
            assert.isTrue(rect.width < elRect.width, 'reduced width');
          });

          test('element max-width is updated', function() {
            parent.style.left = (100 - parentRect.width) + 'px';
            el.horizontalAlign = 'right';
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.right, 100, 'right ok');
            assert.equal(rect.width, 100, 'width ok');
          });

          test('min-width is preserved: element is displayed even if partially', function() {
            parent.style.left = (elRect.width - 10 - parentRect.width) + 'px';
            el.horizontalAlign = 'right';
            el.style.minWidth = elRect.width + 'px';
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.left, 0, 'left ok');
            assert.equal(rect.width, elRect.width, 'min-width ok');
            assert.isTrue(intersects(rect, fitRect), 'partially visible');
          });

          test('dynamicAlign will prefer left align if it minimizes the cropping', function() {
            parent.style.left = (window.innerWidth - elRect.width) + 'px';
            parentRect = parent.getBoundingClientRect();
            el.horizontalAlign = 'right';
            el.dynamicAlign = true;
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.left, parentRect.left, 'left ok');
            assert.equal(rect.height, elRect.height, 'no cropping');
          });

        });

        suite('when horizontalAlign is auto', function() {
          test('element is aligned to the positionTarget left', function() {
            el.horizontalAlign = 'auto';
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.left, parentRect.left, 'auto aligned to left');
            assert.equal(rect.width, elRect.width, 'no cropping');
          });

          test('element is aligned to the positionTarget left without overlapping positionTarget', function() {
            // Make space at the parent's left.
            parent.style.left = elRect.width + 'px';
            parentRect = parent.getBoundingClientRect();
            el.horizontalAlign = 'auto';
            el.noOverlap = true;
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.width, elRect.width, 'no cropping');
            assert.isFalse(intersects(rect, parentRect), 'no overlap');
          });

          test('right is preferred to left if it diminishes the cropped area', function() {
            // This would cause a cropping of the element, so it should automatically
            // align to the right to avoid it.
            parent.style.left = '-10px';
            parentRect = parent.getBoundingClientRect();
            el.horizontalAlign = 'auto';
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.right, parentRect.right, 'auto aligned to right');
            assert.equal(rect.width, elRect.width, 'no cropping');
          });

          test('right is preferred to left if it diminishes the cropped area, without overlapping positionTarget', function() {
            // Make space at the parent's right.
            parent.style.width = '10px';
            parentRect = parent.getBoundingClientRect();
            el.horizontalAlign = 'auto';
            el.noOverlap = true;
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.width, elRect.width, 'no cropping');
            assert.isFalse(intersects(rect, parentRect), 'no overlap');
          });
        });

        suite('prefer horizontal overlap to vertical overlap', function() {
          setup(function() {
            el.noOverlap = true;
            el.dynamicAlign = true;
            // Make space around the positionTarget.
            parent.style.top = elRect.height + 'px';
            parent.style.left = elRect.width + 'px';
            parent.style.width = '10px';
            parent.style.height = '10px';
            parentRect = parent.getBoundingClientRect();
          });

          test('top-left aligns to target bottom-left', function() {
            el.verticalAlign = 'top';
            el.horizontalAlign = 'left';
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.left, parentRect.left, 'left ok');
            assert.equal(rect.top, parentRect.bottom, 'top ok');
          });

          test('top-right aligns to target bottom-right', function() {
            el.verticalAlign = 'top';
            el.horizontalAlign = 'right';
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.right, parentRect.right, 'right ok');
            assert.equal(rect.top, parentRect.bottom, 'top ok');
          });

          test('bottom-left aligns to target top-left', function() {
            el.verticalAlign = 'bottom';
            el.horizontalAlign = 'left';
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.left, parentRect.left, 'left ok');
            assert.equal(rect.bottom, parentRect.top, 'bottom ok');
          });

          test('bottom-right aligns to target top-right', function() {
            el.verticalAlign = 'bottom';
            el.horizontalAlign = 'right';
            el.refit();
            var rect = el.getBoundingClientRect();
            assert.equal(rect.right, parentRect.right, 'right ok');
            assert.equal(rect.bottom, parentRect.top, 'bottom ok');
          });

        });

      });
    </script>

  </body>
</html>