diff --git a/app/src/test/java/org/fdroid/fdroid/data/AppProviderTest.java b/app/src/test/java/org/fdroid/fdroid/data/AppProviderTest.java index 0d8102b8d..00553448e 100644 --- a/app/src/test/java/org/fdroid/fdroid/data/AppProviderTest.java +++ b/app/src/test/java/org/fdroid/fdroid/data/AppProviderTest.java @@ -3,11 +3,11 @@ package org.fdroid.fdroid.data; import android.app.Application; import android.content.ContentResolver; import android.content.ContentValues; +import android.content.Context; import android.database.Cursor; import android.net.Uri; import org.fdroid.fdroid.BuildConfig; -import org.fdroid.fdroid.R; import org.fdroid.fdroid.data.Schema.AppMetadataTable.Cols; import org.junit.Before; import org.junit.Test; @@ -89,7 +89,7 @@ public class AppProviderTest extends FDroidProviderTest { boolean ignoreAll, int ignoreVercode) { ContentValues values = new ContentValues(3); values.put(Cols.SUGGESTED_VERSION_CODE, suggestedVercode); - App app = insertApp(packageName, "App: " + packageName, values); + App app = insertApp(contentResolver, context, packageName, "App: " + packageName, values); AppPrefsProvider.Helper.update(context, app, new AppPrefs(ignoreVercode, ignoreAll)); InstalledAppTestUtils.install(context, packageName, installedVercode, "v" + installedVercode); @@ -193,7 +193,7 @@ public class AppProviderTest extends FDroidProviderTest { assertContainsOnlyIds(canUpdateApps, expectedCanUpdate); } - private void assertContainsOnlyIds(List actualApps, String[] expectedIds) { + public static void assertContainsOnlyIds(List actualApps, String[] expectedIds) { List actualIds = new ArrayList<>(actualApps.size()); for (App app : actualApps) { actualIds.add(app.packageName); @@ -265,95 +265,16 @@ public class AppProviderTest extends FDroidProviderTest { return contentResolver.query(AppProvider.getContentUri(), projection, null, null, null); } - - // ======================================================================== - // "Categories" - // (at this point) not an additional table, but we treat them sort of - // like they are. That means that if we change the implementation to - // use a separate table in the future, these should still pass. - // ======================================================================== - - @Test - public void testCategoriesSingle() { - insertAppWithCategory("com.dog", "Dog", "Animal"); - insertAppWithCategory("com.rock", "Rock", "Mineral"); - insertAppWithCategory("com.banana", "Banana", "Vegetable"); - - List categories = AppProvider.Helper.categories(context); - String[] expected = new String[] { - context.getResources().getString(R.string.category_Whats_New), - context.getResources().getString(R.string.category_Recently_Updated), - context.getResources().getString(R.string.category_All), - "Animal", - "Mineral", - "Vegetable", - }; - assertContainsOnly(categories, expected); - } - - @Test - 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 categories = AppProvider.Helper.categories(context); - String[] expected = new String[] { - context.getResources().getString(R.string.category_Whats_New), - context.getResources().getString(R.string.category_Recently_Updated), - context.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 categoriesLonger = AppProvider.Helper.categories(context); - String[] expectedLonger = new String[] { - context.getResources().getString(R.string.category_Whats_New), - context.getResources().getString(R.string.category_Recently_Updated), - context.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); - } - // ======================================================================= // Misc helper functions // (to be used by any tests in this suite) // ======================================================================= private void insertApp(String id, String name) { - insertApp(id, name, new ContentValues()); + insertApp(contentResolver, context, id, name, new ContentValues()); } - private void insertAppWithCategory(String id, String name, String categories) { - ContentValues values = new ContentValues(1); - values.put(Cols.CATEGORIES, categories); - insertApp(id, name, values); - } - - public App insertApp(String id, String name, ContentValues additionalValues) { + public static App insertApp(ShadowContentResolver contentResolver, Context context, String id, String name, ContentValues additionalValues) { ContentValues values = new ContentValues(); values.put(Cols.Package.PACKAGE_NAME, id); diff --git a/app/src/test/java/org/fdroid/fdroid/data/CategoryProviderTest.java b/app/src/test/java/org/fdroid/fdroid/data/CategoryProviderTest.java new file mode 100644 index 000000000..f36147cb1 --- /dev/null +++ b/app/src/test/java/org/fdroid/fdroid/data/CategoryProviderTest.java @@ -0,0 +1,160 @@ +package org.fdroid.fdroid.data; + +import android.app.Application; +import android.content.ContentValues; +import android.database.Cursor; +import android.net.Uri; + +import org.fdroid.fdroid.BuildConfig; +import org.fdroid.fdroid.R; +import org.fdroid.fdroid.data.Schema.AppMetadataTable.Cols; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricGradleTestRunner; +import org.robolectric.annotation.Config; +import org.robolectric.shadows.ShadowContentResolver; + +import java.util.List; + +import static org.fdroid.fdroid.Assert.assertContainsOnly; +import static org.junit.Assert.assertEquals; + +// TODO: Use sdk=24 when Robolectric supports this +@Config(constants = BuildConfig.class, application = Application.class, sdk = 23) +@RunWith(RobolectricGradleTestRunner.class) +public class CategoryProviderTest extends FDroidProviderTest { + + @Before + public void setup() { + ShadowContentResolver.registerProvider(AppProvider.getAuthority(), new AppProvider()); + } + + // ======================================================================== + // "Categories" + // (at this point) not an additional table, but we treat them sort of + // like they are. That means that if we change the implementation to + // use a separate table in the future, these should still pass. + // ======================================================================== + + @Test + public void queryAppsInCategories() { + insertAppWithCategory("com.dog", "Dog", "Animal"); + insertAppWithCategory("com.cat", "Cat", "Animal"); + insertAppWithCategory("com.crow", "Crow", "Animal,Bird"); + insertAppWithCategory("com.chicken", "Chicken", "Animal,Bird,Food"); + insertAppWithCategory("com.bird-statue", "Bird Statue", "Bird,Mineral"); + insertAppWithCategory("com.rock", "Rock", "Mineral"); + insertAppWithCategory("com.banana", "Banana", "Food"); + + assertPackagesInCategory("Animal", new String[] { + "com.dog", + "com.cat", + "com.crow", + "com.chicken", + }); + + assertPackagesInCategory("Bird", new String[]{ + "com.crow", + "com.chicken", + "com.bird-statue", + }); + + assertPackagesInCategory("Food", new String[] { + "com.chicken", + "com.banana", + }); + + assertPackagesInCategory("Mineral", new String[] { + "com.rock", + "com.bird-statue", + }); + + assertNoPackagesInUri(AppProvider.getCategoryUri("Not a category")); + } + + private void assertNoPackagesInUri(Uri uri) { + Cursor noApps = contentResolver.query(uri, Cols.ALL, null, null, null); + assertEquals(noApps.getCount(), 0); + } + + private void assertPackagesInCategory(String category, String[] expectedPackages) { + assertPackagesInUri(AppProvider.getCategoryUri(category), expectedPackages); + } + + private void assertPackagesInUri(Uri uri, String[] expectedPackages) { + List apps = AppProvider.Helper.cursorToList(contentResolver.query(uri, Cols.ALL, null, null, null)); + AppProviderTest.assertContainsOnlyIds(apps, expectedPackages); + } + + @Test + public void testCategoriesSingle() { + insertAppWithCategory("com.dog", "Dog", "Animal"); + insertAppWithCategory("com.rock", "Rock", "Mineral"); + insertAppWithCategory("com.banana", "Banana", "Vegetable"); + + List categories = AppProvider.Helper.categories(context); + String[] expected = new String[] { + context.getResources().getString(R.string.category_Whats_New), + context.getResources().getString(R.string.category_Recently_Updated), + context.getResources().getString(R.string.category_All), + "Animal", + "Mineral", + "Vegetable", + }; + assertContainsOnly(categories, expected); + } + + @Test + 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 categories = AppProvider.Helper.categories(context); + String[] expected = new String[] { + context.getResources().getString(R.string.category_Whats_New), + context.getResources().getString(R.string.category_Recently_Updated), + context.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 categoriesLonger = AppProvider.Helper.categories(context); + String[] expectedLonger = new String[] { + context.getResources().getString(R.string.category_Whats_New), + context.getResources().getString(R.string.category_Recently_Updated), + context.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 insertAppWithCategory(String id, String name, String categories) { + ContentValues values = new ContentValues(1); + values.put(Cols.CATEGORIES, categories); + AppProviderTest.insertApp(contentResolver, context, id, name, values); + } +}