From 8e2e14d703d710a4cc6dd874df9f8f42a2483b7a Mon Sep 17 00:00:00 2001 From: Peter Serwylo Date: Thu, 8 Sep 2016 12:48:57 +1000 Subject: [PATCH] Migrating category tests to their own class in preperation for giving them their own DB table --- .../fdroid/fdroid/data/AppProviderTest.java | 87 +------------- .../fdroid/data/CategoryProviderTest.java | 108 ++++++++++++++++++ 2 files changed, 112 insertions(+), 83 deletions(-) create mode 100644 app/src/test/java/org/fdroid/fdroid/data/CategoryProviderTest.java 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..1995e7d82 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); @@ -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..3f4cff0fb --- /dev/null +++ b/app/src/test/java/org/fdroid/fdroid/data/CategoryProviderTest.java @@ -0,0 +1,108 @@ +package org.fdroid.fdroid.data; + +import android.app.Application; +import android.content.ContentValues; + +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; + +// 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 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); + } + +}