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