1 /* 2 * Copyright (C) 2018 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 static com.google.common.truth.Truth.assertThat; 20 import static org.mockito.ArgumentMatchers.any; 21 import static org.mockito.ArgumentMatchers.anyInt; 22 import static org.mockito.ArgumentMatchers.eq; 23 import static org.mockito.Mockito.mock; 24 import static org.mockito.Mockito.never; 25 import static org.mockito.Mockito.verify; 26 import static org.mockito.Mockito.when; 27 28 import android.content.Context; 29 import android.content.pm.PackageManager; 30 import android.support.v7.preference.ListPreference; 31 import android.support.v7.preference.PreferenceScreen; 32 import android.view.DisplayCutout; 33 34 import com.android.settings.testutils.SettingsRobolectricTestRunner; 35 import com.android.settings.wrapper.OverlayManagerWrapper; 36 import com.android.settings.wrapper.OverlayManagerWrapper.OverlayInfo; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.mockito.Mock; 42 import org.mockito.MockitoAnnotations; 43 44 import java.util.Arrays; 45 46 @RunWith(SettingsRobolectricTestRunner.class) 47 public class EmulateDisplayCutoutPreferenceControllerTest { 48 49 private static final OverlayInfo ONE_DISABLED = createFakeOverlay("emulation.one", false); 50 private static final OverlayInfo ONE_ENABLED = createFakeOverlay("emulation.one", true); 51 private static final OverlayInfo TWO_DISABLED = createFakeOverlay("emulation.two", false); 52 private static final OverlayInfo TWO_ENABLED = createFakeOverlay("emulation.two", true); 53 54 @Mock 55 private Context mContext; 56 @Mock 57 private OverlayManagerWrapper mOverlayManager; 58 @Mock 59 private PackageManager mPackageManager; 60 @Mock 61 private ListPreference mPreference; 62 private EmulateDisplayCutoutPreferenceController mController; 63 64 @Before setUp()65 public void setUp() throws Exception { 66 MockitoAnnotations.initMocks(this); 67 mockCurrentOverlays(); 68 when(mPackageManager.getApplicationInfo(any(), anyInt())) 69 .thenThrow(PackageManager.NameNotFoundException.class); 70 mController = createController(); 71 mController.setPreference(mPreference); 72 } 73 mockCurrentOverlays(OverlayInfo... overlays)74 Object mockCurrentOverlays(OverlayInfo... overlays) { 75 return when(mOverlayManager.getOverlayInfosForTarget(eq("android"), anyInt())) 76 .thenReturn(Arrays.asList(overlays)); 77 } 78 79 @Test isAvailable_true()80 public void isAvailable_true() throws Exception { 81 mockCurrentOverlays(ONE_DISABLED, TWO_DISABLED); 82 83 assertThat(createController().isAvailable()).isTrue(); 84 } 85 86 @Test isAvailable_false()87 public void isAvailable_false() throws Exception { 88 mockCurrentOverlays(); 89 90 assertThat(createController().isAvailable()).isFalse(); 91 } 92 93 @Test onPreferenceChange_enable()94 public void onPreferenceChange_enable() throws Exception { 95 mockCurrentOverlays(ONE_DISABLED, TWO_DISABLED); 96 97 mController.onPreferenceChange(null, TWO_DISABLED.packageName); 98 99 verify(mOverlayManager) 100 .setEnabledExclusiveInCategory(eq(TWO_DISABLED.packageName), anyInt()); 101 } 102 103 @Test onPreferenceChange_disable()104 public void onPreferenceChange_disable() throws Exception { 105 mockCurrentOverlays(ONE_DISABLED, TWO_ENABLED); 106 107 mController.onPreferenceChange(null, ""); 108 109 verify(mOverlayManager).setEnabled(eq(TWO_ENABLED.packageName), eq(false), anyInt()); 110 } 111 112 @Test updateState_enabled()113 public void updateState_enabled() throws Exception { 114 mockCurrentOverlays(ONE_DISABLED, TWO_ENABLED); 115 116 mController.updateState(null); 117 118 verify(mPreference).setValueIndex(2); 119 } 120 121 @Test updateState_disabled()122 public void updateState_disabled() throws Exception { 123 mockCurrentOverlays(ONE_DISABLED, TWO_DISABLED); 124 125 mController.updateState(null); 126 127 verify(mPreference).setValueIndex(0); 128 } 129 130 @Test onDeveloperOptionsSwitchDisabled()131 public void onDeveloperOptionsSwitchDisabled() throws Exception { 132 mockCurrentOverlays(ONE_ENABLED, TWO_DISABLED); 133 final PreferenceScreen screen = mock(PreferenceScreen.class); 134 when(screen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 135 mController.displayPreference(screen); 136 137 mController.onDeveloperOptionsSwitchDisabled(); 138 139 verify(mPreference).setEnabled(false); 140 verify(mOverlayManager).setEnabled(eq(ONE_ENABLED.packageName), eq(false), anyInt()); 141 } 142 createController()143 private EmulateDisplayCutoutPreferenceController createController() { 144 return new EmulateDisplayCutoutPreferenceController(mContext, mPackageManager, 145 mOverlayManager); 146 } 147 createFakeOverlay(String pkg, boolean enabled)148 private static OverlayInfo createFakeOverlay(String pkg, boolean enabled) { 149 return new OverlayInfo(pkg, DisplayCutout.EMULATION_OVERLAY_CATEGORY, enabled); 150 } 151 }