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.content.Context; 20 import android.os.UserManager; 21 import android.support.v7.preference.Preference; 22 import android.support.v7.preference.PreferenceScreen; 23 24 import com.android.settings.testutils.SettingsRobolectricTestRunner; 25 import com.android.settings.TestConfig; 26 27 import org.junit.Before; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 import org.mockito.Mock; 31 import org.mockito.MockitoAnnotations; 32 import org.robolectric.annotation.Config; 33 34 import static org.mockito.Answers.RETURNS_DEEP_STUBS; 35 import static org.mockito.Matchers.any; 36 import static org.mockito.Matchers.anyBoolean; 37 import static org.mockito.Matchers.anyString; 38 import static org.mockito.Mockito.never; 39 import static org.mockito.Mockito.verify; 40 import static org.mockito.Mockito.when; 41 42 @RunWith(SettingsRobolectricTestRunner.class) 43 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 44 public class BugReportPreferenceControllerTest { 45 46 @Mock 47 private Context mContext; 48 @Mock 49 private Preference mPreference; 50 @Mock(answer = RETURNS_DEEP_STUBS) 51 private PreferenceScreen mScreen; 52 @Mock 53 private UserManager mUserManager; 54 55 private BugReportPreferenceController mController; 56 57 @Before 58 public void setUp() { 59 MockitoAnnotations.initMocks(this); 60 when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); 61 when(mScreen.findPreference(anyString())).thenReturn(mPreference); 62 mController = new BugReportPreferenceController(mContext); 63 } 64 65 @Test 66 public void displayPreference_hasDebugRestriction_shouldRemovePreference() { 67 when(mUserManager.hasUserRestriction(anyString())).thenReturn(true); 68 when(mScreen.getPreferenceCount()).thenReturn(1); 69 when(mScreen.getPreference(0)).thenReturn(mPreference); 70 when(mPreference.getKey()).thenReturn(mController.getPreferenceKey()); 71 72 mController.displayPreference(mScreen); 73 74 verify(mScreen).removePreference(any(Preference.class)); 75 } 76 77 @Test 78 public void displayPreference_noDebugRestriction_shouldNotRemovePreference() { 79 when(mUserManager.hasUserRestriction(anyString())).thenReturn(false); 80 81 mController.displayPreference(mScreen); 82 83 verify(mScreen, never()).removePreference(any(Preference.class)); 84 } 85 86 @Test 87 public void enablePreference_hasDebugRestriction_shouldNotEnable() { 88 when(mUserManager.hasUserRestriction(anyString())).thenReturn(true); 89 mController.displayPreference(mScreen); 90 91 mController.enablePreference(true); 92 93 verify(mPreference, never()).setEnabled(anyBoolean()); 94 } 95 96 @Test 97 public void enablePreference_noDebugRestriction_shouldEnable() { 98 when(mUserManager.hasUserRestriction(anyString())).thenReturn(false); 99 mController.displayPreference(mScreen); 100 101 mController.enablePreference(true); 102 103 verify(mPreference).setEnabled(anyBoolean()); 104 } 105 106 } 107