Skip to content

Commit

Permalink
Fix Gradle integration of the `:integration_tests:testparameterinject…
Browse files Browse the repository at this point in the history
…or` module

This test is expected to be independent of the API levels.
However, on GitHub Actions, we inject the API levels to test.
This commit ensures that this test isn't impacted by those properties.
  • Loading branch information
MGaetan89 authored and utzcoz committed Nov 15, 2024
1 parent 4276e0d commit b82eff8
Showing 1 changed file with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.robolectric.integrationtests.testparameterinjector;

import static android.os.Build.VERSION_CODES.P;
import static android.os.Build.VERSION_CODES.S;
import static com.google.common.truth.Truth.assertThat;

import android.os.Build.VERSION;
import com.google.testing.junit.testparameterinjector.TestParameter;
import java.util.ArrayList;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
Expand All @@ -22,10 +24,19 @@
@SuppressWarnings({"TestMethodWithIncorrectSignature", "UnconstructableJUnitTestCase"})
@RunWith(JUnit4.class)
public class RobolectricTestParameterInjectorTest {
private final RunNotifier runNotifier = new RunNotifier();
private String priorAlwaysInclude;
private String priorEnabledSdks;
private RunNotifier runNotifier;

@Before
public void setup() {
priorAlwaysInclude = System.getProperty("robolectric.alwaysIncludeVariantMarkersInTestName");
System.clearProperty("robolectric.alwaysIncludeVariantMarkersInTestName");

priorEnabledSdks = System.getProperty("robolectric.enabledSdks");
System.clearProperty("robolectric.enabledSdks");

runNotifier = new RunNotifier();
runNotifier.addListener(
new RunListener() {
@Override
Expand All @@ -35,6 +46,17 @@ public void testFailure(Failure failure) throws Exception {
});
}

@After
public void tearDown() {
if (priorAlwaysInclude != null) {
System.setProperty("robolectric.alwaysIncludeVariantMarkersInTestName", priorAlwaysInclude);
}

if (priorEnabledSdks != null) {
System.setProperty("robolectric.enabledSdks", priorEnabledSdks);
}
}

@Ignore
public static class NoInjection {
@Config(sdk = S)
Expand Down Expand Up @@ -70,7 +92,7 @@ public void injectedMethod() throws Exception {

assertThat(runner.testCount()).isEqualTo(2);
ArrayList<Description> descriptions = runner.getDescription().getChildren();
// In gradle it's test[false], in bazel it's test[param=false].
// In Gradle it's test[false], in Bazel it's test[param=false].
assertThat(descriptions.get(0).getMethodName()).matches("test\\[(param=)?false\\]");
assertThat(descriptions.get(1).getMethodName()).matches("test\\[(param=)?true\\]");
}
Expand Down Expand Up @@ -123,15 +145,15 @@ public void injectedConstructor() throws Exception {
runner.run(runNotifier);
assertThat(runner.testCount()).isEqualTo(2);
ArrayList<Description> descriptions = runner.getDescription().getChildren();
// In gradle it's test[1], in bazel it's test[param=1].
// In Gradle it's test[1], in Bazel it's test[param=1].
assertThat(descriptions.get(0).getMethodName()).matches("test\\[(param=)?1\\]");
assertThat(descriptions.get(1).getMethodName()).matches("test\\[(param=)?2\\]");
}

@Ignore
@Config(sdk = Config.NEWEST_SDK)
public static class InjectedEnum {
enum Value {
public enum Value {
ONE,
TWO
}
Expand All @@ -158,10 +180,10 @@ public void injectedEnum() throws Exception {
@Ignore
public static class MultiSdk {
@Test
@Config(sdk = {28, 31})
@Config(sdk = {P, S})
public void test(@TestParameter boolean param) {
assertThat(param).isAnyOf(true, false);
assertThat(VERSION.SDK_INT).isAnyOf(28, 31);
assertThat(VERSION.SDK_INT).isAnyOf(P, S);
}
}

Expand All @@ -174,19 +196,19 @@ public void multiSdk() throws Exception {
assertThat(runner.testCount()).isEqualTo(4);

ArrayList<Description> descriptions = runner.getDescription().getChildren();
// In gradle it's test[false][28], in bazel it's test[param=false][28].
// In Gradle it's test[false][28], in Bazel it's test[param=false][28].
assertThat(descriptions.get(0).getMethodName()).matches("test\\[(param=)?false\\]\\[28\\]");
assertThat(descriptions.get(1).getMethodName()).matches("test\\[(param=)?true\\]\\[28\\]");
assertThat(descriptions.get(2).getMethodName()).matches("test\\[(param=)?false\\]");
assertThat(descriptions.get(3).getMethodName()).matches("test\\[(param=)?true\\]");
}

// Simulate behavior of proto lite enum toString which includes the object hashcode (proto lite
// toString tries to avoid dep on enum name so that the name can be stripped by appreduce).
// Simulate the behavior of proto lite enum toString which includes the object hashcode (proto
// lite toString tries to avoid dep on enum name so that the name can be stripped by appreduce).
@Ignore
@Config(sdk = Config.NEWEST_SDK)
public static class HashCodeToString {
enum HashCodeToStringValue {
public enum HashCodeToStringValue {
ONE,
TWO;

Expand Down

0 comments on commit b82eff8

Please sign in to comment.