Home | History | Annotate | Download | only in compat
      1 /*
      2  * Copyright (C) 2016 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 
     17 package com.android.dialer.compat;
     18 
     19 import static org.mockito.Matchers.anyInt;
     20 import static org.mockito.Matchers.anyString;
     21 import static org.mockito.Mockito.mock;
     22 import static org.mockito.Mockito.verify;
     23 import static org.mockito.Mockito.when;
     24 
     25 import android.content.ComponentName;
     26 import android.content.ContentUris;
     27 import android.content.ContentValues;
     28 import android.content.Context;
     29 import android.content.SharedPreferences;
     30 import android.net.Uri;
     31 import android.os.UserManager;
     32 import android.provider.BlockedNumberContract.BlockedNumbers;
     33 import android.test.AndroidTestCase;
     34 import android.test.suitebuilder.annotation.SmallTest;
     35 
     36 import com.android.contacts.common.compat.CompatUtils;
     37 import com.android.dialer.DialerApplication;
     38 import com.android.dialer.database.FilteredNumberContract.FilteredNumber;
     39 import com.android.dialer.database.FilteredNumberContract.FilteredNumberColumns;
     40 import com.android.dialer.database.FilteredNumberContract.FilteredNumberSources;
     41 import com.android.dialer.database.FilteredNumberContract.FilteredNumberTypes;
     42 import com.android.dialer.filterednumber.BlockedNumbersSettingsActivity;
     43 
     44 import org.mockito.Mock;
     45 import org.mockito.MockitoAnnotations;
     46 
     47 import java.util.Arrays;
     48 
     49 @SmallTest
     50 public class FilteredNumberCompatTest extends AndroidTestCase {
     51 
     52     private static final String E164_NUMBER = "+16502530000";
     53     private static final String NON_E164_NUMBER = "6502530000";
     54     private static final String COUNTRY_ISO = "US";
     55 
     56     private static final Uri EXPECTED_BASE_URI = CompatUtils.isNCompatible()
     57             ? BlockedNumbers.CONTENT_URI : FilteredNumber.CONTENT_URI;
     58 
     59     @Mock private Context mContext;
     60     @Mock private SharedPreferences mSharedPreferences;
     61 
     62     @Override
     63     public void setUp() throws Exception {
     64         super.setUp();
     65         MockitoAnnotations.initMocks(this);
     66         DialerApplication.setContextForTest(mContext);
     67         when(mContext.getSharedPreferences(anyString(), anyInt())).thenReturn(mSharedPreferences);
     68         FilteredNumberCompat.setIsEnabledForTest(true);
     69     }
     70 
     71     public void testIsNewFilteringEnabled_TestValueFalse() {
     72         FilteredNumberCompat.setIsEnabledForTest(false);
     73         assertFalse(FilteredNumberCompat.canUseNewFiltering());
     74     }
     75 
     76     public void testIsNewFilteringEnabled_TestValueTrue() {
     77         FilteredNumberCompat.setIsEnabledForTest(true);
     78         assertEquals(CompatUtils.isNCompatible(), FilteredNumberCompat.canUseNewFiltering());
     79     }
     80 
     81     public void testHasMigratedToNewBlocking_False() {
     82         assertFalse(FilteredNumberCompat.hasMigratedToNewBlocking());
     83     }
     84 
     85     public void testHasMigratedToNewBlocking_Migrated() {
     86         when(mSharedPreferences
     87                 .getBoolean(FilteredNumberCompat.HAS_MIGRATED_TO_NEW_BLOCKING_KEY, false))
     88                 .thenReturn(true);
     89         assertTrue(FilteredNumberCompat.hasMigratedToNewBlocking());
     90     }
     91 
     92     public void testGetContentUri_NullId() {
     93         assertEquals(FilteredNumber.CONTENT_URI, FilteredNumberCompat.getContentUri(null));
     94     }
     95 
     96     public void testGetContentUri_NotMigrated() {
     97         assertEquals(ContentUris.withAppendedId(FilteredNumber.CONTENT_URI, 1),
     98                 FilteredNumberCompat.getContentUri(1));
     99     }
    100 
    101     public void testGetContentUri_Migrated() {
    102         when(mSharedPreferences
    103                 .getBoolean(FilteredNumberCompat.HAS_MIGRATED_TO_NEW_BLOCKING_KEY, false))
    104                 .thenReturn(true);
    105         assertEquals(ContentUris.withAppendedId(EXPECTED_BASE_URI, 1),
    106                 FilteredNumberCompat.getContentUri(1));
    107     }
    108 
    109     public void testFilter_NullProjection() {
    110         assertNull(FilteredNumberCompat.filter(null));
    111     }
    112 
    113     public void testFilter_NoNulls() {
    114         assertArrayEquals(new String[] {"a", "b", "c"},
    115                 FilteredNumberCompat.filter(new String[] {"a", "b", "c"}));
    116     }
    117 
    118     public void testFilter_WithNulls() {
    119         assertArrayEquals(new String[] {"a", "b"},
    120                 FilteredNumberCompat.filter(new String[] {"a", null, "b"}));
    121     }
    122 
    123     public void testNewBlockNumberContentValues_NullNumber() {
    124         try {
    125             FilteredNumberCompat.newBlockNumberContentValues(null, null, null);
    126             fail();
    127         } catch (NullPointerException e) {}
    128     }
    129 
    130     public void testNewBlockNumberContentValues_N_NotMigrated() {
    131         if (!CompatUtils.isNCompatible()) {
    132             return;
    133         }
    134         assertEquals(newExpectedContentValuesM(NON_E164_NUMBER, null, null),
    135                 FilteredNumberCompat.newBlockNumberContentValues(NON_E164_NUMBER, null, null));
    136     }
    137 
    138     public void testNewBlockNumberContentValues_N_Migrated() {
    139         if (!CompatUtils.isNCompatible()) {
    140             return;
    141         }
    142         ContentValues contentValues = new ContentValues();
    143         contentValues.put(BlockedNumbers.COLUMN_ORIGINAL_NUMBER, NON_E164_NUMBER);
    144         when(mSharedPreferences
    145                 .getBoolean(FilteredNumberCompat.HAS_MIGRATED_TO_NEW_BLOCKING_KEY, false))
    146                 .thenReturn(true);
    147         assertEquals(contentValues, FilteredNumberCompat.newBlockNumberContentValues(
    148                 NON_E164_NUMBER,
    149                 null, null));
    150     }
    151 
    152     public void testNewBlockNumberContentValues_N_Disabled() {
    153         if (!CompatUtils.isNCompatible()) {
    154             return;
    155         }
    156         FilteredNumberCompat.setIsEnabledForTest(false);
    157         assertEquals(newExpectedContentValuesM(NON_E164_NUMBER, E164_NUMBER, COUNTRY_ISO),
    158                 FilteredNumberCompat.newBlockNumberContentValues(NON_E164_NUMBER, E164_NUMBER, COUNTRY_ISO));
    159     }
    160 
    161     public void testNewBlockNumberContentValues_M_NullE164() {
    162         if (CompatUtils.isNCompatible()) {
    163             return;
    164         }
    165         assertEquals(newExpectedContentValuesM(NON_E164_NUMBER, E164_NUMBER, COUNTRY_ISO),
    166                 FilteredNumberCompat.newBlockNumberContentValues(NON_E164_NUMBER, null, COUNTRY_ISO));
    167     }
    168 
    169     public void testNewBlockNumberContentValues_M_NullCountryIso() {
    170         if (CompatUtils.isNCompatible()) {
    171             return;
    172         }
    173         assertEquals(newExpectedContentValuesM(NON_E164_NUMBER, E164_NUMBER, null),
    174                 FilteredNumberCompat.newBlockNumberContentValues(NON_E164_NUMBER, E164_NUMBER, null));
    175     }
    176 
    177     public void testNewBlockNumberContentValues_M_NullE164AndCountryIso() {
    178         if (CompatUtils.isNCompatible()) {
    179             return;
    180         }
    181         // Number can't be formatted properly without country code
    182         assertEquals(newExpectedContentValuesM(NON_E164_NUMBER, null, null),
    183                 FilteredNumberCompat.newBlockNumberContentValues(NON_E164_NUMBER, null, null));
    184     }
    185 
    186     public void testCreateManageBlockedNumbersIntent_NullContext() {
    187         try {
    188             FilteredNumberCompat.createManageBlockedNumbersIntent(null);
    189             fail();
    190         } catch (NullPointerException e) {}
    191     }
    192 
    193     public void testCreateManageBlockedNumbersIntent_M() {
    194         if (CompatUtils.isNCompatible()) {
    195             return;
    196         }
    197         assertEquals(new ComponentName(getContext(), BlockedNumbersSettingsActivity.class),
    198                 FilteredNumberCompat.createManageBlockedNumbersIntent(getContext()).getComponent());
    199     }
    200 
    201     public void testCreateManageBlockedNumbersIntent_N_Disabled_NotMigrated() {
    202         if (!CompatUtils.isNCompatible()) {
    203             return;
    204         }
    205         FilteredNumberCompat.setIsEnabledForTest(false);
    206         assertEquals(new ComponentName(getContext(), BlockedNumbersSettingsActivity.class),
    207                 FilteredNumberCompat.createManageBlockedNumbersIntent(getContext()).getComponent());
    208     }
    209 
    210     public void testCreateManageBlockedNumbersIntent_N_Enabled_NotMigrated() {
    211         if (!CompatUtils.isNCompatible()) {
    212             return;
    213         }
    214         assertEquals(new ComponentName(getContext(), BlockedNumbersSettingsActivity.class),
    215                 FilteredNumberCompat.createManageBlockedNumbersIntent(getContext()).getComponent());
    216     }
    217 
    218     public void testCreateManageBlockedNumbersIntent_N_Enabled_Migrated() {
    219         if (!CompatUtils.isNCompatible()) {
    220             return;
    221         }
    222         when(mSharedPreferences
    223                 .getBoolean(FilteredNumberCompat.HAS_MIGRATED_TO_NEW_BLOCKING_KEY, false))
    224                 .thenReturn(true);
    225         assertFalse(new ComponentName(getContext(), BlockedNumbersSettingsActivity.class)
    226                 .equals(FilteredNumberCompat.createManageBlockedNumbersIntent(getContext())
    227                         .getComponent()));
    228     }
    229 
    230     public void testCanCurrentUserOpenBlockSettings_M_SecondaryUser() {
    231         if (CompatUtils.isNCompatible()) {
    232             return;
    233         }
    234         UserManager userManager = mock(UserManager.class);
    235         when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(userManager);
    236         when(userManager.isSystemUser()).thenReturn(false);
    237         assertFalse(FilteredNumberCompat.canCurrentUserOpenBlockSettings(mContext));
    238         verify(mContext).getSystemService(Context.USER_SERVICE);
    239         verify(userManager).isSystemUser();
    240     }
    241 
    242     public void testCanCurrentUserOpenBlockSettings_M_PrimaryUser() {
    243         if (CompatUtils.isNCompatible()) {
    244             return;
    245         }
    246         UserManager userManager = mock(UserManager.class);
    247         when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(userManager);
    248         when(userManager.isSystemUser()).thenReturn(true);
    249         assertTrue(FilteredNumberCompat.canCurrentUserOpenBlockSettings(mContext));
    250         verify(mContext).getSystemService(Context.USER_SERVICE);
    251         verify(userManager).isSystemUser();
    252     }
    253 
    254     public void testCanAttemptBlockOperations_M_SecondaryUser() {
    255         if (CompatUtils.isNCompatible()) {
    256             return;
    257         }
    258         UserManager userManager = mock(UserManager.class);
    259         when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(userManager);
    260         when(userManager.isSystemUser()).thenReturn(false);
    261         assertFalse(FilteredNumberCompat.canAttemptBlockOperations(mContext));
    262         verify(mContext).getSystemService(Context.USER_SERVICE);
    263         verify(userManager).isSystemUser();
    264     }
    265 
    266     public void testCanAttemptBlockOperations_M_PrimaryUser() {
    267         if (CompatUtils.isNCompatible()) {
    268             return;
    269         }
    270         UserManager userManager = mock(UserManager.class);
    271         when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(userManager);
    272         when(userManager.isSystemUser()).thenReturn(true);
    273         assertTrue(FilteredNumberCompat.canAttemptBlockOperations(mContext));
    274         verify(mContext).getSystemService(Context.USER_SERVICE);
    275         verify(userManager).isSystemUser();
    276     }
    277 
    278     private ContentValues newExpectedContentValuesM(String number, String e164Number,
    279             String countryIso) {
    280         ContentValues contentValues = new ContentValues();
    281         contentValues.put(FilteredNumberColumns.NUMBER, number);
    282         contentValues.put(FilteredNumberColumns.NORMALIZED_NUMBER, e164Number);
    283         contentValues.put(FilteredNumberColumns.COUNTRY_ISO, countryIso);
    284         contentValues.put(FilteredNumberColumns.TYPE, FilteredNumberTypes.BLOCKED_NUMBER);
    285         contentValues.put(FilteredNumberColumns.SOURCE, FilteredNumberSources.USER);
    286         return contentValues;
    287     }
    288 
    289     private void assertArrayEquals(String[] expected, String[] actual) {
    290         assertEquals(Arrays.toString(expected), Arrays.toString(actual));
    291     }
    292 }
    293