Commit a8f4b170 authored by bsheedy's avatar bsheedy Committed by Commit Bot

Cleanup XR usage of EmbeddedTestServer

Cleans up manual usage of EmbeddedTestServer in XR tests, which was both
adding a lot of duplicate code and had the potential to fail to clean up
the server if the test failed, which could then affect other tests.

This is achieved by making EmbeddedTestServerRule lazy and applying it
to all ChromeActivityTestRules, not just WebappActivityTestRule.

Bug: 901923
Change-Id: I431dac3ce2488a1a5762a1ab1f5ec68aea16c9f5
Reviewed-on: https://chromium-review.googlesource.com/c/1320024
Commit-Queue: Brian Sheedy <bsheedy@chromium.org>
Reviewed-by: default avatarTommy Nyquist <nyquist@chromium.org>
Reviewed-by: default avatarPaul Jensen <pauljensen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#609556}
parent 02d3aba1
......@@ -13,10 +13,8 @@ import static org.chromium.chrome.test.util.ChromeRestriction.RESTRICTION_TYPE_V
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PointF;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.LargeTest;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
......@@ -35,7 +33,6 @@ import org.chromium.chrome.browser.vr.util.VrBrowserTransitionUtils;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.util.RenderTestRule;
import org.chromium.content_public.browser.test.util.JavaScriptUtils;
import org.chromium.net.test.EmbeddedTestServer;
import java.io.File;
import java.io.IOException;
......@@ -71,7 +68,6 @@ public class VrBrowserDialogTest {
new RenderTestRule("components/test/data/permission_dialogs/render_tests");
private VrBrowserTestFramework mVrBrowserTestFramework;
private EmbeddedTestServer mServer;
@Before
public void setUp() throws Exception {
......@@ -81,13 +77,7 @@ public class VrBrowserDialogTest {
if (!sBaseDirectory.exists() && !sBaseDirectory.isDirectory()) {
Assert.assertTrue("Failed to make image capture directory", sBaseDirectory.mkdirs());
}
}
@After
public void tearDown() throws Exception {
if (mServer != null) {
mServer.stopAndDestroyServer();
}
mVrTestRule.getEmbeddedTestServerRule().setServerPort(SERVER_PORT);
}
private void captureScreen(String filenameBase) throws InterruptedException {
......@@ -115,12 +105,9 @@ public class VrBrowserDialogTest {
throws InterruptedException, TimeoutException {
// Trying to grant permissions on file:// URLs ends up hitting DCHECKS, so load from a local
// server instead.
if (mServer == null) {
mServer = EmbeddedTestServer.createAndStartServerWithPort(
InstrumentationRegistry.getContext(), SERVER_PORT);
}
mVrBrowserTestFramework.loadUrlAndAwaitInitialization(
mServer.getURL(VrBrowserTestFramework.getEmbeddedServerPathForHtmlTestFile(page)),
mVrTestRule.getTestServer().getURL(
VrBrowserTestFramework.getEmbeddedServerPathForHtmlTestFile(page)),
PAGE_LOAD_TIMEOUT_S);
// Display the given permission prompt.
......
......@@ -11,7 +11,6 @@ import static org.chromium.chrome.test.util.ChromeRestriction.RESTRICTION_TYPE_V
import android.graphics.PointF;
import android.os.SystemClock;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.MediumTest;
import org.junit.Assert;
......@@ -31,7 +30,6 @@ import org.chromium.chrome.browser.vr.util.VrBrowserTransitionUtils;
import org.chromium.chrome.browser.vr.util.VrShellDelegateUtils;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.util.ChromeTabUtils;
import org.chromium.net.test.EmbeddedTestServer;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
......@@ -93,11 +91,10 @@ public class VrBrowserNativeUiTest {
@MediumTest
public void testPaymentRequest() throws InterruptedException {
// We can't request payment on file:// URLs, so use a local server.
EmbeddedTestServer server =
EmbeddedTestServer.createAndStartServer(InstrumentationRegistry.getContext());
mVrBrowserTestFramework.loadUrlAndAwaitInitialization(
server.getURL(VrBrowserTestFramework.getEmbeddedServerPathForHtmlTestFile(
"test_payment_request")),
mVrTestRule.getTestServer().getURL(
VrBrowserTestFramework.getEmbeddedServerPathForHtmlTestFile(
"test_payment_request")),
PAGE_LOAD_TIMEOUT_S);
// Set up an observer so we'll know if the payment request is shown.
AtomicBoolean requestShown = new AtomicBoolean(false);
......@@ -124,7 +121,6 @@ public class VrBrowserNativeUiTest {
Assert.assertTrue("Payment request caused VR exit",
VrShellDelegateUtils.getDelegateInstance().isVrEntryComplete());
mVrBrowserTestFramework.endTest();
server.stopAndDestroyServer();
}
/**
......
......@@ -51,7 +51,6 @@ import org.chromium.chrome.test.util.ActivityUtils;
import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.content_public.browser.test.util.DOMUtils;
import org.chromium.content_public.browser.test.util.JavaScriptUtils;
import org.chromium.net.test.EmbeddedTestServer;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
......@@ -542,11 +541,10 @@ public class VrBrowserTransitionTest {
@MediumTest
public void testPermissionsPersistWhenEnteringVrBrowser() throws InterruptedException {
// Permissions don't work on file:// URLs, so use a local server.
EmbeddedTestServer server =
EmbeddedTestServer.createAndStartServer(InstrumentationRegistry.getContext());
mVrBrowserTestFramework.loadUrlAndAwaitInitialization(
server.getURL(VrBrowserTestFramework.getEmbeddedServerPathForHtmlTestFile(
"test_permissions_persist_when_entering_vr_browser")),
mTestRule.getTestServer().getURL(
VrBrowserTestFramework.getEmbeddedServerPathForHtmlTestFile(
"test_permissions_persist_when_entering_vr_browser")),
PAGE_LOAD_TIMEOUT_S);
// Ensure that permission requests initially trigger a prompt.
Assert.assertTrue("Camera permission would not trigger prompt",
......@@ -562,8 +560,9 @@ public class VrBrowserTransitionTest {
mVrBrowserTestFramework.waitOnJavaScriptStep();
// Reload the page and ensure that the permissions are still granted.
mVrBrowserTestFramework.loadUrlAndAwaitInitialization(
server.getURL(VrBrowserTestFramework.getEmbeddedServerPathForHtmlTestFile(
"test_permissions_persist_when_entering_vr_browser")),
mTestRule.getTestServer().getURL(
VrBrowserTestFramework.getEmbeddedServerPathForHtmlTestFile(
"test_permissions_persist_when_entering_vr_browser")),
PAGE_LOAD_TIMEOUT_S);
Assert.assertFalse("Camera permission would trigger prompt after reload",
mVrBrowserTestFramework.permissionRequestWouldTriggerPrompt("camera"));
......@@ -573,7 +572,6 @@ public class VrBrowserTransitionTest {
VrBrowserTransitionUtils.forceEnterVrBrowserOrFail(POLL_TIMEOUT_LONG_MS);
mVrBrowserTestFramework.executeStepAndWait("stepRequestPermission()");
mVrBrowserTestFramework.endTest();
server.stopAndDestroyServer();
}
/**
......
......@@ -8,10 +8,8 @@ import static org.chromium.chrome.browser.vr.WebXrArTestFramework.PAGE_LOAD_TIME
import static org.chromium.chrome.browser.vr.WebXrArTestFramework.POLL_TIMEOUT_LONG_MS;
import android.os.Build;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.MediumTest;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
......@@ -30,7 +28,6 @@ import org.chromium.chrome.browser.vr.rules.XrActivityRestriction;
import org.chromium.chrome.browser.vr.util.XrTestRuleUtils;
import org.chromium.chrome.test.ChromeActivityTestRule;
import org.chromium.chrome.test.ChromeJUnit4RunnerDelegate;
import org.chromium.net.test.EmbeddedTestServer;
import java.util.List;
import java.util.concurrent.Callable;
......@@ -52,9 +49,6 @@ public class WebXrArSessionTest {
private ChromeActivityTestRule mTestRule;
private WebXrArTestFramework mWebXrArTestFramework;
private EmbeddedTestServer mServer;
private boolean mShouldCreateServer;
public WebXrArSessionTest(Callable<ChromeActivityTestRule> callable) throws Exception {
mTestRule = callable.call();
......@@ -64,21 +58,6 @@ public class WebXrArSessionTest {
@Before
public void setUp() throws Exception {
mWebXrArTestFramework = new WebXrArTestFramework(mTestRule);
// WebappActivityTestRule automatically creates a test server, and creating multiple causes
// it to crash hitting a DCHECK. So, only handle the server ourselves if whatever test rule
// we're using doesn't create one itself.
mServer = mTestRule.getTestServer();
if (mServer == null) {
mShouldCreateServer = true;
mServer = EmbeddedTestServer.createAndStartServer(InstrumentationRegistry.getContext());
}
}
@After
public void tearDown() throws Exception {
if (mServer != null && mShouldCreateServer) {
mServer.stopAndDestroyServer();
}
}
/**
......@@ -89,8 +68,9 @@ public class WebXrArSessionTest {
@XrActivityRestriction({XrActivityRestriction.SupportedActivity.ALL})
public void testArRequestSessionSucceeds() throws InterruptedException {
mWebXrArTestFramework.loadUrlAndAwaitInitialization(
mServer.getURL(WebXrArTestFramework.getEmbeddedServerPathForHtmlTestFile(
"test_ar_request_session_succeeds")),
mTestRule.getTestServer().getURL(
WebXrArTestFramework.getEmbeddedServerPathForHtmlTestFile(
"test_ar_request_session_succeeds")),
PAGE_LOAD_TIMEOUT_S);
mWebXrArTestFramework.enterSessionWithUserGestureOrFail();
mWebXrArTestFramework.assertNoJavaScriptErrors();
......@@ -105,8 +85,9 @@ public class WebXrArSessionTest {
@XrActivityRestriction({XrActivityRestriction.SupportedActivity.ALL})
public void testRepeatedArSessionsSucceed() throws InterruptedException {
mWebXrArTestFramework.loadUrlAndAwaitInitialization(
mServer.getURL(WebXrArTestFramework.getEmbeddedServerPathForHtmlTestFile(
"test_ar_request_session_succeeds")),
mTestRule.getTestServer().getURL(
WebXrArTestFramework.getEmbeddedServerPathForHtmlTestFile(
"test_ar_request_session_succeeds")),
PAGE_LOAD_TIMEOUT_S);
for (int i = 0; i < 2; i++) {
mWebXrArTestFramework.enterSessionWithUserGestureOrFail();
......@@ -124,8 +105,9 @@ public class WebXrArSessionTest {
@XrActivityRestriction({XrActivityRestriction.SupportedActivity.ALL})
public void testRepeatedArSessionsOnlyPromptPermissionsOnce() throws InterruptedException {
mWebXrArTestFramework.loadUrlAndAwaitInitialization(
mServer.getURL(WebXrArTestFramework.getEmbeddedServerPathForHtmlTestFile(
"test_ar_request_session_succeeds")),
mTestRule.getTestServer().getURL(
WebXrArTestFramework.getEmbeddedServerPathForHtmlTestFile(
"test_ar_request_session_succeeds")),
PAGE_LOAD_TIMEOUT_S);
Assert.assertTrue("First AR session request did not trigger permission prompt",
mWebXrArTestFramework.permissionRequestWouldTriggerPrompt("camera"));
......
......@@ -15,7 +15,6 @@ import static org.chromium.chrome.test.util.ChromeRestriction.RESTRICTION_TYPE_V
import android.graphics.PointF;
import android.os.Build;
import android.os.SystemClock;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.LargeTest;
import android.support.test.filters.MediumTest;
import android.view.MotionEvent;
......@@ -52,7 +51,6 @@ import org.chromium.chrome.test.ChromeJUnit4RunnerDelegate;
import org.chromium.content_public.browser.ViewEventSink;
import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.content_public.browser.test.util.TouchCommon;
import org.chromium.net.test.EmbeddedTestServer;
import java.util.List;
import java.util.concurrent.Callable;
......@@ -738,15 +736,10 @@ public class WebXrVrInputTest {
private void testAppButtonLongPressDisplaysPermissionsImpl() throws InterruptedException {
// Note that we need to pass in the WebContents to use throughout this because automatically
// using the first tab's WebContents doesn't work in Incognito.
EmbeddedTestServer server = mTestRule.getTestServer();
boolean teardownServer = false;
if (server == null) {
server = EmbeddedTestServer.createAndStartServer(InstrumentationRegistry.getContext());
teardownServer = true;
}
mWebXrVrTestFramework.loadUrlAndAwaitInitialization(
server.getURL(WebXrVrTestFramework.getEmbeddedServerPathForHtmlTestFile(
"generic_webxr_permission_page")),
mTestRule.getTestServer().getURL(
WebXrVrTestFramework.getEmbeddedServerPathForHtmlTestFile(
"generic_webxr_permission_page")),
PAGE_LOAD_TIMEOUT_S);
WebXrVrTestFramework.runJavaScriptOrFail("requestPermission({audio:true})",
POLL_TIMEOUT_SHORT_MS, mTestRule.getWebContents());
......@@ -785,9 +778,6 @@ public class WebXrVrInputTest {
UserFriendlyElementName.WEB_XR_AUDIO_INDICATOR, true /* visible */, () -> {});
NativeUiUtils.performActionAndWaitForVisibilityStatus(
UserFriendlyElementName.WEB_XR_AUDIO_INDICATOR, false /* visible */, () -> {});
if (teardownServer) {
server.stopAndDestroyServer();
}
}
/**
......@@ -825,16 +815,10 @@ public class WebXrVrInputTest {
private void testInSessionPermissionRequestsImpl() throws InterruptedException {
// Note that we need to pass in the WebContents to use throughout this because automatically
// using the first tab's WebContents doesn't work in Incognito.
EmbeddedTestServer server = mTestRule.getTestServer();
boolean teardownServer = false;
if (server == null) {
server = EmbeddedTestServer.createAndStartServer(InstrumentationRegistry.getContext());
teardownServer = true;
}
mWebXrVrTestFramework.loadUrlAndAwaitInitialization(
server.getURL(WebXrVrTestFramework.getEmbeddedServerPathForHtmlTestFile(
"generic_webxr_permission_page")),
mTestRule.getTestServer().getURL(
WebXrVrTestFramework.getEmbeddedServerPathForHtmlTestFile(
"generic_webxr_permission_page")),
PAGE_LOAD_TIMEOUT_S);
mWebXrVrTestFramework.enterSessionWithUserGestureOrFail(mTestRule.getWebContents());
NativeUiUtils.performActionAndWaitForVisibilityStatus(
......@@ -861,9 +845,5 @@ public class WebXrVrInputTest {
.runJavaScriptOrFail("lastPermissionRequestSucceeded",
POLL_TIMEOUT_SHORT_MS, mTestRule.getWebContents())
.equals("true"));
if (teardownServer) {
server.stopAndDestroyServer();
}
}
}
......@@ -9,7 +9,6 @@ import static org.chromium.chrome.browser.vr.XrTestFramework.POLL_TIMEOUT_SHORT_
import static org.chromium.chrome.test.util.ChromeRestriction.RESTRICTION_TYPE_VIEWER_DAYDREAM_OR_STANDALONE;
import android.os.Build;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.MediumTest;
import org.junit.Before;
......@@ -33,7 +32,6 @@ import org.chromium.chrome.browser.vr.util.PermissionUtils;
import org.chromium.chrome.browser.vr.util.VrTestRuleUtils;
import org.chromium.chrome.test.ChromeActivityTestRule;
import org.chromium.chrome.test.ChromeJUnit4RunnerDelegate;
import org.chromium.net.test.EmbeddedTestServer;
import java.util.List;
import java.util.concurrent.Callable;
......@@ -128,16 +126,10 @@ public class WebXrVrTabTest {
}
private void testPermissionsInOtherTabImpl(boolean incognito) throws InterruptedException {
EmbeddedTestServer server = mTestRule.getTestServer();
boolean teardownServer = false;
if (server == null) {
server = EmbeddedTestServer.createAndStartServer(InstrumentationRegistry.getContext());
teardownServer = true;
}
mWebXrVrTestFramework.loadUrlAndAwaitInitialization(
server.getURL(WebXrVrTestFramework.getEmbeddedServerPathForHtmlTestFile(
"generic_webxr_permission_page")),
mTestRule.getTestServer().getURL(
WebXrVrTestFramework.getEmbeddedServerPathForHtmlTestFile(
"generic_webxr_permission_page")),
PAGE_LOAD_TIMEOUT_S);
// Be sure to store the stream we're given so that the permission is actually in use, as
// otherwise the toast doesn't show up since another tab isn't actually using the
......@@ -165,9 +157,5 @@ public class WebXrVrTabTest {
mWebXrVrTestFramework.enterSessionWithUserGestureOrFail(mTestRule.getWebContents());
NativeUiUtils.performActionAndWaitForVisibilityStatus(
UserFriendlyElementName.WEB_XR_AUDIO_INDICATOR, true /* visible */, () -> {});
if (teardownServer) {
server.stopAndDestroyServer();
}
}
}
......@@ -13,7 +13,6 @@ import android.support.test.InstrumentationRegistry;
import android.view.ViewGroup;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
......@@ -27,8 +26,6 @@ import org.chromium.content_public.browser.test.util.Criteria;
import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.content_public.browser.test.util.DOMUtils;
import org.chromium.content_public.browser.test.util.JavaScriptUtils;
import org.chromium.net.test.EmbeddedTestServer;
import org.chromium.net.test.EmbeddedTestServerRule;
/**
* Custom {@link ChromeActivityTestRule} for tests using {@link WebappActivity}.
......@@ -73,18 +70,10 @@ public class WebappActivityTestRule extends ChromeActivityTestRule<WebappActivit
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
+ "AAAAAAAAAOA3AvAAAdln8YgAAAAASUVORK5CYII=";
@Rule
private EmbeddedTestServerRule mTestServerRule = new EmbeddedTestServerRule();
public WebappActivityTestRule() {
super(WebappActivity0.class);
}
@Override
public EmbeddedTestServer getTestServer() {
return mTestServerRule.getServer();
}
/**
* Creates the Intent that starts the WebAppActivity. This is meant to be overriden by other
* tests in order for them to pass some specific values, but it defaults to a web app that just
......@@ -151,8 +140,7 @@ public class WebappActivityTestRule extends ChromeActivityTestRule<WebappActivit
}
};
Statement testServerStatement = mTestServerRule.apply(webappTestRuleStatement, description);
return super.apply(testServerStatement, description);
return super.apply(webappTestRuleStatement, description);
}
/**
......
......@@ -19,6 +19,7 @@ import android.text.TextUtils;
import android.view.Menu;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
......@@ -60,6 +61,7 @@ import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.content_public.browser.test.util.JavaScriptUtils;
import org.chromium.content_public.browser.test.util.RenderProcessLimit;
import org.chromium.net.test.EmbeddedTestServer;
import org.chromium.net.test.EmbeddedTestServerRule;
import org.chromium.ui.KeyboardVisibilityDelegate;
import org.chromium.ui.base.PageTransition;
......@@ -92,6 +94,9 @@ public class ChromeActivityTestRule<T extends ChromeActivity> extends ActivityTe
private T mSetActivity;
private String mCurrentTestName;
@Rule
private EmbeddedTestServerRule mTestServerRule = new EmbeddedTestServerRule();
public ChromeActivityTestRule(Class<T> activityClass) {
this(activityClass, false);
}
......@@ -104,7 +109,7 @@ public class ChromeActivityTestRule<T extends ChromeActivity> extends ActivityTe
@Override
public Statement apply(final Statement base, Description description) {
mCurrentTestName = description.getMethodName();
final Statement superBase = super.apply(new Statement() {
Statement chromeActivityStatement = new Statement() {
@Override
public void evaluate() throws Throwable {
mDefaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
......@@ -123,8 +128,9 @@ public class ChromeActivityTestRule<T extends ChromeActivity> extends ActivityTe
base.evaluate();
}
}, description);
return superBase;
};
Statement testServerStatement = mTestServerRule.apply(chromeActivityStatement, description);
return super.apply(testServerStatement, description);
}
/**
......@@ -651,7 +657,14 @@ public class ChromeActivityTestRule<T extends ChromeActivity> extends ActivityTe
* Gets the ChromeActivityTestRule's EmbeddedTestServer instance if it has one.
*/
public EmbeddedTestServer getTestServer() {
return null;
return mTestServerRule.getServer();
}
/**
* Gets the underlying EmbeddedTestServerRule for getTestServer().
*/
public EmbeddedTestServerRule getEmbeddedTestServerRule() {
return mTestServerRule;
}
/**
......
......@@ -9,39 +9,66 @@ import android.support.test.InstrumentationRegistry;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import javax.annotation.concurrent.GuardedBy;
/**
* Junit4 rule for starting embedded test server before a test starts, and shutting it down when it
* finishes.
* Junit4 rule for starting embedded test server when necessary (i.e. when accessed via
* {@link #getServer()}), and shutting it down when the test finishes.
*/
public class EmbeddedTestServerRule extends TestWatcher {
EmbeddedTestServer mServer;
private final Object mLock = new Object();
@GuardedBy("mLock")
private EmbeddedTestServer mServer;
@Override
protected void starting(Description description) {
try {
mServer = EmbeddedTestServer.createAndStartServer(InstrumentationRegistry.getContext());
} catch (InterruptedException e) {
throw new EmbeddedTestServer.EmbeddedTestServerFailure("Test server didn't start");
}
super.starting(description);
}
// The default value of 0 will result in the same behavior as createAndStartServer
// (auto-selected port).
@GuardedBy("mLock")
private int mServerPort;
@Override
protected void finished(Description description) {
super.finished(description);
mServer.stopAndDestroyServer();
synchronized (mLock) {
if (mServer != null) {
mServer.stopAndDestroyServer();
}
}
}
/**
* Get the test server.
* Get the test server, creating and starting it if it doesn't exist yet.
*
* @return the test server.
*/
public EmbeddedTestServer getServer() {
return mServer;
synchronized (mLock) {
if (mServer == null) {
try {
mServer = EmbeddedTestServer.createAndStartServerWithPort(
InstrumentationRegistry.getContext(), mServerPort);
} catch (InterruptedException e) {
throw new EmbeddedTestServer.EmbeddedTestServerFailure(
"Test server didn't start");
}
}
return mServer;
}
}
public String getOrigin() {
return mServer.getURL("/");
return getServer().getURL("/");
}
/**
* Sets the port that the server will be started with. Must be called before the first
* {@link #getServer()} call.
*
* @param port the port to start the server with, or 0 for an automatically selected one.
*/
public void setServerPort(int port) {
synchronized (mLock) {
assert mServer == null;
mServerPort = port;
}
}
}
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