001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.user.client;
017:
018: import com.google.gwt.core.client.GWT;
019: import com.google.gwt.core.client.GWT.UncaughtExceptionHandler;
020: import com.google.gwt.junit.client.GWTTestCase;
021:
022: /**
023: * Test cases for {@link CommandExecutor}.
024: */
025: public class CommandExecutorTest extends GWTTestCase {
026:
027: private static class NonRestartingCommandExecutor extends
028: CommandExecutor {
029: protected void maybeStartExecutionTimer() {
030: // keeps the executing timer for interfering with the test
031: }
032: }
033:
034: private static class TestCommand implements Command {
035: private boolean executed;
036:
037: public boolean didExecute() {
038: return executed;
039: }
040:
041: public void execute() {
042: executed = true;
043: }
044: }
045:
046: private static class TestIncrementalCommand implements
047: IncrementalCommand {
048: private boolean done = false;
049: private int executeCount;
050:
051: public boolean execute() {
052: ++executeCount;
053:
054: return !isDone();
055: }
056:
057: public int getExecuteCount() {
058: return executeCount;
059: }
060:
061: public boolean isDone() {
062: return done;
063: }
064:
065: public void setDone(boolean done) {
066: this .done = done;
067: }
068: }
069:
070: /**
071: * A sufficiently large delay to let the SSW triggers.
072: */
073: private static final int TEST_FINISH_DELAY_MILLIS = 40000;
074:
075: public String getModuleName() {
076: return "com.google.gwt.user.User";
077: }
078:
079: /**
080: * Test method for
081: * {@link com.google.gwt.user.client.CommandExecutor#doExecuteCommands(int)}.
082: *
083: * Checks that we can recover after a cancellation
084: */
085: public void testDoExecuteCommands_CancellationRecovery() {
086: final CommandExecutor ce = new NonRestartingCommandExecutor();
087:
088: final Command c1 = new Command() {
089: public void execute() {
090: }
091: };
092:
093: ce.setExecuting(true);
094: ce.submit(c1);
095: ce.setLast(0);
096:
097: final UncaughtExceptionHandler originalUEH = GWT
098: .getUncaughtExceptionHandler();
099:
100: UncaughtExceptionHandler ueh1 = new UncaughtExceptionHandler() {
101: public void onUncaughtException(Throwable e) {
102: if (!(e instanceof CommandCanceledException)
103: && !(e instanceof IncrementalCommandCanceledException)) {
104: originalUEH.onUncaughtException(e);
105: return;
106: }
107:
108: CommandCanceledException cce = (CommandCanceledException) e;
109: if (cce.getCommand() != c1) {
110: fail("CommandCanceledException did not contain the correct failed command");
111: }
112:
113: // Submit some more work and do another dispatch
114: ce.submit(new IncrementalCommand() {
115: public boolean execute() {
116: return false;
117: }
118: });
119:
120: ce.submit(new Command() {
121: public void execute() {
122: finishTest();
123: }
124: });
125:
126: delayTestFinish(TEST_FINISH_DELAY_MILLIS);
127: ce.doExecuteCommands(System.currentTimeMillis());
128: }
129: };
130:
131: GWT.setUncaughtExceptionHandler(ueh1);
132: ce.doCommandCanceled();
133: }
134:
135: /**
136: * Test method for
137: * {@link com.google.gwt.user.client.CommandExecutor#doExecuteCommands(int)}.
138: *
139: * Checks Command cancellation detection
140: */
141: public void testDoExecuteCommands_CommandCancellation() {
142: final CommandExecutor ce = new NonRestartingCommandExecutor();
143:
144: final Command c1 = new Command() {
145: public void execute() {
146: }
147: };
148:
149: // Setup the cancellation state
150: ce.setExecuting(true);
151: ce.submit(c1);
152: ce.setLast(0);
153:
154: final UncaughtExceptionHandler originalUEH = GWT
155: .getUncaughtExceptionHandler();
156:
157: UncaughtExceptionHandler ueh1 = new UncaughtExceptionHandler() {
158: public void onUncaughtException(Throwable e) {
159: if (!(e instanceof CommandCanceledException)) {
160: originalUEH.onUncaughtException(e);
161: return;
162: }
163:
164: CommandCanceledException cce = (CommandCanceledException) e;
165: if (cce.getCommand() != c1) {
166: fail("CommandCanceledException did not contain the correct failed command");
167: }
168: }
169: };
170:
171: GWT.setUncaughtExceptionHandler(ueh1);
172: ce.doCommandCanceled();
173: }
174:
175: /**
176: * Test method for
177: * {@link com.google.gwt.user.client.CommandExecutor#doExecuteCommands(int)}.
178: *
179: * Checks that calling {@link CommandExecutor#doExecuteCommands(long)} with no
180: * items in the queue is safe
181: */
182: public void testDoExecuteCommands_emptyQueue() {
183: final CommandExecutor ce = new NonRestartingCommandExecutor();
184:
185: ce.doExecuteCommands(System.currentTimeMillis());
186: }
187:
188: /**
189: * Test method for
190: * {@link com.google.gwt.user.client.CommandExecutor#doExecuteCommands(int)}.
191: *
192: * Checks IncrementalCommand cancellation detection
193: */
194: public void testDoExecuteCommands_IncrementalCommandCancellation() {
195: final CommandExecutor ce = new NonRestartingCommandExecutor();
196:
197: final IncrementalCommand ic = new IncrementalCommand() {
198: public boolean execute() {
199: return false;
200: }
201: };
202:
203: // setup the cancellation state
204: ce.setExecuting(true);
205: ce.submit(ic);
206: ce.setLast(0);
207:
208: final UncaughtExceptionHandler originalUEH = GWT
209: .getUncaughtExceptionHandler();
210:
211: UncaughtExceptionHandler ueh1 = new UncaughtExceptionHandler() {
212: public void onUncaughtException(Throwable e) {
213: if (!(e instanceof CommandCanceledException)) {
214: originalUEH.onUncaughtException(e);
215: return;
216: }
217:
218: IncrementalCommandCanceledException icce = (IncrementalCommandCanceledException) e;
219: if (icce.getCommand() != ic) {
220: fail("IncrementalCommandCanceledException did not contain the correct failed command");
221: }
222: }
223: };
224:
225: GWT.setUncaughtExceptionHandler(ueh1);
226: ce.doCommandCanceled();
227: }
228:
229: /**
230: * Test method for
231: * {@link com.google.gwt.user.client.CommandExecutor#doExecuteCommands(int)}.
232: *
233: * Checks that an incremental command executes and is removed from the queue
234: * when it is done
235: */
236: public void testDoExecuteCommands_IncrementalCommands() {
237: TestIncrementalCommand tic = new TestIncrementalCommand();
238: final CommandExecutor ce = new NonRestartingCommandExecutor();
239:
240: tic.setDone(true);
241: ce.submit(tic);
242: ce.doExecuteCommands(System.currentTimeMillis());
243: assertTrue(tic.getExecuteCount() > 0);
244: assertTrue(ce.getPendingCommands().isEmpty());
245: }
246:
247: /**
248: * Test method for
249: * {@link com.google.gwt.user.client.CommandExecutor#doExecuteCommands(int)}.
250: *
251: * Checks that null does in fact cause a pause.
252: */
253: public void testDoExecuteCommands_pause() {
254: final CommandExecutor ce = new NonRestartingCommandExecutor();
255:
256: TestCommand tc1 = new TestCommand();
257: TestCommand tc2 = new TestCommand();
258:
259: ce.submit(tc1);
260: ce.submit((Command) null);
261: ce.submit(tc2);
262:
263: ce.doExecuteCommands(System.currentTimeMillis());
264:
265: assertTrue(tc1.didExecute() && !tc2.didExecute());
266: assertEquals(1, ce.getPendingCommands().size());
267: assertTrue(ce.getPendingCommands().contains(tc2));
268: }
269:
270: /**
271: * Test method for
272: * {@link com.google.gwt.user.client.CommandExecutor#doExecuteCommands(int)}.
273: *
274: * Checks that after one pass dispatch pass, we still have the incremental
275: * command in the queue
276: */
277: public void testDoExecuteCommands_timeSliceUsage() {
278: final CommandExecutor ce = new NonRestartingCommandExecutor();
279:
280: Command tc = new TestCommand();
281: ce.submit(tc);
282:
283: TestIncrementalCommand tic = new TestIncrementalCommand();
284: ce.submit(tic);
285: ce.doExecuteCommands(System.currentTimeMillis());
286:
287: assertEquals(1, ce.getPendingCommands().size());
288: assertTrue(ce.getPendingCommands().contains(tic));
289: assertTrue(tic.getExecuteCount() > 0);
290: }
291:
292: /**
293: * Test method for
294: * {@link com.google.gwt.user.client.CommandExecutor#submit(com.google.gwt.user.client.Command)}.
295: *
296: * <p/> Cases:
297: * <ul>
298: * <li>Submit <code>null</code></li>
299: * <li>Submit {@link Command} and make sure that it fires</li>
300: * </ul>
301: */
302: public void testSubmitCommand() {
303: CommandExecutor ce = new CommandExecutor();
304: ce.submit((Command) null);
305:
306: delayTestFinish(TEST_FINISH_DELAY_MILLIS);
307:
308: ce.submit(new Command() {
309: public void execute() {
310: finishTest();
311: }
312: });
313: }
314:
315: /**
316: * Test method for
317: * {@link com.google.gwt.user.client.CommandExecutor#submit(com.google.gwt.user.client.IncrementalCommand)}.
318: *
319: * <p/> Cases:
320: * <ul>
321: * <li>Submit <code>null</code></li>
322: * <li>Submit {@link IncrementalCommand} and make sure that it fires as many
323: * times as we want it to</li>
324: * </ul>
325: */
326: public void testSubmitIncrementalCommand() {
327: CommandExecutor ce = new CommandExecutor();
328: ce.submit((Command) null);
329:
330: delayTestFinish(TEST_FINISH_DELAY_MILLIS);
331:
332: ce.submit(new IncrementalCommand() {
333: private int executionCount = 0;
334:
335: public boolean execute() {
336: if (++executionCount >= 10) {
337: fail("IncrementalCommand was fired more than 10 times");
338: }
339:
340: if (executionCount == 9) {
341: finishTest();
342: }
343:
344: return executionCount < 10;
345: }
346: });
347: }
348: }
|