1 /* 2 * Copyright (C) 2016 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 android.preference.PreferenceManager; 19 import android.test.ActivityInstrumentationTestCase2; 20 import android.test.suitebuilder.annotation.MediumTest; 21 22 import com.android.emergency.PreferenceKeys; 23 import com.android.emergency.R; 24 import com.android.emergency.edit.EditEmergencyInfoFragment; 25 import com.android.emergency.edit.EditInfoActivity; 26 27 /** 28 * Tests for {@link EmergencyEditTextPreference}. 29 */ 30 @MediumTest 31 public class EmergencyEditTextPreferenceTest 32 extends ActivityInstrumentationTestCase2<EditInfoActivity> { 33 private EmergencyEditTextPreference mPreference; 34 private EditEmergencyInfoFragment mEditInfoFragment; 35 EmergencyEditTextPreferenceTest()36 public EmergencyEditTextPreferenceTest() { 37 super(EditInfoActivity.class); 38 } 39 40 @Override setUp()41 protected void setUp() throws Exception { 42 super.setUp(); 43 mEditInfoFragment = (EditEmergencyInfoFragment) getActivity().getFragments().get(0).second; 44 mPreference = (EmergencyEditTextPreference) 45 mEditInfoFragment.findPreference(PreferenceKeys.KEY_MEDICAL_CONDITIONS); 46 try { 47 runTestOnUiThread(new Runnable() { 48 @Override 49 public void run() { 50 mPreference.setText(""); 51 } 52 }); 53 } catch (Throwable throwable) { 54 fail("Should not throw exception"); 55 } 56 } 57 58 @Override tearDown()59 protected void tearDown() throws Exception { 60 PreferenceManager.getDefaultSharedPreferences(getActivity()).edit().clear().commit(); 61 super.tearDown(); 62 } 63 testSummary()64 public void testSummary() { 65 String summary = (String) mPreference.getSummary(); 66 String summaryExp = 67 getActivity().getResources().getString(R.string.unknown_medical_conditions); 68 assertEquals(summaryExp, summary); 69 } 70 testTitle()71 public void testTitle() { 72 String title = (String) mPreference.getTitle(); 73 String titleExp = 74 getActivity().getResources().getString(R.string.medical_conditions); 75 assertEquals(titleExp, title); 76 } 77 testProperties()78 public void testProperties() { 79 assertNotNull(mPreference); 80 assertEquals(PreferenceKeys.KEY_MEDICAL_CONDITIONS, mPreference.getKey()); 81 assertTrue(mPreference.isEnabled()); 82 assertTrue(mPreference.isPersistent()); 83 assertTrue(mPreference.isSelectable()); 84 assertTrue(mPreference.isNotSet()); 85 assertEquals("", mPreference.getText()); 86 } 87 testReloadFromPreference()88 public void testReloadFromPreference() throws Throwable { 89 String medicalConditions = "Asthma"; 90 mEditInfoFragment.getPreferenceManager().getSharedPreferences().edit() 91 .putString(mPreference.getKey(), medicalConditions).commit(); 92 runTestOnUiThread(new Runnable() { 93 @Override 94 public void run() { 95 mPreference.reloadFromPreference(); 96 } 97 }); 98 assertEquals(medicalConditions, mPreference.getText()); 99 assertFalse(mPreference.isNotSet()); 100 } 101 testSetText()102 public void testSetText() throws Throwable { 103 final String medicalConditions = "Asthma"; 104 runTestOnUiThread(new Runnable() { 105 @Override 106 public void run() { 107 mPreference.setText(medicalConditions); 108 } 109 }); 110 111 assertEquals(medicalConditions, mPreference.getText()); 112 assertEquals(medicalConditions, mPreference.getSummary()); 113 } 114 } 115