plugin_test.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. goog.provide('goog.editor.PluginTest');
  15. goog.setTestOnly('goog.editor.PluginTest');
  16. goog.require('goog.editor.Field');
  17. goog.require('goog.editor.Plugin');
  18. goog.require('goog.functions');
  19. goog.require('goog.testing.StrictMock');
  20. goog.require('goog.testing.jsunit');
  21. goog.require('goog.userAgent');
  22. var plugin;
  23. var fieldObject;
  24. function setUp() {
  25. plugin = new goog.editor.Plugin();
  26. fieldObject = {};
  27. }
  28. function tearDown() {
  29. plugin.dispose();
  30. }
  31. function testRegisterFieldObject() {
  32. plugin.registerFieldObject(fieldObject);
  33. assertEquals(
  34. 'Register field object must be stored in protected field.', fieldObject,
  35. plugin.fieldObject);
  36. assertFalse(
  37. 'Newly registered plugin must not be enabled.',
  38. plugin.isEnabled(fieldObject));
  39. }
  40. function testUnregisterFieldObject() {
  41. plugin.registerFieldObject(fieldObject);
  42. plugin.enable(fieldObject);
  43. plugin.unregisterFieldObject(fieldObject);
  44. assertNull(
  45. 'fieldObject property must be undefined after ' +
  46. 'unregistering a field object.',
  47. plugin.fieldObject);
  48. assertFalse(
  49. 'Unregistered field object must not be enabled',
  50. plugin.isEnabled(fieldObject));
  51. }
  52. function testEnable() {
  53. plugin.registerFieldObject(fieldObject);
  54. plugin.enable(fieldObject);
  55. assertTrue(
  56. 'Enabled field object must be enabled according to isEnabled().',
  57. plugin.isEnabled(fieldObject));
  58. }
  59. function testDisable() {
  60. plugin.registerFieldObject(fieldObject);
  61. plugin.enable(fieldObject);
  62. plugin.disable(fieldObject);
  63. assertFalse(
  64. 'Disabled field object must be disabled according to ' +
  65. 'isEnabled().',
  66. plugin.isEnabled(fieldObject));
  67. }
  68. function testIsEnabled() {
  69. // Other base cases covered while testing enable() and disable().
  70. assertFalse(
  71. 'Unregistered field object must be disabled according ' +
  72. 'to isEnabled().',
  73. plugin.isEnabled(fieldObject));
  74. }
  75. function testIsSupportedCommand() {
  76. assertFalse(
  77. 'Base plugin class must not support any commands.',
  78. plugin.isSupportedCommand('+indent'));
  79. }
  80. function testExecCommand() {
  81. var mockField = new goog.testing.StrictMock(goog.editor.Field);
  82. plugin.registerFieldObject(mockField);
  83. if (goog.userAgent.GECKO) {
  84. mockField.stopChangeEvents(true, true);
  85. }
  86. mockField.dispatchBeforeChange();
  87. // Note(user): dispatch change turns back on (delayed) change events.
  88. mockField.dispatchChange();
  89. mockField.dispatchSelectionChangeEvent();
  90. mockField.$replay();
  91. var passedCommand, passedArg;
  92. plugin.execCommandInternal = function(command, arg) {
  93. passedCommand = command;
  94. passedArg = arg;
  95. };
  96. plugin.execCommand('+indent', true);
  97. // Verify that execCommand dispatched the expected events.
  98. mockField.$verify();
  99. mockField.$reset();
  100. // Verify that execCommandInternal was called with the correct arguments.
  101. assertEquals('+indent', passedCommand);
  102. assertTrue(passedArg);
  103. plugin.isSilentCommand = goog.functions.constant(true);
  104. mockField.$replay();
  105. plugin.execCommand('+outdent', false);
  106. // Verify that execCommand on a silent plugin dispatched no events.
  107. mockField.$verify();
  108. // Verify that execCommandInternal was called with the correct arguments.
  109. assertEquals('+outdent', passedCommand);
  110. assertFalse(passedArg);
  111. }
  112. /**
  113. * Regression test for http://b/issue?id=1471355 .
  114. */
  115. function testExecCommandException() {
  116. var mockField = new goog.testing.StrictMock(goog.editor.Field);
  117. plugin.registerFieldObject(mockField);
  118. plugin.execCommandInternal = function() { throw 1; };
  119. if (goog.userAgent.GECKO) {
  120. mockField.stopChangeEvents(true, true);
  121. }
  122. mockField.dispatchBeforeChange();
  123. // Note(user): dispatch change turns back on (delayed) change events.
  124. mockField.dispatchChange();
  125. mockField.dispatchSelectionChangeEvent();
  126. mockField.$replay();
  127. assertThrows('Exception should not be swallowed', function() {
  128. plugin.execCommand();
  129. });
  130. // Verifies that cleanup is done despite the exception.
  131. mockField.$verify();
  132. }
  133. function testDisposed() {
  134. plugin.registerFieldObject(fieldObject);
  135. plugin.dispose();
  136. assert(plugin.getDisposed());
  137. assertNull(
  138. 'Disposed plugin must not have a field object.', plugin.fieldObject);
  139. assertFalse(
  140. 'Disposed plugin must not have an enabled field object.',
  141. plugin.isEnabled(fieldObject));
  142. }
  143. function testIsAndSetAutoDispose() {
  144. assertTrue('Plugin must start auto-disposable', plugin.isAutoDispose());
  145. plugin.setAutoDispose(false);
  146. assertFalse(plugin.isAutoDispose());
  147. plugin.setAutoDispose(true);
  148. assertTrue(plugin.isAutoDispose());
  149. }