1 package com.android.emergency.edit; 2 3 import static com.google.common.truth.Truth.assertThat; 4 import static org.mockito.ArgumentMatchers.any; 5 import static org.mockito.ArgumentMatchers.anyInt; 6 import static org.mockito.ArgumentMatchers.eq; 7 import static org.mockito.Mockito.doReturn; 8 import static org.mockito.Mockito.spy; 9 import static org.mockito.Mockito.verify; 10 11 import android.content.ComponentName; 12 import android.content.Context; 13 import android.content.pm.PackageManager; 14 import androidx.preference.Preference; 15 import androidx.preference.Preference.OnPreferenceChangeListener; 16 import com.android.emergency.edit.EditInfoFragment.PreferenceChangeListener; 17 import org.junit.Before; 18 import org.junit.Test; 19 import org.junit.runner.RunWith; 20 import org.mockito.Mock; 21 import org.mockito.MockitoAnnotations; 22 import org.robolectric.RobolectricTestRunner; 23 import org.robolectric.RuntimeEnvironment; 24 25 @RunWith(RobolectricTestRunner.class) 26 public class EditInfoFragmentTest { 27 28 @Mock 29 private PackageManager mPackageManager; 30 private Context mContext; 31 32 @Before setUp()33 public void setUp() { 34 MockitoAnnotations.initMocks(this); 35 mContext = spy(RuntimeEnvironment.application); 36 doReturn(mPackageManager).when(mContext).getPackageManager(); 37 } 38 39 @Test testOnPreferenceChange_without_listener()40 public void testOnPreferenceChange_without_listener() { 41 final PreferenceChangeListener listener = new PreferenceChangeListener(mContext); 42 final PreferenceWithoutListener preference = spy(new PreferenceWithoutListener(mContext)); 43 final Object value = new Object(); 44 45 assertThat(listener.onPreferenceChange(preference, value)).isTrue(); 46 verify(mPackageManager).setComponentEnabledSetting(any(ComponentName.class), anyInt(), 47 eq(PackageManager.DONT_KILL_APP)); 48 } 49 50 @Test testOnPreferenceChange_with_listener()51 public void testOnPreferenceChange_with_listener() { 52 final PreferenceChangeListener listener = new PreferenceChangeListener(mContext); 53 final PreferenceWithListener preference = spy(new PreferenceWithListener(mContext)); 54 final Object value = new Object(); 55 final boolean resultValue = false; 56 doReturn(resultValue).when(preference).onPreferenceChange(preference, value); 57 58 assertThat(listener.onPreferenceChange(preference, value)).isEqualTo(resultValue); 59 verify(mPackageManager).setComponentEnabledSetting(any(ComponentName.class), anyInt(), 60 eq(PackageManager.DONT_KILL_APP)); 61 verify(preference).onPreferenceChange(preference, value); 62 } 63 64 public class PreferenceWithoutListener extends Preference { 65 PreferenceWithoutListener(Context context)66 public PreferenceWithoutListener(Context context) { 67 super(context); 68 } 69 } 70 71 public class PreferenceWithListener extends Preference implements OnPreferenceChangeListener { 72 PreferenceWithListener(Context context)73 public PreferenceWithListener(Context context) { 74 super(context); 75 } 76 77 @Override onPreferenceChange(Preference preference, Object o)78 public boolean onPreferenceChange(Preference preference, Object o) { 79 return false; 80 } 81 } 82 } 83