Home | History | Annotate | Download | only in development
      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 
     17 package com.android.settings.development;
     18 
     19 import android.app.Activity;
     20 import android.content.Context;
     21 import android.provider.SearchIndexableResource;
     22 import android.support.v7.preference.Preference;
     23 import android.support.v7.preference.PreferenceManager;
     24 import android.support.v7.preference.PreferenceScreen;
     25 
     26 import com.android.settings.R;
     27 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     28 import com.android.settings.TestConfig;
     29 import com.android.settings.testutils.FakeFeatureFactory;
     30 import com.android.settings.testutils.shadow.SettingsShadowResources;
     31 import com.android.settingslib.drawer.CategoryKey;
     32 
     33 import org.junit.Before;
     34 import org.junit.Test;
     35 import org.junit.runner.RunWith;
     36 import org.mockito.Answers;
     37 import org.mockito.Mock;
     38 import org.mockito.MockitoAnnotations;
     39 import org.robolectric.RuntimeEnvironment;
     40 import org.robolectric.annotation.Config;
     41 import org.robolectric.shadows.ShadowApplication;
     42 
     43 import java.util.ArrayList;
     44 import java.util.List;
     45 
     46 import static com.google.common.truth.Truth.assertThat;
     47 import static org.mockito.Answers.RETURNS_DEEP_STUBS;
     48 import static org.mockito.Matchers.any;
     49 import static org.mockito.Mockito.doReturn;
     50 import static org.mockito.Mockito.spy;
     51 import static org.mockito.Mockito.times;
     52 import static org.mockito.Mockito.verify;
     53 import static org.mockito.Mockito.when;
     54 
     55 @RunWith(SettingsRobolectricTestRunner.class)
     56 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION,
     57         shadows = {
     58                 SettingsShadowResources.class,
     59                 SettingsShadowResources.SettingsShadowTheme.class
     60         })
     61 public class DevelopmentSettingsTest {
     62 
     63     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     64     private Context mContext;
     65     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     66     private Activity mActivity;
     67     @Mock(answer = RETURNS_DEEP_STUBS)
     68     private PreferenceScreen mScreen;
     69     @Mock
     70     private PreferenceManager mPreferenceManager;
     71 
     72     private FakeFeatureFactory mFeatureFactory;
     73     private DevelopmentSettings mSettings;
     74 
     75     @Before
     76     public void setUp() {
     77         MockitoAnnotations.initMocks(this);
     78         FakeFeatureFactory.setupForTest(mContext);
     79         mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
     80         mSettings = spy(new DevelopmentSettings());
     81     }
     82 
     83     @Test
     84     public void addDashboardCategoryPreference_shouldAddToScreen() {
     85         final List<Preference> preferences = new ArrayList<>();
     86         preferences.add(new Preference(ShadowApplication.getInstance().getApplicationContext()));
     87         preferences.add(new Preference(ShadowApplication.getInstance().getApplicationContext()));
     88         doReturn(mScreen).when(mSettings).getPreferenceScreen();
     89         doReturn(mPreferenceManager).when(mSettings).getPreferenceManager();
     90         doReturn(mActivity).when(mSettings).getActivity();
     91         when(mPreferenceManager.getContext()).thenReturn(mContext);
     92         when(mFeatureFactory.dashboardFeatureProvider.getPreferencesForCategory(
     93                 mActivity, mContext, mSettings.getMetricsCategory(),
     94                 CategoryKey.CATEGORY_SYSTEM_DEVELOPMENT))
     95                 .thenReturn(preferences);
     96 
     97         mSettings.onAttach(mContext);
     98         mSettings.addDashboardCategoryPreferences();
     99 
    100         verify(mScreen, times(2)).addPreference(any(Preference.class));
    101     }
    102 
    103     @Test
    104     public void searchIndex_shouldIndexFromPrefXml() {
    105         final List<SearchIndexableResource> index =
    106                 DevelopmentSettings.SEARCH_INDEX_DATA_PROVIDER.getXmlResourcesToIndex(
    107                         RuntimeEnvironment.application, true);
    108 
    109         assertThat(index.size()).isEqualTo(1);
    110         assertThat(index.get(0).xmlResId).isEqualTo(R.xml.development_prefs);
    111     }
    112 
    113     @Test
    114     public void searchIndex_pageDisabled_shouldAddAllKeysToNonIndexable() {
    115         final Context appContext = RuntimeEnvironment.application;
    116         new DevelopmentSettingsEnabler(appContext, null /* lifecycle */)
    117                 .disableDevelopmentSettings();
    118 
    119         final List<String> nonIndexableKeys =
    120                 DevelopmentSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(appContext);
    121 
    122         assertThat(nonIndexableKeys).contains("development_prefs_screen");
    123     }
    124 
    125     @Test
    126     public void searchIndex_pageEnabled_shouldNotAddKeysToNonIndexable() {
    127         final Context appContext = RuntimeEnvironment.application;
    128         new DevelopmentSettingsEnabler(appContext, null /* lifecycle */)
    129                 .enableDevelopmentSettings();
    130 
    131         final List<String> nonIndexableKeys =
    132                 DevelopmentSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(appContext);
    133 
    134         assertThat(nonIndexableKeys).doesNotContain("development_prefs_screen");
    135     }
    136 }
    137