Home | History | Annotate | Download | only in cts
      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 
     17 package android.preference2.cts;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertFalse;
     21 import static org.junit.Assert.assertTrue;
     22 
     23 import android.preference.Preference;
     24 import android.preference.PreferenceScreen;
     25 import android.support.test.filters.SmallTest;
     26 import android.support.test.rule.ActivityTestRule;
     27 import android.support.test.runner.AndroidJUnit4;
     28 import android.test.UiThreadTest;
     29 
     30 import com.android.compatibility.common.util.PollingCheck;
     31 
     32 import org.junit.Before;
     33 import org.junit.Rule;
     34 import org.junit.Test;
     35 import org.junit.runner.RunWith;
     36 
     37 /**
     38  * Tests for {@link android.preference.Preference#setRecycleEnabled()} API.
     39  */
     40 @SmallTest
     41 @RunWith(AndroidJUnit4.class)
     42 public class PreferenceRecycleTest {
     43 
     44     private PreferencesFromXmlRecycle mActivity;
     45 
     46     private static int TIMEOUT_MS = 5000;
     47 
     48     @Rule
     49     public ActivityTestRule<PreferencesFromXmlRecycle> mActivityRule =
     50             new ActivityTestRule<>(PreferencesFromXmlRecycle.class);
     51 
     52 
     53     @Before
     54     public void setup() {
     55         mActivity = mActivityRule.getActivity();
     56     }
     57 
     58     /**
     59      * Tests that recycling is enabled as default.
     60      */
     61     @Test
     62     @UiThreadTest
     63     public void recycleIsOnByDefaultTest() {
     64         CustomCheckBoxPreference pref = new CustomCheckBoxPreference(mActivity);
     65         assertTrue(pref.isRecycleEnabled());
     66     }
     67 
     68     @Test
     69     @UiThreadTest
     70     public void recycleSetGetTest() {
     71         Preference pref = new Preference(mActivity);
     72         pref.setRecycleEnabled(false);
     73         assertFalse(pref.isRecycleEnabled());
     74     }
     75 
     76     /**
     77      * Tests that recycleEnabled attribute is correctly reflected when defined via XLM.
     78      */
     79     @Test
     80     public void recycleSetViaXmlTest() throws Throwable {
     81         PreferenceScreen screen = mActivity.getPreferenceScreen();
     82 
     83         RecycleCheckPreference recyclePref =
     84                 (RecycleCheckPreference)screen.findPreference("pref_checkbox_recycle");
     85         RecycleCheckPreference noRecyclePref =
     86                 (RecycleCheckPreference)screen.findPreference("pref_checkbox_no_recycle");
     87 
     88         // At the beginning the views must be always created (no recycling involved).
     89         assertEquals(1, recyclePref.getViewCalledCnt);
     90         assertTrue(recyclePref.wasConvertViewNullInLastCall);
     91 
     92         assertEquals(1, noRecyclePref.getViewCalledCnt);
     93         assertTrue(noRecyclePref.wasConvertViewNullInLastCall);
     94 
     95         // Change a value of some pref to force the list to refresh
     96         mActivityRule.runOnUiThread(() -> recyclePref.setChecked(!recyclePref.isChecked()));
     97 
     98         // Wait for the list to refresh
     99         PollingCheck.waitFor(TIMEOUT_MS,
    100                 () -> recyclePref.getViewCalledCnt == 2 && noRecyclePref.getViewCalledCnt == 2);
    101 
    102         assertEquals(2, recyclePref.getViewCalledCnt);
    103         assertFalse(recyclePref.wasConvertViewNullInLastCall); // Recycling
    104 
    105         assertEquals(2, noRecyclePref.getViewCalledCnt);
    106         assertTrue(noRecyclePref.wasConvertViewNullInLastCall); // Not recycling
    107     }
    108 
    109     /**
    110      * Tests that recycleEnabled attribute is correctly reflected when defined via
    111      * {@link android.preference.Preference#setRecycleEnabled}.
    112      */
    113     @Test
    114     public void recycleSetViaCodeTest() throws Throwable {
    115 
    116         final PreferenceScreen screen = mActivity.getPreferenceScreen();
    117 
    118         mActivityRule.runOnUiThread(() -> {
    119             RecycleCheckPreference recyclePref = new RecycleCheckPreference(mActivity);
    120             recyclePref.setKey("recyclePref");
    121             recyclePref.setRecycleEnabled(true);
    122             screen.addPreference(recyclePref);
    123 
    124             RecycleCheckPreference noRecyclePref = new RecycleCheckPreference(mActivity);
    125             noRecyclePref.setKey("noRecyclePref");
    126             noRecyclePref.setRecycleEnabled(false);
    127             screen.addPreference(noRecyclePref);
    128         });
    129 
    130         // Select the last item in the list to make sure the newly added prefs is actually
    131         // displayed even on small screen like watches.
    132         mActivityRule.runOnUiThread(() -> {
    133             mActivity.getListView().setSelection(mActivity.getListView().getCount() - 1);
    134         });
    135 
    136         // Grab the preferences we just created on the Ui thread.
    137         RecycleCheckPreference recyclePref =
    138                 (RecycleCheckPreference)screen.findPreference("recyclePref");
    139         RecycleCheckPreference noRecyclePref =
    140                 (RecycleCheckPreference)screen.findPreference("noRecyclePref");
    141 
    142         // Wait for the views to be created (because we may scroll the screen to display the
    143         // latest views, these views may get refreshed more than once).
    144         PollingCheck.waitFor(TIMEOUT_MS,
    145                 () -> recyclePref.getViewCalledCnt > 0 && noRecyclePref.getViewCalledCnt > 0);
    146 
    147         // Change a value of some pref to force the list to refresh
    148         mActivityRule.runOnUiThread(() -> recyclePref.setChecked(!recyclePref.isChecked()));
    149 
    150         // Wait for the list to refresh
    151         PollingCheck.waitFor(TIMEOUT_MS,
    152                 () -> recyclePref.getViewCalledCnt > 1 && noRecyclePref.getViewCalledCnt > 1);
    153 
    154         assertFalse(recyclePref.wasConvertViewNullInLastCall); // Recycling
    155         assertTrue(noRecyclePref.wasConvertViewNullInLastCall); // Not recycling
    156     }
    157 }
    158