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.app.AlertDialog; 19 import android.content.DialogInterface; 20 import android.preference.PreferenceManager; 21 import android.test.ActivityInstrumentationTestCase2; 22 import android.test.suitebuilder.annotation.LargeTest; 23 import android.widget.AutoCompleteTextView; 24 25 import com.android.emergency.PreferenceKeys; 26 import com.android.emergency.R; 27 import com.android.emergency.edit.EditEmergencyInfoFragment; 28 import com.android.emergency.edit.EditInfoActivity; 29 30 /** 31 * Tests for {@link NameAutoCompletePreference}. 32 */ 33 @LargeTest 34 public class NameAutoCompletePreferenceTest 35 extends ActivityInstrumentationTestCase2<EditInfoActivity> { 36 private NameAutoCompletePreference mNameAutoCompletePreference; 37 private EditEmergencyInfoFragment mEditInfoFragment; 38 NameAutoCompletePreferenceTest()39 public NameAutoCompletePreferenceTest() { 40 super(EditInfoActivity.class); 41 } 42 43 @Override setUp()44 protected void setUp() throws Exception { 45 super.setUp(); 46 mEditInfoFragment = (EditEmergencyInfoFragment) getActivity().getFragments().get(0).second; 47 mNameAutoCompletePreference = (NameAutoCompletePreference) 48 mEditInfoFragment.findPreference(PreferenceKeys.KEY_NAME); 49 try { 50 runTestOnUiThread(new Runnable() { 51 @Override 52 public void run() { 53 mNameAutoCompletePreference.setText(""); 54 } 55 }); 56 } catch (Throwable throwable) { 57 fail("Should not throw exception: " + throwable.getMessage()); 58 } 59 } 60 61 @Override tearDown()62 protected void tearDown() throws Exception { 63 PreferenceManager.getDefaultSharedPreferences(getActivity()).edit().clear().commit(); 64 super.tearDown(); 65 } 66 testSummary()67 public void testSummary() { 68 String summary = (String) mNameAutoCompletePreference.getSummary(); 69 String summaryExp = 70 getActivity().getResources().getString(R.string.unknown_name); 71 assertEquals(summaryExp, summary); 72 } 73 testTitle()74 public void testTitle() { 75 String title = (String) mNameAutoCompletePreference.getTitle(); 76 String titleExp = 77 getActivity().getResources().getString(R.string.name); 78 assertEquals(titleExp, title); 79 } 80 testProperties()81 public void testProperties() { 82 assertNotNull(mNameAutoCompletePreference); 83 assertEquals(PreferenceKeys.KEY_NAME, mNameAutoCompletePreference.getKey()); 84 assertTrue(mNameAutoCompletePreference.isEnabled()); 85 assertTrue(mNameAutoCompletePreference.isPersistent()); 86 assertTrue(mNameAutoCompletePreference.isSelectable()); 87 assertTrue(mNameAutoCompletePreference.isNotSet()); 88 assertEquals("", mNameAutoCompletePreference.getText()); 89 } 90 testReloadFromPreference()91 public void testReloadFromPreference() throws Throwable { 92 String name = "John"; 93 mEditInfoFragment.getPreferenceManager().getSharedPreferences().edit() 94 .putString(mNameAutoCompletePreference.getKey(), name).commit(); 95 96 runTestOnUiThread(new Runnable() { 97 @Override 98 public void run() { 99 mNameAutoCompletePreference.reloadFromPreference(); 100 } 101 }); 102 assertEquals(name, mNameAutoCompletePreference.getText()); 103 assertFalse(mNameAutoCompletePreference.isNotSet()); 104 } 105 testSetText()106 public void testSetText() throws Throwable { 107 final String name = "John"; 108 runTestOnUiThread(new Runnable() { 109 @Override 110 public void run() { 111 mNameAutoCompletePreference.setText(name); 112 } 113 }); 114 115 assertEquals(name, mNameAutoCompletePreference.getText()); 116 assertEquals(name, mNameAutoCompletePreference.getSummary()); 117 } 118 testGetAutoCompleteTextView()119 public void testGetAutoCompleteTextView() { 120 AutoCompleteTextView autoCompleteTextView = 121 mNameAutoCompletePreference.getAutoCompleteTextView(); 122 assertNotNull(autoCompleteTextView); 123 } 124 testDialogShowAndDismiss_positiveButton()125 public void testDialogShowAndDismiss_positiveButton() throws Throwable { 126 assertNull(mNameAutoCompletePreference.getDialog()); 127 assertNotNull(mNameAutoCompletePreference); 128 runTestOnUiThread(new Runnable() { 129 @Override 130 public void run() { 131 mNameAutoCompletePreference.onClick(); 132 } 133 }); 134 final AlertDialog dialog = (AlertDialog) mNameAutoCompletePreference.getDialog(); 135 assertTrue(dialog.isShowing()); 136 runTestOnUiThread(new Runnable() { 137 @Override 138 public void run() { 139 dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick(); 140 } 141 }); 142 getInstrumentation().waitForIdleSync(); 143 assertFalse(dialog.isShowing()); 144 } 145 testDialogShowAndDismiss_negativeButton()146 public void testDialogShowAndDismiss_negativeButton() throws Throwable { 147 assertNull(mNameAutoCompletePreference.getDialog()); 148 runTestOnUiThread(new Runnable() { 149 @Override 150 public void run() { 151 mNameAutoCompletePreference.onClick(); 152 } 153 }); 154 final AlertDialog dialog = (AlertDialog) mNameAutoCompletePreference.getDialog(); 155 assertTrue(dialog.isShowing()); 156 runTestOnUiThread(new Runnable() { 157 @Override 158 public void run() { 159 dialog.getButton(DialogInterface.BUTTON_NEGATIVE).performClick(); 160 } 161 }); 162 getInstrumentation().waitForIdleSync(); 163 assertFalse(dialog.isShowing()); 164 } 165 } 166