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;
26import android.test.suitebuilder.annotation.SmallTest;
27
28/**
29 * Unit tests for {@link VoicemailCleanupService}.
30 */
31@SmallTest
32public class VoicemailCleanupServiceTest extends BaseVoicemailProviderTest {
33    private static final String TEST_PACKAGE_1 = "package1";
34    private static final String TEST_PACKAGE_2 = "package2";
35    // Object under test.
36    private VoicemailCleanupService mCleanupService;
37
38    @Override
39    protected void setUp() throws Exception {
40        super.setUp();
41        setUpForFullPermission();
42        mCleanupService = new VoicemailCleanupService();
43    }
44
45    public void testIntentHandling() {
46        mCleanupService = new VoicemailCleanupService();
47        insertDataForPackage(TEST_PACKAGE_1);
48        insertDataForPackage(TEST_PACKAGE_2);
49        checkDataExistsForPackages(TEST_PACKAGE_1, TEST_PACKAGE_2);
50        // No action on PACKAGE_CHANGED.
51        sendIntent(TEST_PACKAGE_1, Intent.ACTION_PACKAGE_CHANGED, null);
52        checkDataExistsForPackages(TEST_PACKAGE_1, TEST_PACKAGE_2);
53
54        // No action on PACKAGE_REPLACED.
55        sendIntent(TEST_PACKAGE_1, Intent.ACTION_PACKAGE_REPLACED, null);
56        checkDataExistsForPackages(TEST_PACKAGE_1, TEST_PACKAGE_2);
57
58        // No action on PACKAGE_REMOVED with EXTRA_REPLACING = true.
59        sendIntent(TEST_PACKAGE_1, Intent.ACTION_PACKAGE_REMOVED, true);
60        checkDataExistsForPackages(TEST_PACKAGE_1, TEST_PACKAGE_2);
61
62        // Data removed on PACKAGE_REMOVED but with no EXTRA_REPLACING.
63        sendIntent(TEST_PACKAGE_1, Intent.ACTION_PACKAGE_REMOVED, null);
64        checkDataDoesNotExistForPackage(TEST_PACKAGE_1);
65        checkDataExistsForPackages(TEST_PACKAGE_2);
66
67        // Data removed on PACKAGE_REMOVED with EXTRA_REPLACING = false.
68        sendIntent(TEST_PACKAGE_2, Intent.ACTION_PACKAGE_REMOVED, false);
69        checkDataDoesNotExistForPackage(TEST_PACKAGE_1);
70        checkDataDoesNotExistForPackage(TEST_PACKAGE_2);
71    }
72
73    private void sendIntent(String sourcePackage, String action, Boolean replacingExtra) {
74        Intent packageIntent = new Intent(action, Uri.parse("package:" + sourcePackage));
75        if (replacingExtra != null) {
76            packageIntent.putExtra(Intent.EXTRA_REPLACING, replacingExtra);
77        }
78        mCleanupService.handleIntentInternal(packageIntent, mResolver);
79    }
80
81    private void insertDataForPackage(String sourcePackage) {
82        ContentValues values = new ContentValues();
83        values.put(VoicemailContract.SOURCE_PACKAGE_FIELD, sourcePackage);
84        mResolver.insert(Voicemails.buildSourceUri(sourcePackage), values);
85        mResolver.insert(Status.buildSourceUri(sourcePackage), values);
86    }
87
88    void checkDataExistsForPackages(String... sourcePackages) {
89        for (String sourcePackage : sourcePackages) {
90            checkDataExistsForPackage(sourcePackage);
91        }
92    }
93
94    private void checkDataExistsForPackage(String sourcePackage) {
95        Cursor cursor = mResolver.query(
96                Voicemails.buildSourceUri(sourcePackage), null, null, null, null);
97        assertNotSame(0, cursor.getCount());
98        cursor = mResolver.query(
99                Status.buildSourceUri(sourcePackage), null, null, null, null);
100        assertNotSame(0, cursor.getCount());
101    }
102
103    private void checkDataDoesNotExistForPackage(String sourcePackage) {
104        Cursor cursor = mResolver.query(
105                Voicemails.buildSourceUri(sourcePackage), null, null, null, null);
106        assertEquals(0, cursor.getCount());
107        cursor = mResolver.query(
108                Status.buildSourceUri(sourcePackage), null, null, null, null);
109        assertEquals(0, cursor.getCount());
110    }
111}
112