Skip to content

Commit 173858b

Browse files
manny-jimenezManny Jimenez
and
Manny Jimenez
authored
New build version now includes different version name (#3259)
* New build version now includes different version name * Fixing tests * Responding to comments * Responding to comments part 2 * Adding try catch to getPackageInfo Co-authored-by: Manny Jimenez <[email protected]>
1 parent 15f59af commit 173858b

File tree

2 files changed

+49
-15
lines changed

2 files changed

+49
-15
lines changed

firebase-app-distribution/src/main/java/com/google/firebase/app/distribution/NewReleaseFetcher.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ AppDistributionReleaseInternal getNewReleaseFromClient(
135135
}
136136

137137
if (isNewerBuildVersion(retrievedNewRelease)
138-
|| !isSameAsInstalledRelease(retrievedNewRelease)) {
138+
|| !isSameAsInstalledRelease(retrievedNewRelease)
139+
|| hasDifferentAppVersionName(retrievedNewRelease)) {
139140
return retrievedNewRelease;
140141
} else {
141142
// Return null if retrieved new release is older or currently installed
@@ -163,6 +164,13 @@ private boolean isNewerBuildVersion(AppDistributionReleaseInternal newRelease)
163164
> getInstalledAppVersionCode(firebaseApp.getApplicationContext());
164165
}
165166

167+
private boolean hasDifferentAppVersionName(AppDistributionReleaseInternal newRelease)
168+
throws FirebaseAppDistributionException {
169+
return !newRelease
170+
.getDisplayVersion()
171+
.equals(getInstalledAppVersionName(firebaseApp.getApplicationContext()));
172+
}
173+
166174
@VisibleForTesting
167175
boolean isSameAsInstalledRelease(AppDistributionReleaseInternal newRelease)
168176
throws FirebaseAppDistributionException {
@@ -182,17 +190,25 @@ boolean isSameAsInstalledRelease(AppDistributionReleaseInternal newRelease)
182190
}
183191

184192
private long getInstalledAppVersionCode(Context context) throws FirebaseAppDistributionException {
185-
PackageInfo pInfo;
193+
return PackageInfoCompat.getLongVersionCode(getPackageInfo(context));
194+
}
195+
196+
private String getInstalledAppVersionName(Context context)
197+
throws FirebaseAppDistributionException {
198+
return getPackageInfo(context).versionName;
199+
}
200+
201+
private PackageInfo getPackageInfo(Context context) throws FirebaseAppDistributionException {
186202
try {
187-
pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
203+
return context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
188204
} catch (PackageManager.NameNotFoundException e) {
189-
LogWrapper.getInstance().e(TAG + "Unable to locate Firebase App.", e);
205+
LogWrapper.getInstance()
206+
.e(TAG + "Unable to find package with name " + context.getPackageName(), e);
190207
throw new FirebaseAppDistributionException(
191208
Constants.ErrorMessages.UNKNOWN_ERROR,
192209
FirebaseAppDistributionException.Status.UNKNOWN,
193210
e);
194211
}
195-
return PackageInfoCompat.getLongVersionCode(pInfo);
196212
}
197213

198214
@VisibleForTesting

firebase-app-distribution/src/test/java/com/google/firebase/app/distribution/NewReleaseFetcherTest.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public void setup() {
118118
.setApplicationInfo(applicationInfo)
119119
.build();
120120
packageInfo.setLongVersionCode(INSTALLED_VERSION_CODE);
121+
packageInfo.versionName = "1.0";
121122
shadowPackageManager.installPackage(packageInfo);
122123

123124
newReleaseFetcher =
@@ -265,25 +266,42 @@ public void getNewReleaseFromClient_whenNewReleaseIsLowerVersionCode_returnsNull
265266

266267
@Test
267268
public void handleNewReleaseFromClient_whenNewAabIsAvailable_returnsRelease() throws Exception {
269+
AppDistributionReleaseInternal expectedRelease =
270+
getTestNewRelease()
271+
.setDownloadUrl("http://fake-download-url")
272+
.setIasArtifactId("test-ias-artifact-id-2")
273+
.setBinaryType(BinaryType.AAB)
274+
.build();
268275
when(mockFirebaseAppDistributionTesterApiClient.fetchNewRelease(
269276
any(), any(), any(), any(), any()))
270-
.thenReturn(
271-
getTestNewRelease()
272-
.setDownloadUrl("http://fake-download-url")
273-
.setIasArtifactId("test-ias-artifact-id-2")
274-
.setBinaryType(BinaryType.AAB)
275-
.build());
277+
.thenReturn(expectedRelease);
276278

277279
AppDistributionReleaseInternal result =
278280
newReleaseFetcher.getNewReleaseFromClient(
279281
TEST_FID_1, TEST_APP_ID_1, TEST_API_KEY, TEST_AUTH_TOKEN);
280-
assertEquals(
282+
283+
assertEquals(expectedRelease, result);
284+
}
285+
286+
@Test
287+
public void
288+
handleNewReleaseFromClient_whenNewReleaseHasDifferentVersionNameThanInstalled_returnsRelease()
289+
throws Exception {
290+
AppDistributionReleaseInternal expectedRelease =
281291
getTestNewRelease()
282292
.setDownloadUrl("http://fake-download-url")
283-
.setIasArtifactId("test-ias-artifact-id-2")
293+
.setIasArtifactId(TEST_IAS_ARTIFACT_ID)
284294
.setBinaryType(BinaryType.AAB)
285-
.build(),
286-
result);
295+
.setDisplayVersion("2.0")
296+
.build();
297+
when(mockFirebaseAppDistributionTesterApiClient.fetchNewRelease(
298+
any(), any(), any(), any(), any()))
299+
.thenReturn(expectedRelease);
300+
301+
AppDistributionReleaseInternal result =
302+
newReleaseFetcher.getNewReleaseFromClient(
303+
TEST_FID_1, TEST_APP_ID_1, TEST_API_KEY, TEST_AUTH_TOKEN);
304+
assertEquals(expectedRelease, result);
287305
}
288306

289307
@Test

0 commit comments

Comments
 (0)