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 */ 16package com.android.settingslib.core; 17 18import static com.google.common.truth.Truth.assertThat; 19import static org.mockito.Mockito.when; 20 21import android.content.Context; 22import android.support.v7.preference.Preference; 23import android.support.v7.preference.PreferenceScreen; 24 25import org.junit.Before; 26import org.junit.Test; 27import org.junit.runner.RunWith; 28import org.mockito.Mock; 29import org.mockito.MockitoAnnotations; 30import org.robolectric.RobolectricTestRunner; 31import org.robolectric.RuntimeEnvironment; 32 33@RunWith(RobolectricTestRunner.class) 34public class AbstractPreferenceControllerTest { 35 36 @Mock 37 private PreferenceScreen mScreen; 38 39 private Context mContext; 40 private Preference mPreference; 41 private TestPrefController mTestPrefController; 42 43 @Before 44 public void setUp() { 45 MockitoAnnotations.initMocks(this); 46 mContext = RuntimeEnvironment.application; 47 mPreference = new Preference(mContext); 48 mPreference.setKey(TestPrefController.KEY_PREF); 49 when(mScreen.findPreference(TestPrefController.KEY_PREF)).thenReturn(mPreference); 50 mTestPrefController = new TestPrefController(mContext); 51 } 52 53 @Test 54 public void displayPref_ifAvailable() { 55 mTestPrefController.isAvailable = true; 56 57 mTestPrefController.displayPreference(mScreen); 58 59 assertThat(mPreference.isVisible()).isTrue(); 60 } 61 62 @Test 63 public void setVisible_prefIsVisible_shouldSetToVisible() { 64 mTestPrefController.setVisible(mScreen, TestPrefController.KEY_PREF, true /* visible */); 65 66 assertThat(mPreference.isVisible()).isTrue(); 67 } 68 69 @Test 70 public void setVisible_prefNotVisible_shouldSetToInvisible() { 71 mTestPrefController.setVisible(mScreen, TestPrefController.KEY_PREF, false /* visible */); 72 73 assertThat(mPreference.isVisible()).isFalse(); 74 } 75 76 @Test 77 public void doNotDisplayPref_ifNotAvailable() { 78 mTestPrefController.isAvailable = false; 79 80 mTestPrefController.displayPreference(mScreen); 81 82 assertThat(mPreference.isVisible()).isFalse(); 83 } 84 85 @Test 86 public void updateState_hasSummary_shouldSetSummary() { 87 mTestPrefController.updateState(mPreference); 88 89 assertThat(mPreference.getSummary()).isEqualTo(TestPrefController.TEST_SUMMARY); 90 } 91 92 private static class TestPrefController extends AbstractPreferenceController { 93 private static final String KEY_PREF = "test_pref"; 94 private static final CharSequence TEST_SUMMARY = "Test"; 95 96 public boolean isAvailable; 97 98 public TestPrefController(Context context) { 99 super(context); 100 } 101 102 @Override 103 public boolean handlePreferenceTreeClick(Preference preference) { 104 return false; 105 } 106 107 @Override 108 public boolean isAvailable() { 109 return isAvailable; 110 } 111 112 @Override 113 public String getPreferenceKey() { 114 return KEY_PREF; 115 } 116 117 @Override 118 public CharSequence getSummary() { 119 return TEST_SUMMARY; 120 } 121 } 122 123} 124