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 com.android.settings; 18 19 20 import android.content.Context; 21 import android.support.v7.preference.Preference; 22 import android.support.v7.preference.PreferenceCategory; 23 import android.support.v7.preference.PreferenceManager; 24 import android.support.v7.preference.PreferenceScreen; 25 26 import org.junit.Before; 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 import org.mockito.Mock; 30 import org.mockito.MockitoAnnotations; 31 import org.robolectric.RuntimeEnvironment; 32 import org.robolectric.annotation.Config; 33 34 import static com.google.common.truth.Truth.assertThat; 35 import static org.mockito.Mockito.mock; 36 import static org.mockito.Mockito.spy; 37 import static org.mockito.Mockito.when; 38 39 @RunWith(SettingsRobolectricTestRunner.class) 40 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 41 public class SettingsPreferenceFragmentTest { 42 43 @Mock 44 private PreferenceManager mPreferenceManager; 45 private Context mContext; 46 private TestFragment mFragment; 47 48 @Before setUp()49 public void setUp() { 50 MockitoAnnotations.initMocks(this); 51 mContext = RuntimeEnvironment.application; 52 mFragment = new TestFragment(); 53 } 54 55 @Test removePreference_nested_shouldRemove()56 public void removePreference_nested_shouldRemove() { 57 final String key = "test_key"; 58 final PreferenceScreen mScreen = spy(new PreferenceScreen(mContext, null)); 59 when(mScreen.getPreferenceManager()).thenReturn(mock(PreferenceManager.class)); 60 61 final PreferenceCategory nestedCategory = new ProgressCategory(mContext); 62 final Preference preference = new Preference(mContext); 63 preference.setKey(key); 64 preference.setPersistent(false); 65 66 mScreen.addPreference(nestedCategory); 67 nestedCategory.addPreference(preference); 68 69 assertThat(mFragment.removePreference(mScreen, key)).isTrue(); 70 assertThat(nestedCategory.getPreferenceCount()).isEqualTo(0); 71 } 72 73 @Test removePreference_flat_shouldRemove()74 public void removePreference_flat_shouldRemove() { 75 final String key = "test_key"; 76 final PreferenceScreen mScreen = spy(new PreferenceScreen(mContext, null)); 77 when(mScreen.getPreferenceManager()).thenReturn(mock(PreferenceManager.class)); 78 79 final Preference preference = mock(Preference.class); 80 when(preference.getKey()).thenReturn(key); 81 82 mScreen.addPreference(preference); 83 84 assertThat(mFragment.removePreference(mScreen, key)).isTrue(); 85 assertThat(mScreen.getPreferenceCount()).isEqualTo(0); 86 } 87 88 @Test removePreference_doNotExist_shouldNotRemove()89 public void removePreference_doNotExist_shouldNotRemove() { 90 final String key = "test_key"; 91 final PreferenceScreen mScreen = spy(new PreferenceScreen(mContext, null)); 92 when(mScreen.getPreferenceManager()).thenReturn(mock(PreferenceManager.class)); 93 94 final Preference preference = mock(Preference.class); 95 when(preference.getKey()).thenReturn(key); 96 97 mScreen.addPreference(preference); 98 99 assertThat(mFragment.removePreference(mScreen, "not" + key)).isFalse(); 100 assertThat(mScreen.getPreferenceCount()).isEqualTo(1); 101 } 102 103 public static final class TestFragment extends SettingsPreferenceFragment { 104 105 @Override getMetricsCategory()106 public int getMetricsCategory() { 107 return 0; 108 } 109 } 110 111 112 } 113