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 17package com.android.settings.wifi.p2p; 18 19import android.support.v7.preference.Preference; 20import android.support.v7.preference.PreferenceCategory; 21import android.support.v7.preference.PreferenceScreen; 22 23import com.android.settings.testutils.SettingsRobolectricTestRunner; 24import com.android.settings.TestConfig; 25 26import org.junit.Before; 27import org.junit.Test; 28import org.junit.runner.RunWith; 29import org.mockito.Mock; 30import org.mockito.MockitoAnnotations; 31import org.robolectric.RuntimeEnvironment; 32import org.robolectric.annotation.Config; 33 34import static com.google.common.truth.Truth.assertThat; 35import static org.mockito.Matchers.anyString; 36import static org.mockito.Mockito.verify; 37import static org.mockito.Mockito.when; 38 39@RunWith(SettingsRobolectricTestRunner.class) 40@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 41public class P2pCategoryPreferenceControllerTest { 42 43 @Mock 44 private PreferenceScreen mPreferenceScreen; 45 @Mock 46 private PreferenceCategory mCategory; 47 private P2pCategoryPreferenceController mController; 48 49 @Before 50 public void setUp() { 51 MockitoAnnotations.initMocks(this); 52 when(mPreferenceScreen.findPreference(anyString())).thenReturn(mCategory); 53 54 mController = new P2pCategoryPreferenceController(RuntimeEnvironment.application) { 55 56 @Override 57 public String getPreferenceKey() { 58 return "test_key"; 59 } 60 }; 61 mController.displayPreference(mPreferenceScreen); 62 } 63 64 @Test 65 public void isAlwaysAvailable() { 66 assertThat(mController.isAvailable()).isTrue(); 67 } 68 69 70 @Test 71 public void removeAllChildren_shouldRemove() { 72 mController.removeAllChildren(); 73 74 verify(mCategory).removeAll(); 75 verify(mCategory).setVisible(false); 76 } 77 78 @Test 79 public void addChild_shouldAdd() { 80 final Preference pref = new Preference(RuntimeEnvironment.application); 81 mController.addChild(pref); 82 83 verify(mCategory).addPreference(pref); 84 verify(mCategory).setVisible(true); 85 } 86 87 @Test 88 public void shouldToggleEnable() { 89 mController.setEnabled(false); 90 91 verify(mCategory).setEnabled(false); 92 93 mController.setEnabled(true); 94 95 verify(mCategory).setEnabled(true); 96 } 97} 98