Debian Codegraph @

closure-library/closure/goog/ui/editor/bubble_test.js

raw
1 // Copyright 2008 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.ui.editor.BubbleTest');
16 goog.setTestOnly('goog.ui.editor.BubbleTest');
17
18 goog.require('goog.dom');
19 goog.require('goog.dom.TagName');
20 goog.require('goog.events');
21 goog.require('goog.events.EventType');
22 goog.require('goog.positioning.Corner');
23 goog.require('goog.positioning.OverflowStatus');
24 goog.require('goog.string');
25 goog.require('goog.style');
26 goog.require('goog.testing.editor.TestHelper');
27 goog.require('goog.testing.events');
28 goog.require('goog.testing.jsunit');
29 goog.require('goog.ui.Component');
30 goog.require('goog.ui.editor.Bubble');
31 goog.require('goog.userAgent.product');
32
33 var testHelper;
34 var fieldDiv;
35 var bubble;
36 var link;
37 var link2;
38 var panelId;
39
40 function setUpPage() {
41 fieldDiv = goog.dom.getElement('field');
42 var viewportSize = goog.dom.getViewportSize();
43 // Some tests depends on enough size of viewport.
44 if (viewportSize.width < 600 || viewportSize.height < 440) {
45 window.moveTo(0, 0);
46 window.resizeTo(640, 480);
47 }
48 }
49
50 function setUp() {
51 testHelper = new goog.testing.editor.TestHelper(fieldDiv);
52 testHelper.setUpEditableElement();
53
54 bubble = new goog.ui.editor.Bubble(document.body, 999);
55
56 fieldDiv.innerHTML = '<a href="http://www.google.com">Google</a>' +
57 '<a href="http://www.google.com">Google2</a>';
58 link = fieldDiv.firstChild;
59 link2 = fieldDiv.lastChild;
60
61 window.scrollTo(0, 0);
62 goog.style.setStyle(document.body, 'direction', 'ltr');
63 goog.style.setStyle(document.getElementById('field'), 'position', 'static');
64 }
65
66 function tearDown() {
67 if (panelId) {
68 bubble.removePanel(panelId);
69 panelId = null;
70 }
71 testHelper.tearDownEditableElement();
72 }
73
74
75 /**
76 * This is a helper function for setting up the target element with a
77 * given direction.
78 *
79 * @param {string} dir The direction of the target element, 'ltr' or 'rtl'.
80 * @param {boolean=} opt_preferTopPosition Whether to prefer placing the bubble
81 * above the element instead of below it. Defaults to preferring below.
82 */
83 function prepareTargetWithGivenDirection(dir, opt_preferTopPosition) {
84 goog.style.setStyle(document.body, 'direction', dir);
85
86 fieldDiv.style.direction = dir;
87 fieldDiv.innerHTML = '<a href="http://www.google.com">Google</a>';
88 link = fieldDiv.firstChild;
89
90 panelId = bubble.addPanel('A', 'Link', link, function(el) {
91 el.innerHTML = '<div style="border:1px solid blue;">B</div>';
92 }, opt_preferTopPosition);
93 }
94
95
96 /**
97 * This is a helper function for getting the expected position of the bubble.
98 * (align to the right or the left of the target element). Align left by
99 * default and align right if opt_alignRight is true. The expected Y is
100 * unaffected by alignment.
101 *
102 * @param {boolean=} opt_alignRight Sets the expected alignment to be right.
103 */
104 function getExpectedBubblePositionWithGivenAlignment(opt_alignRight) {
105 var targetPosition = goog.style.getFramedPageOffset(link, window);
106 var targetWidth = link.offsetWidth;
107 var bubbleSize = goog.style.getSize(bubble.bubbleContainer_);
108 var expectedBubbleX = opt_alignRight ?
109 targetPosition.x + targetWidth - bubbleSize.width :
110 targetPosition.x;
111 var expectedBubbleY = link.offsetHeight + targetPosition.y +
112 goog.ui.editor.Bubble.VERTICAL_CLEARANCE_;
113
114 return {x: expectedBubbleX, y: expectedBubbleY};
115 }
116
117 function testCreateBubbleWithLinkPanel() {
118 var id = goog.string.createUniqueString();
119 panelId = bubble.addPanel('A', 'Link', link, function(container) {
120 container.innerHTML = '<span id="' + id + '">Test</span>';
121 });
122 assertNotNull('Bubble should be created', bubble.bubbleContents_);
123 assertNotNull('Added element should be present', goog.dom.getElement(id));
124 assertTrue('Bubble should be visible', bubble.isVisible());
125 }
126
127 function testCloseBubble() {
128 testCreateBubbleWithLinkPanel();
129
130 var count = 0;
131 goog.events.listen(
132 bubble, goog.ui.Component.EventType.HIDE, function() { count++; });
133
134 bubble.removePanel(panelId);
135 panelId = null;
136
137 assertFalse('Bubble should not be visible', bubble.isVisible());
138 assertEquals('Hide event should be dispatched', 1, count);
139 }
140
141 function testCloseBox() {
142 testCreateBubbleWithLinkPanel();
143
144 var count = 0;
145 goog.events.listen(
146 bubble, goog.ui.Component.EventType.HIDE, function() { count++; });
147
148 var closeBox = goog.dom.getElementsByTagNameAndClass(
149 goog.dom.TagName.DIV, 'tr_bubble_closebox', bubble.bubbleContainer_)[0];
150 goog.testing.events.fireClickSequence(closeBox);
151 panelId = null;
152
153 assertFalse('Bubble should not be visible', bubble.isVisible());
154 assertEquals('Hide event should be dispatched', 1, count);
155 }
156
157 function testViewPortSizeMonitorEvent() {
158 testCreateBubbleWithLinkPanel();
159
160 var numCalled = 0;
161 bubble.reposition = function() { numCalled++; };
162
163 assertNotUndefined(
164 'viewPortSizeMonitor_ should not be undefined',
165 bubble.viewPortSizeMonitor_);
166 bubble.viewPortSizeMonitor_.dispatchEvent(goog.events.EventType.RESIZE);
167
168 assertEquals('reposition not called', 1, numCalled);
169 }
170
171 function testBubblePositionPreferTop() {
172 called = false;
173 bubble.positionAtAnchor_ = function(targetCorner, bubbleCorner, overflow) {
174 called = true;
175
176 // Assert that the bubble is positioned below the target.
177 assertEquals(goog.positioning.Corner.TOP_START, targetCorner);
178 assertEquals(goog.positioning.Corner.BOTTOM_START, bubbleCorner);
179
180 return goog.positioning.OverflowStatus.NONE;
181 };
182 prepareTargetWithGivenDirection('ltr', true);
183 assertTrue(called);
184 }
185
186 function testBubblePosition() {
187 panelId = bubble.addPanel('A', 'Link', link, goog.nullFunction);
188 var CLEARANCE = goog.ui.editor.Bubble.VERTICAL_CLEARANCE_;
189 var bubbleContainer = bubble.bubbleContainer_;
190
191 // The field is at a normal place, alomost the top of the viewport, and
192 // there is enough space at the bottom of the field.
193 var targetPos = goog.style.getFramedPageOffset(link, window);
194 var targetSize = goog.style.getSize(link);
195 var pos = goog.style.getFramedPageOffset(bubbleContainer);
196 assertEquals(targetPos.y + targetSize.height + CLEARANCE, pos.y);
197 assertEquals(targetPos.x, pos.x);
198
199 // Move the target to the bottom of the viewport.
200 var field = document.getElementById('field');
201 var fieldPos = goog.style.getFramedPageOffset(field, window);
202 fieldPos.y +=
203 bubble.dom_.getViewportSize().height - (targetPos.y + targetSize.height);
204 goog.style.setStyle(field, 'position', 'absolute');
205 goog.style.setPosition(field, fieldPos);
206 bubble.reposition();
207 var bubbleSize = goog.style.getSize(bubbleContainer);
208 targetPosition = goog.style.getFramedPageOffset(link, window);
209 pos = goog.style.getFramedPageOffset(bubbleContainer);
210 assertEquals(targetPosition.y - CLEARANCE - bubbleSize.height, pos.y);
211 }
212
213 function testBubblePositionRightAligned() {
214 if (goog.userAgent.product.SAFARI) {
215 // TODO(b/20733468): Disabled so we can get the rest of the Closure test
216 // suite running in a continuous build. Will investigate later.
217 return;
218 }
219
220 prepareTargetWithGivenDirection('rtl');
221
222 var expectedPos = getExpectedBubblePositionWithGivenAlignment(true);
223 var pos = goog.style.getFramedPageOffset(bubble.bubbleContainer_);
224 assertRoughlyEquals(expectedPos.x, pos.x, 0.1);
225 assertRoughlyEquals(expectedPos.y, pos.y, 0.1);
226 }
227
228
229 /**
230 * Test for bug 1955511, the bubble should align to the right side
231 * of the target element when the bubble is RTL, regardless of the
232 * target element's directionality.
233 */
234 function testBubblePositionLeftToRight() {
235 if (goog.userAgent.product.SAFARI) {
236 // TODO(b/20733468): Disabled so we can get the rest of the Closure test
237 // suite running in a continuous build. Will investigate later.
238 return;
239 }
240
241 goog.style.setStyle(bubble.bubbleContainer_, 'direction', 'ltr');
242 prepareTargetWithGivenDirection('rtl');
243
244 var expectedPos = getExpectedBubblePositionWithGivenAlignment();
245 var pos = goog.style.getFramedPageOffset(bubble.bubbleContainer_);
246 assertRoughlyEquals(expectedPos.x, pos.x, 0.1);
247 assertRoughlyEquals(expectedPos.y, pos.y, 0.1);
248 }
249
250
251 /**
252 * Test for bug 1955511, the bubble should align to the left side
253 * of the target element when the bubble is LTR, regardless of the
254 * target element's directionality.
255 */
256 function testBubblePositionRightToLeft() {
257 goog.style.setStyle(bubble.bubbleContainer_, 'direction', 'rtl');
258 prepareTargetWithGivenDirection('ltr');
259
260 var expectedPos = getExpectedBubblePositionWithGivenAlignment(true);
261 var pos = goog.style.getFramedPageOffset(bubble.bubbleContainer_);
262 assertEquals(expectedPos.x, pos.x);
263 assertEquals(expectedPos.y, pos.y);
264 }