aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/ui/editor/linkdialog_test.html
blob: 80e6571562b6e375b0c90192ff2849b3df443d48 (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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
<!DOCTYPE html>
<html>
<!--
Copyright 2010 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.ui.editor.LinkDialog Tests</title>
<script src="../../base.js"></script>
<script>
  goog.require('goog.dom');
  goog.require('goog.dom.DomHelper');
  goog.require('goog.editor.BrowserFeature');
  goog.require('goog.editor.Link');
  goog.require('goog.events.Event');
  goog.require('goog.events.EventHandler');
  goog.require('goog.math.Size');
  goog.require('goog.testing.MockControl');
  goog.require('goog.testing.PropertyReplacer');
  goog.require('goog.testing.dom');
  goog.require('goog.testing.events');
  goog.require('goog.testing.jsunit');
  goog.require('goog.testing.mockmatchers');
  goog.require('goog.testing.mockmatchers.ArgumentMatcher');
  goog.require('goog.ui.editor.AbstractDialog.EventType');
  goog.require('goog.ui.editor.LinkDialog');
  goog.require('goog.ui.editor.LinkDialog.OkEvent');
  goog.require('goog.ui.editor.messages');
</script>
<link rel="stylesheet" href="../../css/dialog.css"/>
<link rel="stylesheet" href="../../css/linkbutton.css"/>
<link rel="stylesheet" href="../../css/editor/dialog.css"/>
<link rel="stylesheet" href="../../css/editor/linkdialog.css"/>
</head>
<body>
  <iframe id="appWindowIframe" src="javascript:''"></iframe>
<script>

  var dialog;
  var mockCtrl;
  var mockLink;
  var mockOkHandler;
  var mockGetViewportSize;
  var mockWindowOpen;
  var isNew;
  var anchorElem;
  var stubs = new goog.testing.PropertyReplacer();

  var ANCHOR_TEXT = 'anchor text';
  var ANCHOR_URL = 'http://www.google.com/';
  var ANCHOR_EMAIL = 'm@r.cos';
  var ANCHOR_MAILTO = 'mailto:' + ANCHOR_EMAIL;

  function setUp() {
    anchorElem = goog.dom.createElement(goog.dom.TagName.A);
    goog.dom.appendChild(goog.dom.getDocument().body, anchorElem);

    mockCtrl = new goog.testing.MockControl();
    mockLink = mockCtrl.createLooseMock(goog.editor.Link);
    mockOkHandler = mockCtrl.createLooseMock(goog.events.EventHandler);

    isNew = false;
    mockLink.isNew();
    mockLink.$anyTimes();
    mockLink.$does(function() {
      return isNew;
    });
    mockLink.getCurrentText();
    mockLink.$anyTimes();
    mockLink.$does(function() {
      return anchorElem.innerHTML;
    });
    mockLink.setTextAndUrl(goog.testing.mockmatchers.isString,
        goog.testing.mockmatchers.isString);
    mockLink.$anyTimes();
    mockLink.$does(function(text, url) {
      anchorElem.innerHTML = text;
      anchorElem.href = url;
    });
    mockLink.$registerArgumentListVerifier('placeCursorRightOf', function() {
      return true;
    });
    mockLink.placeCursorRightOf(goog.testing.mockmatchers.iBoolean);
    mockLink.$anyTimes();
    mockLink.getAnchor();
    mockLink.$anyTimes();
    mockLink.$returns(anchorElem);

    mockWindowOpen = mockCtrl.createFunctionMock('open');
    stubs.set(window, 'open', mockWindowOpen);
  }

  function tearDown() {
    dialog.dispose();
    goog.dom.removeNode(anchorElem);
    stubs.reset();
  }

  function setUpAnchor(href, text, opt_isNew, opt_target, opt_rel) {
    anchorElem.href = href;
    anchorElem.innerHTML = text;
    isNew = !!opt_isNew;
    if (opt_target) {
      anchorElem.target = opt_target;
    }
    if (opt_rel) {
      anchorElem.rel = opt_rel;
    }
  }

  /**
   * Creates and shows the dialog to be tested.
   * @param {Document=} opt_document Document to render the dialog into.
   *     Defaults to the main window's document.
   * @param {boolean=} opt_openInNewWindow Whether the open in new window
   *     checkbox should be shown.
   * @param {boolean=} opt_noFollow Whether rel=nofollow checkbox should be
   *     shown.
   */
  function createAndShow(opt_document, opt_openInNewWindow, opt_noFollow) {
    dialog = new goog.ui.editor.LinkDialog(new goog.dom.DomHelper(opt_document),
                                           mockLink);
    if (opt_openInNewWindow) {
      dialog.showOpenLinkInNewWindow(false);
    }
    if (opt_noFollow) {
      dialog.showRelNoFollow();
    }
    dialog.addEventListener(goog.ui.editor.AbstractDialog.EventType.OK,
        mockOkHandler);
    dialog.show();
  }

  /**
   * Sets up the mock event handler to expect an OK event with the given text
   * and url.
   */
  function expectOk(linkText, linkUrl, opt_openInNewWindow, opt_noFollow) {
    mockOkHandler.handleEvent(new goog.testing.mockmatchers.ArgumentMatcher(
        function(arg) {
          return arg.type == goog.ui.editor.AbstractDialog.EventType.OK &&
                 arg.linkText == linkText &&
                 arg.linkUrl == linkUrl &&
                 arg.openInNewWindow == !!opt_openInNewWindow &&
                 arg.noFollow == !!opt_noFollow;
        },
        '{linkText: ' + linkText + ', linkUrl: ' + linkUrl +
            ', openInNewWindow: ' + opt_openInNewWindow +
            ', noFollow: ' + opt_noFollow + '}'));
  }

  /**
   * Tests that when you show the dialog for a new link, the input fields are
   * empty, the web tab is selected and focus is in the url input field.
   * @param {Document=} opt_document Document to render the dialog into.
   *     Defaults to the main window's document.
   */
  function testShowForNewLink(opt_document) {
    mockCtrl.$replayAll();
    setUpAnchor('', '', true); // Must be done before creating the dialog.
    createAndShow(opt_document);

    assertEquals('Display text input field should be empty',
                 '',
                 getDisplayInputText());
    assertEquals('Url input field should be empty',
                 '',
                 getUrlInputText());
    assertEquals('On the web tab should be selected',
                 goog.ui.editor.LinkDialog.Id_.ON_WEB,
                 dialog.curTabId_);
    if (goog.editor.BrowserFeature.HAS_ACTIVE_ELEMENT) {
      assertEquals('Focus should be on url input',
                   getUrlInput(),
                   dialog.dom.getDocument().activeElement);
    }

    mockCtrl.$verifyAll();
  }

  /**
   * Fakes that the mock field is using an iframe and does the same test as
   * testShowForNewLink().
   */
  function testShowForNewLinkWithDiffAppWindow() {
    testShowForNewLink(goog.dom.getElement('appWindowIframe').contentDocument);
  }

  /**
   * Tests that when you show the dialog for a url link, the input fields are
   * filled in, the web tab is selected and focus is in the url input field.
   */
  function testShowForUrlLink() {
    mockCtrl.$replayAll();
    setUpAnchor(ANCHOR_URL, ANCHOR_TEXT);
    createAndShow();

    assertEquals('Display text input field should be filled in',
                 ANCHOR_TEXT,
                 getDisplayInputText());
    assertEquals('Url input field should be filled in',
                 ANCHOR_URL,
                 getUrlInputText());
    assertEquals('On the web tab should be selected',
                 goog.ui.editor.LinkDialog.Id_.ON_WEB,
                 dialog.curTabId_);
    if (goog.editor.BrowserFeature.HAS_ACTIVE_ELEMENT) {
      assertEquals('Focus should be on url input',
                   getUrlInput(),
                   dialog.dom.getDocument().activeElement);
    }

    mockCtrl.$verifyAll();
  }

  /**
   * Tests that when you show the dialog for a mailto link, the input fields are
   * filled in, the email tab is selected and focus is in the email input field.
   */
  function testShowForMailtoLink() {
    mockCtrl.$replayAll();
    setUpAnchor(ANCHOR_MAILTO, ANCHOR_TEXT);
    createAndShow();

    assertEquals('Display text input field should be filled in',
                 ANCHOR_TEXT,
                 getDisplayInputText());
    assertEquals('Email input field should be filled in',
                 ANCHOR_EMAIL, // The 'mailto:' is not in the input!
                 getEmailInputText());
    assertEquals('Email tab should be selected',
                 goog.ui.editor.LinkDialog.Id_.EMAIL_ADDRESS,
                 dialog.curTabId_);
    if (goog.editor.BrowserFeature.HAS_ACTIVE_ELEMENT) {
      assertEquals('Focus should be on email input',
                   getEmailInput(),
                   dialog.dom.getDocument().activeElement);
    }

    mockCtrl.$verifyAll();
  }

  /**
   * Tests that the display text is autogenerated from the url input in the
   * right situations (and not generated when appropriate too).
   */
  function testAutogeneration() {
    mockCtrl.$replayAll();
    setUpAnchor('', '', true);
    createAndShow();

    // Simulate typing a url when everything is empty, should autogen.
    setUrlInputText(ANCHOR_URL);
    assertEquals('Display text should have been autogenerated',
                 ANCHOR_URL,
                 getDisplayInputText());

    // Simulate typing text when url is set, afterwards should not autogen.
    setDisplayInputText(ANCHOR_TEXT);
    setUrlInputText(ANCHOR_MAILTO);
    assertNotEquals('Display text should not have been autogenerated',
                    ANCHOR_MAILTO,
                    getDisplayInputText());
    assertEquals('Display text should have remained the same',
                 ANCHOR_TEXT,
                 getDisplayInputText());

    // Simulate typing text equal to existing url, afterwards should autogen.
    setDisplayInputText(ANCHOR_MAILTO);
    setUrlInputText(ANCHOR_URL);
    assertEquals('Display text should have been autogenerated',
                 ANCHOR_URL,
                 getDisplayInputText());

    mockCtrl.$verifyAll();
  }

  /**
   * Tests that the display text is not autogenerated from the url input in all
   * situations when the autogeneration feature is turned off.
   */
  function testAutogenerationOff() {
    mockCtrl.$replayAll();

    setUpAnchor('', '', true);
    createAndShow();

    // Disable the autogen feature
    dialog.setAutogenFeatureEnabled(false);

    // Simulate typing a url when everything is empty, should not autogen.
    setUrlInputText(ANCHOR_URL);
    assertEquals('Display text should not have been autogenerated',
                 '',
                 getDisplayInputText());

    // Simulate typing text when url is set, afterwards should not autogen.
    setDisplayInputText(ANCHOR_TEXT);
    setUrlInputText(ANCHOR_MAILTO);
    assertNotEquals('Display text should not have been autogenerated',
                    ANCHOR_MAILTO,
                    getDisplayInputText());
    assertEquals('Display text should have remained the same',
                 ANCHOR_TEXT,
                 getDisplayInputText());

    // Simulate typing text equal to existing url, afterwards should not
    // autogen.
    setDisplayInputText(ANCHOR_MAILTO);
    setUrlInputText(ANCHOR_URL);
    assertEquals('Display text should not have been autogenerated',
                 ANCHOR_MAILTO,
                 getDisplayInputText());

    mockCtrl.$verifyAll();
  }

  /**
   * Tests that clicking OK with the url tab selected dispatches an event with
   * the proper link data.
   */
  function testOkForUrl() {
    expectOk(ANCHOR_TEXT, ANCHOR_URL);
    mockCtrl.$replayAll();
    setUpAnchor('', '', true);
    createAndShow();

    dialog.tabPane_.setSelectedTabId(goog.ui.editor.LinkDialog.Id_.ON_WEB_TAB);
    setDisplayInputText(ANCHOR_TEXT);
    setUrlInputText(ANCHOR_URL);
    goog.testing.events.fireClickSequence(dialog.getOkButtonElement());

    mockCtrl.$verifyAll();
  }

  /**
   * Tests that clicking OK with the url tab selected but with an email address
   * in the url field dispatches an event with the proper link data.
   */
  function testOkForUrlWithEmail() {
    expectOk(ANCHOR_TEXT, ANCHOR_MAILTO);
    mockCtrl.$replayAll();
    setUpAnchor('', '', true);
    createAndShow();

    dialog.tabPane_.setSelectedTabId(goog.ui.editor.LinkDialog.Id_.ON_WEB_TAB);
    setDisplayInputText(ANCHOR_TEXT);
    setUrlInputText(ANCHOR_EMAIL);
    goog.testing.events.fireClickSequence(dialog.getOkButtonElement());

    mockCtrl.$verifyAll();
  }

  /**
   * Tests that clicking OK with the email tab selected dispatches an event with
   * the proper link data.
   */
  function testOkForEmail() {
    expectOk(ANCHOR_TEXT, ANCHOR_MAILTO);
    mockCtrl.$replayAll();
    setUpAnchor('', '', true);
    createAndShow();

    dialog.tabPane_.setSelectedTabId(
        goog.ui.editor.LinkDialog.Id_.EMAIL_ADDRESS_TAB);

    setDisplayInputText(ANCHOR_TEXT);
    setEmailInputText(ANCHOR_EMAIL);
    goog.testing.events.fireClickSequence(dialog.getOkButtonElement());

    mockCtrl.$verifyAll();
  }

  function testOpenLinkInNewWindowNewLink() {
    expectOk(ANCHOR_TEXT, ANCHOR_URL, true);
    expectOk(ANCHOR_TEXT, ANCHOR_URL, false);
    mockCtrl.$replayAll();

    setUpAnchor('', '', true);
    createAndShow(undefined, true);
    dialog.tabPane_.setSelectedTabId(goog.ui.editor.LinkDialog.Id_.ON_WEB_TAB);
    setDisplayInputText(ANCHOR_TEXT);
    setUrlInputText(ANCHOR_URL);

    assertFalse('"Open in new window" should start unchecked',
        getOpenInNewWindowCheckboxChecked());
    setOpenInNewWindowCheckboxChecked(true);
    assertTrue('"Open in new window" should have gotten checked',
        getOpenInNewWindowCheckboxChecked());
    goog.testing.events.fireClickSequence(dialog.getOkButtonElement());

    // Reopen same dialog
    dialog.show();
    dialog.tabPane_.setSelectedTabId(goog.ui.editor.LinkDialog.Id_.ON_WEB_TAB);
    setDisplayInputText(ANCHOR_TEXT);
    setUrlInputText(ANCHOR_URL);

    assertTrue('"Open in new window" should remember it was checked',
        getOpenInNewWindowCheckboxChecked());
    setOpenInNewWindowCheckboxChecked(false);
    assertFalse('"Open in new window" should have gotten unchecked',
        getOpenInNewWindowCheckboxChecked());
    goog.testing.events.fireClickSequence(dialog.getOkButtonElement());
  }

  function testOpenLinkInNewWindowExistingLink() {
    mockCtrl.$replayAll();

    // Edit an existing link that already opens in a new window.
    setUpAnchor('', '', false, '_blank');
    createAndShow(undefined, true);
    dialog.tabPane_.setSelectedTabId(goog.ui.editor.LinkDialog.Id_.ON_WEB_TAB);
    setDisplayInputText(ANCHOR_TEXT);
    setUrlInputText(ANCHOR_URL);

    assertTrue('"Open in new window" should start checked for existing link',
        getOpenInNewWindowCheckboxChecked());

    mockCtrl.$verifyAll();
  }

  function testRelNoFollowNewLink() {
    expectOk(ANCHOR_TEXT, ANCHOR_URL, null, true);
    expectOk(ANCHOR_TEXT, ANCHOR_URL, null, false);
    mockCtrl.$replayAll();

    setUpAnchor('', '', true, true);
    createAndShow(null, null, true);
    dialog.tabPane_.setSelectedTabId(goog.ui.editor.LinkDialog.Id_.ON_WEB_TAB);
    setDisplayInputText(ANCHOR_TEXT);
    setUrlInputText(ANCHOR_URL);
    assertFalse('rel=nofollow should start unchecked',
        dialog.relNoFollowCheckbox_.checked);

    // Check rel=nofollow and close the dialog.
    dialog.relNoFollowCheckbox_.checked = true;
    goog.testing.events.fireClickSequence(dialog.getOkButtonElement());

    // Reopen the same dialog.
    anchorElem.rel = 'foo nofollow bar';
    dialog.show();
    dialog.tabPane_.setSelectedTabId(goog.ui.editor.LinkDialog.Id_.ON_WEB_TAB);
    setDisplayInputText(ANCHOR_TEXT);
    setUrlInputText(ANCHOR_URL);
    assertTrue('rel=nofollow should start checked when reopening the dialog',
        dialog.relNoFollowCheckbox_.checked);
  }

  function testRelNoFollowExistingLink() {
    mockCtrl.$replayAll();

    setUpAnchor('', '', null, null, 'foo nofollow bar');
    createAndShow(null, null, true);
    assertTrue('rel=nofollow should start checked for existing link',
        dialog.relNoFollowCheckbox_.checked);

    mockCtrl.$verifyAll();
  }

  /**
   * Test that clicking on the test button opens a new window with the correct
   * options.
   */
  function testWebTestButton() {
    if (goog.userAgent.GECKO) {
      // TODO(robbyw): Figure out why this is flaky and fix it.
      return;
    }

    var width, height;
    mockWindowOpen(ANCHOR_URL, '_blank',
        new goog.testing.mockmatchers.ArgumentMatcher(function(str) {
          return str == 'width=' + width + ',height=' + height +
                        ',toolbar=1,scrollbars=1,location=1,statusbar=0,' +
                        'menubar=1,resizable=1'
        }, "3rd arg: (string) window.open() options"));

    mockCtrl.$replayAll();
    setUpAnchor(ANCHOR_URL, ANCHOR_TEXT);
    createAndShow();

    // Measure viewport after opening dialog because that might cause scrollbars
    // to appear and reduce the viewport size.
    var size = goog.dom.getViewportSize(window);
    width = Math.max(size.width - 50, 50);
    height = Math.max(size.height - 50, 50);

    var testLink = goog.testing.dom.findTextNode(
        goog.ui.editor.messages.MSG_TEST_THIS_LINK,
        dialog.dialogInternal_.getElement());
    goog.testing.events.fireClickSequence(testLink.parentNode);

    mockCtrl.$verifyAll();
  }

  /**
   * Test that clicking on the test button does not open a new window when
   * the event is canceled.
   */
  function testWebTestButtonPreventDefault() {
    mockCtrl.$replayAll();
    setUpAnchor(ANCHOR_URL, ANCHOR_TEXT);
    createAndShow();

    goog.events.listen(dialog,
        goog.ui.editor.LinkDialog.EventType.BEFORE_TEST_LINK,
        function(e) {
          assertEquals(e.url, ANCHOR_URL);
          e.preventDefault();
        });
    var testLink = goog.testing.dom.findTextNode(
        goog.ui.editor.messages.MSG_TEST_THIS_LINK,
        dialog.dialogInternal_.getElement());
    goog.testing.events.fireClickSequence(testLink.parentNode);

    mockCtrl.$verifyAll();
  }

  /**
   * Test that the setTextToDisplayVisible() correctly works.
   * options.
   */
  function testSetTextToDisplayVisible() {
    mockCtrl.$replayAll();
    setUpAnchor('', '', true);
    createAndShow();

    assertNotEquals('none',
                    goog.style.getStyle(dialog.textToDisplayDiv_, 'display'));
    dialog.setTextToDisplayVisible(false);
    assertEquals('none',
                 goog.style.getStyle(dialog.textToDisplayDiv_, 'display'));
    dialog.setTextToDisplayVisible(true);
    assertNotEquals('none',
                    goog.style.getStyle(dialog.textToDisplayDiv_, 'display'));

    mockCtrl.$verifyAll();
  }

  function getDisplayInput() {
    return dialog.dom.getElement(goog.ui.editor.LinkDialog.Id_.TEXT_TO_DISPLAY);
  }

  function getDisplayInputText() {
    return getDisplayInput().value;
  }

  function setDisplayInputText(text) {
    var textInput = getDisplayInput();
    textInput.value = text;
    // Fire event so that dialog behaves like when user types.
    goog.testing.events.fireBrowserEvent(new goog.events.Event('keyup',
                                                               textInput));
  }

  function getUrlInput() {
    return dialog.dom.getElement(goog.ui.editor.LinkDialog.Id_.ON_WEB_INPUT);
  }

  function getUrlInputText() {
    return getUrlInput().value;
  }

  function setUrlInputText(text) {
    var urlInput = getUrlInput();
    urlInput.value = text;
    // Fire event so that dialog behaves like when user types.
    dialog.urlInputHandler_.dispatchEvent(
        goog.events.InputHandler.EventType.INPUT);
  }

  function getEmailInput() {
    return dialog.dom.getElement(
        goog.ui.editor.LinkDialog.Id_.EMAIL_ADDRESS_INPUT);
  }

  function getEmailInputText() {
    return getEmailInput().value;
  }

  function setEmailInputText(text) {
    var emailInput = getEmailInput();
    emailInput.value = text;
    // Fire event so that dialog behaves like when user types.
    dialog.emailInputHandler_.dispatchEvent(
        goog.events.InputHandler.EventType.INPUT);
  }

  function getOpenInNewWindowCheckboxChecked() {
    return dialog.openInNewWindowCheckbox_.checked;
  }

  function setOpenInNewWindowCheckboxChecked(checked) {
    dialog.openInNewWindowCheckbox_.checked = checked;
  }

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