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.preferences; 17 18 import static com.google.common.truth.Truth.assertThat; 19 import static org.mockito.Mockito.eq; 20 import static org.mockito.Mockito.nullable; 21 import static org.mockito.Mockito.spy; 22 import static org.mockito.Mockito.when; 23 24 import android.content.Context; 25 import android.content.SharedPreferences; 26 import android.support.v7.preference.PreferenceGroup; 27 import android.support.v7.preference.PreferenceManager; 28 import android.support.v7.preference.PreferenceScreen; 29 import android.text.TextUtils; 30 31 import com.android.emergency.PreferenceKeys; 32 import com.android.emergency.R; 33 import com.android.emergency.TestConfig; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 import org.mockito.invocation.InvocationOnMock; 41 import org.mockito.stubbing.Answer; 42 import org.robolectric.RobolectricTestRunner; 43 import org.robolectric.RuntimeEnvironment; 44 import org.robolectric.annotation.Config; 45 46 /** Unit tests for {@link EmergencyListPreference}. */ 47 @RunWith(RobolectricTestRunner.class) 48 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 49 public final class EmergencyListPreferenceTest { 50 @Mock private PreferenceManager mPreferenceManager; 51 @Mock private SharedPreferences mSharedPreferences; 52 private EmergencyListPreference mPreference; 53 54 @Before 55 public void setUp() { 56 MockitoAnnotations.initMocks(this); 57 58 when(mPreferenceManager.getSharedPreferences()).thenReturn(mSharedPreferences); 59 60 Context context = RuntimeEnvironment.application; 61 62 mPreference = spy( 63 new EmergencyListPreference(RuntimeEnvironment.application, null /* attrs */)); 64 65 PreferenceGroup prefRoot = spy(new PreferenceScreen(context, null /* attrs */)); 66 when(prefRoot.getPreferenceManager()).thenReturn(mPreferenceManager); 67 prefRoot.addPreference(mPreference); 68 } 69 70 @Test 71 public void testReloadFromPreference() { 72 CharSequence[] organDonorValues = 73 RuntimeEnvironment.application.getResources().getStringArray( 74 R.array.organ_donor_entries); 75 when(mSharedPreferences.getString( 76 eq(PreferenceKeys.KEY_ORGAN_DONOR), nullable(String.class))) 77 .thenReturn((String) organDonorValues[0]); 78 79 mPreference.setKey(PreferenceKeys.KEY_ORGAN_DONOR); 80 mPreference.setEntryValues(organDonorValues); 81 82 mPreference.reloadFromPreference(); 83 assertThat(mPreference.getValue()).isEqualTo(mPreference.getEntryValues()[0]); 84 assertThat(mPreference.isNotSet()).isFalse(); 85 } 86 87 @Test 88 public void testSetValue() { 89 CharSequence[] organDonorEntries = 90 RuntimeEnvironment.application.getResources().getStringArray( 91 R.array.organ_donor_entries); 92 CharSequence[] organDonorValues = 93 RuntimeEnvironment.application.getResources().getStringArray( 94 R.array.organ_donor_values); 95 mPreference.setKey(PreferenceKeys.KEY_ORGAN_DONOR); 96 mPreference.setEntries(organDonorEntries); 97 mPreference.setEntryValues(organDonorValues); 98 when(mSharedPreferences.getString( 99 eq(PreferenceKeys.KEY_ORGAN_DONOR), nullable(String.class))) 100 .thenAnswer(new CyclingStringArrayAnswer(organDonorValues)); 101 102 for (int i = 0; i < mPreference.getEntryValues().length; i++) { 103 mPreference.setValue((String) mPreference.getEntryValues()[i]); 104 105 assertThat(mPreference.getValue()).isEqualTo(mPreference.getEntryValues()[i]); 106 if (!TextUtils.isEmpty(mPreference.getEntryValues()[i])) { 107 assertThat(mPreference.getSummary()).isEqualTo(mPreference.getEntries()[i]); 108 } else { 109 assertThat(mPreference.getSummary()).isEqualTo( 110 RuntimeEnvironment.application.getResources().getString( 111 R.string.unknown_organ_donor)); 112 } 113 } 114 } 115 116 /** An Answer that cycles through a list of string values in its answer. */ 117 private static class CyclingStringArrayAnswer implements Answer<String> { 118 private CharSequence[] mValues; 119 private int mIndex; 120 121 public CyclingStringArrayAnswer(CharSequence[] values) { 122 mValues = values; 123 mIndex = 0; 124 } 125 126 @Override 127 public String answer(InvocationOnMock invocation) { 128 String value = (String) mValues[mIndex % mValues.length]; 129 mIndex++; 130 return value; 131 } 132 } 133 } 134