Exercise RunAsyncCode class for prefetching fragments. Recent changes to code splitting fixed a bug in RunAsyncCode.isLoaded(). I wanted to write a test to see that things were really working. Review at http://gwt-code-reviews.appspot.com/1453805 git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@10307 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/core/client/impl/AsyncFragmentLoader.java b/user/src/com/google/gwt/core/client/impl/AsyncFragmentLoader.java index 8c5e0af..8e315b6 100644 --- a/user/src/com/google/gwt/core/client/impl/AsyncFragmentLoader.java +++ b/user/src/com/google/gwt/core/client/impl/AsyncFragmentLoader.java
@@ -93,13 +93,14 @@ */ public static class StandardLogger implements Logger { /** - * Always use this as {@link isStatsAvailable} && + * Always use this as {@link #isStatsAvailable()} && * {@link #stats(JavaScriptObject)}. */ private static native boolean stats(JavaScriptObject data) /*-{ return $stats(data); }-*/; + @Override public void logEventProgress(String eventGroup, String type, int fragment, int size) { @SuppressWarnings("unused") boolean toss = @@ -211,6 +212,7 @@ this.fragment = myFragment; } + @Override public void loadTerminated(Throwable reason) { if (fragmentLoading != fragment) { // fragment already loaded successfully @@ -600,6 +602,7 @@ if (!isLoading(fragment)) { inject(fragment, new AsyncFragmentLoader.LoadTerminatedHandler() { + @Override public void loadTerminated(Throwable reason) { Object[] callbacks = allCallbacks[fragment]; if (callbacks != null) {
diff --git a/user/test/com/google/gwt/core/CoreSuite.java b/user/test/com/google/gwt/core/CoreSuite.java index 170da31..72bdc4b 100644 --- a/user/test/com/google/gwt/core/CoreSuite.java +++ b/user/test/com/google/gwt/core/CoreSuite.java
@@ -25,6 +25,7 @@ import com.google.gwt.core.client.impl.SchedulerImplTest; import com.google.gwt.core.client.impl.StackTraceCreatorTest; import com.google.gwt.core.client.impl.XhrLoadingStrategyTest; +import com.google.gwt.core.client.prefetch.RunAsyncCodeTest; import com.google.gwt.dev.StrictModeTest; import com.google.gwt.junit.tools.GWTTestSuite; @@ -49,6 +50,7 @@ suite.addTestSuite(StackTraceCreatorTest.class); suite.addTestSuite(StrictModeTest.class); suite.addTestSuite(XhrLoadingStrategyTest.class); + suite.addTestSuite(RunAsyncCodeTest.class); // $JUnit-END$ return suite;
diff --git a/user/test/com/google/gwt/core/client/prefetch/RunAsyncCodeTest.java b/user/test/com/google/gwt/core/client/prefetch/RunAsyncCodeTest.java new file mode 100644 index 0000000..1d59484 --- /dev/null +++ b/user/test/com/google/gwt/core/client/prefetch/RunAsyncCodeTest.java
@@ -0,0 +1,132 @@ +/* + * Copyright 2011 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.gwt.core.client.prefetch; + +import com.google.gwt.core.client.GWT; +import com.google.gwt.core.client.RunAsyncCallback; +import com.google.gwt.core.client.Scheduler; +import com.google.gwt.core.client.Scheduler.RepeatingCommand; +import com.google.gwt.junit.client.GWTTestCase; + +/** + * Exercises isLoaded() method in {@link RunAsyncCode} + */ +public class RunAsyncCodeTest extends GWTTestCase { + /** + * Used as a label for GWT.runAsync() call. + */ + public static class IsLoadedMarker { + } + + /** + * Used as a label for GWT.runAsync() call. + */ + public static class TestPrefetchMarker { + } + + public static final int ASYNC_DELAY_MSEC = 10000; + + // Used to cancel the repeating timer in testPrefetch() + boolean cancelPrefetchTest = false; + + @Override + public String getModuleName() { + return "com.google.gwt.core.Core"; + } + + @Override + public void gwtTearDown() { + cancelPrefetchTest = true; + } + + /** + * Checks the return of isLoaded() to make sure it returns + * <code>false</false> for a yet to be referenced split point and <code>true</code> + * after onSuccess is called. + */ + public void testIsLoaded() { + // This test is not applicable to DevMode + if (!GWT.isScript()) { + return; + } + + delayTestFinish(ASYNC_DELAY_MSEC); + final RunAsyncCode prefetchSplitPoint = RunAsyncCode.runAsyncCode(IsLoadedMarker.class); + assertFalse(prefetchSplitPoint.isLoaded()); + + GWT.runAsync(IsLoadedMarker.class, new RunAsyncCallback() { + @Override + public void onFailure(Throwable reason) { + fail("runAsync() call failed."); + } + + @Override + public void onSuccess() { + assertTrue(prefetchSplitPoint.isLoaded()); + finishTest(); + } + }); + } + + /** + * Make a call to {@link Prefetcher} to prefetch a split point and wait for it + * to be loaded before invoking it. + */ + public void testPrefetch() { + cancelPrefetchTest = false; + // This test is not applicable to DevMode + if (!GWT.isScript()) { + return; + } + + delayTestFinish(ASYNC_DELAY_MSEC); + final RunAsyncCode prefetchSplitPoint = RunAsyncCode.runAsyncCode(TestPrefetchMarker.class); + Prefetcher.prefetch(prefetchSplitPoint); + + assertFalse(prefetchSplitPoint.isLoaded()); + Prefetcher.start(); + + Scheduler.get().scheduleFixedDelay(new RepeatingCommand() { + + @Override + public boolean execute() { + // gwtTearDown() will set this flag to stop the repeating timer if + // the test case is already finished due to a timeout. + if (cancelPrefetchTest) { + return false; + } + + if (prefetchSplitPoint.isLoaded()) { + GWT.runAsync(TestPrefetchMarker.class, new RunAsyncCallback() { + + @Override + public void onFailure(Throwable reason) { + fail("runAsync call failed."); + } + + @Override + public void onSuccess() { + finishTest(); + } + }); + return false; + } + return true; + } + }, 50); + } +}