1 /* 2 * Copyright (C) 2012 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 17 package android.preference2.cts; 18 19 import android.preference.ListPreference; 20 import android.test.ActivityInstrumentationTestCase2; 21 22 import android.preference2.cts.R; 23 24 public class ListPreferenceTest 25 extends ActivityInstrumentationTestCase2<PreferenceFromCodeActivity> { 26 27 private PreferenceFromCodeActivity mActivity; 28 private ListPreference mListPref; 29 30 public ListPreferenceTest() { 31 super(PreferenceFromCodeActivity.class); 32 } 33 34 @Override 35 protected void setUp() throws Exception { 36 super.setUp(); 37 mActivity = getActivity(); 38 mListPref = (ListPreference) mActivity.findPreference("list_preference"); 39 } 40 41 public void testGetEntries() { 42 String[] entries = convertToStringArray((CharSequence[]) mListPref.getEntries()); 43 String[] entriesExp = mActivity.getResources().getStringArray( 44 R.array.entries_list_preference); 45 compareArrays(entriesExp, entries); 46 } 47 48 public void testGetEntryValues() { 49 String[] entryValues = convertToStringArray((CharSequence[]) mListPref.getEntryValues()); 50 String[] entryValuesExp = mActivity.getResources().getStringArray( 51 R.array.entryvalues_list_preference); 52 compareArrays(entryValuesExp,entryValues); 53 } 54 55 public void testIsEnabled() { 56 boolean isEnabled = mListPref.isEnabled(); 57 assertTrue(isEnabled); 58 } 59 60 private synchronized String[] convertToStringArray(CharSequence[] array){ 61 String[] strArray = new String[array.length]; 62 for(int i = 0; i < array.length; i++){ 63 strArray[i] = (String) array[i]; 64 } 65 return strArray; 66 } 67 68 private void compareArrays(String[] firstArray,String[] secArray){ 69 assertEquals(firstArray.length, secArray.length); 70 for(int i = 0; i < firstArray.length; i++) { 71 assertEquals(firstArray[i], secArray[i]); 72 } 73 } 74 } 75