1 /* 2 * Copyright (C) 2017 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 package com.android.emergency.util; 17 18 import static com.google.common.truth.Truth.assertThat; 19 import static org.mockito.Mockito.any; 20 import static org.mockito.Mockito.anyInt; 21 import static org.mockito.Mockito.anyString; 22 import static org.mockito.Mockito.eq; 23 import static org.mockito.Mockito.verify; 24 import static org.mockito.Mockito.when; 25 26 import android.content.ComponentName; 27 import android.content.ContentResolver; 28 import android.content.Context; 29 import android.content.SharedPreferences; 30 import android.content.pm.PackageManager; 31 import android.database.Cursor; 32 import android.net.Uri; 33 34 import com.android.emergency.ContactTestUtils; 35 import com.android.emergency.PreferenceKeys; 36 37 import org.junit.Before; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 import org.mockito.Mock; 41 import org.mockito.MockitoAnnotations; 42 import org.robolectric.RobolectricTestRunner; 43 import org.robolectric.RuntimeEnvironment; 44 45 /** Unit tests for {@link PreferenceUtils}. */ 46 @RunWith(RobolectricTestRunner.class) 47 public final class PreferenceUtilsTest { 48 private static final String PACKAGE_NAME = "com.android.emergency"; 49 private static final String NAME = "Jane"; 50 private static final String PHONE_NUMBER = "5150"; 51 52 @Mock ContentResolver mContentResolver; 53 @Mock Context mContext; 54 @Mock Cursor mCursor; 55 @Mock PackageManager mPackageManager; 56 @Mock SharedPreferences mSharedPreferences; 57 @Mock SharedPreferences.Editor mSharedPreferencesEditor; 58 59 private ComponentName mComponentName; 60 61 @Before setUp()62 public void setUp() { 63 MockitoAnnotations.initMocks(this); 64 65 mComponentName = new ComponentName( 66 PACKAGE_NAME, 67 PACKAGE_NAME + PreferenceUtils.SETTINGS_SUGGESTION_ACTIVITY_ALIAS); 68 69 when(mContext.getContentResolver()).thenReturn(mContentResolver); 70 when(mContext.getPackageManager()).thenReturn(mPackageManager); 71 when(mContext.getPackageName()).thenReturn(PACKAGE_NAME); 72 when(mContext.getSharedPreferences(anyString(), anyInt())).thenReturn(mSharedPreferences); 73 when(mSharedPreferences.edit()).thenReturn(mSharedPreferencesEditor); 74 when(mSharedPreferencesEditor.putString(anyString(), anyString())) 75 .thenReturn(mSharedPreferencesEditor); 76 } 77 78 @Test testHasAtLeastOnePreferenceSet_notSet()79 public void testHasAtLeastOnePreferenceSet_notSet() { 80 when(mSharedPreferences.getString(anyString(), anyString())).thenReturn(""); 81 82 assertThat(PreferenceUtils.hasAtLeastOnePreferenceSet(mContext)).isFalse(); 83 } 84 85 @Test testHasAtLeastOnePreferenceSet_set()86 public void testHasAtLeastOnePreferenceSet_set() { 87 when(mSharedPreferences.getString(anyString(), anyString())).thenReturn("mxyzptlk"); 88 89 assertThat(PreferenceUtils.hasAtLeastOnePreferenceSet(mContext)).isTrue(); 90 } 91 92 @Test testHasAtLeastOneEmergencyContact_notSet()93 public void testHasAtLeastOneEmergencyContact_notSet() { 94 when(mSharedPreferences.getString(eq(PreferenceKeys.KEY_EMERGENCY_CONTACTS), any())) 95 .thenReturn(""); 96 97 assertThat(PreferenceUtils.hasAtLeastOneEmergencyContact(mContext)).isFalse(); 98 } 99 100 @Test testHasAtLeastOneEmergencyContact_set()101 public void testHasAtLeastOneEmergencyContact_set() { 102 final Uri contactUri = ContactTestUtils.createContact( 103 RuntimeEnvironment.application.getContentResolver(), NAME, PHONE_NUMBER); 104 when(mSharedPreferences.getString(eq(PreferenceKeys.KEY_EMERGENCY_CONTACTS), any())) 105 .thenReturn(contactUri.toString()); 106 when(mContentResolver.query(eq(contactUri), any(), any(), any(), any())) 107 .thenReturn(mCursor); 108 when(mCursor.moveToFirst()).thenReturn(true); 109 110 assertThat(PreferenceUtils.hasAtLeastOneEmergencyContact(mContext)).isTrue(); 111 } 112 113 @Test testEnableSettingsSuggestion()114 public void testEnableSettingsSuggestion() { 115 PreferenceUtils.enableSettingsSuggestion(mContext); 116 117 verify(mPackageManager).setComponentEnabledSetting( 118 eq(mComponentName), 119 eq(PackageManager.COMPONENT_ENABLED_STATE_ENABLED), 120 eq(PackageManager.DONT_KILL_APP)); 121 } 122 123 @Test testUpdateSettingsSuggestionState_contactSet()124 public void testUpdateSettingsSuggestionState_contactSet() { 125 final Uri contactUri = ContactTestUtils.createContact( 126 RuntimeEnvironment.application.getContentResolver(), NAME, PHONE_NUMBER); 127 when(mSharedPreferences.getString(eq(PreferenceKeys.KEY_EMERGENCY_CONTACTS), any())) 128 .thenReturn(contactUri.toString()); 129 when(mContentResolver.query(eq(contactUri), any(), any(), any(), any())) 130 .thenReturn(mCursor); 131 when(mCursor.moveToFirst()).thenReturn(true); 132 133 PreferenceUtils.updateSettingsSuggestionState(mContext); 134 135 verify(mPackageManager).setComponentEnabledSetting( 136 eq(mComponentName), 137 eq(PackageManager.COMPONENT_ENABLED_STATE_DISABLED), 138 eq(PackageManager.DONT_KILL_APP)); 139 } 140 141 @Test testUpdateSettingsSuggestionState_noPreferencesOrContactSet()142 public void testUpdateSettingsSuggestionState_noPreferencesOrContactSet() { 143 when(mSharedPreferences.getString(anyString(), anyString())).thenReturn(""); 144 145 PreferenceUtils.updateSettingsSuggestionState(mContext); 146 147 verify(mPackageManager).setComponentEnabledSetting( 148 eq(mComponentName), 149 eq(PackageManager.COMPONENT_ENABLED_STATE_ENABLED), 150 eq(PackageManager.DONT_KILL_APP)); 151 } 152 153 @Test testUpdateSettingsSuggestionState_preferenceSet()154 public void testUpdateSettingsSuggestionState_preferenceSet() { 155 when(mSharedPreferences.getString(eq(PreferenceKeys.KEY_ALLERGIES), any())) 156 .thenReturn("peanuts"); 157 158 PreferenceUtils.updateSettingsSuggestionState(mContext); 159 160 verify(mPackageManager).setComponentEnabledSetting( 161 eq(mComponentName), 162 eq(PackageManager.COMPONENT_ENABLED_STATE_DISABLED), 163 eq(PackageManager.DONT_KILL_APP)); 164 } 165 } 166