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