Commit f9ffca11 authored by Sam Maier's avatar Sam Maier Committed by Commit Bot

Fixing built-in Robolectric TestRunners

Built-in Robolectric test runners require knowledge of where our resource
apk is in order to run with binary resources mode. This change gives all
junit_binary targets a .properties file that Robolectric uses to provide
the location of this resource apk.

Bug: 1008309
Change-Id: Ie80bb1f5a927b254c08b4aed8c30630e24dc6000
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1829472Reviewed-by: default avatarAndrew Grieve <agrieve@chromium.org>
Commit-Queue: Sam Maier <smaier@chromium.org>
Cr-Commit-Position: refs/heads/master@{#701543}
parent 49ad1ffd
......@@ -37,13 +37,14 @@ if os.getcwd() != self_dir:
offset = os.path.relpath(self_dir, os.getcwd())
classpath = [os.path.join(offset, p) for p in classpath]
bootclasspath = [os.path.join(offset, p) for p in bootclasspath]
java_cmd = ["java"]
# This is a simple argparser for jvm and jar arguments.
java_cmd = ['java']
# This is a simple argparser for jvm, jar, and classpath arguments.
parser = argparse.ArgumentParser()
parser.add_argument('--jar-args')
parser.add_argument('--jvm-args')
parser.add_argument('--classpath')
known_args, unknown_args = parser.parse_known_args(sys.argv[1:])
if known_args.jvm_args:
jvm_arguments = known_args.jvm_args.strip('"').split()
java_cmd.extend(jvm_arguments)
......@@ -54,14 +55,17 @@ if known_args.jar_args:
else:
jar_arguments = unknown_args
if known_args.classpath:
classpath += [known_args.classpath]
{noverify_flag}
if bootclasspath:
java_cmd.append("-Xbootclasspath/p:" + ":".join(bootclasspath))
java_cmd.append('-Xbootclasspath/p:' + ':'.join(bootclasspath))
java_cmd.extend(
["-classpath", ":".join(classpath), "-enableassertions", \"{main_class}\"])
['-classpath', ':'.join(classpath), '-enableassertions', \"{main_class}\"])
java_cmd.extend(extra_program_args)
java_cmd.extend(jar_arguments)
os.execvp("java", java_cmd)
os.execvp('java', java_cmd)
"""
def main(argv):
......
......@@ -5,6 +5,7 @@
import json
import logging
import os
import zipfile
from devil.utils import cmd_helper
from pylib import constants
......@@ -31,7 +32,6 @@ class LocalMachineJunitTestRun(test_run.TestRun):
def RunTests(self, results):
with tempfile_ext.NamedTemporaryDirectory() as temp_dir:
json_file_path = os.path.join(temp_dir, 'results.json')
java_script = os.path.join(
constants.GetOutDirectory(), 'bin', 'helper',
self._test_instance.suite)
......@@ -55,8 +55,6 @@ class LocalMachineJunitTestRun(test_run.TestRun):
self._test_instance.robolectric_runtime_deps_dir,
'-Ddir.source.root=%s' % constants.DIR_SOURCE_ROOT,
'-Drobolectric.resourcesMode=binary',
'-Dchromium.robolectric.resource.ap_=%s' %
self._test_instance.resource_apk
]
if logging.getLogger().isEnabledFor(logging.INFO):
......@@ -90,6 +88,14 @@ class LocalMachineJunitTestRun(test_run.TestRun):
if jvm_args:
command.extend(['--jvm-args', '"%s"' % ' '.join(jvm_args)])
# Create properties file for Robolectric test runners so they can find the
# binary resources.
properties_jar_path = os.path.join(temp_dir, 'properties.jar')
with zipfile.ZipFile(properties_jar_path, 'w') as z:
z.writestr('com/android/tools/test_config.properties',
'android_resource_apk=%s' % self._test_instance.resource_apk)
command.extend(['--classpath', properties_jar_path])
cmd_helper.RunCmd(command)
try:
with open(json_file_path, 'r') as f:
......
......@@ -41,9 +41,6 @@ public class HostBrowserUtilsTest {
int NO = 1;
}
// Cannot specify a custom package name with {@link ParameterizedRobolectricTestRunner}.
private static final String WEBAPK_PACKAGE_NAME = "org.robolectric.default";
private static final String DEFAULT_BROWSER_SUPPORTING_WEBAPKS =
"com.google.android.apps.chrome";
private static final String DEFAULT_BROWSER_NOT_SUPPORTING_WEBAPKS =
......@@ -354,6 +351,6 @@ public class HostBrowserUtilsTest {
Bundle bundle = new Bundle();
bundle.putString(WebApkMetaDataKeys.RUNTIME_HOST, hostBrowserPackage);
WebApkTestHelper.registerWebApkWithMetaData(
WEBAPK_PACKAGE_NAME, bundle, null /* shareTargetMetaData */);
mContext.getPackageName(), bundle, null /* shareTargetMetaData */);
}
}
......@@ -21,7 +21,6 @@ java_library("junit_test_support") {
"java/src/org/chromium/testing/local/JsonLogger.java",
"java/src/org/chromium/testing/local/JunitTestArgParser.java",
"java/src/org/chromium/testing/local/JunitTestMain.java",
"java/src/org/chromium/testing/local/GNManifestFactory.java",
"java/src/org/chromium/testing/local/LocalRobolectricTestRunner.java",
"java/src/org/chromium/testing/local/PackageFilter.java",
"java/src/org/chromium/testing/local/RunnerFilter.java",
......
// Copyright 2017 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.testing.local;
import org.robolectric.annotation.Config;
import org.robolectric.internal.ManifestFactory;
import org.robolectric.internal.ManifestIdentifier;
import org.robolectric.res.Fs;
import org.robolectric.res.FsFile;
import java.io.File;
import java.net.MalformedURLException;
/**
* Class that manages passing Android manifest information to Robolectric.
*/
public class GNManifestFactory implements ManifestFactory {
private static final String CHROMIUM_RES_APK = "chromium.robolectric.resource.ap_";
@Override
public ManifestIdentifier identify(Config config) {
String resourceApk = System.getProperty(CHROMIUM_RES_APK);
FsFile apkFile = null;
try {
apkFile = Fs.fromURL(new File(resourceApk).toURI().toURL());
} catch (MalformedURLException e) {
}
return new ManifestIdentifier(config.packageName(), null, null, null, null, apkFile);
}
}
......@@ -7,7 +7,6 @@ package org.chromium.testing.local;
import org.junit.runners.model.InitializationError;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.internal.ManifestFactory;
/**
* A custom Robolectric Junit4 Test Runner with Chromium specific settings.
......@@ -34,9 +33,4 @@ public class LocalRobolectricTestRunner extends RobolectricTestRunner {
.setShadows(new Class[] {CustomShadowApplicationPackageManager.class})
.build();
}
@Override
protected ManifestFactory getManifestFactory(Config config) {
return new GNManifestFactory();
}
}
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