diff options
author | Makoto Onuki <omakoto@google.com> | 2017-03-06 13:43:50 -0800 |
---|---|---|
committer | Makoto Onuki <omakoto@google.com> | 2017-03-06 14:00:08 -0800 |
commit | 442d7200fb77e2a6d0d3c8d1b17b3b8090581b02 (patch) | |
tree | c6452ad7b559918811aeb9bf0b6ef0ce3d0bda8d /tests | |
parent | fcc2655b0fca7032fcde306660da303c85125218 (diff) | |
download | ContactsProvider-442d7200fb77e2a6d0d3c8d1b17b3b8090581b02.tar.gz |
Don't use manifest receiver
- Only use a runtime receiver to handle package broadcasts.
We still do mostly the same tasks in the receiver.
- Also remove the BG service used by the voicemail provider.
- When the voicemail provider starts, remove records made by
packages that have been uninstalled.
- The package receiver was used to update GAL providers too.
The previous CL already takes care of scanning GAL providers on
startup.
Test: adb shell am instrument -w com.android.providers.contacts.tests
Bug 35388445
Bug 35385207
Change-Id: I743a769bd51e2acdbd13df1440cce04b35331fda
Diffstat (limited to 'tests')
-rw-r--r-- | tests/src/com/android/providers/contacts/BaseVoicemailProviderTest.java | 7 | ||||
-rw-r--r-- | tests/src/com/android/providers/contacts/ContactsMockPackageManager.java | 7 | ||||
-rw-r--r-- | tests/src/com/android/providers/contacts/VoicemailCleanupTest.java (renamed from tests/src/com/android/providers/contacts/VoicemailCleanupServiceTest.java) | 101 |
3 files changed, 74 insertions, 41 deletions
diff --git a/tests/src/com/android/providers/contacts/BaseVoicemailProviderTest.java b/tests/src/com/android/providers/contacts/BaseVoicemailProviderTest.java index f19f9bec..1020da89 100644 --- a/tests/src/com/android/providers/contacts/BaseVoicemailProviderTest.java +++ b/tests/src/com/android/providers/contacts/BaseVoicemailProviderTest.java @@ -43,7 +43,6 @@ public abstract class BaseVoicemailProviderTest extends BaseContactsProvider2Tes @Override protected void setUp() throws Exception { super.setUp(); - addProvider(TestVoicemailProvider.class, VoicemailContract.AUTHORITY); TestVoicemailProvider.setVvmProviderCallDelegate(createMockProviderCalls()); mPackageManager = (ContactsMockPackageManager) getProvider() @@ -165,6 +164,12 @@ public abstract class BaseVoicemailProviderTest extends BaseContactsProvider2Tes mDelegate = delegate; } + // Run the tasks synchronously. + @Override + void scheduleTask(int taskId, Object arg) { + performBackgroundTask(taskId, arg); + } + @Override protected CallLogDatabaseHelper getDatabaseHelper(Context context) { return new CallLogDatabaseHelperTestable(context, /* contactsDbForMigration = */ null); diff --git a/tests/src/com/android/providers/contacts/ContactsMockPackageManager.java b/tests/src/com/android/providers/contacts/ContactsMockPackageManager.java index 9a8c98b0..a9420dda 100644 --- a/tests/src/com/android/providers/contacts/ContactsMockPackageManager.java +++ b/tests/src/com/android/providers/contacts/ContactsMockPackageManager.java @@ -81,10 +81,15 @@ public class ContactsMockPackageManager extends MockPackageManager { } @Override - public ApplicationInfo getApplicationInfo(String packageName, int flags) { + public ApplicationInfo getApplicationInfo(String packageName, int flags) + throws NameNotFoundException { ApplicationInfo info = new ApplicationInfo(); Integer uid = mReverse.get(packageName); + if (uid == null) { + throw new NameNotFoundException(); + } info.uid = (uid != null) ? uid : -1; + info.flags = ApplicationInfo.FLAG_INSTALLED; return info; } diff --git a/tests/src/com/android/providers/contacts/VoicemailCleanupServiceTest.java b/tests/src/com/android/providers/contacts/VoicemailCleanupTest.java index f8d76ee1..f3e1f7ea 100644 --- a/tests/src/com/android/providers/contacts/VoicemailCleanupServiceTest.java +++ b/tests/src/com/android/providers/contacts/VoicemailCleanupTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2011 The Android Open Source Project + * Copyright (C) 2017 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,69 +13,61 @@ * See the License for the specific language governing permissions and * limitations under the License */ - package com.android.providers.contacts; import android.content.ContentValues; -import android.content.Intent; import android.database.Cursor; -import android.net.Uri; import android.provider.VoicemailContract; import android.provider.VoicemailContract.Status; import android.provider.VoicemailContract.Voicemails; import android.test.suitebuilder.annotation.SmallTest; +import com.android.providers.contacts.testutil.TestUtil; + /** - * Unit tests for {@link VoicemailCleanupService}. + * Tests for {@link VoicemailCleanupTest}. */ @SmallTest -public class VoicemailCleanupServiceTest extends BaseVoicemailProviderTest { +public class VoicemailCleanupTest extends BaseVoicemailProviderTest { private static final String TEST_PACKAGE_1 = "package1"; private static final String TEST_PACKAGE_2 = "package2"; - // Object under test. - private VoicemailCleanupService mCleanupService; + private static final String TEST_PACKAGE_3 = "package3"; @Override protected void setUp() throws Exception { super.setUp(); setUpForFullPermission(); - mCleanupService = new VoicemailCleanupService(); } - public void testIntentHandling() { - mCleanupService = new VoicemailCleanupService(); + /** + * Test for {@link ContactsPackageMonitor#cleanupVoicemail}. + */ + public void testCleanupVoicemail() { insertDataForPackage(TEST_PACKAGE_1); insertDataForPackage(TEST_PACKAGE_2); - checkDataExistsForPackages(TEST_PACKAGE_1, TEST_PACKAGE_2); - // No action on PACKAGE_CHANGED. - sendIntent(TEST_PACKAGE_1, Intent.ACTION_PACKAGE_CHANGED, null); - checkDataExistsForPackages(TEST_PACKAGE_1, TEST_PACKAGE_2); - // No action on PACKAGE_REPLACED. - sendIntent(TEST_PACKAGE_1, Intent.ACTION_PACKAGE_REPLACED, null); - checkDataExistsForPackages(TEST_PACKAGE_1, TEST_PACKAGE_2); + mPackageManager.addPackage(123456, TEST_PACKAGE_2); + + checkDataExistsForPackage(TEST_PACKAGE_1); + checkDataExistsForPackage(TEST_PACKAGE_2); - // No action on PACKAGE_REMOVED with EXTRA_REPLACING = true. - sendIntent(TEST_PACKAGE_1, Intent.ACTION_PACKAGE_REMOVED, true); - checkDataExistsForPackages(TEST_PACKAGE_1, TEST_PACKAGE_2); + ContactsPackageMonitor.cleanupVoicemail(getMockContext(), TEST_PACKAGE_1); - // Data removed on PACKAGE_REMOVED but with no EXTRA_REPLACING. - sendIntent(TEST_PACKAGE_1, Intent.ACTION_PACKAGE_REMOVED, null); checkDataDoesNotExistForPackage(TEST_PACKAGE_1); - checkDataExistsForPackages(TEST_PACKAGE_2); + checkDataExistsForPackage(TEST_PACKAGE_2); + + // Call for TEST_PACKAGE_2, which is still installed. + ContactsPackageMonitor.cleanupVoicemail(getMockContext(), TEST_PACKAGE_2); - // Data removed on PACKAGE_REMOVED with EXTRA_REPLACING = false. - sendIntent(TEST_PACKAGE_2, Intent.ACTION_PACKAGE_REMOVED, false); checkDataDoesNotExistForPackage(TEST_PACKAGE_1); - checkDataDoesNotExistForPackage(TEST_PACKAGE_2); - } + checkDataExistsForPackage(TEST_PACKAGE_2); - private void sendIntent(String sourcePackage, String action, Boolean replacingExtra) { - Intent packageIntent = new Intent(action, Uri.parse("package:" + sourcePackage)); - if (replacingExtra != null) { - packageIntent.putExtra(Intent.EXTRA_REPLACING, replacingExtra); - } - mCleanupService.handleIntentInternal(packageIntent, mResolver); + // Uninstall the package and try again. + mPackageManager.removePackage(123456); + ContactsPackageMonitor.cleanupVoicemail(getMockContext(), TEST_PACKAGE_2); + + checkDataDoesNotExistForPackage(TEST_PACKAGE_1); + checkDataDoesNotExistForPackage(TEST_PACKAGE_2); } private void insertDataForPackage(String sourcePackage) { @@ -85,19 +77,19 @@ public class VoicemailCleanupServiceTest extends BaseVoicemailProviderTest { mResolver.insert(Status.buildSourceUri(sourcePackage), values); } - void checkDataExistsForPackages(String... sourcePackages) { - for (String sourcePackage : sourcePackages) { - checkDataExistsForPackage(sourcePackage); + private static void assertBigger(int smaller, int actual) { + if (smaller >= actual) { + fail("Expected to be bigger than " + smaller + ", but was " + actual); } } private void checkDataExistsForPackage(String sourcePackage) { Cursor cursor = mResolver.query( Voicemails.buildSourceUri(sourcePackage), null, null, null, null); - assertNotSame(0, cursor.getCount()); + assertBigger(0, cursor.getCount()); cursor = mResolver.query( Status.buildSourceUri(sourcePackage), null, null, null, null); - assertNotSame(0, cursor.getCount()); + assertBigger(0, cursor.getCount()); } private void checkDataDoesNotExistForPackage(String sourcePackage) { @@ -108,4 +100,35 @@ public class VoicemailCleanupServiceTest extends BaseVoicemailProviderTest { Status.buildSourceUri(sourcePackage), null, null, null, null); assertEquals(0, cursor.getCount()); } + + public void testRemoveStalePackagesAtStartUp() { + insertDataForPackage(TEST_PACKAGE_1); + insertDataForPackage(TEST_PACKAGE_2); + insertDataForPackage(TEST_PACKAGE_3); + + mPackageManager.addPackage(10001, TEST_PACKAGE_1); + mPackageManager.addPackage(10002, TEST_PACKAGE_2); + + checkDataExistsForPackage(TEST_PACKAGE_1); + checkDataExistsForPackage(TEST_PACKAGE_2); + checkDataExistsForPackage(TEST_PACKAGE_3); + + final VoicemailContentProvider provider = + (VoicemailContentProvider) mActor.provider; + + // In unit tests, BG tasks are synchronous. + provider.scheduleScanStalePackages(); + + checkDataExistsForPackage(TEST_PACKAGE_1); + checkDataExistsForPackage(TEST_PACKAGE_2); + checkDataDoesNotExistForPackage(TEST_PACKAGE_3); + + mPackageManager.removePackage(10001); + + provider.scheduleScanStalePackages(); + + checkDataDoesNotExistForPackage(TEST_PACKAGE_1); + checkDataExistsForPackage(TEST_PACKAGE_2); + checkDataDoesNotExistForPackage(TEST_PACKAGE_3); + } } |