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 androidx.preference.PreferenceGroup;
27 import androidx.preference.PreferenceManager;
28 import androidx.preference.PreferenceScreen;
29 
30 import com.android.emergency.PreferenceKeys;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.robolectric.RobolectricTestRunner;
38 import org.robolectric.RuntimeEnvironment;
39 
40 /** Unit tests for {@link EmergencyEditTextPreference}. */
41 @RunWith(RobolectricTestRunner.class)
42 public class EmergencyEditTextPreferenceTest {
43     @Mock private PreferenceManager mPreferenceManager;
44     @Mock private SharedPreferences mSharedPreferences;
45     private EmergencyEditTextPreference mPreference;
46 
47     @Before
setUp()48     public void setUp() {
49         MockitoAnnotations.initMocks(this);
50 
51         when(mPreferenceManager.getSharedPreferences()).thenReturn(mSharedPreferences);
52 
53         Context context = RuntimeEnvironment.application;
54 
55         mPreference = spy(new EmergencyEditTextPreference(context, null));
56 
57         PreferenceGroup prefRoot = spy(new PreferenceScreen(context, null));
58         when(prefRoot.getPreferenceManager()).thenReturn(mPreferenceManager);
59         prefRoot.addPreference(mPreference);
60     }
61 
62     @Test
testDefaultProperties()63     public void testDefaultProperties() {
64         assertThat(mPreference.isEnabled()).isTrue();
65         assertThat(mPreference.isPersistent()).isTrue();
66         assertThat(mPreference.isSelectable()).isTrue();
67         assertThat(mPreference.isNotSet()).isTrue();
68     }
69 
70     @Test
testReloadFromPreference()71     public void testReloadFromPreference() throws Throwable {
72         mPreference.setKey(PreferenceKeys.KEY_MEDICAL_CONDITIONS);
73 
74         String medicalConditions = "Asthma";
75         when(mSharedPreferences.getString(eq(mPreference.getKey()), nullable(String.class)))
76                 .thenReturn(medicalConditions);
77 
78         mPreference.reloadFromPreference();
79         assertThat(mPreference.getText()).isEqualTo(medicalConditions);
80         assertThat(mPreference.isNotSet()).isFalse();
81     }
82 
83     @Test
testOnPreferenceChange()84     public void testOnPreferenceChange() throws Throwable {
85         final String medicalConditions = "Asthma";
86         mPreference.onPreferenceChange(mPreference, medicalConditions);
87 
88         assertThat(mPreference.getSummary()).isEqualTo(medicalConditions);
89     }
90 
91     @Test
testSetText()92     public void testSetText() throws Throwable {
93         final String medicalConditions = "Asthma";
94         mPreference.setText(medicalConditions);
95 
96         assertThat(mPreference.getText()).isEqualTo(medicalConditions);
97         assertThat(mPreference.getSummary()).isEqualTo(medicalConditions);
98     }
99 }
100