Use binary resource support in robolectric
The resources available to tests are now exactly the merged resources
located in the APK under test.
Bug: 74359828
Test: make -j56 RunCarSettingsLibRoboTests
Change-Id: Ia87ca5430f74589fdd4e2c0f67c653dd134d5479
diff --git a/car-settings-lib/tests/robotests/Android.mk b/car-settings-lib/tests/robotests/Android.mk
index 2b503cd..bbb03e6 100644
--- a/car-settings-lib/tests/robotests/Android.mk
+++ b/car-settings-lib/tests/robotests/Android.mk
@@ -1,8 +1,8 @@
LOCAL_PATH := $(call my-dir)
-############################################################
-# CarSettingsLib app just for Robolectric test target. #
-############################################################
+##################################################################
+# Car Settings Library app just for Robolectric test target. #
+##################################################################
include $(CLEAR_VARS)
LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res
@@ -21,18 +21,18 @@
include $(BUILD_PACKAGE)
-################################################
-# Car SettingsLibrary Robolectric test target. #
-################################################
+##################################################################
+# Car Settings Library Robolectric test target. #
+##################################################################
include $(CLEAR_VARS)
LOCAL_MODULE := CarSettingsLibRoboTests
+LOCAL_MODULE_CLASS := JAVA_LIBRARIES
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_JAVA_RESOURCE_DIRS := config
-# Include the testing libraries
LOCAL_JAVA_LIBRARIES := \
robolectric_android-all-stub \
Robolectric_all-target \
@@ -44,6 +44,9 @@
LOCAL_MODULE_TAGS := optional
+# Generate test_config.properties
+include external/robolectric-shadows/gen_test_config.mk
+
include $(BUILD_STATIC_JAVA_LIBRARY)
##################################################################
diff --git a/car-settings-lib/tests/robotests/config/robolectric.properties b/car-settings-lib/tests/robotests/config/robolectric.properties
index 6a233c1..3626c87 100644
--- a/car-settings-lib/tests/robotests/config/robolectric.properties
+++ b/car-settings-lib/tests/robotests/config/robolectric.properties
@@ -13,5 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
-manifest=packages/apps/Car/libs/car-settings-lib/tests/robotests/AndroidManifest.xml
sdk=NEWEST_SDK
\ No newline at end of file
diff --git a/car-settings-lib/tests/robotests/src/com/android/car/settingslib/language/LocaleListItemProviderTest.java b/car-settings-lib/tests/robotests/src/com/android/car/settingslib/language/LocaleListItemProviderTest.java
index af62285..952e4dc 100644
--- a/car-settings-lib/tests/robotests/src/com/android/car/settingslib/language/LocaleListItemProviderTest.java
+++ b/car-settings-lib/tests/robotests/src/com/android/car/settingslib/language/LocaleListItemProviderTest.java
@@ -26,7 +26,6 @@
import androidx.car.widget.PagedListView;
import com.android.car.settingslib.robolectric.BaseRobolectricTest;
-import com.android.car.settingslib.robolectric.CarSettingsLibRobolectricTestRunner;
import com.android.car.settingslib.shadows.ShadowLocalePicker;
import com.android.internal.app.LocaleStore;
import com.android.internal.app.SuggestedLocaleAdapter;
@@ -35,6 +34,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
+import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
@@ -47,7 +47,7 @@
/**
* Tests {@link LocaleListItemProvider} to validate
*/
-@RunWith(CarSettingsLibRobolectricTestRunner.class)
+@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowLocalePicker.class)
public class LocaleListItemProviderTest extends BaseRobolectricTest {
private static final int TYPE_HEADER_SUGGESTED = 0;
diff --git a/car-settings-lib/tests/robotests/src/com/android/car/settingslib/log/LoggerBaseTest.java b/car-settings-lib/tests/robotests/src/com/android/car/settingslib/log/LoggerBaseTest.java
index 1fbda08..468c010 100644
--- a/car-settings-lib/tests/robotests/src/com/android/car/settingslib/log/LoggerBaseTest.java
+++ b/car-settings-lib/tests/robotests/src/com/android/car/settingslib/log/LoggerBaseTest.java
@@ -16,17 +16,16 @@
package com.android.car.settingslib.log;
-import com.android.car.settingslib.robolectric.CarSettingsLibRobolectricTestRunner;
-
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
+import org.robolectric.RobolectricTestRunner;
/**
* Tests {@link LoggerBase}
*/
-@RunWith(CarSettingsLibRobolectricTestRunner.class)
+@RunWith(RobolectricTestRunner.class)
public class LoggerBaseTest {
@Rule
diff --git a/car-settings-lib/tests/robotests/src/com/android/car/settingslib/robolectric/BaseRobolectricTest.java b/car-settings-lib/tests/robotests/src/com/android/car/settingslib/robolectric/BaseRobolectricTest.java
index ccadbbe..280faea 100644
--- a/car-settings-lib/tests/robotests/src/com/android/car/settingslib/robolectric/BaseRobolectricTest.java
+++ b/car-settings-lib/tests/robotests/src/com/android/car/settingslib/robolectric/BaseRobolectricTest.java
@@ -18,14 +18,12 @@
import org.junit.Rule;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
-import org.robolectric.annotation.Config;
/**
- * Base test for CarSettingsLib Robolectric tests that sets the manifest and sdk config parameters
+ * Base test for CarSettingsLib Robolectric tests.
*/
-@Config(packageName = "com.android.car.settingslib")
public abstract class BaseRobolectricTest {
- //This rule automatically initializes any mocks created using the @Mock annotation
+ // This rule automatically initializes any mocks created using the @Mock annotation
@Rule
public MockitoRule mMockitoRule = MockitoJUnit.rule();
}
diff --git a/car-settings-lib/tests/robotests/src/com/android/car/settingslib/robolectric/CarSettingsLibRobolectricTestRunner.java b/car-settings-lib/tests/robotests/src/com/android/car/settingslib/robolectric/CarSettingsLibRobolectricTestRunner.java
deleted file mode 100644
index 0498c3d..0000000
--- a/car-settings-lib/tests/robotests/src/com/android/car/settingslib/robolectric/CarSettingsLibRobolectricTestRunner.java
+++ /dev/null
@@ -1,108 +0,0 @@
-/*
- * Copyright (C) 2018 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.android.car.settingslib.robolectric;
-
-import androidx.annotation.NonNull;
-
-import org.junit.runners.model.InitializationError;
-import org.robolectric.RobolectricTestRunner;
-import org.robolectric.annotation.Config;
-import org.robolectric.manifest.AndroidManifest;
-import org.robolectric.res.Fs;
-import org.robolectric.res.ResourcePath;
-
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-/**
- * Custom test runner for the testing of CarSettingsLib. This is needed because the
- * default behavior for robolectric is just to grab the resource directory in the target package.
- * We want to override this to add several spanning different projects.
- */
-public class CarSettingsLibRobolectricTestRunner extends RobolectricTestRunner {
- private static final Map<String, String> AAR_VERSIONS;
- private static final String SUPPORT_RESOURCE_PATH_TEMPLATE =
- "jar:file:prebuilts/sdk/current/androidx/m2repository/androidx/"
- + "%1$s/%1$s/%2$s/%1$s-%2$s.aar!/res";
-
- static {
- AAR_VERSIONS = new HashMap<>();
- AAR_VERSIONS.put("car", "1.0.0-alpha6");
- AAR_VERSIONS.put("appcompat", "1.1.0-alpha01");
- }
-
- /**
- * We don't actually want to change this behavior, so we just call super.
- */
- public CarSettingsLibRobolectricTestRunner(Class<?> testClass) throws InitializationError {
- super(testClass);
- }
-
- private static ResourcePath createResourcePath(@NonNull String filePath) {
- try {
- return new ResourcePath(null, Fs.fromURL(new URL(filePath)), null);
- } catch (MalformedURLException e) {
- throw new RuntimeException("CarSettingsLibRobolectricTestRunner failure", e);
- }
- }
-
- /**
- * Create the resource path for a support library component's JAR.
- */
- private static String createSupportResourcePathFromJar(@NonNull String componentId) {
- if (!AAR_VERSIONS.containsKey(componentId)) {
- throw new IllegalArgumentException("Unknown component " + componentId
- + ". Update test with appropriate component name and version.");
- }
- return String.format(SUPPORT_RESOURCE_PATH_TEMPLATE, componentId,
- AAR_VERSIONS.get(componentId));
- }
-
- /**
- * We are going to create our own custom manifest so that we can add multiple resource
- * paths to it. This lets us access resources in both Settings and SettingsLib in our tests.
- */
- @Override
- protected AndroidManifest getAppManifest(Config config) {
- // Using the manifest file's relative path, we can figure out the application directory.
- final String appRoot = "packages/apps/Car/libs/car-settings-lib/";
- final String manifestPath = appRoot + "/AndroidManifest.xml";
- final String resDir = appRoot + "/res";
- final String assetsDir = appRoot + config.assetDir();
-
- // By adding any resources from libraries we need to the AndroidManifest, we can access
- // them from within the parallel universe's resource loader.
- final AndroidManifest manifest = new AndroidManifest(Fs.fileFromPath(manifestPath),
- Fs.fileFromPath(resDir), Fs.fileFromPath(assetsDir)) {
- @Override
- public List<ResourcePath> getIncludedResourcePaths() {
- List<ResourcePath> paths = super.getIncludedResourcePaths();
- paths.add(createResourcePath("file:packages/apps/Car/libs/car-settings-lib/res"));
-
- // Support library resources. These need to point to the prebuilts of support
- // library and not the source.
- paths.add(createResourcePath(createSupportResourcePathFromJar("appcompat")));
- paths.add(createResourcePath(createSupportResourcePathFromJar("car")));
- return paths;
- }
- };
-
- return manifest;
- }
-}
diff --git a/car-settings-lib/tests/robotests/src/com/android/car/settingslib/shadows/ShadowLocalePicker.java b/car-settings-lib/tests/robotests/src/com/android/car/settingslib/shadows/ShadowLocalePicker.java
index 369d82c..1b2139f 100644
--- a/car-settings-lib/tests/robotests/src/com/android/car/settingslib/shadows/ShadowLocalePicker.java
+++ b/car-settings-lib/tests/robotests/src/com/android/car/settingslib/shadows/ShadowLocalePicker.java
@@ -24,9 +24,6 @@
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.Resetter;
-import java.util.Locale;
-
-
/**
* Shadow class for {@link LocalePicker} intended to help provide a defined set of locales that can
* be set and changed within the test.
@@ -34,29 +31,17 @@
@Implements(LocalePicker.class)
public class ShadowLocalePicker {
- static Locale sUpdatedLocale;
private static String[] sSystemAssetLocales;
private static String[] sSupportedLocales;
@Implementation
- public static void updateLocale(Locale locale) {
- sUpdatedLocale = locale;
+ protected static String[] getSystemAssetLocales() {
+ return sSystemAssetLocales == null ? new String[0] : sSystemAssetLocales;
}
@Implementation
- public static String[] getSystemAssetLocales() {
- if (sSystemAssetLocales == null) {
- return new String[0];
- }
- return sSystemAssetLocales;
- }
-
- @Implementation
- public static String[] getSupportedLocales(Context context) {
- if (sSupportedLocales == null) {
- return new String[0];
- }
- return sSupportedLocales;
+ protected static String[] getSupportedLocales(Context context) {
+ return sSupportedLocales == null ? new String[0] : sSupportedLocales;
}
public static void setSystemAssetLocales(String... locales) {
@@ -69,7 +54,6 @@
@Resetter
public static void reset() {
- sUpdatedLocale = null;
sSystemAssetLocales = null;
sSupportedLocales = null;
}