1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17package com.android.providers.contacts; 18 19import android.content.ContentValues; 20import android.content.Intent; 21import android.database.Cursor; 22import android.net.Uri; 23import android.provider.VoicemailContract; 24import android.provider.VoicemailContract.Status; 25import android.provider.VoicemailContract.Voicemails; 26 27/** 28 * Unit tests for {@link VoicemailCleanupService}. 29 */ 30public class VoicemailCleanupServiceTest extends BaseVoicemailProviderTest { 31 private static final String TEST_PACKAGE_1 = "package1"; 32 private static final String TEST_PACKAGE_2 = "package2"; 33 // Object under test. 34 private VoicemailCleanupService mCleanupService; 35 36 @Override 37 protected void setUp() throws Exception { 38 super.setUp(); 39 setUpForFullPermission(); 40 mCleanupService = new VoicemailCleanupService(); 41 } 42 43 public void testIntentHandling() { 44 mCleanupService = new VoicemailCleanupService(); 45 insertDataForPackage(TEST_PACKAGE_1); 46 insertDataForPackage(TEST_PACKAGE_2); 47 checkDataExistsForPackages(TEST_PACKAGE_1, TEST_PACKAGE_2); 48 // No action on PACKAGE_CHANGED. 49 sendIntent(TEST_PACKAGE_1, Intent.ACTION_PACKAGE_CHANGED, null); 50 checkDataExistsForPackages(TEST_PACKAGE_1, TEST_PACKAGE_2); 51 52 // No action on PACKAGE_REPLACED. 53 sendIntent(TEST_PACKAGE_1, Intent.ACTION_PACKAGE_REPLACED, null); 54 checkDataExistsForPackages(TEST_PACKAGE_1, TEST_PACKAGE_2); 55 56 // No action on PACKAGE_REMOVED with EXTRA_REPLACING = true. 57 sendIntent(TEST_PACKAGE_1, Intent.ACTION_PACKAGE_REMOVED, true); 58 checkDataExistsForPackages(TEST_PACKAGE_1, TEST_PACKAGE_2); 59 60 // Data removed on PACKAGE_REMOVED but with no EXTRA_REPLACING. 61 sendIntent(TEST_PACKAGE_1, Intent.ACTION_PACKAGE_REMOVED, null); 62 checkDataDoesNotExistForPackage(TEST_PACKAGE_1); 63 checkDataExistsForPackages(TEST_PACKAGE_2); 64 65 // Data removed on PACKAGE_REMOVED with EXTRA_REPLACING = false. 66 sendIntent(TEST_PACKAGE_2, Intent.ACTION_PACKAGE_REMOVED, false); 67 checkDataDoesNotExistForPackage(TEST_PACKAGE_1); 68 checkDataDoesNotExistForPackage(TEST_PACKAGE_2); 69 } 70 71 private void sendIntent(String sourcePackage, String action, Boolean replacingExtra) { 72 Intent packageIntent = new Intent(action, Uri.parse("package:" + sourcePackage)); 73 if (replacingExtra != null) { 74 packageIntent.putExtra(Intent.EXTRA_REPLACING, replacingExtra); 75 } 76 mCleanupService.handleIntentInternal(packageIntent, mResolver); 77 } 78 79 private void insertDataForPackage(String sourcePackage) { 80 ContentValues values = new ContentValues(); 81 values.put(VoicemailContract.SOURCE_PACKAGE_FIELD, sourcePackage); 82 mResolver.insert(Voicemails.buildSourceUri(sourcePackage), values); 83 mResolver.insert(Status.buildSourceUri(sourcePackage), values); 84 } 85 86 void checkDataExistsForPackages(String... sourcePackages) { 87 for (String sourcePackage : sourcePackages) { 88 checkDataExistsForPackage(sourcePackage); 89 } 90 } 91 92 private void checkDataExistsForPackage(String sourcePackage) { 93 Cursor cursor = mResolver.query( 94 Voicemails.buildSourceUri(sourcePackage), null, null, null, null); 95 assertNotSame(0, cursor.getCount()); 96 cursor = mResolver.query( 97 Status.buildSourceUri(sourcePackage), null, null, null, null); 98 assertNotSame(0, cursor.getCount()); 99 } 100 101 private void checkDataDoesNotExistForPackage(String sourcePackage) { 102 Cursor cursor = mResolver.query( 103 Voicemails.buildSourceUri(sourcePackage), null, null, null, null); 104 assertEquals(0, cursor.getCount()); 105 cursor = mResolver.query( 106 Status.buildSourceUri(sourcePackage), null, null, null, null); 107 assertEquals(0, cursor.getCount()); 108 } 109} 110