Commit bc516bc5 authored by Clark DuVall's avatar Clark DuVall Committed by Commit Bot

[WebLayer] Clean up EmbeddedTestServer usage in tests

Adds EmbeddedTestServerRule to InstrumentationActivityTestRule so all
tests have access to the test server without needing to start/stop it
themselves. This is similar to what ChromeActivityTestRule does.

Change-Id: I17820927c9bd9d8ac2d36fecb794f611de697975
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1904689
Commit-Queue: Clark DuVall <cduvall@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Cr-Commit-Position: refs/heads/master@{#713698}
parent 550642a7
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
package org.chromium.weblayer.test; package org.chromium.weblayer.test;
import android.net.Uri; import android.net.Uri;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest; import android.support.test.filters.SmallTest;
import android.util.Pair; import android.util.Pair;
...@@ -19,7 +18,6 @@ import org.chromium.base.test.BaseJUnit4ClassRunner; ...@@ -19,7 +18,6 @@ import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.content_public.browser.test.util.Criteria; 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.CriteriaHelper;
import org.chromium.content_public.browser.test.util.TestThreadUtils; import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.net.test.EmbeddedTestServer;
import org.chromium.net.test.util.TestWebServer; import org.chromium.net.test.util.TestWebServer;
import org.chromium.weblayer.DownloadCallback; import org.chromium.weblayer.DownloadCallback;
import org.chromium.weblayer.shell.InstrumentationActivity; import org.chromium.weblayer.shell.InstrumentationActivity;
...@@ -117,20 +115,11 @@ public class DownloadCallbackTest { ...@@ -117,20 +115,11 @@ public class DownloadCallbackTest {
@Test @Test
@SmallTest @SmallTest
public void testDownloadByLinkAttribute() { public void testDownloadByLinkAttribute() {
EmbeddedTestServer testServer = new EmbeddedTestServer(); String pageUrl = mActivityTestRule.getTestDataURL("download.html");
testServer.initializeNative(InstrumentationRegistry.getInstrumentation().getContext(),
EmbeddedTestServer.ServerHTTPSSetting.USE_HTTP);
testServer.addDefaultHandlers("weblayer/test/data");
Assert.assertTrue(testServer.start(0));
String pageUrl = testServer.getURL("/download.html");
mActivityTestRule.navigateAndWait(pageUrl); mActivityTestRule.navigateAndWait(pageUrl);
EventUtils.simulateTouchCenterOfView(mActivity.getWindow().getDecorView()); EventUtils.simulateTouchCenterOfView(mActivity.getWindow().getDecorView());
mCallback.waitForDownload(); mCallback.waitForDownload();
Assert.assertEquals(testServer.getURL("/lorem_ipsum.txt"), mCallback.mUrl); Assert.assertEquals(mActivityTestRule.getTestDataURL("lorem_ipsum.txt"), mCallback.mUrl);
testServer.stopAndDestroyServer();
} }
} }
...@@ -4,10 +4,8 @@ ...@@ -4,10 +4,8 @@
package org.chromium.weblayer.test; package org.chromium.weblayer.test;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest; import android.support.test.filters.SmallTest;
import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
...@@ -18,7 +16,6 @@ import org.chromium.base.test.BaseJUnit4ClassRunner; ...@@ -18,7 +16,6 @@ import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.content_public.browser.test.util.Criteria; 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.CriteriaHelper;
import org.chromium.content_public.browser.test.util.TestThreadUtils; import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.net.test.EmbeddedTestServer;
import org.chromium.weblayer.FullscreenCallback; import org.chromium.weblayer.FullscreenCallback;
import org.chromium.weblayer.shell.InstrumentationActivity; import org.chromium.weblayer.shell.InstrumentationActivity;
...@@ -31,7 +28,6 @@ public class FullscreenCallbackTest { ...@@ -31,7 +28,6 @@ public class FullscreenCallbackTest {
public InstrumentationActivityTestRule mActivityTestRule = public InstrumentationActivityTestRule mActivityTestRule =
new InstrumentationActivityTestRule(); new InstrumentationActivityTestRule();
private EmbeddedTestServer mTestServer;
private InstrumentationActivity mActivity; private InstrumentationActivity mActivity;
private Delegate mDelegate; private Delegate mDelegate;
...@@ -72,13 +68,7 @@ public class FullscreenCallbackTest { ...@@ -72,13 +68,7 @@ public class FullscreenCallbackTest {
@Before @Before
public void setUp() { public void setUp() {
mTestServer = new EmbeddedTestServer(); String url = mActivityTestRule.getTestDataURL("fullscreen.html");
mTestServer.initializeNative(InstrumentationRegistry.getInstrumentation().getContext(),
EmbeddedTestServer.ServerHTTPSSetting.USE_HTTP);
mTestServer.addDefaultHandlers("weblayer/test/data");
Assert.assertTrue(mTestServer.start(0));
String url = mTestServer.getURL("/fullscreen.html");
mActivity = mActivityTestRule.launchShellWithUrl(url); mActivity = mActivityTestRule.launchShellWithUrl(url);
Assert.assertNotNull(mActivity); Assert.assertNotNull(mActivity);
mDelegate = new Delegate(); mDelegate = new Delegate();
...@@ -91,11 +81,6 @@ public class FullscreenCallbackTest { ...@@ -91,11 +81,6 @@ public class FullscreenCallbackTest {
Assert.assertEquals(1, mDelegate.mEnterFullscreenCount); Assert.assertEquals(1, mDelegate.mEnterFullscreenCount);
} }
@After
public void tearDown() {
mTestServer.stopAndDestroyServer();
}
@Test @Test
@SmallTest @SmallTest
public void testFullscreen() { public void testFullscreen() {
......
...@@ -16,7 +16,6 @@ import android.os.Bundle; ...@@ -16,7 +16,6 @@ import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Parcelable; import android.os.Parcelable;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest; import android.support.test.filters.SmallTest;
import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
...@@ -34,7 +33,6 @@ import org.chromium.base.test.util.InMemorySharedPreferencesContext; ...@@ -34,7 +33,6 @@ import org.chromium.base.test.util.InMemorySharedPreferencesContext;
import org.chromium.base.test.util.MinAndroidSdkLevel; import org.chromium.base.test.util.MinAndroidSdkLevel;
import org.chromium.content_public.browser.test.util.CriteriaHelper; import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.content_public.browser.test.util.TestThreadUtils; import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.net.test.EmbeddedTestServer;
import org.chromium.weblayer.shell.InstrumentationActivity; import org.chromium.weblayer.shell.InstrumentationActivity;
import java.io.File; import java.io.File;
...@@ -49,7 +47,6 @@ public class InputTypesTest { ...@@ -49,7 +47,6 @@ public class InputTypesTest {
public InstrumentationActivityTestRule mActivityTestRule = public InstrumentationActivityTestRule mActivityTestRule =
new InstrumentationActivityTestRule(); new InstrumentationActivityTestRule();
private EmbeddedTestServer mTestServer;
private File mTempFile; private File mTempFile;
private int mCameraPermission = PackageManager.PERMISSION_GRANTED; private int mCameraPermission = PackageManager.PERMISSION_GRANTED;
...@@ -127,12 +124,6 @@ public class InputTypesTest { ...@@ -127,12 +124,6 @@ public class InputTypesTest {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
mTestServer = new EmbeddedTestServer();
mTestServer.initializeNative(InstrumentationRegistry.getInstrumentation().getContext(),
EmbeddedTestServer.ServerHTTPSSetting.USE_HTTP);
mTestServer.addDefaultHandlers("weblayer/test/data");
Assert.assertTrue(mTestServer.start(0));
InstrumentationActivity activity = mActivityTestRule.launchShell(new Bundle()); InstrumentationActivity activity = mActivityTestRule.launchShell(new Bundle());
TestThreadUtils.runOnUiThreadBlocking(() -> { TestThreadUtils.runOnUiThreadBlocking(() -> {
activity.createWebLayer( activity.createWebLayer(
...@@ -148,7 +139,7 @@ public class InputTypesTest { ...@@ -148,7 +139,7 @@ public class InputTypesTest {
null) null)
.get(); .get();
}); });
mActivityTestRule.navigateAndWait(mTestServer.getURL("/input_types.html")); mActivityTestRule.navigateAndWait(mActivityTestRule.getTestDataURL("input_types.html"));
mTempFile = File.createTempFile("file", null); mTempFile = File.createTempFile("file", null);
activity.setIntentInterceptor(mIntentInterceptor); activity.setIntentInterceptor(mIntentInterceptor);
ActivityCompat.setPermissionCompatDelegate(mPermissionCompatDelegate); ActivityCompat.setPermissionCompatDelegate(mPermissionCompatDelegate);
......
...@@ -16,10 +16,15 @@ import android.support.test.rule.ActivityTestRule; ...@@ -16,10 +16,15 @@ import android.support.test.rule.ActivityTestRule;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Rule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.chromium.base.test.util.CallbackHelper; import org.chromium.base.test.util.CallbackHelper;
import org.chromium.content_public.browser.test.util.CriteriaHelper; import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.content_public.browser.test.util.TestThreadUtils; import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.net.test.EmbeddedTestServer;
import org.chromium.net.test.EmbeddedTestServerRule;
import org.chromium.weblayer.Navigation; import org.chromium.weblayer.Navigation;
import org.chromium.weblayer.NavigationCallback; import org.chromium.weblayer.NavigationCallback;
import org.chromium.weblayer.Tab; import org.chromium.weblayer.Tab;
...@@ -36,6 +41,9 @@ import java.util.concurrent.TimeoutException; ...@@ -36,6 +41,9 @@ import java.util.concurrent.TimeoutException;
* Test can use this ActivityTestRule to launch or get InstrumentationActivity. * Test can use this ActivityTestRule to launch or get InstrumentationActivity.
*/ */
public class InstrumentationActivityTestRule extends ActivityTestRule<InstrumentationActivity> { public class InstrumentationActivityTestRule extends ActivityTestRule<InstrumentationActivity> {
@Rule
private EmbeddedTestServerRule mTestServerRule = new EmbeddedTestServerRule();
private static final class NavigationWaiter { private static final class NavigationWaiter {
private String mUrl; private String mUrl;
private Tab mTab; private Tab mTab;
...@@ -114,6 +122,11 @@ public class InstrumentationActivityTestRule extends ActivityTestRule<Instrument ...@@ -114,6 +122,11 @@ public class InstrumentationActivityTestRule extends ActivityTestRule<Instrument
super(InstrumentationActivity.class, false, false); super(InstrumentationActivity.class, false, false);
} }
@Override
public Statement apply(final Statement base, Description description) {
return super.apply(mTestServerRule.apply(base, description), description);
}
public WebLayer getWebLayer() { public WebLayer getWebLayer() {
return TestThreadUtils.runOnUiThreadBlockingNoException(() -> { return TestThreadUtils.runOnUiThreadBlockingNoException(() -> {
return WebLayer return WebLayer
...@@ -249,4 +262,12 @@ public class InstrumentationActivityTestRule extends ActivityTestRule<Instrument ...@@ -249,4 +262,12 @@ public class InstrumentationActivityTestRule extends ActivityTestRule<Instrument
String url = "data:text,foo"; String url = "data:text,foo";
return launchShellWithUrl(url, extras); return launchShellWithUrl(url, extras);
} }
public EmbeddedTestServer getTestServer() {
return mTestServerRule.getServer();
}
public String getTestDataURL(String path) {
return getTestServer().getURL("/weblayer/test/data/" + path);
}
} }
...@@ -4,19 +4,15 @@ ...@@ -4,19 +4,15 @@
package org.chromium.weblayer.test; package org.chromium.weblayer.test;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest; import android.support.test.filters.SmallTest;
import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.chromium.base.test.BaseJUnit4ClassRunner; import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.content_public.browser.test.util.TestThreadUtils; import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.net.test.EmbeddedTestServer;
import org.chromium.weblayer.Tab; import org.chromium.weblayer.Tab;
import org.chromium.weblayer.shell.InstrumentationActivity; import org.chromium.weblayer.shell.InstrumentationActivity;
...@@ -29,27 +25,12 @@ public class NewTabCallbackTest { ...@@ -29,27 +25,12 @@ public class NewTabCallbackTest {
public InstrumentationActivityTestRule mActivityTestRule = public InstrumentationActivityTestRule mActivityTestRule =
new InstrumentationActivityTestRule(); new InstrumentationActivityTestRule();
private EmbeddedTestServer mTestServer;
private InstrumentationActivity mActivity; private InstrumentationActivity mActivity;
@Before
public void setUp() {
mTestServer = new EmbeddedTestServer();
mTestServer.initializeNative(InstrumentationRegistry.getInstrumentation().getContext(),
EmbeddedTestServer.ServerHTTPSSetting.USE_HTTP);
mTestServer.addDefaultHandlers("weblayer/test/data");
Assert.assertTrue(mTestServer.start(0));
}
@After
public void tearDown() {
mTestServer.stopAndDestroyServer();
}
@Test @Test
@SmallTest @SmallTest
public void testNewBrowser() { public void testNewBrowser() {
String url = mTestServer.getURL("/new_browser.html"); String url = mActivityTestRule.getTestDataURL("new_browser.html");
mActivity = mActivityTestRule.launchShellWithUrl(url); mActivity = mActivityTestRule.launchShellWithUrl(url);
Assert.assertNotNull(mActivity); Assert.assertNotNull(mActivity);
NewTabCallbackImpl callback = new NewTabCallbackImpl(); NewTabCallbackImpl callback = new NewTabCallbackImpl();
......
...@@ -5,10 +5,8 @@ ...@@ -5,10 +5,8 @@
package org.chromium.weblayer.test; package org.chromium.weblayer.test;
import android.os.Bundle; import android.os.Bundle;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest; import android.support.test.filters.SmallTest;
import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
...@@ -17,7 +15,6 @@ import org.junit.runner.RunWith; ...@@ -17,7 +15,6 @@ import org.junit.runner.RunWith;
import org.chromium.base.test.BaseJUnit4ClassRunner; import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.content_public.browser.test.util.TestThreadUtils; import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.net.test.EmbeddedTestServer;
import org.chromium.weblayer.Browser; import org.chromium.weblayer.Browser;
import org.chromium.weblayer.Tab; import org.chromium.weblayer.Tab;
import org.chromium.weblayer.TabListCallback; import org.chromium.weblayer.TabListCallback;
...@@ -36,7 +33,6 @@ public class TabListCallbackTest { ...@@ -36,7 +33,6 @@ public class TabListCallbackTest {
public InstrumentationActivityTestRule mActivityTestRule = public InstrumentationActivityTestRule mActivityTestRule =
new InstrumentationActivityTestRule(); new InstrumentationActivityTestRule();
private EmbeddedTestServer mTestServer;
private InstrumentationActivity mActivity; private InstrumentationActivity mActivity;
private Tab mFirstTab; private Tab mFirstTab;
private Tab mSecondTab; private Tab mSecondTab;
...@@ -75,13 +71,7 @@ public class TabListCallbackTest { ...@@ -75,13 +71,7 @@ public class TabListCallbackTest {
@Before @Before
public void setUp() { public void setUp() {
mTestServer = new EmbeddedTestServer(); String url = mActivityTestRule.getTestDataURL("new_browser.html");
mTestServer.initializeNative(InstrumentationRegistry.getInstrumentation().getContext(),
EmbeddedTestServer.ServerHTTPSSetting.USE_HTTP);
mTestServer.addDefaultHandlers("weblayer/test/data");
Assert.assertTrue(mTestServer.start(0));
String url = mTestServer.getURL("/new_browser.html");
mActivity = mActivityTestRule.launchShellWithUrl(url); mActivity = mActivityTestRule.launchShellWithUrl(url);
Assert.assertNotNull(mActivity); Assert.assertNotNull(mActivity);
NewTabCallbackImpl callback = new NewTabCallbackImpl(); NewTabCallbackImpl callback = new NewTabCallbackImpl();
...@@ -100,11 +90,6 @@ public class TabListCallbackTest { ...@@ -100,11 +90,6 @@ public class TabListCallbackTest {
}); });
} }
@After
public void tearDown() {
mTestServer.stopAndDestroyServer();
}
@Test @Test
@SmallTest @SmallTest
public void testActiveTabChanged() { public void testActiveTabChanged() {
......
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