From 8758634a7c0c79481469f50c9c8f1c580169adec Mon Sep 17 00:00:00 2001 From: mikecase Date: Mon, 23 Mar 2015 11:46:47 -0700 Subject: Add a custom Robolectric testrunner. The default Robolectric testrunner has some behaviors we want to override. By default the Robolectric testrunner will download the Robolectric runtime dependencies from the Maven central reposoitory. In addition, Robolectric won't run at all if the API level in the AndroidManifest isn't officially supported by Robolectric which isn't what we want. BUG=448030 Review URL: https://codereview.chromium.org/942083003 Cr-Commit-Position: refs/heads/master@{#321819} --- build/android/pylib/junit/test_runner.py | 4 ++ testing/android/junit/BUILD.gn | 1 + .../testing/local/LocalRobolectricTestRunner.java | 58 ++++++++++++++++++++++ testing/android/junit/junit_test.gyp | 1 + third_party/robolectric/BUILD.gn | 18 +++---- third_party/robolectric/robolectric.gyp | 18 +++---- 6 files changed, 82 insertions(+), 18 deletions(-) create mode 100644 testing/android/junit/java/src/org/chromium/testing/local/LocalRobolectricTestRunner.java diff --git a/build/android/pylib/junit/test_runner.py b/build/android/pylib/junit/test_runner.py index 35ac666..b85967b 100644 --- a/build/android/pylib/junit/test_runner.py +++ b/build/android/pylib/junit/test_runner.py @@ -22,9 +22,13 @@ class JavaTestRunner(object): def RunTest(self, _test): """Runs junit tests from |self._test_suite|.""" + command = ['java', + '-Drobolectric.dependency.dir=%s' % + os.path.join(constants.GetOutDirectory(), 'lib.java'), '-jar', os.path.join(constants.GetOutDirectory(), 'lib.java', '%s.jar' % self._test_suite)] + if self._test_filter: command.extend(['-gtest-filter', self._test_filter]) if self._package_filter: diff --git a/testing/android/junit/BUILD.gn b/testing/android/junit/BUILD.gn index e4cd871..f80d0b5 100644 --- a/testing/android/junit/BUILD.gn +++ b/testing/android/junit/BUILD.gn @@ -12,6 +12,7 @@ java_library("junit_test_support") { deps = [ "//third_party/junit", "//third_party/mockito:mockito_java", + "//third_party/robolectric:robolectric_java", ] } diff --git a/testing/android/junit/java/src/org/chromium/testing/local/LocalRobolectricTestRunner.java b/testing/android/junit/java/src/org/chromium/testing/local/LocalRobolectricTestRunner.java new file mode 100644 index 0000000..293525e --- /dev/null +++ b/testing/android/junit/java/src/org/chromium/testing/local/LocalRobolectricTestRunner.java @@ -0,0 +1,58 @@ +// Copyright 2015 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.junit.runners.model.InitializationError; + +import org.robolectric.AndroidManifest; +import org.robolectric.DependencyResolver; +import org.robolectric.LocalDependencyResolver; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.SdkConfig; +import org.robolectric.annotation.Config; + +import java.io.File; + +/** + * A custom Robolectric Junit4 Test Runner. This test runner will load the + * "real" android jars from a local directory rather than use Maven to fetch + * them from the Maven Central repository. Additionally, it will ignore the + * API level written in the AndroidManifest as that can cause issues if + * robolectric does not support that API level. + */ +public class LocalRobolectricTestRunner extends RobolectricTestRunner { + + private static final int ANDROID_API_LEVEL = 18; + + public LocalRobolectricTestRunner(Class testClass) throws InitializationError { + super(testClass); + } + + @Override + protected final DependencyResolver getJarResolver() { + String dependencyDir = System.getProperty("robolectric.dependency.dir"); + if (dependencyDir == null) { + throw new IllegalStateException("robolectric.dependency.dir not specified. Make sure" + + " you are setting the robolectric.dependency.dir system property to the" + + " directory containing Robolectric's runtime dependency jars."); + } + return new LocalDependencyResolver(new File(dependencyDir)); + } + + @Override + protected SdkConfig pickSdkVersion(AndroidManifest appManifest, Config config) { + // Pulling from the manifest is dangerous as the apk might target a version of + // android that robolectric does not yet support. We still allow the API level to + // be overridden with the Config annotation. + return config.emulateSdk() < 0 + ? new SdkConfig(ANDROID_API_LEVEL) : super.pickSdkVersion(null, config); + } + + @Override + protected int pickReportedSdkVersion(Config config, AndroidManifest appManifest) { + return config.reportSdk() < 0 + ? ANDROID_API_LEVEL : super.pickReportedSdkVersion(config, appManifest); + } +} \ No newline at end of file diff --git a/testing/android/junit/junit_test.gyp b/testing/android/junit/junit_test.gyp index f0f8b8c..16e0048 100644 --- a/testing/android/junit/junit_test.gyp +++ b/testing/android/junit/junit_test.gyp @@ -11,6 +11,7 @@ 'dependencies': [ '../../../third_party/junit/junit.gyp:junit_jar', '../../../third_party/mockito/mockito.gyp:mockito_jar', + '../../../third_party/robolectric/robolectric.gyp:robolectric_jar' ], 'variables': { 'src_paths': [ diff --git a/third_party/robolectric/BUILD.gn b/third_party/robolectric/BUILD.gn index 3271e12..07613ac 100644 --- a/third_party/robolectric/BUILD.gn +++ b/third_party/robolectric/BUILD.gn @@ -4,20 +4,20 @@ import("//build/config/android/rules.gni") -# GYP: //third_party/robolectric/robolectric.gyp:android_all_jar -java_prebuilt("android_all_java") { +# GYP: //third_party/robolectric/robolectric.gyp:android-all-4.3_r2-robolectric-0 +java_prebuilt("android-all-4.3_r2-robolectric-0") { visibility = [ ":*" ] jar_path = "lib/android-all-4.3_r2-robolectric-0.jar" } -# GYP: //third_party/robolectric/robolectric.gyp:tagsoup_jar -java_prebuilt("tagsoup_java") { +# GYP: //third_party/robolectric/robolectric.gyp:tagsoup-1.2 +java_prebuilt("tagsoup-1.2") { visibility = [ ":*" ] jar_path = "lib/tagsoup-1.2.jar" } -# GYP: //third_party/robolectric/robolectric.gyp:json_jar -java_prebuilt("json_java") { +# GYP: //third_party/robolectric/robolectric.gyp:json-20080701 +java_prebuilt("json-20080701") { visibility = [ ":*" ] jar_path = "lib/json-20080701.jar" } @@ -27,8 +27,8 @@ java_prebuilt("robolectric_java") { testonly = true jar_path = "lib/robolectric-2.4-jar-with-dependencies.jar" deps = [ - ":android_all_java", - ":tagsoup_java", - ":json_java", + ":android-all-4.3_r2-robolectric-0", + ":tagsoup-1.2", + ":json-20080701", ] } diff --git a/third_party/robolectric/robolectric.gyp b/third_party/robolectric/robolectric.gyp index 60cbe3e..7c3d785 100644 --- a/third_party/robolectric/robolectric.gyp +++ b/third_party/robolectric/robolectric.gyp @@ -5,8 +5,8 @@ { 'targets': [ { - # GN: //third_party/robolectric:android_all_java - 'target_name': 'android_all_jar', + # GN: //third_party/robolectric:android-all-4.3_r2-robolectric-0 + 'target_name': 'android-all-4.3_r2-robolectric-0', 'type': 'none', 'variables': { 'jar_path': 'lib/android-all-4.3_r2-robolectric-0.jar', @@ -16,8 +16,8 @@ ] }, { - # GN: //third_party/robolectric:tagsoup_java - 'target_name': 'tagsoup_jar', + # GN: //third_party/robolectric:tagsoup-1.2 + 'target_name': 'tagsoup-1.2', 'type': 'none', 'variables': { 'jar_path': 'lib/tagsoup-1.2.jar', @@ -27,8 +27,8 @@ ] }, { - # GN: //third_party/robolectric:json_java - 'target_name': 'json_jar', + # GN: //third_party/robolectric:json-20080701 + 'target_name': 'json-20080701', 'type': 'none', 'variables': { 'jar_path': 'lib/json-20080701.jar', @@ -42,9 +42,9 @@ 'target_name': 'robolectric_jar', 'type': 'none', 'dependencies': [ - 'android_all_jar', - 'tagsoup_jar', - 'json_jar', + 'android-all-4.3_r2-robolectric-0', + 'tagsoup-1.2', + 'json-20080701', ], 'variables': { 'jar_path': 'lib/robolectric-2.4-jar-with-dependencies.jar', -- cgit v1.1