Debian Codegraph @

closure-library/closure/goog/testing/style/style_test.js

raw
1 // Copyright 2011 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.testing.styleTest');
16 goog.setTestOnly('goog.testing.styleTest');
17
18 goog.require('goog.dom');
19 goog.require('goog.dom.TagName');
20 goog.require('goog.style');
21 goog.require('goog.testing.jsunit');
22 goog.require('goog.testing.style');
23
24 var div1;
25 var div2;
26
27 function setUp() {
28 var createDiv = function(color) {
29 var div = goog.dom.createDom(goog.dom.TagName.DIV, {
30 style: 'position:absolute;top:0;left:0;' +
31 'width:200px;height:100px;' +
32 'background-color:' + color,
33 innerHTML: 'abc'
34 });
35 document.body.appendChild(div);
36 return div;
37 };
38
39 div1 = createDiv('#EEE');
40 div2 = createDiv('#F00');
41 }
42
43
44 function tearDown() {
45 if (div1.parentNode) div1.parentNode.removeChild(div1);
46 if (div2.parentNode) div2.parentNode.removeChild(div2);
47
48 div1 = null;
49 div2 = null;
50 }
51
52
53 function testIsVisible() {
54 assertTrue(
55 'The div should be detected as visible.',
56 goog.testing.style.isVisible(div1));
57
58 // Tests with hidden element
59 goog.style.setElementShown(div1, false /* display */);
60 assertFalse(
61 'The div should be detected as not visible.',
62 goog.testing.style.isVisible(div1));
63 }
64
65 function testIsOnScreen() {
66 var el = goog.dom.createElement(goog.dom.TagName.DIV);
67 document.body.appendChild(el);
68
69 var dom = goog.dom.getDomHelper(el);
70 var winScroll = dom.getDocumentScroll();
71 var winSize = dom.getViewportSize();
72
73 el.style.position = 'absolute';
74 goog.style.setSize(el, 100, 100);
75
76 goog.style.setPosition(el, winScroll.x, winScroll.y);
77 assertTrue(
78 'An element fully on the screen is on screen.',
79 goog.testing.style.isOnScreen(el));
80
81 goog.style.setPosition(el, winScroll.x - 10, winScroll.y - 10);
82 assertTrue(
83 'An element partially off the top-left of the screen is on screen.',
84 goog.testing.style.isOnScreen(el));
85
86 goog.style.setPosition(el, winScroll.x - 150, winScroll.y - 10);
87 assertFalse(
88 'An element completely off the top of the screen is off screen.',
89 goog.testing.style.isOnScreen(el));
90
91 goog.style.setPosition(el, winScroll.x - 10, winScroll.y - 150);
92 assertFalse(
93 'An element completely off the left of the screen is off screen.',
94 goog.testing.style.isOnScreen(el));
95
96 goog.style.setPosition(el, winScroll.x + winSize.width + 1, winScroll.y);
97 assertFalse(
98 'An element completely off the right of the screen is off screen.',
99 goog.testing.style.isOnScreen(el));
100
101 goog.style.setPosition(el, winScroll.x, winScroll.y + winSize.height + 1);
102 assertFalse(
103 'An element completely off the bottom of the screen is off screen.',
104 goog.testing.style.isOnScreen(el));
105
106 goog.style.setPosition(el, winScroll.x + winSize.width - 10, winScroll.y);
107 assertTrue(
108 'An element partially off the right of the screen is on screen.',
109 goog.testing.style.isOnScreen(el));
110
111 goog.style.setPosition(el, winScroll.x, winScroll.y + winSize.height - 10);
112 assertTrue(
113 'An element partially off the bottom of the screen is on screen.',
114 goog.testing.style.isOnScreen(el));
115
116 var el2 = goog.dom.createElement(goog.dom.TagName.DIV);
117 el2.style.position = 'absolute';
118 goog.style.setSize(el2, 100, 100);
119 goog.style.setPosition(el2, winScroll.x, winScroll.y);
120 assertFalse(
121 'An element not in the DOM is not on screen.',
122 goog.testing.style.isOnScreen(el2));
123 }
124
125 function testHasVisibleDimensions() {
126 goog.style.setSize(div1, 0, 0);
127 assertFalse(
128 '0x0 should not be considered visible dimensions.',
129 goog.testing.style.hasVisibleDimensions(div1));
130 goog.style.setSize(div1, 10, 0);
131 assertFalse(
132 '10x0 should not be considered visible dimensions.',
133 goog.testing.style.hasVisibleDimensions(div1));
134 goog.style.setSize(div1, 10, 10);
135 assertTrue(
136 '10x10 should be considered visible dimensions.',
137 goog.testing.style.hasVisibleDimensions(div1));
138 goog.style.setSize(div1, 0, 10);
139 assertFalse(
140 '0x10 should not be considered visible dimensions.',
141 goog.testing.style.hasVisibleDimensions(div1));
142 }
143
144 function testIntersects() {
145 // No intersection
146 goog.style.setPosition(div1, 0, 0);
147 goog.style.setPosition(div2, 500, 500);
148 assertFalse(
149 'The divs should not be determined to itersect.',
150 goog.testing.style.intersects(div1, div2));
151
152 // Some intersection
153 goog.style.setPosition(div1, 0, 0);
154 goog.style.setPosition(div2, 50, 50);
155 assertTrue(
156 'The divs should be determined to itersect.',
157 goog.testing.style.intersects(div1, div2));
158
159 // Completely superimposed.
160 goog.style.setPosition(div1, 0, 0);
161 goog.style.setPosition(div2, 0, 0);
162 assertTrue(
163 'The divs should be determined to itersect.',
164 goog.testing.style.intersects(div1, div2));
165 }