aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/positioning/viewportclientposition_test.html
blob: dcd8de566c9048d9c3940f8f2347db2b6bd91098 (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
<!DOCTYPE HTML>
<!--

  Author: eae@google.com (Emil A Eklund)
-->
<html>
<!--
Copyright 2009 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Closure Unit Tests - goog.positioning.ViewportClientPosition</title>
  <script src="../base.js"></script>
  <script>
    goog.require('goog.dom');
    goog.require('goog.positioning.ViewportClientPosition');
    goog.require('goog.style');
    goog.require('goog.testing.jsunit');
    goog.require('goog.userAgent');
  </script>
</head>
<body>
  <!--
    Uses an iframe to avoid window size problems when running under Selenium.
  -->
  <iframe id="frame1" style="width: 200px; height: 200px;"
          src="anchoredviewportposition_test_iframe.html">
  </iframe>

  <script>
    var viewportSize, anchor, popup, dom, frameRect;
    var corner = goog.positioning.Corner;

    // Allow positions to be off by one in gecko as it reports scrolling
    // offsets in steps of 2.
    var ALLOWED_OFFSET = goog.userAgent.GECKO ? 1 : 0;


    function setUp() {
      var frame = document.getElementById('frame1');
      var doc = goog.dom.getFrameContentDocument(frame);

      dom = goog.dom.getDomHelper(doc);
      viewportSize = dom.getViewportSize();
      anchor = dom.getElement('anchor');
      popup = dom.getElement('popup');
      popup.style.overflowY = 'visible';
      goog.style.setSize(popup, 20, 20);
      frameRect = goog.style.getVisibleRectForElement(doc.body);
    }


    function testPositionAtCoordinateTopLeft() {
      var pos = new goog.positioning.ViewportClientPosition(100, 100);
      pos.reposition(popup, corner.TOP_LEFT);

      var offset = goog.style.getPageOffset(popup);
      assertEquals('Left edge of popup should be at specified x coordinate.',
                   100,
                   offset.x);
      assertEquals('Top edge of popup should be at specified y coordinate.',
                   100,
                   offset.y);
    }


    function testPositionAtCoordinateBottomRight() {
      var pos = new goog.positioning.ViewportClientPosition(100, 100);
      pos.reposition(popup, corner.BOTTOM_RIGHT);

      var bounds = goog.style.getBounds(popup);
      assertEquals('Right edge of popup should be at specified x coordinate.',
                   100,
                   bounds.left + bounds.width);
      assertEquals('Bottom edge of popup should be at specified x coordinate.',
                   100,
                   bounds.top + bounds.height);
    }


    function testPositionAtCoordinateTopLeftWithScroll() {
      dom.getDocument().body.style.paddingTop = '300px';
      dom.getDocument().body.style.height = '3000px';
      dom.getDocumentScrollElement().scrollTop = 50;
      dom.getDocument().body.scrollTop = 50;

      var pos = new goog.positioning.ViewportClientPosition(0, 0);
      pos.reposition(popup, corner.TOP_LEFT);

      var offset = goog.style.getPageOffset(popup);
      assertEquals('Left edge of popup should be at specified x coordinate.',
                   0,
                   offset.x);
      assertTrue('Top edge of popup should be at specified y coordinate ' +
                 'adjusted for scroll.',
                 Math.abs(offset.y - 50) <= ALLOWED_OFFSET);

      dom.getDocument().body.style.paddingLeft = '1000px';
      dom.getDocumentScrollElement().scrollLeft = 500;

      pos.reposition(popup, corner.TOP_LEFT);
      offset = goog.style.getPageOffset(popup);
      assertTrue('Left edge of popup should be at specified x coordinate ' +
                 'adjusted for scroll.',
                 Math.abs(offset.x - 500) <= ALLOWED_OFFSET);

      dom.getDocumentScrollElement().scrollLeft = 0;
      dom.getDocumentScrollElement().scrollTop = 0;
      dom.getDocument().body.style.paddingLeft = '';
      dom.getDocument().body.style.paddingTop = '';

      pos.reposition(popup, corner.TOP_LEFT);
      offset = goog.style.getPageOffset(popup);
      assertEquals('Left edge of popup should be at specified x coordinate.',
                   0,
                   offset.x);
      assertEquals('Top edge of popup should be at specified y coordinate.',
                   0,
                   offset.y);
    }


    function testOverflowRightFlipHor() {
      var pos = new goog.positioning.ViewportClientPosition(frameRect.right,
                                                            100);
      pos.reposition(popup, corner.TOP_LEFT);

      var offset = goog.style.getPageOffset(popup);
      assertEquals('Left edge of popup should have been adjusted so that it ' +
                  'fits inside the viewport.',
                   frameRect.right - popup.offsetWidth,
                   offset.x);
      assertEquals('Top edge of popup should be at specified y coordinate.',
                   100,
                   offset.y);
    }


    function testOverflowTopFlipVer() {
      var pos = new goog.positioning.ViewportClientPosition(100, 0);
      pos.reposition(popup, corner.TOP_RIGHT);

      var offset = goog.style.getPageOffset(popup);
      assertEquals('Left edge of popup should be at specified x coordinate.',
                   80,
                   offset.x);
      assertEquals('Top edge of popup should have been adjusted so that it ' +
                  'fits inside the viewport.',
                   0,
                   offset.y);
    }


    function testOverflowBottomRightFlipBoth() {
      var pos = new goog.positioning.ViewportClientPosition(frameRect.right,
                                                            frameRect.bottom);
      pos.reposition(popup, corner.TOP_LEFT);

      var offset = goog.style.getPageOffset(popup);
      assertEquals('Left edge of popup should have been adjusted so that it ' +
                  'fits inside the viewport.',
                   frameRect.right - popup.offsetWidth,
                   offset.x);
      assertEquals('Top edge of popup should have been adjusted so that it ' +
                  'fits inside the viewport.',
                   frameRect.bottom - popup.offsetHeight,
                   offset.y);
    }


    function testLastRespotOverflow() {
      var large = 2000;
      goog.style.setSize(popup, 20, large);
      popup.style.overflowY = 'auto';

      var pos = new goog.positioning.ViewportClientPosition(0, 0);
      pos.reposition(popup, corner.TOP_LEFT);

      assertEquals(large, popup.offsetHeight);
      pos.setLastResortOverflow(goog.positioning.Overflow.RESIZE_HEIGHT);
      pos.reposition(popup, corner.TOP_LEFT);
      assertNotEquals(large, popup.offsetHeight);
    }
  </script>
</body>
</html>