From fa1a0da2a0468d759104412052fc9c7df556870f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Muller?= Date: Thu, 8 Feb 2024 22:45:50 +0100 Subject: [PATCH] Remove deprecated method These methods were supposed to be removed in older versions of Robolectric --- .../java/org/robolectric/ConfigMerger.java | 152 ------------------ .../robolectric/RobolectricTestRunner.java | 68 +------- .../RobolectricTestRunnerTest.java | 16 -- .../shadows/ShadowDisplayTest.java | 41 ----- .../android/controller/ServiceController.java | 11 -- .../robolectric/shadows/ShadowDisplay.java | 106 ------------ 6 files changed, 7 insertions(+), 387 deletions(-) delete mode 100644 robolectric/src/main/java/org/robolectric/ConfigMerger.java diff --git a/robolectric/src/main/java/org/robolectric/ConfigMerger.java b/robolectric/src/main/java/org/robolectric/ConfigMerger.java deleted file mode 100644 index b2b5a8ae877..00000000000 --- a/robolectric/src/main/java/org/robolectric/ConfigMerger.java +++ /dev/null @@ -1,152 +0,0 @@ -package org.robolectric; - -import static com.google.common.collect.Lists.reverse; - -import com.google.common.annotations.VisibleForTesting; -import java.io.IOException; -import java.io.InputStream; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import org.robolectric.annotation.Config; -import org.robolectric.util.Join; - -/** - * Computes the effective Robolectric configuration for a given test method. - * - *

This class is no longer used directly by Robolectric, but is left for convenience during - * migration. - * - * @deprecated Provide an implementation of {@link javax.inject.Provider}. This class will - * be removed in Robolectric 4.3. - * @see Migration Notes for more - * details. - */ -@Deprecated -public class ConfigMerger { - private final Map packageConfigCache = new LinkedHashMap() { - @Override - protected boolean removeEldestEntry(Map.Entry eldest) { - return size() > 10; - } - }; - - /** - * Calculate the {@link Config} for the given test. - * - * @param testClass the class containing the test - * @param method the test method - * @param globalConfig global configuration values - * @return the effective configuration - * @since 3.2 - */ - public Config getConfig(Class testClass, Method method, Config globalConfig) { - Config config = Config.Builder.defaults().build(); - config = override(config, globalConfig); - - for (String packageName : reverse(packageHierarchyOf(testClass))) { - Config packageConfig = cachedPackageConfig(packageName); - config = override(config, packageConfig); - } - - for (Class clazz : reverse(parentClassesFor(testClass))) { - Config classConfig = (Config) clazz.getAnnotation(Config.class); - config = override(config, classConfig); - } - - Config methodConfig = method.getAnnotation(Config.class); - config = override(config, methodConfig); - - return config; - } - - /** - * Generate {@link Config} for the specified package. - * - * More specific packages, test classes, and test method configurations - * will override values provided here. - * - * The default implementation uses properties provided by {@link #getConfigProperties(String)}. - * - * The returned object is likely to be reused for many tests. - * - * @param packageName the name of the package, or empty string ({@code ""}) for the top level package - * @return {@link Config} object for the specified package - * @since 3.2 - */ - @Nullable - private Config buildPackageConfig(String packageName) { - return Config.Implementation.fromProperties(getConfigProperties(packageName)); - } - - /** - * Return a {@link Properties} file for the given package name, or {@code null} if none is available. - * - * @since 3.2 - */ - protected Properties getConfigProperties(String packageName) { - List packageParts = new ArrayList<>(Arrays.asList(packageName.split("\\."))); - packageParts.add(RobolectricTestRunner.CONFIG_PROPERTIES); - final String resourceName = Join.join("/", packageParts); - try (InputStream resourceAsStream = getResourceAsStream(resourceName)) { - if (resourceAsStream == null) return null; - Properties properties = new Properties(); - properties.load(resourceAsStream); - return properties; - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - @Nonnull @VisibleForTesting - List packageHierarchyOf(Class javaClass) { - Package aPackage = javaClass.getPackage(); - String testPackageName = aPackage == null ? "" : aPackage.getName(); - List packageHierarchy = new ArrayList<>(); - while (!testPackageName.isEmpty()) { - packageHierarchy.add(testPackageName); - int lastDot = testPackageName.lastIndexOf('.'); - testPackageName = lastDot > 1 ? testPackageName.substring(0, lastDot) : ""; - } - packageHierarchy.add(""); - return packageHierarchy; - } - - @Nonnull - private List parentClassesFor(Class testClass) { - List testClassHierarchy = new ArrayList<>(); - while (testClass != null && !testClass.equals(Object.class)) { - testClassHierarchy.add(testClass); - testClass = testClass.getSuperclass(); - } - return testClassHierarchy; - } - - private Config override(Config config, Config classConfig) { - return classConfig != null ? new Config.Builder(config).overlay(classConfig).build() : config; - } - - @Nullable - private Config cachedPackageConfig(String packageName) { - synchronized (packageConfigCache) { - Config config = packageConfigCache.get(packageName); - if (config == null && !packageConfigCache.containsKey(packageName)) { - config = buildPackageConfig(packageName); - packageConfigCache.put(packageName, config); - } - return config; - } - } - - // visible for testing - @SuppressWarnings("WeakerAccess") - InputStream getResourceAsStream(String resourceName) { - return getClass().getClassLoader().getResourceAsStream(resourceName); - } -} diff --git a/robolectric/src/main/java/org/robolectric/RobolectricTestRunner.java b/robolectric/src/main/java/org/robolectric/RobolectricTestRunner.java index def624b88a8..a6e07eae38f 100644 --- a/robolectric/src/main/java/org/robolectric/RobolectricTestRunner.java +++ b/robolectric/src/main/java/org/robolectric/RobolectricTestRunner.java @@ -50,7 +50,6 @@ import org.robolectric.pluginapi.config.ConfigurationStrategy; import org.robolectric.pluginapi.config.ConfigurationStrategy.Configuration; import org.robolectric.pluginapi.config.GlobalConfigProvider; -import org.robolectric.plugins.HierarchicalConfigurationStrategy.ConfigurationImpl; import org.robolectric.util.Logger; import org.robolectric.util.PerfStatsCollector; import org.robolectric.util.ReflectionHelpers; @@ -63,7 +62,11 @@ @SuppressWarnings("NewApi") public class RobolectricTestRunner extends SandboxTestRunner { - public static final String CONFIG_PROPERTIES = "robolectric.properties"; + /** + * @deprecated No longer used. This constant will be removed in a future version of Robolectric. + */ + @Deprecated public static final String CONFIG_PROPERTIES = "robolectric.properties"; + private static final Injector DEFAULT_INJECTOR = defaultInjector().build(); private static final Map appManifestsCache = new HashMap<>(); @@ -107,7 +110,7 @@ protected RobolectricTestRunner(final Class testClass, Injector injector) super(testClass, injector); if (DeprecatedTestRunnerDefaultConfigProvider.globalConfig == null) { - DeprecatedTestRunnerDefaultConfigProvider.globalConfig = buildGlobalConfig(); + DeprecatedTestRunnerDefaultConfigProvider.globalConfig = new Config.Builder().build(); } this.sandboxManager = injector.getInstance(SandboxManager.class); @@ -411,66 +414,9 @@ public static AndroidManifest createAndroidManifest(ManifestIdentifier manifestI manifestIdentifier.getApkFile()); } - /** - * Compute the effective Robolectric configuration for a given test method. - * - *

Configuration information is collected from package-level {@code robolectric.properties} - * files and {@link Config} annotations on test classes, superclasses, and methods. - * - *

Custom TestRunner subclasses may wish to override this method to provide alternate - * configuration. - * - * @param method the test method - * @return the effective Robolectric configuration for the given test method - * @deprecated Provide an implementation of {@link javax.inject.Provider} instead. This - * method will be removed in Robolectric 4.3. - * @since 2.0 - * @see Migration Notes for more - * details. - */ - @Deprecated - public Config getConfig(Method method) { - throw new UnsupportedOperationException(); - } - /** Calculate the configuration for a given test method. */ private Configuration getConfiguration(Method method) { - Configuration configuration = - configurationStrategy.getConfig(getTestClass().getJavaClass(), method); - - // in case #getConfig(Method) has been overridden... - try { - Config config = getConfig(method); - ((ConfigurationImpl) configuration).put(Config.class, config); - } catch (UnsupportedOperationException e) { - // no problem - } - - return configuration; - } - - /** - * Provides the base Robolectric configuration {@link Config} used for all tests. - * - *

Configuration provided for specific packages, test classes, and test method configurations - * will override values provided here. - * - *

Custom TestRunner subclasses may wish to override this method to provide alternate - * configuration. Consider using a {@link Config.Builder}. - * - *

The default implementation has appropriate values for most use cases. - * - * @return global {@link Config} object - * @deprecated Provide a service implementation of {@link GlobalConfigProvider} instead. This - * method will be removed in Robolectric 4.3. - * @since 3.1.3 - * @see Migration Notes for more - * details. - */ - @Deprecated - @SuppressWarnings("InlineMeSuggester") - protected Config buildGlobalConfig() { - return new Config.Builder().build(); + return configurationStrategy.getConfig(getTestClass().getJavaClass(), method); } @AutoService(GlobalConfigProvider.class) diff --git a/robolectric/src/test/java/org/robolectric/RobolectricTestRunnerTest.java b/robolectric/src/test/java/org/robolectric/RobolectricTestRunnerTest.java index 69b17a2f446..dfdb9d4957c 100644 --- a/robolectric/src/test/java/org/robolectric/RobolectricTestRunnerTest.java +++ b/robolectric/src/test/java/org/robolectric/RobolectricTestRunnerTest.java @@ -42,11 +42,9 @@ import org.junit.runner.notification.RunNotifier; import org.junit.runners.JUnit4; import org.junit.runners.MethodSorters; -import org.junit.runners.model.FrameworkMethod; import org.robolectric.RobolectricTestRunner.RobolectricFrameworkMethod; import org.robolectric.android.internal.AndroidTestEnvironment; import org.robolectric.annotation.Config; -import org.robolectric.annotation.Config.Implementation; import org.robolectric.annotation.experimental.LazyApplication; import org.robolectric.annotation.experimental.LazyApplication.LazyLoad; import org.robolectric.config.ConfigurationRegistry; @@ -142,20 +140,6 @@ public void testsWithUnsupportedSdkShouldBeIgnored() throws Exception { .inOrder(); } - @Test - public void supportsOldGetConfigUntil4dot3() throws Exception { - Implementation overriddenConfig = Config.Builder.defaults().build(); - List children = new SingleSdkRobolectricTestRunner(TestWithTwoMethods.class) { - @Override - public Config getConfig(Method method) { - return overriddenConfig; - } - }.getChildren(); - Config config = ((RobolectricFrameworkMethod) children.get(0)) - .getConfiguration().get(Config.class); - assertThat(config).isSameInstanceAs(overriddenConfig); - } - @Test public void failureInResetterDoesntBreakAllTests() throws Exception { RobolectricTestRunner runner = diff --git a/robolectric/src/test/java/org/robolectric/shadows/ShadowDisplayTest.java b/robolectric/src/test/java/org/robolectric/shadows/ShadowDisplayTest.java index 361312110ac..2806310dfdf 100644 --- a/robolectric/src/test/java/org/robolectric/shadows/ShadowDisplayTest.java +++ b/robolectric/src/test/java/org/robolectric/shadows/ShadowDisplayTest.java @@ -15,7 +15,6 @@ import android.graphics.Point; import android.graphics.Rect; import android.hardware.display.DisplayManagerGlobal; -import android.util.DisplayMetrics; import android.view.Display; import android.view.Display.HdrCapabilities; import android.view.DisplayCutout; @@ -38,46 +37,6 @@ public void setUp() throws Exception { shadow = Shadows.shadowOf(display); } - @Test - public void shouldProvideDisplayMetrics() { - shadow.setDensity(1.5f); - shadow.setDensityDpi(DisplayMetrics.DENSITY_HIGH); - shadow.setScaledDensity(1.6f); - shadow.setWidth(1024); - shadow.setHeight(600); - shadow.setRealWidth(1400); - shadow.setRealHeight(900); - shadow.setXdpi(183.0f); - shadow.setYdpi(184.0f); - shadow.setRefreshRate(123f); - - DisplayMetrics metrics = new DisplayMetrics(); - - display.getMetrics(metrics); - - assertEquals(1.5f, metrics.density, 0.05); - assertEquals(DisplayMetrics.DENSITY_HIGH, metrics.densityDpi); - assertEquals(1.6f, metrics.scaledDensity, 0.05); - assertEquals(1024, metrics.widthPixels); - assertEquals(600, metrics.heightPixels); - assertEquals(183.0f, metrics.xdpi, 0.05); - assertEquals(184.0f, metrics.ydpi, 0.05); - - metrics = new DisplayMetrics(); - - display.getRealMetrics(metrics); - - assertEquals(1.5f, metrics.density, 0.05); - assertEquals(DisplayMetrics.DENSITY_HIGH, metrics.densityDpi); - assertEquals(1.6f, metrics.scaledDensity, 0.05); - assertEquals(1400, metrics.widthPixels); - assertEquals(900, metrics.heightPixels); - assertEquals(183.0f, metrics.xdpi, 0.05); - assertEquals(184.0f, metrics.ydpi, 0.05); - - assertEquals(0, 123f, display.getRefreshRate()); - } - @Test public void changedStateShouldApplyToOtherInstancesOfSameDisplay() { shadow.setName("another name"); diff --git a/shadows/framework/src/main/java/org/robolectric/android/controller/ServiceController.java b/shadows/framework/src/main/java/org/robolectric/android/controller/ServiceController.java index 1c518236579..18329cee8d2 100644 --- a/shadows/framework/src/main/java/org/robolectric/android/controller/ServiceController.java +++ b/shadows/framework/src/main/java/org/robolectric/android/controller/ServiceController.java @@ -91,15 +91,4 @@ public ServiceController unbind() { shadowMainLooper.idleIfPaused(); return this; } - - /** - * @deprecated Use the appropriate builder in {@link org.robolectric.Robolectric} instead. - * - * This method will be removed in Robolectric 3.6. - */ - @Deprecated - public ServiceController withIntent(Intent intent) { - this.intent = intent; - return this; - } } diff --git a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowDisplay.java b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowDisplay.java index 97ab21cd1dc..d60fb5843c5 100644 --- a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowDisplay.java +++ b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowDisplay.java @@ -60,79 +60,6 @@ public static Display getDefaultDisplay() { private Integer rotation; private Integer pixelFormat; - /** - * If {@link #setScaledDensity(float)} has been called, {@link DisplayMetrics#scaledDensity} will - * be modified to reflect the value specified. Note that this is not a realistic state. - * - * @deprecated This behavior is deprecated and will be removed in Robolectric 3.7. - */ - @Deprecated - @Implementation - protected void getMetrics(DisplayMetrics outMetrics) { - reflector(_Display_.class, realObject).getMetrics(outMetrics); - if (scaledDensity != null) { - outMetrics.scaledDensity = scaledDensity; - } - } - - /** - * If {@link #setScaledDensity(float)} has been called, {@link DisplayMetrics#scaledDensity} will - * be modified to reflect the value specified. Note that this is not a realistic state. - * - * @deprecated This behavior is deprecated and will be removed in Robolectric 3.7. - */ - @Deprecated - @Implementation - protected void getRealMetrics(DisplayMetrics outMetrics) { - reflector(_Display_.class, realObject).getRealMetrics(outMetrics); - if (scaledDensity != null) { - outMetrics.scaledDensity = scaledDensity; - } - } - - /** - * If {@link #setDisplayId(int)} has been called, this method will return the specified value. - * - * @deprecated This behavior is deprecated and will be removed in Robolectric 3.7. - */ - @Deprecated - @Implementation - protected int getDisplayId() { - return displayId == null ? reflector(_Display_.class, realObject).getDisplayId() : displayId; - } - - /** - * If {@link #setRefreshRate(float)} has been called, this method will return the specified value. - * - * @deprecated This behavior is deprecated and will be removed in Robolectric 3.7. - */ - @Deprecated - @Implementation - protected float getRefreshRate() { - if (refreshRate != null) { - return refreshRate; - } - float realRefreshRate = reflector(_Display_.class, realObject).getRefreshRate(); - // refresh rate may be set by native code. if its 0, set to 60fps - if (realRefreshRate < 0.1) { - realRefreshRate = 60; - } - return realRefreshRate; - } - - /** - * If {@link #setPixelFormat(int)} has been called, this method will return the specified value. - * - * @deprecated This behavior is deprecated and will be removed in Robolectric 3.7. - */ - @Deprecated - @Implementation - protected int getPixelFormat() { - return pixelFormat == null - ? reflector(_Display_.class, realObject).getPixelFormat() - : pixelFormat; - } - @Implementation(maxSdk = JELLY_BEAN) protected void getSizeInternal(Point outSize, boolean doCompat) { outSize.x = width; @@ -193,29 +120,6 @@ public void setYdpi(float ydpi) { ShadowDisplayManager.changeDisplay(realObject.getDisplayId(), di -> di.physicalYDpi = ydpi); } - /** - * Changes the scaled density for this display. - * - * @deprecated This method is deprecated and will be removed in Robolectric 3.7. - */ - @Deprecated - public void setScaledDensity(float scaledDensity) { - this.scaledDensity = scaledDensity; - } - - /** - * Changes the ID for this display. - * - *

Any registered {@link android.hardware.display.DisplayManager.DisplayListener}s will be - * notified of the change. - * - * @deprecated This method is deprecated and will be removed in Robolectric 3.7. - */ - @Deprecated - public void setDisplayId(int displayId) { - this.displayId = displayId; - } - /** * Changes the name for this display. * @@ -309,16 +213,6 @@ public void setRotation(int rotation) { ShadowDisplayManager.changeDisplay(realObject.getDisplayId(), di -> di.rotation = rotation); } - /** - * Changes the pixel format for this display. - * - * @deprecated This method is deprecated and will be removed in Robolectric 3.7. - */ - @Deprecated - public void setPixelFormat(int pixelFormat) { - this.pixelFormat = pixelFormat; - } - /** * Changes the simulated state for this display, such as whether it is on or off *