Commit fdb2d73d authored by Bo Liu's avatar Bo Liu Committed by Commit Bot

weblayer: Add InstrumentationActivity

WebLayerShellActivity currently serves two purposes.
* As a demo app for weblayer where user can type in a url and expect it
  behave somewhat like a browser.
* As the target activity for instrumentation tests.

Keeping both purposes in the same activity is a maintenance burden and
makes the more complicated than it needs to be. This CL creates a new
InstrumentationActivity (copied from WebLayerShellActivity) and removes
code from both Activity that's needed for their purpose.

Also rename WebLayerShellActivityTestRule to
InstrumentationActivityTestRule.

Change-Id: I306fecb57ed15ee7a59487ced40262094ef7c438
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1888451
Commit-Queue: Scott Violet <sky@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Cr-Commit-Position: refs/heads/master@{#710848}
parent 627c85bd
......@@ -28,8 +28,10 @@ android_library("weblayer_shell_java") {
"//third_party/android_deps:android_support_v4_java",
"//weblayer/public/java",
]
java_files =
[ "shell_apk/src/org/chromium/weblayer/shell/WebLayerShellActivity.java" ]
java_files = [
"shell_apk/src/org/chromium/weblayer/shell/InstrumentationActivity.java",
"shell_apk/src/org/chromium/weblayer/shell/WebLayerShellActivity.java",
]
}
template("weblayer_shell_apk_helper") {
......@@ -210,20 +212,19 @@ instrumentation_test_apk("weblayer_instrumentation_test_apk") {
]
java_files = [
"javatests/src/org/chromium/weblayer/test/BrowserObserverTest.java",
"javatests/src/org/chromium/weblayer/test/DataClearingTest.java",
"javatests/src/org/chromium/weblayer/test/DownloadDelegateTest.java",
"javatests/src/org/chromium/weblayer/test/EventUtils.java",
"javatests/src/org/chromium/weblayer/test/FullscreenDelegateTest.java",
"javatests/src/org/chromium/weblayer/test/NavigationTest.java",
"javatests/src/org/chromium/weblayer/test/DataClearingTest.java",
"javatests/src/org/chromium/weblayer/test/SmokeTest.java",
"javatests/src/org/chromium/weblayer/test/EventUtils.java",
"javatests/src/org/chromium/weblayer/test/ExecuteScriptTest.java",
"javatests/src/org/chromium/weblayer/test/RenderingTest.java",
"javatests/src/org/chromium/weblayer/test/TopControlsTest.java",
"javatests/src/org/chromium/weblayer/test/WebLayerShellActivityTestRule.java",
"javatests/src/org/chromium/weblayer/test/FragmentRestoreTest.java",
"javatests/src/org/chromium/weblayer/test/FullscreenDelegateTest.java",
"javatests/src/org/chromium/weblayer/test/InputTypesTest.java",
"shell_apk/src/org/chromium/weblayer/shell/WebLayerShellActivity.java",
"javatests/src/org/chromium/weblayer/test/InstrumentationActivityTestRule.java",
"javatests/src/org/chromium/weblayer/test/NavigationTest.java",
"javatests/src/org/chromium/weblayer/test/RenderingTest.java",
"javatests/src/org/chromium/weblayer/test/SmokeTest.java",
"javatests/src/org/chromium/weblayer/test/TopControlsTest.java",
]
additional_apks = [
"//weblayer/shell/android:weblayer_support_apk",
......
......@@ -16,7 +16,7 @@
<uses-library android:name="android.test.runner" />
<activity android:name="org.chromium.test.broker.OnDeviceInstrumentationBroker"
android:exported="true"/>
<activity android:name="org.chromium.weblayer.shell.WebLayerShellActivity">
<activity android:name="org.chromium.weblayer.shell.InstrumentationActivity">
<!-- Add these intent filters so tests can resolve these intents. -->
<intent-filter>
<action android:name="android.provider.MediaStore.RECORD_SOUND" />
......
......@@ -16,7 +16,7 @@ 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.TestThreadUtils;
import org.chromium.weblayer.BrowserObserver;
import org.chromium.weblayer.shell.WebLayerShellActivity;
import org.chromium.weblayer.shell.InstrumentationActivity;
import java.util.ArrayList;
import java.util.Collections;
......@@ -28,7 +28,8 @@ import java.util.List;
@RunWith(BaseJUnit4ClassRunner.class)
public class BrowserObserverTest {
@Rule
public WebLayerShellActivityTestRule mActivityTestRule = new WebLayerShellActivityTestRule();
public InstrumentationActivityTestRule mActivityTestRule =
new InstrumentationActivityTestRule();
private static class Observer extends BrowserObserver {
public static class BrowserObserverValueRecorder {
......@@ -69,7 +70,7 @@ public class BrowserObserverTest {
@SmallTest
public void testLoadEvents() {
String startupUrl = "about:blank";
WebLayerShellActivity activity = mActivityTestRule.launchShellWithUrl(startupUrl);
InstrumentationActivity activity = mActivityTestRule.launchShellWithUrl(startupUrl);
Observer observer = new Observer();
TestThreadUtils.runOnUiThreadBlocking(
......
......@@ -20,7 +20,7 @@ import org.junit.runner.RunWith;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.weblayer.Profile;
import org.chromium.weblayer.shell.WebLayerShellActivity;
import org.chromium.weblayer.shell.InstrumentationActivity;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
......@@ -31,7 +31,8 @@ import java.util.concurrent.TimeUnit;
@RunWith(BaseJUnit4ClassRunner.class)
public class DataClearingTest {
@Rule
public WebLayerShellActivityTestRule mActivityTestRule = new WebLayerShellActivityTestRule();
public InstrumentationActivityTestRule mActivityTestRule =
new InstrumentationActivityTestRule();
@Test
@SmallTest
......@@ -68,7 +69,7 @@ public class DataClearingTest {
@Test
@SmallTest
public void twoSuccesiveRequestsTriggerCallbacks() throws InterruptedException {
WebLayerShellActivity activity = launchWithProfile("profile");
InstrumentationActivity activity = launchWithProfile("profile");
CountDownLatch latch = new CountDownLatch(2);
runOnUiThreadBlocking(() -> {
......@@ -84,7 +85,7 @@ public class DataClearingTest {
@Test
@SmallTest
public void clearingAgainAfterClearFinished_TriggersCallback() throws InterruptedException {
WebLayerShellActivity activity = launchWithProfile("profile");
InstrumentationActivity activity = launchWithProfile("profile");
CountDownLatch latch = new CountDownLatch(1);
runOnUiThreadBlocking(() -> {
......@@ -100,7 +101,7 @@ public class DataClearingTest {
@Test
@SmallTest
public void destroyingProfileDuringDataClear_DoesntCrash() throws InterruptedException {
WebLayerShellActivity activity = launchWithProfile("profile");
InstrumentationActivity activity = launchWithProfile("profile");
CountDownLatch latch = new CountDownLatch(1);
runOnUiThreadBlocking(() -> {
......@@ -119,19 +120,18 @@ public class DataClearingTest {
private void checkTriggersCallbackOnClearData(int[] dataTypes, String profileName)
throws InterruptedException {
WebLayerShellActivity activity = launchWithProfile(profileName);
InstrumentationActivity activity = launchWithProfile(profileName);
CountDownLatch latch = new CountDownLatch(1);
runOnUiThreadBlocking(() -> activity.getBrowserFragmentController().getProfile()
.clearBrowsingData(dataTypes).addCallback((ignored) -> latch.countDown()));
assertTrue(latch.await(3, TimeUnit.SECONDS));
}
private WebLayerShellActivity launchWithProfile(String profileName) {
private InstrumentationActivity launchWithProfile(String profileName) {
Bundle extras = new Bundle();
extras.putString(WebLayerShellActivity.EXTRA_PROFILE_NAME, profileName);
extras.putString(InstrumentationActivity.EXTRA_PROFILE_NAME, profileName);
String url = "data:text,foo";
WebLayerShellActivity activity = mActivityTestRule.launchShellWithUrl(url, extras);
InstrumentationActivity activity = mActivityTestRule.launchShellWithUrl(url, extras);
return activity;
}
}
......@@ -22,7 +22,7 @@ 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.weblayer.DownloadDelegate;
import org.chromium.weblayer.shell.WebLayerShellActivity;
import org.chromium.weblayer.shell.InstrumentationActivity;
import java.util.ArrayList;
import java.util.List;
......@@ -33,9 +33,10 @@ import java.util.List;
@RunWith(BaseJUnit4ClassRunner.class)
public class DownloadDelegateTest {
@Rule
public WebLayerShellActivityTestRule mActivityTestRule = new WebLayerShellActivityTestRule();
public InstrumentationActivityTestRule mActivityTestRule =
new InstrumentationActivityTestRule();
private WebLayerShellActivity mActivity;
private InstrumentationActivity mActivity;
private Delegate mDelegate;
private static class Delegate extends DownloadDelegate {
......
......@@ -16,7 +16,7 @@ import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.UrlUtils;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.weblayer.BrowserController;
import org.chromium.weblayer.shell.WebLayerShellActivity;
import org.chromium.weblayer.shell.InstrumentationActivity;
/**
* Tests that script execution works as expected.
......@@ -24,7 +24,8 @@ import org.chromium.weblayer.shell.WebLayerShellActivity;
@RunWith(BaseJUnit4ClassRunner.class)
public class ExecuteScriptTest {
@Rule
public WebLayerShellActivityTestRule mActivityTestRule = new WebLayerShellActivityTestRule();
public InstrumentationActivityTestRule mActivityTestRule =
new InstrumentationActivityTestRule();
private static final String DATA_URL = UrlUtils.encodeHtmlDataUri(
"<html><head><script>var bar = 10;</script></head><body>foo</body></html>");
......@@ -32,7 +33,7 @@ public class ExecuteScriptTest {
@Test
@SmallTest
public void testBasicScript() throws Exception {
WebLayerShellActivity activity = mActivityTestRule.launchShellWithUrl(DATA_URL);
InstrumentationActivity activity = mActivityTestRule.launchShellWithUrl(DATA_URL);
JSONObject result = mActivityTestRule.executeScriptSync("document.body.innerHTML");
Assert.assertEquals(result.getString(BrowserController.SCRIPT_RESULT_KEY), "foo");
}
......@@ -40,7 +41,7 @@ public class ExecuteScriptTest {
@Test
@SmallTest
public void testScriptIsolatedFromPage() throws Exception {
WebLayerShellActivity activity = mActivityTestRule.launchShellWithUrl(DATA_URL);
InstrumentationActivity activity = mActivityTestRule.launchShellWithUrl(DATA_URL);
JSONObject result = mActivityTestRule.executeScriptSync("bar");
Assert.assertTrue(result.isNull(BrowserController.SCRIPT_RESULT_KEY));
}
......@@ -48,7 +49,7 @@ public class ExecuteScriptTest {
@Test
@SmallTest
public void testScriptNotIsolatedFromOtherScript() throws Exception {
WebLayerShellActivity activity = mActivityTestRule.launchShellWithUrl(DATA_URL);
InstrumentationActivity activity = mActivityTestRule.launchShellWithUrl(DATA_URL);
mActivityTestRule.executeScriptSync("var foo = 20;");
JSONObject result = mActivityTestRule.executeScriptSync("foo");
Assert.assertEquals(result.getInt(BrowserController.SCRIPT_RESULT_KEY), 20);
......@@ -57,7 +58,7 @@ public class ExecuteScriptTest {
@Test
@SmallTest
public void testClearedOnNavigate() throws Exception {
WebLayerShellActivity activity = mActivityTestRule.launchShellWithUrl(DATA_URL);
InstrumentationActivity activity = mActivityTestRule.launchShellWithUrl(DATA_URL);
mActivityTestRule.executeScriptSync("var foo = 20;");
String newUrl = UrlUtils.encodeHtmlDataUri("<html></html>");
......@@ -69,7 +70,7 @@ public class ExecuteScriptTest {
@Test
@SmallTest
public void testNullCallback() throws Exception {
WebLayerShellActivity activity = mActivityTestRule.launchShellWithUrl(DATA_URL);
InstrumentationActivity activity = mActivityTestRule.launchShellWithUrl(DATA_URL);
TestThreadUtils.runOnUiThreadBlocking(() -> {
// Null callback should not crash.
activity.getBrowserController().executeScript("null", null);
......
......@@ -15,7 +15,8 @@ import org.chromium.base.test.BaseJUnit4ClassRunner;
@RunWith(BaseJUnit4ClassRunner.class)
public class FragmentRestoreTest {
@Rule
public WebLayerShellActivityTestRule mActivityTestRule = new WebLayerShellActivityTestRule();
public InstrumentationActivityTestRule mActivityTestRule =
new InstrumentationActivityTestRule();
@Test
@SmallTest
......
......@@ -20,7 +20,7 @@ import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.net.test.EmbeddedTestServer;
import org.chromium.weblayer.FullscreenDelegate;
import org.chromium.weblayer.shell.WebLayerShellActivity;
import org.chromium.weblayer.shell.InstrumentationActivity;
/**
* Tests that FullscreenDelegate methods are invoked as expected.
......@@ -28,10 +28,11 @@ import org.chromium.weblayer.shell.WebLayerShellActivity;
@RunWith(BaseJUnit4ClassRunner.class)
public class FullscreenDelegateTest {
@Rule
public WebLayerShellActivityTestRule mActivityTestRule = new WebLayerShellActivityTestRule();
public InstrumentationActivityTestRule mActivityTestRule =
new InstrumentationActivityTestRule();
private EmbeddedTestServer mTestServer;
private WebLayerShellActivity mActivity;
private InstrumentationActivity mActivity;
private Delegate mDelegate;
private static class Delegate extends FullscreenDelegate {
......
......@@ -26,7 +26,7 @@ import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.net.test.EmbeddedTestServer;
import org.chromium.weblayer.shell.WebLayerShellActivity;
import org.chromium.weblayer.shell.InstrumentationActivity;
import java.io.File;
......@@ -36,12 +36,13 @@ import java.io.File;
@RunWith(BaseJUnit4ClassRunner.class)
public class InputTypesTest {
@Rule
public WebLayerShellActivityTestRule mActivityTestRule = new WebLayerShellActivityTestRule();
public InstrumentationActivityTestRule mActivityTestRule =
new InstrumentationActivityTestRule();
private EmbeddedTestServer mTestServer;
private File mTempFile;
private class FileIntentInterceptor implements WebLayerShellActivity.IntentInterceptor {
private class FileIntentInterceptor implements InstrumentationActivity.IntentInterceptor {
public Intent mLastIntent;
private Intent mResponseIntent;
......@@ -82,7 +83,7 @@ public class InputTypesTest {
mTestServer.addDefaultHandlers("weblayer/test/data");
Assert.assertTrue(mTestServer.start(0));
WebLayerShellActivity activity =
InstrumentationActivity activity =
mActivityTestRule.launchShellWithUrl(mTestServer.getURL("/input_types.html"));
mTempFile = File.createTempFile("file", null);
activity.setIntentInterceptor(mIntentInterceptor);
......
......@@ -23,17 +23,17 @@ import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.weblayer.BrowserController;
import org.chromium.weblayer.Navigation;
import org.chromium.weblayer.NavigationObserver;
import org.chromium.weblayer.shell.WebLayerShellActivity;
import org.chromium.weblayer.shell.InstrumentationActivity;
import java.lang.reflect.Field;
import java.util.concurrent.TimeoutException;
/**
* ActivityTestRule for WebLayerShellActivity.
* ActivityTestRule for InstrumentationActivity.
*
* Test can use this ActivityTestRule to launch or get WebLayerShellActivity.
* Test can use this ActivityTestRule to launch or get InstrumentationActivity.
*/
public class WebLayerShellActivityTestRule extends ActivityTestRule<WebLayerShellActivity> {
public class InstrumentationActivityTestRule extends ActivityTestRule<InstrumentationActivity> {
private static final class NavigationWaiter {
private String mUrl;
private BrowserController mController;
......@@ -97,25 +97,23 @@ public class WebLayerShellActivityTestRule extends ActivityTestRule<WebLayerShel
}
}
public WebLayerShellActivityTestRule() {
super(WebLayerShellActivity.class, false, false);
public InstrumentationActivityTestRule() {
super(InstrumentationActivity.class, false, false);
}
/**
* Starts the WebLayer activity with the given extras Bundle and completely loads the given URL
* (this calls navigateAndWait()).
*/
public WebLayerShellActivity launchShellWithUrl(String url, Bundle extras) {
public InstrumentationActivity launchShellWithUrl(String url, Bundle extras) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.putExtras(extras);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Prevent URL from being loaded on start.
intent.putExtra(WebLayerShellActivity.EXTRA_NO_LOAD, true);
intent.setComponent(
new ComponentName(InstrumentationRegistry.getInstrumentation().getTargetContext(),
WebLayerShellActivity.class));
WebLayerShellActivity activity = launchActivity(intent);
InstrumentationActivity.class));
InstrumentationActivity activity = launchActivity(intent);
Assert.assertNotNull(activity);
if (url != null) navigateAndWait(url);
return activity;
......@@ -125,7 +123,7 @@ public class WebLayerShellActivityTestRule extends ActivityTestRule<WebLayerShel
* Starts the WebLayer activity and completely loads the given URL (this calls
* navigateAndWait()).
*/
public WebLayerShellActivity launchShellWithUrl(String url) {
public InstrumentationActivity launchShellWithUrl(String url) {
return launchShellWithUrl(url, new Bundle());
}
......@@ -144,15 +142,14 @@ public class WebLayerShellActivityTestRule extends ActivityTestRule<WebLayerShel
public void recreateActivity() {
Activity activity = getActivity();
ActivityMonitor monitor = new ActivityMonitor(WebLayerShellActivity.class.getName(),
null, false);
ActivityMonitor monitor =
new ActivityMonitor(InstrumentationActivity.class.getName(), null, false);
InstrumentationRegistry.getInstrumentation().addMonitor(monitor);
TestThreadUtils.runOnUiThreadBlocking(activity::recreate);
CriteriaHelper.pollUiThread(() ->
monitor.getLastActivity() != null && monitor.getLastActivity() != activity
);
CriteriaHelper.pollUiThread(
() -> monitor.getLastActivity() != null && monitor.getLastActivity() != activity);
InstrumentationRegistry.getInstrumentation().removeMonitor(monitor);
// There is no way to rotate the activity using ActivityTestRule or even notify it.
......
......@@ -25,7 +25,7 @@ import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.weblayer.Navigation;
import org.chromium.weblayer.NavigationController;
import org.chromium.weblayer.NavigationObserver;
import org.chromium.weblayer.shell.WebLayerShellActivity;
import org.chromium.weblayer.shell.InstrumentationActivity;
import java.util.ArrayList;
import java.util.Collections;
......@@ -38,7 +38,8 @@ import java.util.concurrent.TimeoutException;
@RunWith(BaseJUnit4ClassRunner.class)
public class NavigationTest {
@Rule
public WebLayerShellActivityTestRule mActivityTestRule = new WebLayerShellActivityTestRule();
public InstrumentationActivityTestRule mActivityTestRule =
new InstrumentationActivityTestRule();
// URLs used for base tests.
private static final String URL1 = "data:text,foo";
......@@ -146,7 +147,7 @@ public class NavigationTest {
@Test
@SmallTest
public void testNavigationEvents() throws Exception {
WebLayerShellActivity activity = mActivityTestRule.launchShellWithUrl(URL1);
InstrumentationActivity activity = mActivityTestRule.launchShellWithUrl(URL1);
setNavigationObserver(activity);
int curStartedCount = mObserver.onStartedCallback.getCallCount();
......@@ -166,7 +167,7 @@ public class NavigationTest {
@Test
@SmallTest
public void testLoadStateUpdates() throws Exception {
WebLayerShellActivity activity = mActivityTestRule.launchShellWithUrl(null);
InstrumentationActivity activity = mActivityTestRule.launchShellWithUrl(null);
setNavigationObserver(activity);
mActivityTestRule.navigateAndWait(URL1);
......@@ -199,7 +200,7 @@ public class NavigationTest {
@Test
@SmallTest
public void testGoBackAndForward() throws Exception {
WebLayerShellActivity activity = mActivityTestRule.launchShellWithUrl(URL1);
InstrumentationActivity activity = mActivityTestRule.launchShellWithUrl(URL1);
setNavigationObserver(activity);
mActivityTestRule.navigateAndWait(URL2);
......@@ -237,7 +238,7 @@ public class NavigationTest {
@Test
@SmallTest
public void testSameDocument() throws Exception {
WebLayerShellActivity activity = mActivityTestRule.launchShellWithUrl(URL1);
InstrumentationActivity activity = mActivityTestRule.launchShellWithUrl(URL1);
setNavigationObserver(activity);
int curCompletedCount = mObserver.onCompletedCallback.getCallCount();
......@@ -248,7 +249,7 @@ public class NavigationTest {
curCompletedCount, "data:text,foo#bar", true);
}
private void setNavigationObserver(WebLayerShellActivity activity) {
private void setNavigationObserver(InstrumentationActivity activity) {
runOnUiThreadBlocking(() ->
activity.getBrowserController().getNavigationController().addObserver(mObserver)
);
......
......@@ -13,19 +13,20 @@ import org.junit.runner.RunWith;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.weblayer.shell.WebLayerShellActivity;
import org.chromium.weblayer.shell.InstrumentationActivity;
import java.util.concurrent.CountDownLatch;
@RunWith(BaseJUnit4ClassRunner.class)
public class RenderingTest {
@Rule
public WebLayerShellActivityTestRule mActivityTestRule = new WebLayerShellActivityTestRule();
public InstrumentationActivityTestRule mActivityTestRule =
new InstrumentationActivityTestRule();
@Test
@SmallTest
public void testSetSupportEmbeddingFromCallback() {
WebLayerShellActivity activity = mActivityTestRule.launchShellWithUrl("about:blank");
InstrumentationActivity activity = mActivityTestRule.launchShellWithUrl("about:blank");
CountDownLatch latch = new CountDownLatch(1);
String url = "data:text,foo";
......@@ -53,7 +54,7 @@ public class RenderingTest {
@Test
@SmallTest
public void testRepeatSetSupportEmbeddingGeneratesCallback() {
WebLayerShellActivity activity = mActivityTestRule.launchShellWithUrl("about:blank");
InstrumentationActivity activity = mActivityTestRule.launchShellWithUrl("about:blank");
CountDownLatch latch = new CountDownLatch(2);
String url = "data:text,foo";
......
......@@ -13,19 +13,20 @@ import org.junit.runner.RunWith;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.weblayer.shell.WebLayerShellActivity;
import org.chromium.weblayer.shell.InstrumentationActivity;
import java.util.concurrent.CountDownLatch;
@RunWith(BaseJUnit4ClassRunner.class)
public class SmokeTest {
@Rule
public WebLayerShellActivityTestRule mActivityTestRule = new WebLayerShellActivityTestRule();
public InstrumentationActivityTestRule mActivityTestRule =
new InstrumentationActivityTestRule();
@Test
@SmallTest
public void testSetSupportEmbedding() {
WebLayerShellActivity activity = mActivityTestRule.launchShellWithUrl("about:blank");
InstrumentationActivity activity = mActivityTestRule.launchShellWithUrl("about:blank");
TestThreadUtils.runOnUiThreadBlocking(
() -> { activity.getBrowserFragmentController().setSupportsEmbedding(true); });
......
......@@ -19,7 +19,7 @@ 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.TestThreadUtils;
import org.chromium.weblayer.BrowserController;
import org.chromium.weblayer.shell.WebLayerShellActivity;
import org.chromium.weblayer.shell.InstrumentationActivity;
/**
* Test for top-controls.
......@@ -27,7 +27,8 @@ import org.chromium.weblayer.shell.WebLayerShellActivity;
@RunWith(BaseJUnit4ClassRunner.class)
public class TopControlsTest {
@Rule
public WebLayerShellActivityTestRule mActivityTestRule = new WebLayerShellActivityTestRule();
public InstrumentationActivityTestRule mActivityTestRule =
new InstrumentationActivityTestRule();
private int mTopControlsHeight;
private int mInitialVisiblePageHeight;
......@@ -49,7 +50,7 @@ public class TopControlsTest {
@SmallTest
public void testBasic() throws Exception {
final String url = UrlUtils.encodeHtmlDataUri("<body><p style='height:5000px'>");
WebLayerShellActivity activity = mActivityTestRule.launchShellWithUrl(url);
InstrumentationActivity activity = mActivityTestRule.launchShellWithUrl(url);
TestThreadUtils.runOnUiThreadBlocking(() -> {
mTopControlsHeight = activity.getTopContentsContainer().getHeight();
......
......@@ -20,6 +20,8 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name="InstrumentationActivity">
</activity>
<meta-data android:name="org.chromium.weblayer.WebLayerPackage"
android:value="{{ weblayer_package }}"/>
</application>
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.weblayer.shell;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import org.chromium.weblayer.BrowserController;
import org.chromium.weblayer.BrowserFragment;
import org.chromium.weblayer.BrowserFragmentController;
import org.chromium.weblayer.BrowserObserver;
import org.chromium.weblayer.Profile;
import org.chromium.weblayer.UnsupportedVersionException;
import org.chromium.weblayer.WebLayer;
import java.io.File;
import java.util.List;
/**
* Activity for running instrumentation tests.
*/
public class InstrumentationActivity extends FragmentActivity {
private static final String TAG = "WLInstrumentation";
public static final String EXTRA_PROFILE_NAME = "EXTRA_PROFILE_NAME";
private Profile mProfile;
private BrowserFragmentController mBrowserFragmentController;
private BrowserController mBrowserController;
private EditText mUrlView;
private View mMainView;
private int mMainViewId;
private ViewGroup mTopContentsContainer;
private BrowserFragment mFragment;
private IntentInterceptor mIntentInterceptor;
public BrowserController getBrowserController() {
return mBrowserController;
}
public BrowserFragmentController getBrowserFragmentController() {
return mBrowserFragmentController;
}
/** Interface used to intercept intents for testing. */
public static interface IntentInterceptor {
void interceptIntent(Fragment fragment, Intent intent, int requestCode, Bundle options);
}
public void setIntentInterceptor(IntentInterceptor interceptor) {
mIntentInterceptor = interceptor;
}
@Override
public void startActivityFromFragment(
Fragment fragment, Intent intent, int requestCode, Bundle options) {
if (mIntentInterceptor != null) {
mIntentInterceptor.interceptIntent(fragment, intent, requestCode, options);
return;
}
super.startActivityFromFragment(fragment, intent, requestCode, options);
}
public View getTopContentsContainer() {
return mTopContentsContainer;
}
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout mainView = new LinearLayout(this);
mMainViewId = View.generateViewId();
mainView.setId(mMainViewId);
mMainView = mainView;
setContentView(mainView);
mUrlView = new EditText(this);
mUrlView.setId(View.generateViewId());
mUrlView.setSelectAllOnFocus(true);
mUrlView.setInputType(InputType.TYPE_TEXT_VARIATION_URI);
mUrlView.setImeOptions(EditorInfo.IME_ACTION_GO);
// The background of the top-view must be opaque, otherwise it bleeds through to the
// cc::Layer that mirrors the contents of the top-view.
mUrlView.setBackgroundColor(0xFFa9a9a9);
// The progress bar sits above the URL bar in Z order and at its bottom in Y.
mTopContentsContainer = new RelativeLayout(this);
mTopContentsContainer.addView(mUrlView,
new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
try {
// This ensures asynchronous initialization of WebLayer on first start of activity.
// If activity is re-created during process restart, FragmentManager attaches
// BrowserFragment immediately, resulting in synchronous init. By the time this line
// executes, the synchronous init has already happened.
WebLayer.create(getApplication())
.addCallback(webLayer -> onWebLayerReady(savedInstanceState));
} catch (UnsupportedVersionException e) {
throw new RuntimeException("Failed to initialize WebLayer", e);
}
}
private void onWebLayerReady(Bundle savedInstanceState) {
if (isFinishing() || isDestroyed()) return;
mFragment = getOrCreateBrowserFragment(savedInstanceState);
mBrowserFragmentController = mFragment.getController();
mProfile = mBrowserFragmentController.getProfile();
mBrowserFragmentController.setTopView(mTopContentsContainer);
mBrowserController = mBrowserFragmentController.getBrowserController();
mBrowserController.addObserver(new BrowserObserver() {
@Override
public void visibleUrlChanged(Uri uri) {
mUrlView.setText(uri.toString());
}
});
}
private BrowserFragment getOrCreateBrowserFragment(Bundle savedInstanceState) {
FragmentManager fragmentManager = getSupportFragmentManager();
if (savedInstanceState != null) {
// FragmentManager could have re-created the fragment.
List<Fragment> fragments = fragmentManager.getFragments();
if (fragments.size() > 1) {
throw new IllegalStateException("More than one fragment added, shouldn't happen");
}
if (fragments.size() == 1) {
return (BrowserFragment) fragments.get(0);
}
}
String profileName = getIntent().hasExtra(EXTRA_PROFILE_NAME)
? getIntent().getStringExtra(EXTRA_PROFILE_NAME)
: "DefaultProfile";
String profilePath = null;
if (!TextUtils.isEmpty(profileName)) {
profilePath = new File(getFilesDir(), profileName).getPath();
} // else create an in-memory Profile.
BrowserFragment fragment = WebLayer.createBrowserFragment(profilePath);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(mMainViewId, fragment);
// Note the commitNow() instead of commit(). We want the fragment to get attached to
// activity synchronously, so we can use all the functionality immediately. Otherwise we'd
// have to wait until the commit is executed.
transaction.commitNow();
return fragment;
}
public void loadUrl(String url) {
mBrowserController.getNavigationController().navigate(Uri.parse(url));
mUrlView.clearFocus();
}
private static String getUrlFromIntent(Intent intent) {
return intent != null ? intent.getDataString() : null;
}
}
......@@ -46,13 +46,9 @@ import java.util.List;
* Activity for managing the Demo Shell.
*/
public class WebLayerShellActivity extends FragmentActivity {
public static final String EXTRA_NO_LOAD = "extra_no_load";
private static final String TAG = "WebLayerShell";
private static final String KEY_MAIN_VIEW_ID = "mainViewId";
public static final String EXTRA_PROFILE_NAME = "EXTRA_PROFILE_NAME";
private Profile mProfile;
private BrowserFragmentController mBrowserFragmentController;
private BrowserController mBrowserController;
......@@ -62,38 +58,6 @@ public class WebLayerShellActivity extends FragmentActivity {
private int mMainViewId;
private ViewGroup mTopContentsContainer;
private BrowserFragment mFragment;
private IntentInterceptor mIntentInterceptor;
public BrowserController getBrowserController() {
return mBrowserController;
}
public BrowserFragmentController getBrowserFragmentController() {
return mBrowserFragmentController;
}
/** Interface used to intercept intents for testing. */
public static interface IntentInterceptor {
void interceptIntent(Fragment fragment, Intent intent, int requestCode, Bundle options);
}
public void setIntentInterceptor(IntentInterceptor interceptor) {
mIntentInterceptor = interceptor;
}
@Override
public void startActivityFromFragment(
Fragment fragment, Intent intent, int requestCode, Bundle options) {
if (mIntentInterceptor != null) {
mIntentInterceptor.interceptIntent(fragment, intent, requestCode, options);
return;
}
super.startActivityFromFragment(fragment, intent, requestCode, options);
}
public View getTopContentsContainer() {
return mTopContentsContainer;
}
@Override
protected void onCreate(final Bundle savedInstanceState) {
......@@ -205,15 +169,11 @@ public class WebLayerShellActivity extends FragmentActivity {
mBrowserFragmentController.setTopView(mTopContentsContainer);
mBrowserController = mBrowserFragmentController.getBrowserController();
boolean blockFirstLoad = getIntent().getExtras() != null
&& getIntent().getExtras().getBoolean(EXTRA_NO_LOAD, false);
if (!blockFirstLoad) {
String startupUrl = getUrlFromIntent(getIntent());
if (TextUtils.isEmpty(startupUrl)) {
startupUrl = "http://google.com";
}
loadUrl(startupUrl);
String startupUrl = getUrlFromIntent(getIntent());
if (TextUtils.isEmpty(startupUrl)) {
startupUrl = "http://google.com";
}
loadUrl(startupUrl);
mBrowserController.addObserver(new BrowserObserver() {
@Override
public void visibleUrlChanged(Uri uri) {
......@@ -257,8 +217,7 @@ public class WebLayerShellActivity extends FragmentActivity {
}
}
String profileName = getIntent().hasExtra(EXTRA_PROFILE_NAME)
? getIntent().getStringExtra(EXTRA_PROFILE_NAME) : "DefaultProfile";
String profileName = "DefaultProfile";
String profilePath = null;
if (!TextUtils.isEmpty(profileName)) {
profilePath = new File(getFilesDir(), profileName).getPath();
......@@ -275,11 +234,6 @@ public class WebLayerShellActivity extends FragmentActivity {
return fragment;
}
@Override
protected void onStart() {
super.onStart();
}
public void loadUrl(String url) {
mBrowserController.getNavigationController().navigate(Uri.parse(sanitizeUrl(url)));
mUrlView.clearFocus();
......
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