Skip to content

Commit a8f4b17

Browse files
bsheedyCommit Bot
bsheedy
authored and
Commit Bot
committed
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: Tommy Nyquist <nyquist@chromium.org> Reviewed-by: Paul Jensen <pauljensen@chromium.org> Cr-Commit-Position: refs/heads/master@{#609556}
1 parent 02d3aba commit a8f4b17

File tree

9 files changed

+91
-132
lines changed

9 files changed

+91
-132
lines changed

chrome/android/javatests/src/org/chromium/chrome/browser/vr/VrBrowserDialogTest.java

+3-16
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313
import android.graphics.Bitmap;
1414
import android.graphics.BitmapFactory;
1515
import android.graphics.PointF;
16-
import android.support.test.InstrumentationRegistry;
1716
import android.support.test.filters.LargeTest;
1817

19-
import org.junit.After;
2018
import org.junit.Assert;
2119
import org.junit.Before;
2220
import org.junit.Rule;
@@ -35,7 +33,6 @@
3533
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
3634
import org.chromium.chrome.test.util.RenderTestRule;
3735
import org.chromium.content_public.browser.test.util.JavaScriptUtils;
38-
import org.chromium.net.test.EmbeddedTestServer;
3936

4037
import java.io.File;
4138
import java.io.IOException;
@@ -71,7 +68,6 @@ public class VrBrowserDialogTest {
7168
new RenderTestRule("components/test/data/permission_dialogs/render_tests");
7269

7370
private VrBrowserTestFramework mVrBrowserTestFramework;
74-
private EmbeddedTestServer mServer;
7571

7672
@Before
7773
public void setUp() throws Exception {
@@ -81,13 +77,7 @@ public void setUp() throws Exception {
8177
if (!sBaseDirectory.exists() && !sBaseDirectory.isDirectory()) {
8278
Assert.assertTrue("Failed to make image capture directory", sBaseDirectory.mkdirs());
8379
}
84-
}
85-
86-
@After
87-
public void tearDown() throws Exception {
88-
if (mServer != null) {
89-
mServer.stopAndDestroyServer();
90-
}
80+
mVrTestRule.getEmbeddedTestServerRule().setServerPort(SERVER_PORT);
9181
}
9282

9383
private void captureScreen(String filenameBase) throws InterruptedException {
@@ -115,12 +105,9 @@ private void navigateAndDisplayPermissionPrompt(String page, String promptComman
115105
throws InterruptedException, TimeoutException {
116106
// Trying to grant permissions on file:// URLs ends up hitting DCHECKS, so load from a local
117107
// server instead.
118-
if (mServer == null) {
119-
mServer = EmbeddedTestServer.createAndStartServerWithPort(
120-
InstrumentationRegistry.getContext(), SERVER_PORT);
121-
}
122108
mVrBrowserTestFramework.loadUrlAndAwaitInitialization(
123-
mServer.getURL(VrBrowserTestFramework.getEmbeddedServerPathForHtmlTestFile(page)),
109+
mVrTestRule.getTestServer().getURL(
110+
VrBrowserTestFramework.getEmbeddedServerPathForHtmlTestFile(page)),
124111
PAGE_LOAD_TIMEOUT_S);
125112

126113
// Display the given permission prompt.

chrome/android/javatests/src/org/chromium/chrome/browser/vr/VrBrowserNativeUiTest.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
import android.graphics.PointF;
1313
import android.os.SystemClock;
14-
import android.support.test.InstrumentationRegistry;
1514
import android.support.test.filters.MediumTest;
1615

1716
import org.junit.Assert;
@@ -31,7 +30,6 @@
3130
import org.chromium.chrome.browser.vr.util.VrShellDelegateUtils;
3231
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
3332
import org.chromium.chrome.test.util.ChromeTabUtils;
34-
import org.chromium.net.test.EmbeddedTestServer;
3533

3634
import java.util.concurrent.TimeoutException;
3735
import java.util.concurrent.atomic.AtomicBoolean;
@@ -93,11 +91,10 @@ public void testUrlOnNonNativeUi()
9391
@MediumTest
9492
public void testPaymentRequest() throws InterruptedException {
9593
// We can't request payment on file:// URLs, so use a local server.
96-
EmbeddedTestServer server =
97-
EmbeddedTestServer.createAndStartServer(InstrumentationRegistry.getContext());
9894
mVrBrowserTestFramework.loadUrlAndAwaitInitialization(
99-
server.getURL(VrBrowserTestFramework.getEmbeddedServerPathForHtmlTestFile(
100-
"test_payment_request")),
95+
mVrTestRule.getTestServer().getURL(
96+
VrBrowserTestFramework.getEmbeddedServerPathForHtmlTestFile(
97+
"test_payment_request")),
10198
PAGE_LOAD_TIMEOUT_S);
10299
// Set up an observer so we'll know if the payment request is shown.
103100
AtomicBoolean requestShown = new AtomicBoolean(false);
@@ -124,7 +121,6 @@ public void onPaymentRequestResultReady(PaymentRequestUI ui) {}
124121
Assert.assertTrue("Payment request caused VR exit",
125122
VrShellDelegateUtils.getDelegateInstance().isVrEntryComplete());
126123
mVrBrowserTestFramework.endTest();
127-
server.stopAndDestroyServer();
128124
}
129125

130126
/**

chrome/android/javatests/src/org/chromium/chrome/browser/vr/VrBrowserTransitionTest.java

+6-8
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import org.chromium.content_public.browser.test.util.CriteriaHelper;
5252
import org.chromium.content_public.browser.test.util.DOMUtils;
5353
import org.chromium.content_public.browser.test.util.JavaScriptUtils;
54-
import org.chromium.net.test.EmbeddedTestServer;
5554

5655
import java.util.concurrent.TimeoutException;
5756
import java.util.concurrent.atomic.AtomicBoolean;
@@ -542,11 +541,10 @@ public boolean setAsyncReprojectionEnabled(boolean enabled) {
542541
@MediumTest
543542
public void testPermissionsPersistWhenEnteringVrBrowser() throws InterruptedException {
544543
// Permissions don't work on file:// URLs, so use a local server.
545-
EmbeddedTestServer server =
546-
EmbeddedTestServer.createAndStartServer(InstrumentationRegistry.getContext());
547544
mVrBrowserTestFramework.loadUrlAndAwaitInitialization(
548-
server.getURL(VrBrowserTestFramework.getEmbeddedServerPathForHtmlTestFile(
549-
"test_permissions_persist_when_entering_vr_browser")),
545+
mTestRule.getTestServer().getURL(
546+
VrBrowserTestFramework.getEmbeddedServerPathForHtmlTestFile(
547+
"test_permissions_persist_when_entering_vr_browser")),
550548
PAGE_LOAD_TIMEOUT_S);
551549
// Ensure that permission requests initially trigger a prompt.
552550
Assert.assertTrue("Camera permission would not trigger prompt",
@@ -562,8 +560,9 @@ public void testPermissionsPersistWhenEnteringVrBrowser() throws InterruptedExce
562560
mVrBrowserTestFramework.waitOnJavaScriptStep();
563561
// Reload the page and ensure that the permissions are still granted.
564562
mVrBrowserTestFramework.loadUrlAndAwaitInitialization(
565-
server.getURL(VrBrowserTestFramework.getEmbeddedServerPathForHtmlTestFile(
566-
"test_permissions_persist_when_entering_vr_browser")),
563+
mTestRule.getTestServer().getURL(
564+
VrBrowserTestFramework.getEmbeddedServerPathForHtmlTestFile(
565+
"test_permissions_persist_when_entering_vr_browser")),
567566
PAGE_LOAD_TIMEOUT_S);
568567
Assert.assertFalse("Camera permission would trigger prompt after reload",
569568
mVrBrowserTestFramework.permissionRequestWouldTriggerPrompt("camera"));
@@ -573,7 +572,6 @@ public void testPermissionsPersistWhenEnteringVrBrowser() throws InterruptedExce
573572
VrBrowserTransitionUtils.forceEnterVrBrowserOrFail(POLL_TIMEOUT_LONG_MS);
574573
mVrBrowserTestFramework.executeStepAndWait("stepRequestPermission()");
575574
mVrBrowserTestFramework.endTest();
576-
server.stopAndDestroyServer();
577575
}
578576

579577
/**

chrome/android/javatests/src/org/chromium/chrome/browser/vr/WebXrArSessionTest.java

+9-27
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
import static org.chromium.chrome.browser.vr.WebXrArTestFramework.POLL_TIMEOUT_LONG_MS;
99

1010
import android.os.Build;
11-
import android.support.test.InstrumentationRegistry;
1211
import android.support.test.filters.MediumTest;
1312

14-
import org.junit.After;
1513
import org.junit.Assert;
1614
import org.junit.Before;
1715
import org.junit.Rule;
@@ -30,7 +28,6 @@
3028
import org.chromium.chrome.browser.vr.util.XrTestRuleUtils;
3129
import org.chromium.chrome.test.ChromeActivityTestRule;
3230
import org.chromium.chrome.test.ChromeJUnit4RunnerDelegate;
33-
import org.chromium.net.test.EmbeddedTestServer;
3431

3532
import java.util.List;
3633
import java.util.concurrent.Callable;
@@ -52,9 +49,6 @@ public class WebXrArSessionTest {
5249

5350
private ChromeActivityTestRule mTestRule;
5451
private WebXrArTestFramework mWebXrArTestFramework;
55-
private EmbeddedTestServer mServer;
56-
57-
private boolean mShouldCreateServer;
5852

5953
public WebXrArSessionTest(Callable<ChromeActivityTestRule> callable) throws Exception {
6054
mTestRule = callable.call();
@@ -64,21 +58,6 @@ public WebXrArSessionTest(Callable<ChromeActivityTestRule> callable) throws Exce
6458
@Before
6559
public void setUp() throws Exception {
6660
mWebXrArTestFramework = new WebXrArTestFramework(mTestRule);
67-
// WebappActivityTestRule automatically creates a test server, and creating multiple causes
68-
// it to crash hitting a DCHECK. So, only handle the server ourselves if whatever test rule
69-
// we're using doesn't create one itself.
70-
mServer = mTestRule.getTestServer();
71-
if (mServer == null) {
72-
mShouldCreateServer = true;
73-
mServer = EmbeddedTestServer.createAndStartServer(InstrumentationRegistry.getContext());
74-
}
75-
}
76-
77-
@After
78-
public void tearDown() throws Exception {
79-
if (mServer != null && mShouldCreateServer) {
80-
mServer.stopAndDestroyServer();
81-
}
8261
}
8362

8463
/**
@@ -89,8 +68,9 @@ public void tearDown() throws Exception {
8968
@XrActivityRestriction({XrActivityRestriction.SupportedActivity.ALL})
9069
public void testArRequestSessionSucceeds() throws InterruptedException {
9170
mWebXrArTestFramework.loadUrlAndAwaitInitialization(
92-
mServer.getURL(WebXrArTestFramework.getEmbeddedServerPathForHtmlTestFile(
93-
"test_ar_request_session_succeeds")),
71+
mTestRule.getTestServer().getURL(
72+
WebXrArTestFramework.getEmbeddedServerPathForHtmlTestFile(
73+
"test_ar_request_session_succeeds")),
9474
PAGE_LOAD_TIMEOUT_S);
9575
mWebXrArTestFramework.enterSessionWithUserGestureOrFail();
9676
mWebXrArTestFramework.assertNoJavaScriptErrors();
@@ -105,8 +85,9 @@ public void testArRequestSessionSucceeds() throws InterruptedException {
10585
@XrActivityRestriction({XrActivityRestriction.SupportedActivity.ALL})
10686
public void testRepeatedArSessionsSucceed() throws InterruptedException {
10787
mWebXrArTestFramework.loadUrlAndAwaitInitialization(
108-
mServer.getURL(WebXrArTestFramework.getEmbeddedServerPathForHtmlTestFile(
109-
"test_ar_request_session_succeeds")),
88+
mTestRule.getTestServer().getURL(
89+
WebXrArTestFramework.getEmbeddedServerPathForHtmlTestFile(
90+
"test_ar_request_session_succeeds")),
11091
PAGE_LOAD_TIMEOUT_S);
11192
for (int i = 0; i < 2; i++) {
11293
mWebXrArTestFramework.enterSessionWithUserGestureOrFail();
@@ -124,8 +105,9 @@ public void testRepeatedArSessionsSucceed() throws InterruptedException {
124105
@XrActivityRestriction({XrActivityRestriction.SupportedActivity.ALL})
125106
public void testRepeatedArSessionsOnlyPromptPermissionsOnce() throws InterruptedException {
126107
mWebXrArTestFramework.loadUrlAndAwaitInitialization(
127-
mServer.getURL(WebXrArTestFramework.getEmbeddedServerPathForHtmlTestFile(
128-
"test_ar_request_session_succeeds")),
108+
mTestRule.getTestServer().getURL(
109+
WebXrArTestFramework.getEmbeddedServerPathForHtmlTestFile(
110+
"test_ar_request_session_succeeds")),
129111
PAGE_LOAD_TIMEOUT_S);
130112
Assert.assertTrue("First AR session request did not trigger permission prompt",
131113
mWebXrArTestFramework.permissionRequestWouldTriggerPrompt("camera"));

chrome/android/javatests/src/org/chromium/chrome/browser/vr/WebXrVrInputTest.java

+6-26
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import android.graphics.PointF;
1616
import android.os.Build;
1717
import android.os.SystemClock;
18-
import android.support.test.InstrumentationRegistry;
1918
import android.support.test.filters.LargeTest;
2019
import android.support.test.filters.MediumTest;
2120
import android.view.MotionEvent;
@@ -52,7 +51,6 @@
5251
import org.chromium.content_public.browser.ViewEventSink;
5352
import org.chromium.content_public.browser.test.util.CriteriaHelper;
5453
import org.chromium.content_public.browser.test.util.TouchCommon;
55-
import org.chromium.net.test.EmbeddedTestServer;
5654

5755
import java.util.List;
5856
import java.util.concurrent.Callable;
@@ -738,15 +736,10 @@ public void testAppButtonLongPressDisplaysPermissionsIncognito()
738736
private void testAppButtonLongPressDisplaysPermissionsImpl() throws InterruptedException {
739737
// Note that we need to pass in the WebContents to use throughout this because automatically
740738
// using the first tab's WebContents doesn't work in Incognito.
741-
EmbeddedTestServer server = mTestRule.getTestServer();
742-
boolean teardownServer = false;
743-
if (server == null) {
744-
server = EmbeddedTestServer.createAndStartServer(InstrumentationRegistry.getContext());
745-
teardownServer = true;
746-
}
747739
mWebXrVrTestFramework.loadUrlAndAwaitInitialization(
748-
server.getURL(WebXrVrTestFramework.getEmbeddedServerPathForHtmlTestFile(
749-
"generic_webxr_permission_page")),
740+
mTestRule.getTestServer().getURL(
741+
WebXrVrTestFramework.getEmbeddedServerPathForHtmlTestFile(
742+
"generic_webxr_permission_page")),
750743
PAGE_LOAD_TIMEOUT_S);
751744
WebXrVrTestFramework.runJavaScriptOrFail("requestPermission({audio:true})",
752745
POLL_TIMEOUT_SHORT_MS, mTestRule.getWebContents());
@@ -785,9 +778,6 @@ private void testAppButtonLongPressDisplaysPermissionsImpl() throws InterruptedE
785778
UserFriendlyElementName.WEB_XR_AUDIO_INDICATOR, true /* visible */, () -> {});
786779
NativeUiUtils.performActionAndWaitForVisibilityStatus(
787780
UserFriendlyElementName.WEB_XR_AUDIO_INDICATOR, false /* visible */, () -> {});
788-
if (teardownServer) {
789-
server.stopAndDestroyServer();
790-
}
791781
}
792782

793783
/**
@@ -825,16 +815,10 @@ public void testInSessionPermissionRequestsIncognito() throws InterruptedExcepti
825815
private void testInSessionPermissionRequestsImpl() throws InterruptedException {
826816
// Note that we need to pass in the WebContents to use throughout this because automatically
827817
// using the first tab's WebContents doesn't work in Incognito.
828-
EmbeddedTestServer server = mTestRule.getTestServer();
829-
boolean teardownServer = false;
830-
if (server == null) {
831-
server = EmbeddedTestServer.createAndStartServer(InstrumentationRegistry.getContext());
832-
teardownServer = true;
833-
}
834-
835818
mWebXrVrTestFramework.loadUrlAndAwaitInitialization(
836-
server.getURL(WebXrVrTestFramework.getEmbeddedServerPathForHtmlTestFile(
837-
"generic_webxr_permission_page")),
819+
mTestRule.getTestServer().getURL(
820+
WebXrVrTestFramework.getEmbeddedServerPathForHtmlTestFile(
821+
"generic_webxr_permission_page")),
838822
PAGE_LOAD_TIMEOUT_S);
839823
mWebXrVrTestFramework.enterSessionWithUserGestureOrFail(mTestRule.getWebContents());
840824
NativeUiUtils.performActionAndWaitForVisibilityStatus(
@@ -861,9 +845,5 @@ private void testInSessionPermissionRequestsImpl() throws InterruptedException {
861845
.runJavaScriptOrFail("lastPermissionRequestSucceeded",
862846
POLL_TIMEOUT_SHORT_MS, mTestRule.getWebContents())
863847
.equals("true"));
864-
865-
if (teardownServer) {
866-
server.stopAndDestroyServer();
867-
}
868848
}
869849
}

chrome/android/javatests/src/org/chromium/chrome/browser/vr/WebXrVrTabTest.java

+3-15
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import static org.chromium.chrome.test.util.ChromeRestriction.RESTRICTION_TYPE_VIEWER_DAYDREAM_OR_STANDALONE;
1010

1111
import android.os.Build;
12-
import android.support.test.InstrumentationRegistry;
1312
import android.support.test.filters.MediumTest;
1413

1514
import org.junit.Before;
@@ -33,7 +32,6 @@
3332
import org.chromium.chrome.browser.vr.util.VrTestRuleUtils;
3433
import org.chromium.chrome.test.ChromeActivityTestRule;
3534
import org.chromium.chrome.test.ChromeJUnit4RunnerDelegate;
36-
import org.chromium.net.test.EmbeddedTestServer;
3735

3836
import java.util.List;
3937
import java.util.concurrent.Callable;
@@ -128,16 +126,10 @@ public void testPermissionsInOtherTabIncognito() throws InterruptedException {
128126
}
129127

130128
private void testPermissionsInOtherTabImpl(boolean incognito) throws InterruptedException {
131-
EmbeddedTestServer server = mTestRule.getTestServer();
132-
boolean teardownServer = false;
133-
if (server == null) {
134-
server = EmbeddedTestServer.createAndStartServer(InstrumentationRegistry.getContext());
135-
teardownServer = true;
136-
}
137-
138129
mWebXrVrTestFramework.loadUrlAndAwaitInitialization(
139-
server.getURL(WebXrVrTestFramework.getEmbeddedServerPathForHtmlTestFile(
140-
"generic_webxr_permission_page")),
130+
mTestRule.getTestServer().getURL(
131+
WebXrVrTestFramework.getEmbeddedServerPathForHtmlTestFile(
132+
"generic_webxr_permission_page")),
141133
PAGE_LOAD_TIMEOUT_S);
142134
// Be sure to store the stream we're given so that the permission is actually in use, as
143135
// otherwise the toast doesn't show up since another tab isn't actually using the
@@ -165,9 +157,5 @@ private void testPermissionsInOtherTabImpl(boolean incognito) throws Interrupted
165157
mWebXrVrTestFramework.enterSessionWithUserGestureOrFail(mTestRule.getWebContents());
166158
NativeUiUtils.performActionAndWaitForVisibilityStatus(
167159
UserFriendlyElementName.WEB_XR_AUDIO_INDICATOR, true /* visible */, () -> {});
168-
169-
if (teardownServer) {
170-
server.stopAndDestroyServer();
171-
}
172160
}
173161
}

chrome/android/javatests/src/org/chromium/chrome/browser/webapps/WebappActivityTestRule.java

+1-13
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import android.view.ViewGroup;
1414

1515
import org.junit.Assert;
16-
import org.junit.Rule;
1716
import org.junit.runner.Description;
1817
import org.junit.runners.model.Statement;
1918

@@ -27,8 +26,6 @@
2726
import org.chromium.content_public.browser.test.util.CriteriaHelper;
2827
import org.chromium.content_public.browser.test.util.DOMUtils;
2928
import org.chromium.content_public.browser.test.util.JavaScriptUtils;
30-
import org.chromium.net.test.EmbeddedTestServer;
31-
import org.chromium.net.test.EmbeddedTestServerRule;
3229

3330
/**
3431
* Custom {@link ChromeActivityTestRule} for tests using {@link WebappActivity}.
@@ -73,18 +70,10 @@ public class WebappActivityTestRule extends ChromeActivityTestRule<WebappActivit
7370
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
7471
+ "AAAAAAAAAOA3AvAAAdln8YgAAAAASUVORK5CYII=";
7572

76-
@Rule
77-
private EmbeddedTestServerRule mTestServerRule = new EmbeddedTestServerRule();
78-
7973
public WebappActivityTestRule() {
8074
super(WebappActivity0.class);
8175
}
8276

83-
@Override
84-
public EmbeddedTestServer getTestServer() {
85-
return mTestServerRule.getServer();
86-
}
87-
8877
/**
8978
* Creates the Intent that starts the WebAppActivity. This is meant to be overriden by other
9079
* tests in order for them to pass some specific values, but it defaults to a web app that just
@@ -151,8 +140,7 @@ public void run() {
151140
}
152141
};
153142

154-
Statement testServerStatement = mTestServerRule.apply(webappTestRuleStatement, description);
155-
return super.apply(testServerStatement, description);
143+
return super.apply(webappTestRuleStatement, description);
156144
}
157145

158146
/**

0 commit comments

Comments
 (0)