Commit 0632af2e authored by Clark DuVall's avatar Clark DuVall Committed by Commit Bot

[WebLayer] Add simple instrumentation tests

This adds some simple navigation instrumentation tests, which should
make sure WebLayer shell is basically working as expected.

Change-Id: I656a68b7202e86676a2ce75e79e7ef1f418d3772
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1801746Reviewed-by: default avatarBo <boliu@chromium.org>
Reviewed-by: default avatarJohn Abd-El-Malek <jam@chromium.org>
Commit-Queue: Clark DuVall <cduvall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#696841}
parent 0eefac3e
......@@ -506,6 +506,51 @@
},
"test": "content_shell_test_apk"
},
{
"args": [
"--gs-results-bucket=chromium-result-details",
"--recover-devices"
],
"merge": {
"args": [
"--bucket",
"chromium-result-details",
"--test-name",
"weblayer_instrumentation_test_apk"
],
"script": "//build/android/pylib/results/presentation/test_results_presentation.py"
},
"swarming": {
"can_use_on_swarming_builders": true,
"cipd_packages": [
{
"cipd_package": "infra/tools/luci/logdog/butler/${platform}",
"location": "bin",
"revision": "git_revision:ff387eadf445b24c935f1cf7d6ddd279f8a6b04c"
}
],
"dimension_sets": [
{
"device_os": "MMB29Q",
"device_os_type": "userdebug",
"device_type": "bullhead",
"os": "Android"
}
],
"hard_timeout": 3600,
"output_links": [
{
"link": [
"https://luci-logdog.appspot.com/v/?s",
"=android%2Fswarming%2Flogcats%2F",
"${TASK_ID}%2F%2B%2Funified_logcats"
],
"name": "shard #${SHARD_INDEX} logcats"
}
]
},
"test": "weblayer_instrumentation_test_apk"
},
{
"args": [
"--enable-features=NetworkService,NetworkServiceInProcess",
......@@ -7706,6 +7751,50 @@
},
"test": "content_shell_test_apk"
},
{
"args": [
"--gs-results-bucket=chromium-result-details",
"--recover-devices"
],
"merge": {
"args": [
"--bucket",
"chromium-result-details",
"--test-name",
"weblayer_instrumentation_test_apk"
],
"script": "//build/android/pylib/results/presentation/test_results_presentation.py"
},
"swarming": {
"can_use_on_swarming_builders": true,
"cipd_packages": [
{
"cipd_package": "infra/tools/luci/logdog/butler/${platform}",
"location": "bin",
"revision": "git_revision:ff387eadf445b24c935f1cf7d6ddd279f8a6b04c"
}
],
"dimension_sets": [
{
"device_os": "MMB29Q",
"device_os_type": "userdebug",
"device_type": "bullhead",
"os": "Android"
}
],
"output_links": [
{
"link": [
"https://luci-logdog.appspot.com/v/?s",
"=android%2Fswarming%2Flogcats%2F",
"${TASK_ID}%2F%2B%2Funified_logcats"
],
"name": "shard #${SHARD_INDEX} logcats"
}
]
},
"test": "weblayer_instrumentation_test_apk"
},
{
"args": [
"--enable-features=NetworkService,NetworkServiceInProcess",
......
......@@ -3008,6 +3008,10 @@
"label": "//android_webview/tools/automated_ui_tests:webview_ui_test_app_test_apk",
"type": "console_test_launcher",
},
"weblayer_instrumentation_test_apk": {
"label": "//weblayer/shell/android:weblayer_instrumentation_test_apk",
"type": "console_test_launcher",
},
"weblayer_shell": {
"label": "//weblayer/shell/android:run_weblayer_shell",
"type": "additional_compile_target",
......
......@@ -4800,6 +4800,11 @@
'vr_pixeltests': {},
},
'weblayer_android_gtests': {
'weblayer_instrumentation_test_apk': {
},
},
# TODO(crbug.com/888429): Port remaining WebRTC tests to swarming.
'webrtc_chromium_baremetal_gtests': {
# Run capture unittests as well since our bots have real webcams.
......@@ -5761,6 +5766,7 @@
'mojo_android_gtests': [
'network_service_android_gtests',
'weblayer_android_gtests',
],
'mojo_chromiumos_fyi_gtests': [
......
......@@ -28,3 +28,12 @@ Bug tracker: [Internals>WebLayer](https://bugs.chromium.org/p/chromium/issues/li
`common` internal code which runs in the browser and child processes
`renderer` internal code which runs in the renderer process
## Testing
To run instrumentation tests:
$ autoninja -C out/Default weblayer_instrumentation_test_apk
$ out/Default/bin/run_weblayer_instrumentation_test_apk
The test script will build and install all necessary APKs.
......@@ -133,3 +133,21 @@ generate_wrapper("run_weblayer_shell") {
":weblayer_support_apk",
]
}
instrumentation_test_apk("weblayer_instrumentation_test_apk") {
apk_name = "WebLayerInstrumentationTest"
apk_under_test = "//weblayer/shell/android:weblayer_shell_apk"
android_manifest = "javatests/AndroidManifest.xml"
min_sdk_version = 21
deps = [
"//base:base_java_test_support",
"//content/public/test/android:content_java_test_support",
"//third_party/android_support_test_runner:runner_java",
]
java_files = [
"javatests/src/org/chromium/weblayer/test/NavigationTest.java",
"javatests/src/org/chromium/weblayer/test/WebLayerShellActivityTestRule.java",
"shell_apk/src/org/chromium/weblayer/shell/WebLayerShellActivity.java",
]
additional_apks = [ "//weblayer/shell/android:weblayer_support_apk" ]
}
<?xml version="1.0" encoding="utf-8"?>
<!-- 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.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.chromium.weblayer.tests">
<uses-permission android:name="android.permission.RUN_INSTRUMENTATION" />
<!-- We add an application tag here just so that we can indicate that this
package needs to link against the android.test library, which is
needed when building test cases. -->
<application>
<uses-library android:name="android.test.runner" />
<activity android:name="org.chromium.test.broker.OnDeviceInstrumentationBroker"
android:exported="true"/>
</application>
<instrumentation android:name="org.chromium.base.test.BaseChromiumAndroidJUnitRunner"
android:targetPackage="org.chromium.weblayer.shell"
android:label="JUnit4-based tests for org.chromium.weblayer.shell" />
</manifest>
include_rules = [
"+content/public/test",
]
// 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.test;
import android.net.Uri;
import android.support.test.filters.SmallTest;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.weblayer.Navigation;
import org.chromium.weblayer.NavigationObserver;
import org.chromium.weblayer.shell.WebLayerShellActivity;
import java.util.concurrent.TimeoutException;
/**
* Example test that just starts the weblayer shell.
*/
@RunWith(BaseJUnit4ClassRunner.class)
public class NavigationTest {
@Rule
public WebLayerShellActivityTestRule mActivityTestRule = new WebLayerShellActivityTestRule();
// URL used for base tests.
private static final String URL = "data:text";
private static class Observer extends NavigationObserver {
public static class NavigationCallbackHelper extends CallbackHelper {
private Uri mUri;
public void notifyCalled(Uri uri) {
mUri = uri;
notifyCalled();
}
public void assertCalledWith(int currentCallCount, String uri)
throws TimeoutException, InterruptedException {
waitForCallback(currentCallCount);
Assert.assertEquals(mUri.toString(), uri);
}
}
public NavigationCallbackHelper onStartedCallback = new NavigationCallbackHelper();
public NavigationCallbackHelper onCommittedCallback = new NavigationCallbackHelper();
public NavigationCallbackHelper onCompletedCallback = new NavigationCallbackHelper();
@Override
public void navigationStarted(Navigation navigation) {
onStartedCallback.notifyCalled(navigation.getUri());
}
@Override
public void navigationCommitted(Navigation navigation) {
onCommittedCallback.notifyCalled(navigation.getUri());
}
@Override
public void navigationCompleted(Navigation navigation) {
onCompletedCallback.notifyCalled(navigation.getUri());
}
}
@Test
@SmallTest
public void testBaseStartup() throws Exception {
WebLayerShellActivity activity = mActivityTestRule.launchShellWithUrl(URL);
Assert.assertNotNull(activity);
mActivityTestRule.waitForNavigation(URL);
}
@Test
@SmallTest
public void testNavigationEvents() throws Exception {
WebLayerShellActivity activity = mActivityTestRule.launchShellWithUrl(URL);
mActivityTestRule.waitForNavigation(URL);
Observer observer = new Observer();
TestThreadUtils.runOnUiThreadBlocking(() -> {
activity.getBrowserController().getNavigationController().addObserver(observer);
});
int curStartedCount = observer.onStartedCallback.getCallCount();
int curCommittedCount = observer.onCommittedCallback.getCallCount();
int curCompletedCount = observer.onCompletedCallback.getCallCount();
String url = "data:text,foo";
mActivityTestRule.loadUrl(url);
mActivityTestRule.waitForNavigation(url);
observer.onStartedCallback.assertCalledWith(curStartedCount, url);
observer.onCommittedCallback.assertCalledWith(curCommittedCount, url);
observer.onCompletedCallback.assertCalledWith(curCompletedCount, url);
}
}
// 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.test;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
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.NavigationController;
import org.chromium.weblayer.shell.WebLayerShellActivity;
/**
* ActivityTestRule for WebLayerShellActivity.
*
* Test can use this ActivityTestRule to launch or get WebLayerShellActivity.
*/
public class WebLayerShellActivityTestRule extends ActivityTestRule<WebLayerShellActivity> {
private static final long WAIT_FOR_NAVIGATION_TIMEOUT = 10000L;
public WebLayerShellActivityTestRule() {
super(WebLayerShellActivity.class, false, false);
}
/**
* Starts the WebLayer activity and loads the given URL.
*/
public WebLayerShellActivity launchShellWithUrl(String url) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (url != null) intent.setData(Uri.parse(url));
intent.setComponent(
new ComponentName(InstrumentationRegistry.getInstrumentation().getTargetContext(),
WebLayerShellActivity.class));
return launchActivity(intent);
}
/**
* Waits for the shell to navigate to the given URI.
*/
public void waitForNavigation(String uri) {
CriteriaHelper.pollUiThread(new Criteria() {
@Override
public boolean isSatisfied() {
NavigationController navigationController =
getActivity().getBrowserController().getNavigationController();
Uri currentUri = navigationController.getNavigationEntryDisplayUri(
navigationController.getNavigationListCurrentIndex());
return currentUri.toString().equals(uri);
}
}, WAIT_FOR_NAVIGATION_TIMEOUT, CriteriaHelper.DEFAULT_POLLING_INTERVAL);
}
/**
* Loads the given URL in the shell.
*/
public void loadUrl(String url) {
TestThreadUtils.runOnUiThreadBlocking(() -> { getActivity().loadUrl(url); });
}
}
......@@ -4,12 +4,14 @@
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.FragmentTransaction;
import android.text.InputType;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
......@@ -52,6 +54,10 @@ public class WebLayerShellActivity extends FragmentActivity {
}
}
public BrowserController getBrowserController() {
return mBrowserController;
}
@Override
protected void onCreate(final Bundle savedInstanceState) {
// Only call init for main process.
......@@ -94,7 +100,12 @@ public class WebLayerShellActivity extends FragmentActivity {
transaction.commit();
mBrowserFragment.setTopView(mUrlView);
loadUrl("http://google.com");
String startupUrl = getUrlFromIntent(getIntent());
if (TextUtils.isEmpty(startupUrl)) {
startupUrl = "http://google.com";
}
loadUrl(startupUrl);
mBrowserController.addObserver(new BrowserObserver() {
@Override
public void displayURLChanged(Uri uri) {
......@@ -115,11 +126,15 @@ public class WebLayerShellActivity extends FragmentActivity {
super.onStart();
}
private void loadUrl(String url) {
public void loadUrl(String url) {
mBrowserController.getNavigationController().navigate(Uri.parse(sanitizeUrl(url)));
mUrlView.clearFocus();
}
private static String getUrlFromIntent(Intent intent) {
return intent != null ? intent.getDataString() : null;
}
/**
* Given an URL, this performs minimal sanitizing to ensure it will be valid.
* @param url The url to be sanitized.
......
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