Skip to content

Commit aa17350

Browse files
MGaetan89hoisie
authored andcommitted
Revert "Remove deprecated Config#packageName()"
This reverts commit 521031c.
1 parent 49a8c19 commit aa17350

File tree

6 files changed

+83
-4
lines changed

6 files changed

+83
-4
lines changed

annotations/src/main/java/org/robolectric/annotation/Config.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
String DEFAULT_MANIFEST_NAME = "AndroidManifest.xml";
3939
Class<? extends Application> DEFAULT_APPLICATION = DefaultApplication.class;
40+
String DEFAULT_PACKAGE_NAME = "";
4041
String DEFAULT_QUALIFIERS = "";
4142
String DEFAULT_RES_FOLDER = "res";
4243
String DEFAULT_ASSET_FOLDER = "assets";
@@ -87,6 +88,26 @@
8788
Class<? extends Application> application() default
8889
DefaultApplication.class; // DEFAULT_APPLICATION
8990

91+
/**
92+
* Java package name where the "R.class" file is located. This only needs to be specified if you
93+
* define an {@code applicationId} associated with {@code productFlavors} or specify {@code
94+
* applicationIdSuffix} in your build.gradle.
95+
*
96+
* <p>If not specified, Robolectric defaults to the {@code applicationId}.
97+
*
98+
* @return The java package name for R.class.
99+
* @deprecated To change your package name please override the applicationId in your build system.
100+
* Changing package name here is broken as the package name will no longer match the package
101+
* name encoded in the arsc resources file. If you are looking to simulate another application
102+
* you can create another applications Context using {@link
103+
* android.content.Context#createPackageContext(String, int)}. Note that you must add this
104+
* package to {@link
105+
* org.robolectric.shadows.ShadowPackageManager#addPackage(android.content.pm.PackageInfo)}
106+
* first.
107+
*/
108+
@Deprecated
109+
String packageName() default DEFAULT_PACKAGE_NAME;
110+
90111
/**
91112
* Qualifiers specifying device configuration for this test, such as "fr-normal-port-hdpi".
92113
*
@@ -158,6 +179,7 @@ class Implementation implements Config {
158179
private final String qualifiers;
159180
private final String resourceDir;
160181
private final String assetDir;
182+
private final String packageName;
161183
private final Class<?>[] shadows;
162184
private final String[] instrumentedPackages;
163185
private final Class<? extends Application> application;
@@ -172,6 +194,7 @@ public static Config fromProperties(Properties properties) {
172194
properties.getProperty("manifest", DEFAULT_VALUE_STRING),
173195
properties.getProperty("qualifiers", DEFAULT_QUALIFIERS),
174196
Float.parseFloat(properties.getProperty("fontScale", "1.0f")),
197+
properties.getProperty("packageName", DEFAULT_PACKAGE_NAME),
175198
properties.getProperty("resourceDir", DEFAULT_RES_FOLDER),
176199
properties.getProperty("assetDir", DEFAULT_ASSET_FOLDER),
177200
parseClasses(properties.getProperty("shadows", "")),
@@ -272,6 +295,7 @@ public Implementation(
272295
String manifest,
273296
String qualifiers,
274297
float fontScale,
298+
String packageName,
275299
String resourceDir,
276300
String assetDir,
277301
Class<?>[] shadows,
@@ -284,6 +308,7 @@ public Implementation(
284308
this.manifest = manifest;
285309
this.qualifiers = qualifiers;
286310
this.fontScale = fontScale;
311+
this.packageName = packageName;
287312
this.resourceDir = resourceDir;
288313
this.assetDir = assetDir;
289314
this.shadows = shadows;
@@ -329,6 +354,11 @@ public String qualifiers() {
329354
return qualifiers;
330355
}
331356

357+
@Override
358+
public String packageName() {
359+
return packageName;
360+
}
361+
332362
@Override
333363
public String resourceDir() {
334364
return resourceDir;
@@ -381,6 +411,9 @@ public String toString() {
381411
+ ", assetDir='"
382412
+ assetDir
383413
+ '\''
414+
+ ", packageName='"
415+
+ packageName
416+
+ '\''
384417
+ ", shadows="
385418
+ Arrays.toString(shadows)
386419
+ ", instrumentedPackages="
@@ -400,6 +433,7 @@ class Builder {
400433
protected float fontScale = 1.0f;
401434
protected String manifest = Config.DEFAULT_VALUE_STRING;
402435
protected String qualifiers = Config.DEFAULT_QUALIFIERS;
436+
protected String packageName = Config.DEFAULT_PACKAGE_NAME;
403437
protected String resourceDir = Config.DEFAULT_RES_FOLDER;
404438
protected String assetDir = Config.DEFAULT_ASSET_FOLDER;
405439
protected Class<?>[] shadows = new Class[0];
@@ -416,6 +450,7 @@ public Builder(Config config) {
416450
manifest = config.manifest();
417451
qualifiers = config.qualifiers();
418452
fontScale = config.fontScale();
453+
packageName = config.packageName();
419454
resourceDir = config.resourceDir();
420455
assetDir = config.assetDir();
421456
shadows = config.shadows();
@@ -449,6 +484,11 @@ public Builder setQualifiers(String qualifiers) {
449484
return this;
450485
}
451486

487+
public Builder setPackageName(String packageName) {
488+
this.packageName = packageName;
489+
return this;
490+
}
491+
452492
public Builder setResourceDir(String resourceDir) {
453493
this.resourceDir = resourceDir;
454494
return this;
@@ -528,6 +568,7 @@ public Builder overlay(Config overlayConfig) {
528568
}
529569
}
530570

571+
this.packageName = pick(this.packageName, overlayConfig.packageName(), "");
531572
this.resourceDir =
532573
pick(this.resourceDir, overlayConfig.resourceDir(), Config.DEFAULT_RES_FOLDER);
533574
this.assetDir = pick(this.assetDir, overlayConfig.assetDir(), Config.DEFAULT_ASSET_FOLDER);
@@ -568,6 +609,7 @@ public Implementation build() {
568609
manifest,
569610
qualifiers,
570611
fontScale,
612+
packageName,
571613
resourceDir,
572614
assetDir,
573615
shadows,

robolectric/src/main/java/org/robolectric/internal/DefaultManifestFactory.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public ManifestIdentifier identify(Config config) {
4242
assetsDir = getResource(config.assetDir());
4343
}
4444

45+
if (!Config.DEFAULT_PACKAGE_NAME.equals(config.packageName())) {
46+
packageName = config.packageName();
47+
}
48+
4549
List<ManifestIdentifier> libraryDirs = emptyList();
4650
if (config.libraries().length > 0) {
4751
Logger.info("@Config(libraries) specified while using Build System API, ignoring");

robolectric/src/test/java/org/robolectric/ManifestFactoryTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ protected Properties getBuildSystemApiProperties() {
7171
};
7272

7373
Config.Implementation config =
74-
Config.Builder.defaults().setManifest("TestAndroidManifest.xml").build();
74+
Config.Builder.defaults()
75+
.setManifest("TestAndroidManifest.xml")
76+
.setPackageName("another.package")
77+
.build();
7578
ManifestFactory manifestFactory = testRunner.getManifestFactory(config);
7679
assertThat(manifestFactory).isInstanceOf(DefaultManifestFactory.class);
7780
ManifestIdentifier manifestIdentifier = manifestFactory.identify(config);
@@ -80,6 +83,6 @@ protected Properties getBuildSystemApiProperties() {
8083
assertThat(manifestIdentifier.getResDir()).isEqualTo(Paths.get("/path/to/merged-resources"));
8184
assertThat(manifestIdentifier.getAssetDir()).isEqualTo(Paths.get("/path/to/merged-assets"));
8285
assertThat(manifestIdentifier.getLibraries()).isEmpty();
83-
assertThat(manifestIdentifier.getPackageName()).isNull();
86+
assertThat(manifestIdentifier.getPackageName()).isEqualTo("another.package");
8487
}
8588
}

robolectric/src/test/java/org/robolectric/internal/DefaultManifestFactoryTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,14 @@ public void identify_packageCanBeOverriddenFromConfig() {
9191
properties.put("android_merged_resources", "gradle/res");
9292
properties.put("android_merged_assets", "gradle/assets");
9393
DefaultManifestFactory factory = new DefaultManifestFactory(properties);
94-
ManifestIdentifier identifier = factory.identify(Config.Builder.defaults().build());
94+
ManifestIdentifier identifier =
95+
factory.identify(Config.Builder.defaults().setPackageName("overridden.package").build());
9596
AndroidManifest manifest = RobolectricTestRunner.createAndroidManifest(identifier);
9697

9798
assertThat(manifest.getAndroidManifestFile())
9899
.isEqualTo(Paths.get("gradle/AndroidManifest.xml"));
99100
assertThat(manifest.getResDirectory()).isEqualTo(Paths.get("gradle/res"));
100101
assertThat(manifest.getAssetsDirectory()).isEqualTo(Paths.get("gradle/assets"));
101-
assertThat(manifest.getRClassName()).isEqualTo("org.robolectric.default.R");
102+
assertThat(manifest.getRClassName()).isEqualTo("overridden.package.R");
102103
}
103104
}

0 commit comments

Comments
 (0)