Ensure that the suggestedVersionCode is updated after [un]installing.

Previously, it was only done on repo update.

Now it is done whenever an app is installed or unisntalled. The query to
update the suggested version for each app is quite slow when run at the
end of a repo update. However in this change, we are limiting the query
to only update a single app, which means that performance should not be
a problem.
This commit is contained in:
Peter Serwylo
2017-06-30 14:21:25 +10:00
parent 677fd3a522
commit bf4b0d89a1
6 changed files with 124 additions and 30 deletions

View File

@@ -8,6 +8,7 @@ import android.content.ContextWrapper;
import android.content.pm.ProviderInfo;
import org.fdroid.fdroid.data.App;
import org.fdroid.fdroid.data.AppProvider;
import org.fdroid.fdroid.data.Repo;
import org.fdroid.fdroid.data.RepoProvider;
import org.fdroid.fdroid.data.RepoProviderTest;
@@ -157,4 +158,15 @@ public class TestUtils {
}
};
}
/**
* Normally apps/apks are only added to the database in response to a repo update.
* At the end of a repo update, the {@link AppProvider} updates the suggested apks and
* recalculates the preferred metadata for each app. Because we are adding apps/apks
* directly to the database, we need to simulate this update after inserting stuff.
*/
public static void updateDbAfterInserting(Context context) {
AppProvider.Helper.calcSuggestedApks(context);
AppProvider.Helper.recalculatePreferredMetadata(context);
}
}

View File

@@ -25,6 +25,14 @@ public class PreferredSignatureTest extends FDroidProviderTest {
public void setup() {
TestUtils.registerContentProvider(AppProvider.getAuthority(), AppProvider.class);
Preferences.setup(context);
// This is what the FDroidApp does when this preference is changed. Need to also do this under testing.
Preferences.get().registerUnstableUpdatesChangeListener(new Preferences.ChangeListener() {
@Override
public void onPreferenceChange() {
AppProvider.Helper.calcSuggestedApks(context);
}
});
}
@After
@@ -46,6 +54,8 @@ public class PreferredSignatureTest extends FDroidProviderTest {
TestUtils.insertApk(context, app, 2100, TestUtils.UPSTREAM_SIG); // 2.0
TestUtils.insertApk(context, app, 3100, TestUtils.UPSTREAM_SIG); // 3.0
TestUtils.updateDbAfterInserting(context);
return app;
}
@@ -70,6 +80,8 @@ public class PreferredSignatureTest extends FDroidProviderTest {
TestUtils.insertApk(context, app, 5002, TestUtils.THIRD_PARTY_SIG); // 5.0-rc2
TestUtils.insertApk(context, app, 5003, TestUtils.THIRD_PARTY_SIG); // 5.0-rc3
TestUtils.updateDbAfterInserting(context);
return app;
}
@@ -84,6 +96,8 @@ public class PreferredSignatureTest extends FDroidProviderTest {
TestUtils.insertApk(context, app, 3100, TestUtils.UPSTREAM_SIG);
TestUtils.insertApk(context, app, 4100, TestUtils.UPSTREAM_SIG);
TestUtils.updateDbAfterInserting(context);
return app;
}
@@ -265,9 +279,6 @@ public class PreferredSignatureTest extends FDroidProviderTest {
}
private void assertSuggested(Context context, int suggestedVersion, String suggestedSig) {
AppProvider.Helper.calcSuggestedApks(context);
AppProvider.Helper.recalculatePreferredMetadata(context);
App suggestedApp = AppProvider.Helper.findHighestPriorityMetadata(context.getContentResolver(), PACKAGE_NAME);
assertEquals("Suggested version on App", suggestedVersion, suggestedApp.suggestedVersionCode);

View File

@@ -26,6 +26,14 @@ public class SuggestedVersionTest extends FDroidProviderTest {
public void setup() {
TestUtils.registerContentProvider(AppProvider.getAuthority(), AppProvider.class);
Preferences.setup(context);
// This is what the FDroidApp does when this preference is changed. Need to also do this under testing.
Preferences.get().registerUnstableUpdatesChangeListener(new Preferences.ChangeListener() {
@Override
public void onPreferenceChange() {
AppProvider.Helper.calcSuggestedApks(context);
}
});
}
@After
@@ -40,6 +48,7 @@ public class SuggestedVersionTest extends FDroidProviderTest {
TestUtils.insertApk(context, singleApp, 1, TestUtils.FDROID_SIG);
TestUtils.insertApk(context, singleApp, 2, TestUtils.FDROID_SIG);
TestUtils.insertApk(context, singleApp, 3, TestUtils.FDROID_SIG);
TestUtils.updateDbAfterInserting(context);
assertSuggested("single.app", 2);
// By enabling unstable updates, the "upstreamVersionCode" should get ignored, and we should
@@ -59,6 +68,7 @@ public class SuggestedVersionTest extends FDroidProviderTest {
TestUtils.insertApk(context, singleApp, 3, TestUtils.FDROID_SIG);
TestUtils.insertApk(context, singleApp, 4, TestUtils.UPSTREAM_SIG);
TestUtils.insertApk(context, singleApp, 5, TestUtils.UPSTREAM_SIG);
TestUtils.updateDbAfterInserting(context);
// Given we aren't installed yet, we don't care which signature.
// Just get as close to upstreamVersionCode as possible.
@@ -72,6 +82,7 @@ public class SuggestedVersionTest extends FDroidProviderTest {
// This adds the "upstreamVersionCode" version of the app, but signed by f-droid.
TestUtils.insertApk(context, singleApp, 4, TestUtils.FDROID_SIG);
TestUtils.insertApk(context, singleApp, 5, TestUtils.FDROID_SIG);
TestUtils.updateDbAfterInserting(context);
assertSuggested("single.app", 4, TestUtils.FDROID_SIG, 1);
// Version 5 from F-Droid is not the "upstreamVersionCode", but with beta updates it should
@@ -99,6 +110,7 @@ public class SuggestedVersionTest extends FDroidProviderTest {
TestUtils.insertApk(context, thirdPartyApp, 4, TestUtils.THIRD_PARTY_SIG);
TestUtils.insertApk(context, thirdPartyApp, 5, TestUtils.THIRD_PARTY_SIG);
TestUtils.insertApk(context, thirdPartyApp, 6, TestUtils.THIRD_PARTY_SIG);
TestUtils.updateDbAfterInserting(context);
// Given we aren't installed yet, we don't care which signature or even which repo.
// Just get as close to upstreamVersionCode as possible.
@@ -112,6 +124,7 @@ public class SuggestedVersionTest extends FDroidProviderTest {
// This adds the "upstreamVersionCode" version of the app, but signed by f-droid.
TestUtils.insertApk(context, mainApp, 4, TestUtils.FDROID_SIG);
TestUtils.insertApk(context, mainApp, 5, TestUtils.FDROID_SIG);
TestUtils.updateDbAfterInserting(context);
assertSuggested("single.app", 4, TestUtils.FDROID_SIG, 1);
// Uninstalling the F-Droid build and installing v3 of the third party means we can now go
@@ -146,6 +159,7 @@ public class SuggestedVersionTest extends FDroidProviderTest {
TestUtils.insertApk(context, mainApp, 5, TestUtils.UPSTREAM_SIG);
TestUtils.insertApk(context, mainApp, 6, TestUtils.UPSTREAM_SIG);
TestUtils.insertApk(context, mainApp, 7, TestUtils.UPSTREAM_SIG);
TestUtils.updateDbAfterInserting(context);
// If the user was to manually install the app, they should be suggested version 7 from upstream...
assertSuggested("single.app", 7);
@@ -180,9 +194,6 @@ public class SuggestedVersionTest extends FDroidProviderTest {
* apk is not checked.
*/
public void assertSuggested(String packageName, int suggestedVersion, String installedSig, int installedVersion) {
AppProvider.Helper.calcSuggestedApks(context);
AppProvider.Helper.recalculatePreferredMetadata(context);
App suggestedApp = AppProvider.Helper.findHighestPriorityMetadata(context.getContentResolver(), packageName);
assertEquals("Suggested version on App", suggestedVersion, suggestedApp.suggestedVersionCode);
assertEquals("Installed signature on App", installedSig, suggestedApp.installedSig);