Commit fa2b7c1c authored by Nate Fischer's avatar Nate Fischer Committed by Commit Bot

AW: write helper method for waitForNextQueueElement

No change to production logic. This changes some test timeouts for
consistency (but increases them, so this should not be a problem).

This moves waitForNextQueueElement() into a static AwActivityTestRule
helper method. The motivation is just to reduce repeated code.

This also fixes some references to TimeUnit (we should prefer imports,
rather than fully qualifying the name).

Bug: 1013467
Test: run_webview_instrumentation_test_apk -f PostMessageTest.*
Test: run_webview_instrumentation_test_apk -f JsJavaInteractionTest.*
Change-Id: Iee197d1fefb07d2764c3910c16073547b2509bad
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1855200Reviewed-by: default avatarChangwan Ryu <changwan@chromium.org>
Commit-Queue: Nate Fischer <ntfschr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#704946}
parent bb66c0e8
...@@ -37,8 +37,10 @@ import org.chromium.net.test.util.TestWebServer; ...@@ -37,8 +37,10 @@ import org.chromium.net.test.util.TestWebServer;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.util.Map; import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
...@@ -483,6 +485,22 @@ public class AwActivityTestRule extends ActivityTestRule<AwTestRunnerActivity> { ...@@ -483,6 +485,22 @@ public class AwActivityTestRule extends ActivityTestRule<AwTestRunnerActivity> {
pollInstrumentationThread(() -> TestThreadUtils.runOnUiThreadBlocking(callable)); pollInstrumentationThread(() -> TestThreadUtils.runOnUiThreadBlocking(callable));
} }
/**
* Takes an element out of the {@link BlockingQueue} (or times out).
*/
public static <T> T waitForNextQueueElement(BlockingQueue<T> queue) throws Exception {
T value = queue.poll(WAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS);
if (value == null) {
// {@code null} is the special value which means {@link BlockingQueue#poll} has timed
// out (also: there's no risk for collision with real values, because BlockingQueue does
// not allow null entries). Instead of returning this special value, let's throw a
// proper TimeoutException.
throw new TimeoutException(
"Timeout while trying to take next entry from BlockingQueue");
}
return value;
}
/** /**
* Clears the resource cache. Note that the cache is per-application, so this will clear the * Clears the resource cache. Note that the cache is per-application, so this will clear the
* cache for all WebViews used. * cache for all WebViews used.
......
...@@ -4,8 +4,6 @@ ...@@ -4,8 +4,6 @@
package org.chromium.android_webview.test; package org.chromium.android_webview.test;
import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
import android.net.Uri; import android.net.Uri;
import android.support.test.InstrumentationRegistry; import android.support.test.InstrumentationRegistry;
import android.support.test.filters.MediumTest; import android.support.test.filters.MediumTest;
...@@ -29,8 +27,6 @@ import org.chromium.content_public.browser.test.util.TestThreadUtils; ...@@ -29,8 +27,6 @@ import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.net.test.EmbeddedTestServer; import org.chromium.net.test.EmbeddedTestServer;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/** /**
* Test suite for JavaScript Java interaction. * Test suite for JavaScript Java interaction.
...@@ -57,9 +53,6 @@ public class JsJavaInteractionTest { ...@@ -57,9 +53,6 @@ public class JsJavaInteractionTest {
private static final String DATA_HTML = "<html><body>data</body></html>"; private static final String DATA_HTML = "<html><body>data</body></html>";
private static final int MESSAGE_COUNT = 10000; private static final int MESSAGE_COUNT = 10000;
// Timeout to failure, in milliseconds
private static final long TIMEOUT = scaleTimeout(5000);
private EmbeddedTestServer mTestServer; private EmbeddedTestServer mTestServer;
private TestAwContentsClient mContentsClient; private TestAwContentsClient mContentsClient;
private AwContents mAwContents; private AwContents mAwContents;
...@@ -92,7 +85,7 @@ public class JsJavaInteractionTest { ...@@ -92,7 +85,7 @@ public class JsJavaInteractionTest {
} }
public Data waitForOnPostMessage() throws Exception { public Data waitForOnPostMessage() throws Exception {
return waitForNextQueueElement(mQueue); return AwActivityTestRule.waitForNextQueueElement(mQueue);
} }
public boolean hasNoMoreOnPostMessage() { public boolean hasNoMoreOnPostMessage() {
...@@ -100,15 +93,6 @@ public class JsJavaInteractionTest { ...@@ -100,15 +93,6 @@ public class JsJavaInteractionTest {
} }
} }
private static <T> T waitForNextQueueElement(LinkedBlockingQueue<T> queue) throws Exception {
T value = queue.poll(TIMEOUT, TimeUnit.MILLISECONDS);
if (value == null) {
throw new TimeoutException(
"Timeout while trying to take next entry from BlockingQueue");
}
return value;
}
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
mContentsClient = new TestAwContentsClient(); mContentsClient = new TestAwContentsClient();
......
...@@ -34,7 +34,6 @@ import org.chromium.net.test.util.TestWebServer; ...@@ -34,7 +34,6 @@ import org.chromium.net.test.util.TestWebServer;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/** /**
* The tests for content postMessage API. * The tests for content postMessage API.
...@@ -70,7 +69,7 @@ public class PostMessageTest { ...@@ -70,7 +69,7 @@ public class PostMessageTest {
} }
public Data waitForMessage() throws Exception { public Data waitForMessage() throws Exception {
return waitForNextQueueElement(mQueue); return AwActivityTestRule.waitForNextQueueElement(mQueue);
} }
} }
...@@ -106,7 +105,7 @@ public class PostMessageTest { ...@@ -106,7 +105,7 @@ public class PostMessageTest {
} }
public Data waitForMessageCallback() throws Exception { public Data waitForMessageCallback() throws Exception {
return waitForNextQueueElement(mQueue); return AwActivityTestRule.waitForNextQueueElement(mQueue);
} }
public boolean isQueueEmpty() { public boolean isQueueEmpty() {
...@@ -114,15 +113,6 @@ public class PostMessageTest { ...@@ -114,15 +113,6 @@ public class PostMessageTest {
} }
} }
private static <T> T waitForNextQueueElement(LinkedBlockingQueue<T> queue) throws Exception {
T value = queue.poll(TIMEOUT, TimeUnit.MILLISECONDS);
if (value == null) {
throw new TimeoutException(
"Timeout while trying to take next entry from BlockingQueue");
}
return value;
}
private MessageObject mMessageObject; private MessageObject mMessageObject;
private TestAwContentsClient mContentsClient; private TestAwContentsClient mContentsClient;
private AwTestContainerView mTestContainerView; private AwTestContainerView mTestContainerView;
...@@ -260,7 +250,7 @@ public class PostMessageTest { ...@@ -260,7 +250,7 @@ public class PostMessageTest {
} }
Assert.fail(); Assert.fail();
}); });
boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILLISECONDS); boolean ignore = latch.await(TIMEOUT, TimeUnit.MILLISECONDS);
} }
// There are two cases that put a port in a started state. // There are two cases that put a port in a started state.
...@@ -286,7 +276,7 @@ public class PostMessageTest { ...@@ -286,7 +276,7 @@ public class PostMessageTest {
} }
Assert.fail(); Assert.fail();
}); });
boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILLISECONDS); boolean ignore = latch.await(TIMEOUT, TimeUnit.MILLISECONDS);
} }
// see documentation in testStartedPortCannotBeTransferredUsingPostMessageToMainFrame1 // see documentation in testStartedPortCannotBeTransferredUsingPostMessageToMainFrame1
...@@ -310,7 +300,7 @@ public class PostMessageTest { ...@@ -310,7 +300,7 @@ public class PostMessageTest {
} }
Assert.fail(); Assert.fail();
}); });
boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILLISECONDS); boolean ignore = latch.await(TIMEOUT, TimeUnit.MILLISECONDS);
} }
// see documentation in testStartedPortCannotBeTransferredUsingPostMessageToMainFrame1 // see documentation in testStartedPortCannotBeTransferredUsingPostMessageToMainFrame1
...@@ -333,7 +323,7 @@ public class PostMessageTest { ...@@ -333,7 +323,7 @@ public class PostMessageTest {
} }
Assert.fail(); Assert.fail();
}); });
boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILLISECONDS); boolean ignore = latch.await(TIMEOUT, TimeUnit.MILLISECONDS);
} }
// see documentation in testStartedPortCannotBeTransferredUsingPostMessageToMainFrame1 // see documentation in testStartedPortCannotBeTransferredUsingPostMessageToMainFrame1
...@@ -357,7 +347,7 @@ public class PostMessageTest { ...@@ -357,7 +347,7 @@ public class PostMessageTest {
} }
Assert.fail(); Assert.fail();
}); });
boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILLISECONDS); boolean ignore = latch.await(TIMEOUT, TimeUnit.MILLISECONDS);
} }
...@@ -384,7 +374,7 @@ public class PostMessageTest { ...@@ -384,7 +374,7 @@ public class PostMessageTest {
} }
Assert.fail(); Assert.fail();
}); });
boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILLISECONDS); boolean ignore = latch.await(TIMEOUT, TimeUnit.MILLISECONDS);
} }
// Verify a closed port cannot be transferred to a frame. // Verify a closed port cannot be transferred to a frame.
...@@ -406,7 +396,7 @@ public class PostMessageTest { ...@@ -406,7 +396,7 @@ public class PostMessageTest {
} }
Assert.fail(); Assert.fail();
}); });
boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILLISECONDS); boolean ignore = latch.await(TIMEOUT, TimeUnit.MILLISECONDS);
} }
// Verify a closed port cannot be transferred to a port. // Verify a closed port cannot be transferred to a port.
...@@ -428,7 +418,7 @@ public class PostMessageTest { ...@@ -428,7 +418,7 @@ public class PostMessageTest {
} }
Assert.fail(); Assert.fail();
}); });
boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILLISECONDS); boolean ignore = latch.await(TIMEOUT, TimeUnit.MILLISECONDS);
} }
// Verify messages cannot be posted to closed ports. // Verify messages cannot be posted to closed ports.
...@@ -449,7 +439,7 @@ public class PostMessageTest { ...@@ -449,7 +439,7 @@ public class PostMessageTest {
} }
Assert.fail(); Assert.fail();
}); });
boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILLISECONDS); boolean ignore = latch.await(TIMEOUT, TimeUnit.MILLISECONDS);
} }
// Verify messages posted before closing a port is received at the destination port. // Verify messages posted before closing a port is received at the destination port.
...@@ -488,7 +478,7 @@ public class PostMessageTest { ...@@ -488,7 +478,7 @@ public class PostMessageTest {
} }
Assert.fail(); Assert.fail();
}); });
boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILLISECONDS); boolean ignore = latch.await(TIMEOUT, TimeUnit.MILLISECONDS);
} }
// Verify a transferred port using postMessageToMainFrame cannot be closed. // Verify a transferred port using postMessageToMainFrame cannot be closed.
...@@ -512,7 +502,7 @@ public class PostMessageTest { ...@@ -512,7 +502,7 @@ public class PostMessageTest {
} }
Assert.fail(); Assert.fail();
}); });
boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILLISECONDS); boolean ignore = latch.await(TIMEOUT, TimeUnit.MILLISECONDS);
} }
// Create two message channels, and while they are in pending state, transfer the // Create two message channels, and while they are in pending state, transfer the
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment