aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/testing/style/style_test.html
blob: 70748ff70d6ec30b30b2b11db53af2ae9b64be54 (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
<!DOCTYPE html>
<html>
<!--
Copyright 2011 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>goog.testing.style Tests</title>
  <script src="../../base.js"></script>
  <script>
    goog.require('goog.dom');
    goog.require('goog.style');
    goog.require('goog.testing.jsunit');
    goog.require('goog.testing.style');
  </script>
</head>
<body>
  <script>

  var div1;
  var div2;

  function setUp() {
    var createDiv = function(color) {
      var div = goog.dom.createDom(
        'div',
        {
          style: 'position:absolute;top:0;left:0;' +
                 'width:200px;height:100px;' +
                  'background-color:' + color,
          innerHTML: 'abc'
      });
      document.body.appendChild(div);
      return div;
    };

    div1 = createDiv('#EEE');
    div2 = createDiv('#F00');
  }


  function tearDown() {
    if (div1.parentNode)
      div1.parentNode.removeChild(div1);
    if (div2.parentNode)
      div2.parentNode.removeChild(div2);

    div1 = null;
    div2 = null;
  }


  function testIsVisible() {
    assertTrue('The div should be detected as visible.',
               goog.testing.style.isVisible(div1));

    // Tests with hidden element
    goog.style.showElement(div1, false /* display */);
    assertFalse('The div should be detected as not visible.',
                goog.testing.style.isVisible(div1));
  }

  function testIsOnScreen() {
    var el = document.createElement('div');
    document.body.appendChild(el);

    var dom = goog.dom.getDomHelper(el);
    var winScroll = dom.getDocumentScroll();
    var winSize = dom.getViewportSize();

    el.style.position = 'absolute';
    goog.style.setSize(el, 100, 100);

    goog.style.setPosition(el, winScroll.x, winScroll.y);
    assertTrue("An element fully on the screen is on screen.",
                goog.testing.style.isOnScreen(el));

    goog.style.setPosition(el, winScroll.x - 10, winScroll.y - 10);
    assertTrue(
        "An element partially off the top-left of the screen is on screen.",
        goog.testing.style.isOnScreen(el));

    goog.style.setPosition(el, winScroll.x - 150, winScroll.y - 10);
    assertFalse(
        "An element completely off the top of the screen is off screen.",
        goog.testing.style.isOnScreen(el));

    goog.style.setPosition(el, winScroll.x - 10, winScroll.y - 150);
    assertFalse(
        "An element completely off the left of the screen is off screen.",
        goog.testing.style.isOnScreen(el));

    goog.style.setPosition(el, winScroll.x + winSize.width + 1, winScroll.y);
    assertFalse(
        "An element completely off the right of the screen is off screen.",
        goog.testing.style.isOnScreen(el));

    goog.style.setPosition(el, winScroll.x, winScroll.y + winSize.height + 1);
    assertFalse(
        "An element completely off the bottom of the screen is off screen.",
        goog.testing.style.isOnScreen(el));

    goog.style.setPosition(el, winScroll.x + winSize.width - 10, winScroll.y);
    assertTrue(
        "An element partially off the right of the screen is on screen.",
        goog.testing.style.isOnScreen(el));

    goog.style.setPosition(el, winScroll.x, winScroll.y + winSize.height - 10);
    assertTrue(
        "An element partially off the bottom of the screen is on screen.",
        goog.testing.style.isOnScreen(el));

    var el2 = document.createElement('div');
    el2.style.position = 'absolute';
    goog.style.setSize(el2, 100, 100);
    goog.style.setPosition(el2, winScroll.x, winScroll.y);
    assertFalse("An element not in the DOM is not on screen.",
                goog.testing.style.isOnScreen(el2));
  }

  function testHasVisibleDimensions() {
    goog.style.setSize(div1, 0, 0);
    assertFalse('0x0 should not be considered visible dimensions.',
                goog.testing.style.hasVisibleDimensions(div1));
    goog.style.setSize(div1, 10, 0);
    assertFalse('10x0 should not be considered visible dimensions.',
                goog.testing.style.hasVisibleDimensions(div1));
    goog.style.setSize(div1, 10, 10);
    assertTrue('10x10 should be considered visible dimensions.',
               goog.testing.style.hasVisibleDimensions(div1));
    goog.style.setSize(div1, 0, 10);
    assertFalse('0x10 should not be considered visible dimensions.',
                goog.testing.style.hasVisibleDimensions(div1));
  }

  function testIntersects() {
    // No intersection
    goog.style.setPosition(div1, 0, 0);
    goog.style.setPosition(div2, 500, 500);
    assertFalse('The divs should not be determined to itersect.',
                goog.testing.style.intersects(div1, div2));

    // Some intersection
    goog.style.setPosition(div1, 0, 0);
    goog.style.setPosition(div2, 50, 50);
    assertTrue('The divs should be determined to itersect.',
               goog.testing.style.intersects(div1, div2));

    // Completely superimposed.
    goog.style.setPosition(div1, 0, 0);
    goog.style.setPosition(div2, 0, 0);
    assertTrue('The divs should be determined to itersect.',
               goog.testing.style.intersects(div1, div2));
  }

  </script>
</body>
</html>