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