Debian Codegraph @

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