linkshortcutplugin_test.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. goog.provide('goog.editor.plugins.LinkShortcutPluginTest');
  15. goog.setTestOnly('goog.editor.plugins.LinkShortcutPluginTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.TagName');
  18. goog.require('goog.editor.Field');
  19. goog.require('goog.editor.plugins.BasicTextFormatter');
  20. goog.require('goog.editor.plugins.LinkBubble');
  21. goog.require('goog.editor.plugins.LinkShortcutPlugin');
  22. goog.require('goog.events.KeyCodes');
  23. goog.require('goog.testing.PropertyReplacer');
  24. goog.require('goog.testing.dom');
  25. goog.require('goog.testing.events');
  26. goog.require('goog.testing.jsunit');
  27. goog.require('goog.userAgent.product');
  28. var propertyReplacer;
  29. function setUp() {
  30. propertyReplacer = new goog.testing.PropertyReplacer();
  31. }
  32. function tearDown() {
  33. propertyReplacer.reset();
  34. var field = document.getElementById('cleanup');
  35. goog.dom.removeChildren(field);
  36. field.innerHTML = '<div id="field">http://www.google.com/</div>';
  37. }
  38. function testShortcutCreatesALink() {
  39. if (goog.userAgent.product.SAFARI) {
  40. // TODO(b/20733468): Disabled so we can get the rest of the Closure test
  41. // suite running in a continuous build. Will investigate later.
  42. return;
  43. }
  44. propertyReplacer.set(
  45. window, 'prompt', function() { return 'http://www.google.com/'; });
  46. var linkBubble = new goog.editor.plugins.LinkBubble();
  47. var formatter = new goog.editor.plugins.BasicTextFormatter();
  48. var plugin = new goog.editor.plugins.LinkShortcutPlugin();
  49. var fieldEl = document.getElementById('field');
  50. var field = new goog.editor.Field('field');
  51. field.registerPlugin(formatter);
  52. field.registerPlugin(linkBubble);
  53. field.registerPlugin(plugin);
  54. field.makeEditable();
  55. field.focusAndPlaceCursorAtStart();
  56. var textNode =
  57. goog.testing.dom.findTextNode('http://www.google.com/', fieldEl);
  58. goog.testing.events.fireKeySequence(
  59. field.getElement(), goog.events.KeyCodes.K, {ctrlKey: true});
  60. var href =
  61. goog.dom.getElementsByTagName(goog.dom.TagName.A, field.getElement())[0];
  62. assertEquals('http://www.google.com/', href.href);
  63. var bubbleLink =
  64. document.getElementById(goog.editor.plugins.LinkBubble.TEST_LINK_ID_);
  65. assertEquals('http://www.google.com/', bubbleLink.innerHTML);
  66. }