diff --git a/components/cronet/android/test/javatests/src/org/chromium/net/SdchTest.java b/components/cronet/android/test/javatests/src/org/chromium/net/SdchTest.java index 93500d17ba2358..52078c5d5c77ff 100644 --- a/components/cronet/android/test/javatests/src/org/chromium/net/SdchTest.java +++ b/components/cronet/android/test/javatests/src/org/chromium/net/SdchTest.java @@ -4,7 +4,6 @@ package org.chromium.net; -import android.os.ConditionVariable; import android.support.test.filters.SmallTest; import org.json.JSONException; @@ -71,7 +70,7 @@ public void testSdchEnabled() throws Exception { String targetUrl = NativeTestServer.getSdchURL() + "/sdch/test"; long contextAdapter = getContextAdapter((CronetUrlRequestContext) mTestFramework.mCronetEngine); - DictionaryAddedObserver observer = new DictionaryAddedObserver(targetUrl, contextAdapter); + SdchObserver observer = new SdchObserver(targetUrl, contextAdapter); // Make a request to /sdch which advertises the dictionary. TestUrlRequestCallback callback1 = startAndWaitForComplete(mTestFramework.mCronetEngine, @@ -101,8 +100,7 @@ public void testSdchEnabled() throws Exception { null, mTestFramework.getCronetEngineBuilder()); CronetUrlRequestContext newContext = (CronetUrlRequestContext) mTestFramework.mCronetEngine; long newContextAdapter = getContextAdapter(newContext); - DictionaryAddedObserver newObserver = - new DictionaryAddedObserver(targetUrl, newContextAdapter); + SdchObserver newObserver = new SdchObserver(targetUrl, newContextAdapter); newObserver.waitForDictionaryAdded(); // Make a request to fetch encoded response at /sdch/test. @@ -146,27 +144,6 @@ public void testDictionaryNotFound() throws Exception { assertEquals("Sdch is not used.\n", callback2.mResponseAsString); } - private static class DictionaryAddedObserver extends SdchObserver { - private final ConditionVariable mBlock; - - public DictionaryAddedObserver(String targetUrl, long contextAdapter) { - super(targetUrl, contextAdapter); - mBlock = new ConditionVariable(); - } - - @Override - public void onDictionaryAdded() { - mBlock.open(); - } - - public void waitForDictionaryAdded() { - if (!mDictionaryAlreadyPresent) { - mBlock.block(); - mBlock.close(); - } - } - } - private long getContextAdapter(CronetUrlRequestContext requestContext) { return requestContext.getUrlRequestContextAdapter(); } diff --git a/components/cronet/android/test/src/org/chromium/net/SdchObserver.java b/components/cronet/android/test/src/org/chromium/net/SdchObserver.java index 8e8fe127baab85..2028c09040c7b3 100644 --- a/components/cronet/android/test/src/org/chromium/net/SdchObserver.java +++ b/components/cronet/android/test/src/org/chromium/net/SdchObserver.java @@ -6,6 +6,8 @@ import android.os.ConditionVariable; +import junit.framework.Assert; + import org.chromium.base.annotations.CalledByNative; import org.chromium.base.annotations.JNINamespace; @@ -13,11 +15,10 @@ * Class to watch for Sdch dictionary events. The native implementation * unregisters itself when an event happens. Therefore, an instance of this * class is only able to receive a notification of the earliest event. - * Currently, implemented events include {@link #onDictionaryAdded}. */ @JNINamespace("cronet") public class SdchObserver { - protected boolean mDictionaryAlreadyPresent = false; + private static final int BLOCK_WAIT_TIMEOUT_SEC = 20; private final ConditionVariable mAddBlock = new ConditionVariable(); /** @@ -27,29 +28,39 @@ public class SdchObserver { */ public SdchObserver(String targetUrl, long contextAdapter) { nativeAddSdchObserver(targetUrl, contextAdapter); - mAddBlock.block(); - mAddBlock.close(); } /** * Called when a dictionary is added to the SdchManager for the target url. - * Override this method if caller would like to get notified. */ @CalledByNative - public void onDictionaryAdded() { - // Left blank; + protected void onDictionaryAdded() { + mAddBlock.open(); } + /** + * Called after the observer has been registered. + */ @CalledByNative - private void onAddSdchObserverCompleted() { - mAddBlock.open(); + protected void onAddSdchObserverCompleted() { + // Left blank; } + /** + * Called if the dictionary was added before the observer registration. + */ @CalledByNative - private void onDictionarySetAlreadyPresent() { - mDictionaryAlreadyPresent = true; + protected void onDictionarySetAlreadyPresent() { mAddBlock.open(); } + public void waitForDictionaryAdded() { + boolean success = mAddBlock.block(BLOCK_WAIT_TIMEOUT_SEC * 1000); + if (!success) { + Assert.fail("Timeout: the dictionary hasn't been added after waiting for " + + BLOCK_WAIT_TIMEOUT_SEC + " seconds"); + } + } + private native void nativeAddSdchObserver(String targetUrl, long contextAdapter); }