Debian Codegraph @

closure-library/closure/goog/editor/plugins/linkbubble_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.editor.plugins.LinkBubbleTest');
16 goog.setTestOnly('goog.editor.plugins.LinkBubbleTest');
17
18 goog.require('goog.dom');
19 goog.require('goog.dom.Range');
20 goog.require('goog.dom.TagName');
21 goog.require('goog.editor.Command');
22 goog.require('goog.editor.Link');
23 goog.require('goog.editor.plugins.LinkBubble');
24 goog.require('goog.events.BrowserEvent');
25 goog.require('goog.events.Event');
26 goog.require('goog.events.EventType');
27 goog.require('goog.events.KeyCodes');
28 goog.require('goog.string');
29 goog.require('goog.style');
30 goog.require('goog.testing.FunctionMock');
31 goog.require('goog.testing.PropertyReplacer');
32 goog.require('goog.testing.editor.FieldMock');
33 goog.require('goog.testing.editor.TestHelper');
34 goog.require('goog.testing.events');
35 goog.require('goog.testing.jsunit');
36 goog.require('goog.userAgent');
37
38 var fieldDiv;
39 var FIELDMOCK;
40 var linkBubble;
41 var link;
42 var linkChild;
43 var mockWindowOpen;
44 var stubs;
45 var testHelper;
46
47 function setUpPage() {
48 fieldDiv = goog.dom.$('field');
49 stubs = new goog.testing.PropertyReplacer();
50 testHelper = new goog.testing.editor.TestHelper(goog.dom.getElement('field'));
51 }
52
53 function setUp() {
54 testHelper.setUpEditableElement();
55 FIELDMOCK = new goog.testing.editor.FieldMock();
56
57 linkBubble = new goog.editor.plugins.LinkBubble();
58 linkBubble.fieldObject = FIELDMOCK;
59
60 link = fieldDiv.firstChild;
61 linkChild = link.lastChild;
62
63 mockWindowOpen = new goog.testing.FunctionMock('open');
64 stubs.set(window, 'open', mockWindowOpen);
65 }
66
67 function tearDown() {
68 linkBubble.closeBubble();
69 testHelper.tearDownEditableElement();
70 stubs.reset();
71 }
72
73 function testLinkSelected() {
74 FIELDMOCK.$replay();
75 linkBubble.enable(FIELDMOCK);
76 goog.dom.Range.createFromNodeContents(link).select();
77 linkBubble.handleSelectionChange();
78 assertBubble();
79 FIELDMOCK.$verify();
80 }
81
82 function testLinkClicked() {
83 FIELDMOCK.$replay();
84 linkBubble.enable(FIELDMOCK);
85 linkBubble.handleSelectionChange(createMouseEvent(link));
86 assertBubble();
87 FIELDMOCK.$verify();
88 }
89
90 function testImageLink() {
91 FIELDMOCK.$replay();
92 linkBubble.enable(FIELDMOCK);
93 link.setAttribute('imageanchor', 1);
94 linkBubble.handleSelectionChange(createMouseEvent(link));
95 assertBubble();
96 FIELDMOCK.$verify();
97 }
98
99 function closeBox() {
100 var closeBox = goog.dom.getElementsByTagNameAndClass(
101 goog.dom.TagName.DIV, 'tr_bubble_closebox');
102 assertEquals('Should find only one close box', 1, closeBox.length);
103 assertNotNull('Found close box', closeBox[0]);
104 goog.testing.events.fireClickSequence(closeBox[0]);
105 }
106
107 function testCloseBox() {
108 testLinkClicked();
109 closeBox();
110 assertNoBubble();
111 FIELDMOCK.$verify();
112 }
113
114 function testChangeClicked() {
115 FIELDMOCK.execCommand(
116 goog.editor.Command.MODAL_LINK_EDITOR, new goog.editor.Link(link, false));
117 FIELDMOCK.$registerArgumentListVerifier('execCommand', function(arr1, arr2) {
118 return arr1.length == arr2.length && arr1.length == 2 &&
119 arr1[0] == goog.editor.Command.MODAL_LINK_EDITOR &&
120 arr2[0] == goog.editor.Command.MODAL_LINK_EDITOR &&
121 arr1[1] instanceof goog.editor.Link &&
122 arr2[1] instanceof goog.editor.Link;
123 });
124 FIELDMOCK.$times(1);
125 FIELDMOCK.$returns(true);
126 FIELDMOCK.$replay();
127 linkBubble.enable(FIELDMOCK);
128
129 linkBubble.handleSelectionChange(createMouseEvent(link));
130 assertBubble();
131
132 goog.testing.events.fireClickSequence(
133 goog.dom.$(goog.editor.plugins.LinkBubble.CHANGE_LINK_ID_));
134 assertNoBubble();
135 FIELDMOCK.$verify();
136 }
137
138 function testChangePressed() {
139 FIELDMOCK.execCommand(
140 goog.editor.Command.MODAL_LINK_EDITOR, new goog.editor.Link(link, false));
141 FIELDMOCK.$registerArgumentListVerifier('execCommand', function(arr1, arr2) {
142 return arr1.length == arr2.length && arr1.length == 2 &&
143 arr1[0] == goog.editor.Command.MODAL_LINK_EDITOR &&
144 arr2[0] == goog.editor.Command.MODAL_LINK_EDITOR &&
145 arr1[1] instanceof goog.editor.Link &&
146 arr2[1] instanceof goog.editor.Link;
147 });
148 FIELDMOCK.$times(1);
149 FIELDMOCK.$returns(true);
150 FIELDMOCK.$replay();
151 linkBubble.enable(FIELDMOCK);
152
153 linkBubble.handleSelectionChange(createMouseEvent(link));
154 assertBubble();
155
156 var defaultPrevented = !goog.testing.events.fireKeySequence(
157 goog.dom.$(goog.editor.plugins.LinkBubble.CHANGE_LINK_ID_),
158 goog.events.KeyCodes.ENTER);
159 assertTrue(defaultPrevented);
160 assertNoBubble();
161 FIELDMOCK.$verify();
162 }
163
164 function testDeleteClicked() {
165 FIELDMOCK.dispatchBeforeChange();
166 FIELDMOCK.$times(1);
167 FIELDMOCK.dispatchChange();
168 FIELDMOCK.$times(1);
169 FIELDMOCK.focus();
170 FIELDMOCK.$times(1);
171 FIELDMOCK.$replay();
172
173 linkBubble.enable(FIELDMOCK);
174
175 linkBubble.handleSelectionChange(createMouseEvent(link));
176 assertBubble();
177
178 goog.testing.events.fireClickSequence(
179 goog.dom.$(goog.editor.plugins.LinkBubble.DELETE_LINK_ID_));
180 var element = goog.userAgent.GECKO ? document.body : fieldDiv;
181 assertNotEquals(
182 'Link removed', element.firstChild.nodeName, String(goog.dom.TagName.A));
183 assertNoBubble();
184 var range = goog.dom.Range.createFromWindow();
185 assertEquals('Link selection on link text', linkChild, range.getEndNode());
186 assertEquals(
187 'Link selection on link text end',
188 goog.dom.getRawTextContent(linkChild).length, range.getEndOffset());
189 FIELDMOCK.$verify();
190 }
191
192 function testDeletePressed() {
193 FIELDMOCK.dispatchBeforeChange();
194 FIELDMOCK.$times(1);
195 FIELDMOCK.dispatchChange();
196 FIELDMOCK.$times(1);
197 FIELDMOCK.focus();
198 FIELDMOCK.$times(1);
199 FIELDMOCK.$replay();
200
201 linkBubble.enable(FIELDMOCK);
202
203 linkBubble.handleSelectionChange(createMouseEvent(link));
204 assertBubble();
205
206 var defaultPrevented = !goog.testing.events.fireKeySequence(
207 goog.dom.$(goog.editor.plugins.LinkBubble.DELETE_LINK_ID_),
208 goog.events.KeyCodes.ENTER);
209 assertTrue(defaultPrevented);
210 var element = goog.userAgent.GECKO ? document.body : fieldDiv;
211 assertNotEquals(
212 'Link removed', element.firstChild.nodeName, String(goog.dom.TagName.A));
213 assertNoBubble();
214 var range = goog.dom.Range.createFromWindow();
215 assertEquals('Link selection on link text', linkChild, range.getEndNode());
216 assertEquals(
217 'Link selection on link text end',
218 goog.dom.getRawTextContent(linkChild).length, range.getEndOffset());
219 FIELDMOCK.$verify();
220 }
221
222 function testActionClicked() {
223 var SPAN = 'actionSpanId';
224 var LINK = 'actionLinkId';
225 var toShowCount = 0;
226 var actionCount = 0;
227
228 var linkAction = new goog.editor.plugins.LinkBubble.Action(
229 SPAN, LINK, 'message',
230 function() {
231 toShowCount++;
232 return toShowCount == 1; // Show it the first time.
233 },
234 function() { actionCount++; });
235
236 linkBubble = new goog.editor.plugins.LinkBubble(linkAction);
237 linkBubble.fieldObject = FIELDMOCK;
238 FIELDMOCK.$replay();
239 linkBubble.enable(FIELDMOCK);
240
241 // The first time the bubble is shown, show our custom action.
242 linkBubble.handleSelectionChange(createMouseEvent(link));
243 assertBubble();
244 assertEquals('Should check showing the action', 1, toShowCount);
245 assertEquals('Action should not have fired yet', 0, actionCount);
246
247 assertTrue(
248 'Action should be visible 1st time',
249 goog.style.isElementShown(goog.dom.$(SPAN)));
250 goog.testing.events.fireClickSequence(goog.dom.$(LINK));
251
252 assertEquals('Should not check showing again yet', 1, toShowCount);
253 assertEquals('Action should be fired', 1, actionCount);
254
255 closeBox();
256 assertNoBubble();
257
258 // The action won't be shown the second time around.
259 linkBubble.handleSelectionChange(createMouseEvent(link));
260 assertBubble();
261 assertEquals('Should check showing again', 2, toShowCount);
262 assertEquals('Action should not fire again', 1, actionCount);
263 assertFalse(
264 'Action should not be shown 2nd time',
265 goog.style.isElementShown(goog.dom.$(SPAN)));
266
267 FIELDMOCK.$verify();
268 }
269
270 function testLinkTextClicked() {
271 mockWindowOpen('http://www.google.com/', '_blank', '');
272 mockWindowOpen.$replay();
273 FIELDMOCK.$replay();
274 linkBubble.enable(FIELDMOCK);
275
276 linkBubble.handleSelectionChange(createMouseEvent(link));
277 assertBubble();
278
279 goog.testing.events.fireClickSequence(
280 goog.dom.$(goog.editor.plugins.LinkBubble.TEST_LINK_ID_));
281
282 assertBubble();
283 mockWindowOpen.$verify();
284 FIELDMOCK.$verify();
285 }
286
287 function testLinkTextClickedCustomUrlFn() {
288 mockWindowOpen('http://images.google.com/', '_blank', '');
289 mockWindowOpen.$replay();
290 FIELDMOCK.$replay();
291 linkBubble.enable(FIELDMOCK);
292
293 linkBubble.setTestLinkUrlFn(function(url) {
294 return url.replace('www', 'images');
295 });
296
297 linkBubble.handleSelectionChange(createMouseEvent(link));
298 assertBubble();
299
300 goog.testing.events.fireClickSequence(
301 goog.dom.$(goog.editor.plugins.LinkBubble.TEST_LINK_ID_));
302
303 assertBubble();
304 mockWindowOpen.$verify();
305 FIELDMOCK.$verify();
306 }
307
308
309 /**
310 * Urls with invalid schemes shouldn't be linkified.
311 * @bug 2585360
312 */
313 function testDontLinkifyInvalidScheme() {
314 mockWindowOpen.$replay();
315 FIELDMOCK.$replay();
316 linkBubble.enable(FIELDMOCK);
317
318 var badLink = goog.dom.createElement(goog.dom.TagName.A);
319 badLink.href = 'javascript:alert(1)';
320 goog.dom.setTextContent(badLink, 'bad link');
321
322 linkBubble.handleSelectionChange(createMouseEvent(badLink));
323 assertBubble();
324
325 // The link shouldn't exist at all
326 assertNull(goog.dom.$(goog.editor.plugins.LinkBubble.TEST_LINK_ID_));
327
328 assertBubble();
329 mockWindowOpen.$verify();
330 FIELDMOCK.$verify();
331 }
332
333 function testIsSafeSchemeToOpen() {
334 // Urls with no scheme at all are ok too since 'http://' will be prepended.
335 var good = [
336 'http://google.com', 'http://google.com/', 'https://google.com',
337 '[email protected]', 'http://www.google.com', 'http://site.com', 'google.com',
338 'google', 'http://google', 'HTTP://GOOGLE.COM', 'HtTp://www.google.com'
339 ];
340
341 var bad = [
342 'javascript:google.com', 'httpp://google.com', 'data:foo',
343 'javascript:alert(\'hi\');', 'abc:def'
344 ];
345
346 for (var i = 0; i < good.length; i++) {
347 assertTrue(
348 good[i] + ' should have a safe scheme',
349 linkBubble.isSafeSchemeToOpen_(good[i]));
350 }
351
352 for (i = 0; i < bad.length; i++) {
353 assertFalse(
354 bad[i] + ' should have an unsafe scheme',
355 linkBubble.isSafeSchemeToOpen_(bad[i]));
356 }
357 }
358
359 function testShouldOpenWithWhitelist() {
360 linkBubble.setSafeToOpenSchemes(['abc']);
361
362 assertTrue(
363 'Scheme should be safe', linkBubble.shouldOpenUrl('abc://google.com'));
364 assertFalse(
365 'Scheme should be unsafe', linkBubble.shouldOpenUrl('http://google.com'));
366
367 linkBubble.setBlockOpeningUnsafeSchemes(false);
368 assertTrue(
369 'Non-whitelisted should now be safe after disabling blocking',
370 linkBubble.shouldOpenUrl('http://google.com'));
371 }
372
373
374 /**
375 * @bug 763211
376 * @bug 2182147
377 */
378 function testLongUrlTestLinkAnchorTextCorrect() {
379 FIELDMOCK.$replay();
380 linkBubble.enable(FIELDMOCK);
381
382 var longUrl = 'http://www.reallylonglinkthatshouldbetruncated' +
383 'becauseitistoolong.com';
384 var truncatedLongUrl = goog.string.truncateMiddle(longUrl, 48);
385
386 var longLink = goog.dom.createElement(goog.dom.TagName.A);
387 longLink.href = longUrl;
388 goog.dom.setTextContent(longLink, 'Google');
389 fieldDiv.appendChild(longLink);
390
391 linkBubble.handleSelectionChange(createMouseEvent(longLink));
392 assertBubble();
393
394 var testLinkEl = goog.dom.$(goog.editor.plugins.LinkBubble.TEST_LINK_ID_);
395 assertEquals(
396 'The test link\'s anchor text should be the truncated URL.',
397 truncatedLongUrl, testLinkEl.innerHTML);
398
399 fieldDiv.removeChild(longLink);
400 FIELDMOCK.$verify();
401 }
402
403
404 /**
405 * @bug 2416024
406 */
407 function testOverridingCreateBubbleContentsDoesntNpeGetTargetUrl() {
408 FIELDMOCK.$replay();
409 linkBubble.enable(FIELDMOCK);
410
411 stubs.set(linkBubble, 'createBubbleContents', function(elem) {
412 // getTargetUrl would cause an NPE if urlUtil_ wasn't defined yet.
413 linkBubble.getTargetUrl();
414 });
415 assertNotThrows(
416 'Accessing this.urlUtil_ should not NPE',
417 goog.bind(
418 linkBubble.handleSelectionChange, linkBubble,
419 createMouseEvent(link)));
420
421 FIELDMOCK.$verify();
422 }
423
424
425 /**
426 * @bug 15379294
427 */
428 function testUpdateLinkCommandDoesNotTriggerAnException() {
429 FIELDMOCK.$replay();
430 linkBubble.enable(FIELDMOCK);
431
432 // At this point, the bubble was not created yet using its createBubble
433 // public method.
434 assertNotThrows(
435 'Executing goog.editor.Command.UPDATE_LINK_BUBBLE should not trigger ' +
436 'an exception even if the bubble was not created yet using its ' +
437 'createBubble method.',
438 goog.bind(
439 linkBubble.execCommandInternal, linkBubble,
440 goog.editor.Command.UPDATE_LINK_BUBBLE));
441
442 FIELDMOCK.$verify();
443 }
444
445 function assertBubble() {
446 assertTrue('Link bubble visible', linkBubble.isVisible());
447 assertNotNull(
448 'Link bubble created',
449 goog.dom.$(goog.editor.plugins.LinkBubble.LINK_DIV_ID_));
450 }
451
452 function assertNoBubble() {
453 assertFalse('Link bubble not visible', linkBubble.isVisible());
454 assertNull(
455 'Link bubble not created',
456 goog.dom.$(goog.editor.plugins.LinkBubble.LINK_DIV_ID_));
457 }
458
459 function createMouseEvent(target) {
460 var eventObj = new goog.events.Event(goog.events.EventType.MOUSEUP, target);
461 eventObj.button = goog.events.BrowserEvent.MouseButton.LEFT;
462
463 return new goog.events.BrowserEvent(eventObj, target);
464 }