Added category tests.
This will be useful when somebody wants to move categories from a comma separated string in the app table, to a separate table all together.
This commit is contained in:
parent
feec3b1c51
commit
43f8ea0814
21
test/src/mock/MockCategoryResources.java
Normal file
21
test/src/mock/MockCategoryResources.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package mock;
|
||||||
|
|
||||||
|
import android.test.mock.*;
|
||||||
|
import org.fdroid.fdroid.*;
|
||||||
|
|
||||||
|
public class MockCategoryResources extends MockResources {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getString(int id) {
|
||||||
|
if (id == R.string.category_all) {
|
||||||
|
return "All";
|
||||||
|
} else if (id == R.string.category_recentlyupdated) {
|
||||||
|
return "Recently Updated";
|
||||||
|
} else if (id == R.string.category_whatsnew) {
|
||||||
|
return "Whats New";
|
||||||
|
} else {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -2,13 +2,14 @@ package mock;
|
|||||||
|
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
import android.test.mock.MockContext;
|
import android.test.mock.*;
|
||||||
|
|
||||||
public class MockContextSwappableComponents extends MockContext {
|
public class MockContextSwappableComponents extends MockContext {
|
||||||
|
|
||||||
private PackageManager packageManager;
|
private PackageManager packageManager;
|
||||||
|
|
||||||
private Resources resources;
|
private Resources resources;
|
||||||
|
private MockContentResolver contentResolver;
|
||||||
|
|
||||||
public MockContextSwappableComponents setPackageManager(PackageManager pm) {
|
public MockContextSwappableComponents setPackageManager(PackageManager pm) {
|
||||||
packageManager = pm;
|
packageManager = pm;
|
||||||
@ -20,6 +21,11 @@ public class MockContextSwappableComponents extends MockContext {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public MockContextSwappableComponents setContentResolver(MockContentResolver contentResolver) {
|
||||||
|
this.contentResolver = contentResolver;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PackageManager getPackageManager() {
|
public PackageManager getPackageManager() {
|
||||||
return packageManager;
|
return packageManager;
|
||||||
@ -29,4 +35,9 @@ public class MockContextSwappableComponents extends MockContext {
|
|||||||
public Resources getResources() {
|
public Resources getResources() {
|
||||||
return resources;
|
return resources;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MockContentResolver getContentResolver() {
|
||||||
|
return contentResolver;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,18 @@
|
|||||||
package org.fdroid.fdroid;
|
package org.fdroid.fdroid;
|
||||||
|
|
||||||
import android.content.ContentValues;
|
import android.content.ContentValues;
|
||||||
import android.content.pm.PackageInfo;
|
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.provider.ContactsContract;
|
import junit.framework.AssertionFailedError;
|
||||||
|
import mock.MockCategoryResources;
|
||||||
import mock.MockInstallablePackageManager;
|
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 java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class AppProviderTest extends FDroidProviderTest<AppProvider> {
|
public class AppProviderTest extends FDroidProviderTest<AppProvider> {
|
||||||
|
|
||||||
@ -20,6 +20,13 @@ public class AppProviderTest extends FDroidProviderTest<AppProvider> {
|
|||||||
super(AppProvider.class, AppProvider.getAuthority());
|
super(AppProvider.class, AppProvider.getAuthority());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
super.setUp();
|
||||||
|
getSwappableContext().setResources(new MockCategoryResources());
|
||||||
|
getSwappableContext().setContentResolver(getMockContentResolver());
|
||||||
|
}
|
||||||
|
|
||||||
protected String[] getMinimalProjection() {
|
protected String[] getMinimalProjection() {
|
||||||
return new String[] {
|
return new String[] {
|
||||||
AppProvider.DataColumns.APP_ID,
|
AppProvider.DataColumns.APP_ID,
|
||||||
@ -53,6 +60,12 @@ public class AppProviderTest extends FDroidProviderTest<AppProvider> {
|
|||||||
assertNotNull(cursor);
|
assertNotNull(cursor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void insertApps(int count) {
|
||||||
|
for (int i = 0; i < count; i ++) {
|
||||||
|
insertApp("com.example.test." + i, "Test app " + i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void testInstalled() {
|
public void testInstalled() {
|
||||||
|
|
||||||
Utils.clearInstalledApksCache();
|
Utils.clearInstalledApksCache();
|
||||||
@ -60,9 +73,7 @@ public class AppProviderTest extends FDroidProviderTest<AppProvider> {
|
|||||||
MockInstallablePackageManager pm = new MockInstallablePackageManager();
|
MockInstallablePackageManager pm = new MockInstallablePackageManager();
|
||||||
getSwappableContext().setPackageManager(pm);
|
getSwappableContext().setPackageManager(pm);
|
||||||
|
|
||||||
for (int i = 0; i < 100; i ++) {
|
insertApps(100);
|
||||||
insertApp("com.example.test." + i, "Test app " + i);
|
|
||||||
}
|
|
||||||
|
|
||||||
assertAppCount(100, AppProvider.getContentUri());
|
assertAppCount(100, AppProvider.getContentUri());
|
||||||
assertAppCount(0, AppProvider.getInstalledUri());
|
assertAppCount(0, AppProvider.getInstalledUri());
|
||||||
@ -75,7 +86,7 @@ public class AppProviderTest extends FDroidProviderTest<AppProvider> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void assertAppCount(int expectedCount, Uri uri) {
|
private void assertAppCount(int expectedCount, Uri uri) {
|
||||||
Cursor cursor = getProvider().query(uri, getMinimalProjection(), null, null, null);
|
Cursor cursor = getMockContentResolver().query(uri, getMinimalProjection(), null, null, null);
|
||||||
assertNotNull(cursor);
|
assertNotNull(cursor);
|
||||||
assertEquals(expectedCount, cursor.getCount());
|
assertEquals(expectedCount, cursor.getCount());
|
||||||
}
|
}
|
||||||
@ -114,25 +125,134 @@ public class AppProviderTest extends FDroidProviderTest<AppProvider> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Cursor queryAllApps() {
|
private Cursor queryAllApps() {
|
||||||
return getProvider().query(AppProvider.getContentUri(), getMinimalProjection(), null, null, null);
|
return getMockContentResolver().query(AppProvider.getContentUri(), getMinimalProjection(), null, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testCategoriesSingle() {
|
||||||
|
insertAppWithCategory("com.dog", "Dog", "Animal");
|
||||||
|
insertAppWithCategory("com.rock", "Rock", "Mineral");
|
||||||
|
insertAppWithCategory("com.banana", "Banana", "Vegetable");
|
||||||
|
|
||||||
|
List<String> categories = AppProvider.Helper.categories(getMockContext());
|
||||||
|
String[] expected = new String[] {
|
||||||
|
getMockContext().getResources().getString(R.string.category_whatsnew),
|
||||||
|
getMockContext().getResources().getString(R.string.category_recentlyupdated),
|
||||||
|
getMockContext().getResources().getString(R.string.category_all),
|
||||||
|
"Animal",
|
||||||
|
"Mineral",
|
||||||
|
"Vegetable"
|
||||||
|
};
|
||||||
|
assertContainsOnly(categories, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testCategoriesMultiple() {
|
||||||
|
insertAppWithCategory("com.rock.dog", "Rock-Dog", "Mineral,Animal");
|
||||||
|
insertAppWithCategory("com.dog.rock.apple", "Dog-Rock-Apple", "Animal,Mineral,Vegetable");
|
||||||
|
insertAppWithCategory("com.banana.apple", "Banana", "Vegetable,Vegetable");
|
||||||
|
|
||||||
|
List<String> categories = AppProvider.Helper.categories(getMockContext());
|
||||||
|
String[] expected = new String[] {
|
||||||
|
getMockContext().getResources().getString(R.string.category_whatsnew),
|
||||||
|
getMockContext().getResources().getString(R.string.category_recentlyupdated),
|
||||||
|
getMockContext().getResources().getString(R.string.category_all),
|
||||||
|
|
||||||
|
"Animal",
|
||||||
|
"Mineral",
|
||||||
|
"Vegetable"
|
||||||
|
};
|
||||||
|
assertContainsOnly(categories, expected);
|
||||||
|
|
||||||
|
insertAppWithCategory("com.example.game", "Game",
|
||||||
|
"Running,Shooting,Jumping,Bleh,Sneh,Pleh,Blah,Test category," +
|
||||||
|
"The quick brown fox jumps over the lazy dog,With apostrophe's");
|
||||||
|
|
||||||
|
List<String> categoriesLonger = AppProvider.Helper.categories(getMockContext());
|
||||||
|
String[] expectedLonger = new String[] {
|
||||||
|
getMockContext().getResources().getString(R.string.category_whatsnew),
|
||||||
|
getMockContext().getResources().getString(R.string.category_recentlyupdated),
|
||||||
|
getMockContext().getResources().getString(R.string.category_all),
|
||||||
|
|
||||||
|
"Animal",
|
||||||
|
"Mineral",
|
||||||
|
"Vegetable",
|
||||||
|
|
||||||
|
"Running",
|
||||||
|
"Shooting",
|
||||||
|
"Jumping",
|
||||||
|
"Bleh",
|
||||||
|
"Sneh",
|
||||||
|
"Pleh",
|
||||||
|
"Blah",
|
||||||
|
"Test category",
|
||||||
|
"The quick brown fox jumps over the lazy dog",
|
||||||
|
"With apostrophe's"
|
||||||
|
};
|
||||||
|
|
||||||
|
assertContainsOnly(categoriesLonger, expectedLonger);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void insertApp(String id, String name) {
|
private void insertApp(String id, String name) {
|
||||||
ContentValues values = new ContentValues(2);
|
insertApp(id, name, new ContentValues());
|
||||||
values.put(AppProvider.DataColumns.APP_ID, id);
|
}
|
||||||
values.put(AppProvider.DataColumns.NAME, name);
|
|
||||||
|
|
||||||
// Required fields (NOT NULL in the database).
|
private void insertAppWithCategory(String id, String name,
|
||||||
values.put(AppProvider.DataColumns.SUMMARY, "test summary");
|
String categories) {
|
||||||
values.put(AppProvider.DataColumns.DESCRIPTION, "test description");
|
ContentValues values = new ContentValues(1);
|
||||||
values.put(AppProvider.DataColumns.LICENSE, "GPL?");
|
values.put(AppProvider.DataColumns.CATEGORIES, categories);
|
||||||
values.put(AppProvider.DataColumns.IS_COMPATIBLE, 1);
|
insertApp(id, name, values);
|
||||||
values.put(AppProvider.DataColumns.IGNORE_ALLUPDATES, 0);
|
}
|
||||||
values.put(AppProvider.DataColumns.IGNORE_THISUPDATE, 0);
|
|
||||||
|
|
||||||
Uri uri = AppProvider.getContentUri();
|
private void insertApp(String id, String name,
|
||||||
|
ContentValues additionalValues) {
|
||||||
|
TestUtils.insertApp(getMockContentResolver(), id, name, additionalValues);
|
||||||
|
}
|
||||||
|
|
||||||
getProvider().insert(uri, values);
|
private <T extends Comparable> void assertContainsOnly(List<T> actualList, T[] expectedContains) {
|
||||||
|
List<T> containsList = new ArrayList<T>(expectedContains.length);
|
||||||
|
Collections.addAll(containsList, expectedContains);
|
||||||
|
assertContainsOnly(actualList, containsList);
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T> String listToString(List<T> list) {
|
||||||
|
String string = "[";
|
||||||
|
for (int i = 0; i < list.size(); i ++) {
|
||||||
|
if (i > 0) {
|
||||||
|
string += ", ";
|
||||||
|
}
|
||||||
|
string += list.get(i);
|
||||||
|
}
|
||||||
|
string += "]";
|
||||||
|
return string;
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T extends Comparable> void assertContainsOnly(List<T> actualList, List<T> expectedContains) {
|
||||||
|
if (actualList.size() != expectedContains.size()) {
|
||||||
|
String message =
|
||||||
|
"List sizes don't match.\n" +
|
||||||
|
"Expected: " +
|
||||||
|
listToString(expectedContains) + "\n" +
|
||||||
|
"Actual: " +
|
||||||
|
listToString(actualList);
|
||||||
|
throw new AssertionFailedError(message);
|
||||||
|
}
|
||||||
|
for (T required : expectedContains) {
|
||||||
|
boolean containsRequired = false;
|
||||||
|
for (T itemInList : actualList) {
|
||||||
|
if (required.equals(itemInList)) {
|
||||||
|
containsRequired = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!containsRequired) {
|
||||||
|
String message =
|
||||||
|
"List doesn't contain \"" + required + "\".\n" +
|
||||||
|
"Expected: " +
|
||||||
|
listToString(expectedContains) + "\n" +
|
||||||
|
"Actual: " +
|
||||||
|
listToString(actualList);
|
||||||
|
throw new AssertionFailedError(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -53,13 +53,17 @@ public abstract class FDroidProviderTest<T extends FDroidProvider> extends Provi
|
|||||||
|
|
||||||
protected void assertInvalidUri(Uri uri) {
|
protected void assertInvalidUri(Uri uri) {
|
||||||
try {
|
try {
|
||||||
|
// Use getProvdider instead of getContentResolver, because the mock
|
||||||
|
// content resolver wont result in the provider we are testing, and
|
||||||
|
// hence we don't get to see how our provider responds to invalid
|
||||||
|
// uris.
|
||||||
getProvider().query(uri, getMinimalProjection(), null, null, null);
|
getProvider().query(uri, getMinimalProjection(), null, null, null);
|
||||||
fail();
|
fail();
|
||||||
} catch (UnsupportedOperationException e) {}
|
} catch (UnsupportedOperationException e) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void assertValidUri(Uri uri) {
|
protected void assertValidUri(Uri uri) {
|
||||||
Cursor cursor = getProvider().query(uri, getMinimalProjection(), null, null, null);
|
Cursor cursor = getMockContentResolver().query(uri, getMinimalProjection(), null, null, null);
|
||||||
assertNotNull(cursor);
|
assertNotNull(cursor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
30
test/src/org/fdroid/fdroid/TestUtils.java
Normal file
30
test/src/org/fdroid/fdroid/TestUtils.java
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package org.fdroid.fdroid;
|
||||||
|
|
||||||
|
import android.content.*;
|
||||||
|
import android.net.Uri;
|
||||||
|
import org.fdroid.fdroid.data.AppProvider;
|
||||||
|
|
||||||
|
public class TestUtils {
|
||||||
|
|
||||||
|
public static void insertApp(ContentResolver resolver, String id, String name, ContentValues additionalValues) {
|
||||||
|
|
||||||
|
ContentValues values = new ContentValues();
|
||||||
|
values.put(AppProvider.DataColumns.APP_ID, id);
|
||||||
|
values.put(AppProvider.DataColumns.NAME, name);
|
||||||
|
|
||||||
|
// Required fields (NOT NULL in the database).
|
||||||
|
values.put(AppProvider.DataColumns.SUMMARY, "test summary");
|
||||||
|
values.put(AppProvider.DataColumns.DESCRIPTION, "test description");
|
||||||
|
values.put(AppProvider.DataColumns.LICENSE, "GPL?");
|
||||||
|
values.put(AppProvider.DataColumns.IS_COMPATIBLE, 1);
|
||||||
|
values.put(AppProvider.DataColumns.IGNORE_ALLUPDATES, 0);
|
||||||
|
values.put(AppProvider.DataColumns.IGNORE_THISUPDATE, 0);
|
||||||
|
|
||||||
|
values.putAll(additionalValues);
|
||||||
|
|
||||||
|
Uri uri = AppProvider.getContentUri();
|
||||||
|
|
||||||
|
resolver.insert(uri, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user