Fixed broken + commented out tests.
They were all due to the addition of "application label" to the installed app cache. This commit adds a mock ApplicationInfo to the mock package manager and also specifies the label while inserting into the test content provider.
This commit is contained in:
parent
9dfa18aead
commit
a16bc22c4a
19
F-Droid/test/src/mock/MockApplicationInfo.java
Normal file
19
F-Droid/test/src/mock/MockApplicationInfo.java
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package mock;
|
||||||
|
|
||||||
|
import android.content.pm.ApplicationInfo;
|
||||||
|
import android.content.pm.PackageInfo;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
|
|
||||||
|
public class MockApplicationInfo extends ApplicationInfo {
|
||||||
|
|
||||||
|
private final PackageInfo info;
|
||||||
|
|
||||||
|
public MockApplicationInfo(PackageInfo info) {
|
||||||
|
this.info = info;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CharSequence loadLabel(PackageManager pm) {
|
||||||
|
return "Mock app: " + info.packageName;
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
package mock;
|
package mock;
|
||||||
|
|
||||||
|
import android.content.pm.ApplicationInfo;
|
||||||
import android.content.pm.PackageInfo;
|
import android.content.pm.PackageInfo;
|
||||||
import android.test.mock.MockPackageManager;
|
import android.test.mock.MockPackageManager;
|
||||||
|
|
||||||
@ -9,7 +10,7 @@ import java.util.List;
|
|||||||
|
|
||||||
public class MockInstallablePackageManager extends MockPackageManager {
|
public class MockInstallablePackageManager extends MockPackageManager {
|
||||||
|
|
||||||
private List<PackageInfo> info = new ArrayList<PackageInfo>();
|
private List<PackageInfo> info = new ArrayList<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<PackageInfo> getInstalledPackages(int flags) {
|
public List<PackageInfo> getInstalledPackages(int flags) {
|
||||||
@ -30,6 +31,11 @@ public class MockInstallablePackageManager extends MockPackageManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ApplicationInfo getApplicationInfo(String packageName, int flags) throws NameNotFoundException {
|
||||||
|
return new MockApplicationInfo(getPackageInfo(packageName));
|
||||||
|
}
|
||||||
|
|
||||||
public PackageInfo getPackageInfo(String id) {
|
public PackageInfo getPackageInfo(String id) {
|
||||||
for (PackageInfo i : info) {
|
for (PackageInfo i : info) {
|
||||||
if (i.packageName.equals(id)) {
|
if (i.packageName.equals(id)) {
|
||||||
|
@ -1,14 +1,18 @@
|
|||||||
package org.fdroid.fdroid;
|
package org.fdroid.fdroid;
|
||||||
|
|
||||||
|
import android.content.ContentResolver;
|
||||||
import android.content.ContentValues;
|
import android.content.ContentValues;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
|
|
||||||
import mock.MockCategoryResources;
|
import mock.MockCategoryResources;
|
||||||
|
import mock.MockContextSwappableComponents;
|
||||||
|
import mock.MockInstallablePackageManager;
|
||||||
|
|
||||||
import org.fdroid.fdroid.data.ApkProvider;
|
import org.fdroid.fdroid.data.ApkProvider;
|
||||||
import org.fdroid.fdroid.data.App;
|
import org.fdroid.fdroid.data.App;
|
||||||
import org.fdroid.fdroid.data.AppProvider;
|
import org.fdroid.fdroid.data.AppProvider;
|
||||||
|
import org.fdroid.fdroid.data.InstalledAppCacheUpdater;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -43,7 +47,6 @@ public class AppProviderTest extends FDroidProviderTest<AppProvider> {
|
|||||||
* the AppProvider used to stumble across this bug when asking for installed apps,
|
* the AppProvider used to stumble across this bug when asking for installed apps,
|
||||||
* and the device had over 1000 apps installed.
|
* and the device had over 1000 apps installed.
|
||||||
*/
|
*/
|
||||||
/* TODO fix me
|
|
||||||
public void testMaxSqliteParams() {
|
public void testMaxSqliteParams() {
|
||||||
|
|
||||||
MockInstallablePackageManager pm = new MockInstallablePackageManager();
|
MockInstallablePackageManager pm = new MockInstallablePackageManager();
|
||||||
@ -74,7 +77,7 @@ public class AppProviderTest extends FDroidProviderTest<AppProvider> {
|
|||||||
|
|
||||||
assertResultCount(3, AppProvider.getInstalledUri());
|
assertResultCount(3, AppProvider.getInstalledUri());
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
public void testCantFindApp() {
|
public void testCantFindApp() {
|
||||||
assertNull(AppProvider.Helper.findById(getMockContentResolver(), "com.example.doesnt-exist"));
|
assertNull(AppProvider.Helper.findById(getMockContentResolver(), "com.example.doesnt-exist"));
|
||||||
}
|
}
|
||||||
@ -92,7 +95,7 @@ public class AppProviderTest extends FDroidProviderTest<AppProvider> {
|
|||||||
App app = new App();
|
App app = new App();
|
||||||
app.id = "org.fdroid.fdroid";
|
app.id = "org.fdroid.fdroid";
|
||||||
|
|
||||||
List<App> apps = new ArrayList<App>(1);
|
List<App> apps = new ArrayList<>(1);
|
||||||
apps.add(app);
|
apps.add(app);
|
||||||
|
|
||||||
assertValidUri(AppProvider.getContentUri(app));
|
assertValidUri(AppProvider.getContentUri(app));
|
||||||
@ -105,7 +108,6 @@ public class AppProviderTest extends FDroidProviderTest<AppProvider> {
|
|||||||
assertNotNull(cursor);
|
assertNotNull(cursor);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO fix me
|
|
||||||
private void insertApps(int count) {
|
private void insertApps(int count) {
|
||||||
for (int i = 0; i < count; i ++) {
|
for (int i = 0; i < count; i ++) {
|
||||||
insertApp("com.example.test." + i, "Test app " + i);
|
insertApp("com.example.test." + i, "Test app " + i);
|
||||||
@ -122,7 +124,7 @@ public class AppProviderTest extends FDroidProviderTest<AppProvider> {
|
|||||||
values.put(AppProvider.DataColumns.IGNORE_THISUPDATE, ignoreVercode);
|
values.put(AppProvider.DataColumns.IGNORE_THISUPDATE, ignoreVercode);
|
||||||
insertApp(id, "App: " + id, values);
|
insertApp(id, "App: " + id, values);
|
||||||
|
|
||||||
TestUtils.installAndBroadcast(getMockContext(), packageManager, id, installedVercode, "v" + installedVercode);
|
TestUtils.installAndBroadcast(getSwappableContext(), packageManager, id, installedVercode, "v" + installedVercode);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testCanUpdate() {
|
public void testCanUpdate() {
|
||||||
@ -173,7 +175,7 @@ public class AppProviderTest extends FDroidProviderTest<AppProvider> {
|
|||||||
|
|
||||||
Cursor canUpdateCursor = r.query(AppProvider.getCanUpdateUri(), AppProvider.DataColumns.ALL, null, null, null);
|
Cursor canUpdateCursor = r.query(AppProvider.getCanUpdateUri(), AppProvider.DataColumns.ALL, null, null, null);
|
||||||
canUpdateCursor.moveToFirst();
|
canUpdateCursor.moveToFirst();
|
||||||
List<String> canUpdateIds = new ArrayList<String>(canUpdateCursor.getCount());
|
List<String> canUpdateIds = new ArrayList<>(canUpdateCursor.getCount());
|
||||||
while (!canUpdateCursor.isAfterLast()) {
|
while (!canUpdateCursor.isAfterLast()) {
|
||||||
canUpdateIds.add(new App(canUpdateCursor).id);
|
canUpdateIds.add(new App(canUpdateCursor).id);
|
||||||
canUpdateCursor.moveToNext();
|
canUpdateCursor.moveToNext();
|
||||||
@ -224,7 +226,7 @@ public class AppProviderTest extends FDroidProviderTest<AppProvider> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void assertContainsOnlyIds(List<App> actualApps, String[] expectedIds) {
|
private void assertContainsOnlyIds(List<App> actualApps, String[] expectedIds) {
|
||||||
List<String> actualIds = new ArrayList<String>(actualApps.size());
|
List<String> actualIds = new ArrayList<>(actualApps.size());
|
||||||
for (App app : actualApps) {
|
for (App app : actualApps) {
|
||||||
actualIds.add(app.id);
|
actualIds.add(app.id);
|
||||||
}
|
}
|
||||||
@ -241,12 +243,11 @@ public class AppProviderTest extends FDroidProviderTest<AppProvider> {
|
|||||||
assertResultCount(0, AppProvider.getInstalledUri());
|
assertResultCount(0, AppProvider.getInstalledUri());
|
||||||
|
|
||||||
for (int i = 10; i < 20; i ++) {
|
for (int i = 10; i < 20; i ++) {
|
||||||
TestUtils.installAndBroadcast(getMockContext(), pm, "com.example.test." + i, i, "v1");
|
TestUtils.installAndBroadcast(getSwappableContext(), pm, "com.example.test." + i, i, "v1");
|
||||||
}
|
}
|
||||||
|
|
||||||
assertResultCount(10, AppProvider.getInstalledUri());
|
assertResultCount(10, AppProvider.getInstalledUri());
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
public void testInsert() {
|
public void testInsert() {
|
||||||
|
|
||||||
|
@ -37,7 +37,6 @@ public class InstalledAppProviderTest extends FDroidProviderTest<InstalledAppPro
|
|||||||
assertValidUri(InstalledAppProvider.getAppUri("blah"));
|
assertValidUri(InstalledAppProvider.getAppUri("blah"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO fix me
|
|
||||||
public void testInsert() {
|
public void testInsert() {
|
||||||
|
|
||||||
assertResultCount(0, InstalledAppProvider.getContentUri());
|
assertResultCount(0, InstalledAppProvider.getContentUri());
|
||||||
@ -134,7 +133,7 @@ public class InstalledAppProviderTest extends FDroidProviderTest<InstalledAppPro
|
|||||||
assertIsInstalledVersionInDb("com.example.toKeep", 1, "v0.1");
|
assertIsInstalledVersionInDb("com.example.toKeep", 1, "v0.1");
|
||||||
|
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
protected String[] getMinimalProjection() {
|
protected String[] getMinimalProjection() {
|
||||||
return new String[] {
|
return new String[] {
|
||||||
@ -153,6 +152,7 @@ public class InstalledAppProviderTest extends FDroidProviderTest<InstalledAppPro
|
|||||||
if (appId != null) {
|
if (appId != null) {
|
||||||
values.put(InstalledAppProvider.DataColumns.APP_ID, appId);
|
values.put(InstalledAppProvider.DataColumns.APP_ID, appId);
|
||||||
}
|
}
|
||||||
|
values.put(InstalledAppProvider.DataColumns.APPLICATION_LABEL, "Mock app: " + appId);
|
||||||
values.put(InstalledAppProvider.DataColumns.VERSION_CODE, versionCode);
|
values.put(InstalledAppProvider.DataColumns.VERSION_CODE, versionCode);
|
||||||
values.put(InstalledAppProvider.DataColumns.VERSION_NAME, versionNumber);
|
values.put(InstalledAppProvider.DataColumns.VERSION_NAME, versionNumber);
|
||||||
return values;
|
return values;
|
||||||
@ -164,15 +164,15 @@ public class InstalledAppProviderTest extends FDroidProviderTest<InstalledAppPro
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void removeAndBroadcast(String appId) {
|
private void removeAndBroadcast(String appId) {
|
||||||
TestUtils.removeAndBroadcast(getMockContext(), getPackageManager(), appId);
|
TestUtils.removeAndBroadcast(getSwappableContext(), getPackageManager(), appId);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void upgradeAndBroadcast(String appId, int versionCode, String versionName) {
|
private void upgradeAndBroadcast(String appId, int versionCode, String versionName) {
|
||||||
TestUtils.upgradeAndBroadcast(getMockContext(), getPackageManager(), appId, versionCode, versionName);
|
TestUtils.upgradeAndBroadcast(getSwappableContext(), getPackageManager(), appId, versionCode, versionName);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void installAndBroadcast(String appId, int versionCode, String versionName) {
|
private void installAndBroadcast(String appId, int versionCode, String versionName) {
|
||||||
TestUtils.installAndBroadcast(getMockContext(), getPackageManager(), appId, versionCode, versionName);
|
TestUtils.installAndBroadcast(getSwappableContext(), getPackageManager(), appId, versionCode, versionName);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,8 @@ import android.net.Uri;
|
|||||||
import android.os.Environment;
|
import android.os.Environment;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import junit.framework.AssertionFailedError;
|
import junit.framework.AssertionFailedError;
|
||||||
|
|
||||||
|
import mock.MockContextSwappableComponents;
|
||||||
import mock.MockInstallablePackageManager;
|
import mock.MockInstallablePackageManager;
|
||||||
import org.fdroid.fdroid.data.ApkProvider;
|
import org.fdroid.fdroid.data.ApkProvider;
|
||||||
import org.fdroid.fdroid.data.AppProvider;
|
import org.fdroid.fdroid.data.AppProvider;
|
||||||
@ -134,17 +136,12 @@ public class TestUtils {
|
|||||||
* Will tell {@code pm} that we are installing {@code appId}, and then alert the
|
* Will tell {@code pm} that we are installing {@code appId}, and then alert the
|
||||||
* {@link org.fdroid.fdroid.PackageAddedReceiver}. This will in turn update the
|
* {@link org.fdroid.fdroid.PackageAddedReceiver}. This will in turn update the
|
||||||
* "installed apps" table in the database.
|
* "installed apps" table in the database.
|
||||||
*
|
|
||||||
* Note: in order for this to work, the {@link AppProviderTest#getSwappableContext()}
|
|
||||||
* will need to be aware of the package manager that we have passed in. Therefore,
|
|
||||||
* you will have to have called
|
|
||||||
* {@link mock.MockContextSwappableComponents#setPackageManager(android.content.pm.PackageManager)}
|
|
||||||
* on the {@link AppProviderTest#getSwappableContext()} before invoking this method.
|
|
||||||
*/
|
*/
|
||||||
public static void installAndBroadcast(
|
public static void installAndBroadcast(
|
||||||
Context context, MockInstallablePackageManager pm,
|
MockContextSwappableComponents context, MockInstallablePackageManager pm,
|
||||||
String appId, int versionCode, String versionName) {
|
String appId, int versionCode, String versionName) {
|
||||||
|
|
||||||
|
context.setPackageManager(pm);
|
||||||
pm.install(appId, versionCode, versionName);
|
pm.install(appId, versionCode, versionName);
|
||||||
Intent installIntent = new Intent(Intent.ACTION_PACKAGE_ADDED);
|
Intent installIntent = new Intent(Intent.ACTION_PACKAGE_ADDED);
|
||||||
installIntent.setData(Uri.parse("package:" + appId));
|
installIntent.setData(Uri.parse("package:" + appId));
|
||||||
@ -153,15 +150,16 @@ public class TestUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see org.fdroid.fdroid.TestUtils#installAndBroadcast(android.content.Context context, mock.MockInstallablePackageManager, String, int, String)
|
* @see org.fdroid.fdroid.TestUtils#installAndBroadcast(mock.MockContextSwappableComponents, mock.MockInstallablePackageManager, String, int, String)
|
||||||
*/
|
*/
|
||||||
public static void upgradeAndBroadcast(
|
public static void upgradeAndBroadcast(
|
||||||
Context context, MockInstallablePackageManager pm,
|
MockContextSwappableComponents context, MockInstallablePackageManager pm,
|
||||||
String appId, int versionCode, String versionName) {
|
String appId, int versionCode, String versionName) {
|
||||||
/*
|
/*
|
||||||
removeAndBroadcast(context, pm, appId);
|
removeAndBroadcast(context, pm, appId);
|
||||||
installAndBroadcast(context, pm, appId, versionCode, versionName);
|
installAndBroadcast(context, pm, appId, versionCode, versionName);
|
||||||
*/
|
*/
|
||||||
|
context.setPackageManager(pm);
|
||||||
pm.install(appId, versionCode, versionName);
|
pm.install(appId, versionCode, versionName);
|
||||||
Intent installIntent = new Intent(Intent.ACTION_PACKAGE_CHANGED);
|
Intent installIntent = new Intent(Intent.ACTION_PACKAGE_CHANGED);
|
||||||
installIntent.setData(Uri.parse("package:" + appId));
|
installIntent.setData(Uri.parse("package:" + appId));
|
||||||
@ -170,10 +168,11 @@ public class TestUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see org.fdroid.fdroid.TestUtils#installAndBroadcast(android.content.Context context, mock.MockInstallablePackageManager, String, int, String)
|
* @see org.fdroid.fdroid.TestUtils#installAndBroadcast(mock.MockContextSwappableComponents, mock.MockInstallablePackageManager, String, int, String)
|
||||||
*/
|
*/
|
||||||
public static void removeAndBroadcast(Context context, MockInstallablePackageManager pm, String appId) {
|
public static void removeAndBroadcast(MockContextSwappableComponents context, MockInstallablePackageManager pm, String appId) {
|
||||||
|
|
||||||
|
context.setPackageManager(pm);
|
||||||
pm.remove(appId);
|
pm.remove(appId);
|
||||||
Intent installIntent = new Intent(Intent.ACTION_PACKAGE_REMOVED);
|
Intent installIntent = new Intent(Intent.ACTION_PACKAGE_REMOVED);
|
||||||
installIntent.setData(Uri.parse("package:" + appId));
|
installIntent.setData(Uri.parse("package:" + appId));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user