aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/editor/plugins/linkbubble_test.html
blob: f755940e3c378401fec4de7e35a6a3010c340d20 (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<!DOCTYPE html>
<!--
  All Rights Reserved.

  @author tildahl@google.com (Michael Tildahl)
-->
<html>
<!--
Copyright 2008 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.editor.plugins.LinkBubble Tests</title>
<script src="../../base.js"></script>
<script>
  goog.require('goog.dom');
  goog.require('goog.dom.TagName');
  goog.require('goog.editor.Link');
  goog.require('goog.events.BrowserEvent');
  goog.require('goog.events.Event');
  goog.require('goog.events.KeyCodes');
  goog.require('goog.style');
  goog.require('goog.testing.FunctionMock');
  goog.require('goog.testing.editor.FieldMock');
  goog.require('goog.testing.editor.TestHelper');
  goog.require('goog.testing.events');
  goog.require('goog.testing.jsunit');
  goog.require('goog.userAgent');
  goog.require('goog.editor.plugins.LinkBubble');
</script>
</head>
<body>

<div id="field"><a href="http://www.google.com/">Google</a></div>

<script>

var fieldDiv = goog.dom.$('field');
var FIELDMOCK;
var linkBubble;
var link;
var mockWindowOpen;
var stubs = new goog.testing.PropertyReplacer();
var testHelper = new goog.testing.editor.TestHelper(
    goog.dom.getElement('field'));

function setUp() {
  testHelper.setUpEditableElement();
  FIELDMOCK = new goog.testing.editor.FieldMock();

  linkBubble = new goog.editor.plugins.LinkBubble();
  linkBubble.fieldObject = FIELDMOCK;

  link = fieldDiv.firstChild;

  mockWindowOpen = new goog.testing.FunctionMock('open');
  stubs.set(window, 'open', mockWindowOpen);
}

function tearDown() {
  linkBubble.closeBubble();
  testHelper.tearDownEditableElement();
  stubs.reset();
}

function testLinkSelected() {
  FIELDMOCK.$replay();
  linkBubble.enable(FIELDMOCK);
  goog.dom.Range.createFromNodeContents(link).select();
  linkBubble.handleSelectionChange();
  assertBubble();
  FIELDMOCK.$verify();
}

function testLinkClicked() {
  FIELDMOCK.$replay();
  linkBubble.enable(FIELDMOCK);
  linkBubble.handleSelectionChange(createMouseEvent(link));
  assertBubble();
  FIELDMOCK.$verify();
}

function testImageLink() {
  FIELDMOCK.$replay();
  linkBubble.enable(FIELDMOCK);
  link.setAttribute('imageanchor', 1);
  linkBubble.handleSelectionChange(createMouseEvent(link));
  assertBubble();
  FIELDMOCK.$verify();
}

function closeBox() {
  var closeBox = goog.dom.getElementsByTagNameAndClass(goog.dom.TagName.DIV,
      'tr_bubble_closebox');
  assertEquals('Should find only one close box', 1, closeBox.length);
  assertNotNull('Found close box', closeBox[0]);
  goog.testing.events.fireClickSequence(closeBox[0]);
}

function testCloseBox() {
  testLinkClicked();
  closeBox();
  assertNoBubble();
  FIELDMOCK.$verify();
}

function testChangeClicked() {
  FIELDMOCK.execCommand(goog.editor.Command.MODAL_LINK_EDITOR,
      new goog.editor.Link(link, false));
  FIELDMOCK.$registerArgumentListVerifier('execCommand', function(arr1, arr2) {
    return arr1.length == arr2.length &&
           arr1.length == 2 &&
           arr1[0] == goog.editor.Command.MODAL_LINK_EDITOR &&
           arr2[0] == goog.editor.Command.MODAL_LINK_EDITOR &&
           arr1[1] instanceof goog.editor.Link &&
           arr2[1] instanceof goog.editor.Link;
  });
  FIELDMOCK.$times(1);
  FIELDMOCK.$returns(true);
  FIELDMOCK.$replay();
  linkBubble.enable(FIELDMOCK);

  linkBubble.handleSelectionChange(createMouseEvent(link));
  assertBubble();

  goog.testing.events.fireClickSequence(
      goog.dom.$(goog.editor.plugins.LinkBubble.CHANGE_LINK_ID_));
  assertNoBubble();
  FIELDMOCK.$verify();
}

function testDeleteClicked() {
  FIELDMOCK.dispatchBeforeChange();
  FIELDMOCK.$times(1);
  FIELDMOCK.dispatchChange();
  FIELDMOCK.$times(1);
  FIELDMOCK.$replay();
  linkBubble.enable(FIELDMOCK);

  linkBubble.handleSelectionChange(createMouseEvent(link));
  assertBubble();

  goog.testing.events.fireClickSequence(
      goog.dom.$(goog.editor.plugins.LinkBubble.DELETE_LINK_ID_));
  var element = goog.userAgent.GECKO ?  document.body : fieldDiv;
  assertNotEquals('Link removed', element.firstChild.nodeName,
      goog.dom.TagName.A);
  assertNoBubble();
  FIELDMOCK.$verify();
}

function testActionClicked() {
  var SPAN = 'actionSpanId';
  var LINK = 'actionLinkId';
  var toShowCount = 0;
  var actionCount = 0;

  var linkAction = new goog.editor.plugins.LinkBubble.Action(
      SPAN, LINK, 'message',
      function() {
        toShowCount++;
        return toShowCount == 1; // Show it the first time.
      },
      function() {
        actionCount++;
      });

  linkBubble = new goog.editor.plugins.LinkBubble(linkAction);
  linkBubble.fieldObject = FIELDMOCK;
  FIELDMOCK.$replay();
  linkBubble.enable(FIELDMOCK);

  // The first time the bubble is shown, show our custom action.
  linkBubble.handleSelectionChange(createMouseEvent(link));
  assertBubble();
  assertEquals('Should check showing the action', 1, toShowCount);
  assertEquals('Action should not have fired yet', 0, actionCount);

  assertTrue('Action should be visible 1st time', goog.style.isElementShown(
      goog.dom.$(SPAN)));
  goog.testing.events.fireClickSequence(goog.dom.$(LINK));

  assertEquals('Should not check showing again yet', 1, toShowCount);
  assertEquals('Action should be fired', 1, actionCount);

  closeBox();
  assertNoBubble();

  // The action won't be shown the second time around.
  linkBubble.handleSelectionChange(createMouseEvent(link));
  assertBubble();
  assertEquals('Should check showing again', 2, toShowCount);
  assertEquals('Action should not fire again', 1, actionCount);
  assertFalse('Action should not be shown 2nd time', goog.style.isElementShown(
      goog.dom.$(SPAN)));

  FIELDMOCK.$verify();
}

function testLinkTextClicked() {
  mockWindowOpen('http://www.google.com/', '_blank', '');
  mockWindowOpen.$replay();
  FIELDMOCK.$replay();
  linkBubble.enable(FIELDMOCK);

  linkBubble.handleSelectionChange(createMouseEvent(link));
  assertBubble();

  goog.testing.events.fireClickSequence(
      goog.dom.$(goog.editor.plugins.LinkBubble.TEST_LINK_ID_));

  assertBubble();
  mockWindowOpen.$verify();
  FIELDMOCK.$verify();
}

function testLinkTextClickedCustomUrlFn() {
  mockWindowOpen('http://images.google.com/', '_blank', '');
  mockWindowOpen.$replay();
  FIELDMOCK.$replay();
  linkBubble.enable(FIELDMOCK);

  linkBubble.setTestLinkUrlFn(function(url) {
    return url.replace('www', 'images');
  });

  linkBubble.handleSelectionChange(createMouseEvent(link));
  assertBubble();

  goog.testing.events.fireClickSequence(
      goog.dom.$(goog.editor.plugins.LinkBubble.TEST_LINK_ID_));

  assertBubble();
  mockWindowOpen.$verify();
  FIELDMOCK.$verify();
}

/**
 * Urls with invalid schemes shouldn't be linkified.
 * @bug 2585360
 */
function testDontLinkifyInvalidScheme() {
  mockWindowOpen.$replay();
  FIELDMOCK.$replay();
  linkBubble.enable(FIELDMOCK);

  var badLink = document.createElement('a');
  badLink.href = 'javascript:alert(1)';
  badLink.innerHTML = 'bad link';

  linkBubble.handleSelectionChange(createMouseEvent(badLink));
  assertBubble();

  // The link shouldn't exist at all, so this sequence should have no effect.
  goog.testing.events.fireClickSequence(
      goog.dom.$(goog.editor.plugins.LinkBubble.TEST_LINK_ID_));

  assertBubble();
  mockWindowOpen.$verify();
  FIELDMOCK.$verify();
}

function testIsSafeSchemeToOpen() {
  // Urls with no scheme at all are ok too since 'http://' will be prepended.
  var good = [
    'http://google.com', 'http://google.com/', 'https://google.com',
    'null@google.com', 'http://www.google.com', 'http://site.com',
    'google.com', 'google', 'http://google', 'HTTP://GOOGLE.COM',
    'HtTp://www.google.com'
  ];

  var bad = [
    'javascript:google.com', 'httpp://google.com', 'data:foo',
    'javascript:alert(\'hi\');', 'abc:def'
  ];

  for (var i = 0; i < good.length; i++) {
    assertTrue(good[i] + ' should have a safe scheme',
        linkBubble.isSafeSchemeToOpen_(good[i]));
  }

  for (i = 0; i < bad.length; i++) {
    assertFalse(bad[i] + ' should have an unsafe scheme',
        linkBubble.isSafeSchemeToOpen_(bad[i]));
  }
}

function testShouldOpenWithWhitelist() {
  linkBubble.setSafeToOpenSchemes(['abc']);

  assertTrue('Scheme should be safe',
              linkBubble.shouldOpenUrl('abc://google.com'));
  assertFalse('Scheme should be unsafe',
              linkBubble.shouldOpenUrl('http://google.com'));

  linkBubble.setBlockOpeningUnsafeSchemes(false);
  assertTrue('Non-whitelisted should now be safe after disabling blocking',
              linkBubble.shouldOpenUrl('http://google.com'));
}

/**
 * @bug 763211
 * @bug 2182147
 */
function testLongUrlTestLinkAnchorTextCorrect() {
  FIELDMOCK.$replay();
  linkBubble.enable(FIELDMOCK);

  var longUrl = 'http://www.reallylonglinkthatshouldbetruncated' +
      'becauseitistoolong.com';
  var truncatedLongUrl = goog.string.truncateMiddle(longUrl, 48);

  var longLink = document.createElement('a');
  longLink.href = longUrl;
  longLink.innerHTML = 'Google';
  fieldDiv.appendChild(longLink);

  linkBubble.handleSelectionChange(createMouseEvent(longLink));
  assertBubble();

  var testLinkEl = goog.dom.$(goog.editor.plugins.LinkBubble.TEST_LINK_ID_);
  assertEquals(
      'The test link\'s anchor text should be the truncated URL.',
      truncatedLongUrl,
      testLinkEl.innerHTML);

  fieldDiv.removeChild(longLink);
  FIELDMOCK.$verify();
}

/**
 * @bug 2416024
 */
function testOverridingCreateBubbleContentsDoesntNpeGetTargetUrl() {
  FIELDMOCK.$replay();
  linkBubble.enable(FIELDMOCK);

  stubs.set(linkBubble, 'createBubbleContents',
      function(elem) {
        // getTargetUrl would cause an NPE if urlUtil_ wasn't defined yet.
        linkBubble.getTargetUrl();
      });
  assertNotThrows('Accessing this.urlUtil_ should not NPE',
                  goog.bind(linkBubble.handleSelectionChange,
                            linkBubble, createMouseEvent(link)));

  FIELDMOCK.$verify();
}

function assertBubble() {
  assertTrue('Link bubble visible', linkBubble.isVisible());
  assertNotNull('Link bubble created',
      goog.dom.$(goog.editor.plugins.LinkBubble.LINK_DIV_ID_));
}

function assertNoBubble() {
  assertFalse('Link bubble not visible', linkBubble.isVisible());
  assertNull('Link bubble not created',
      goog.dom.$(goog.editor.plugins.LinkBubble.LINK_DIV_ID_));
}

function createMouseEvent(target) {
  var eventObj = new goog.events.Event(goog.events.EventType.MOUSEUP, target);
  eventObj.button = goog.events.BrowserEvent.MouseButton.LEFT;

  return new goog.events.BrowserEvent(eventObj, target);
}
</script>
</body>
</html>