2 * Copyright (C) 2018 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.development;
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;
28 import android.content.Context;
29 import android.content.om.IOverlayManager;
30 import android.content.om.OverlayInfo;
31 import android.content.pm.PackageManager;
32 import android.os.RemoteException;
33 import androidx.preference.ListPreference;
34 import androidx.preference.PreferenceScreen;
35 import android.view.DisplayCutout;
37 import com.android.settings.testutils.SettingsRobolectricTestRunner;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.Mock;
43 import org.mockito.MockitoAnnotations;
45 import java.util.ArrayList;
46 import java.util.Arrays;
48 @RunWith(SettingsRobolectricTestRunner.class)
49 public class EmulateDisplayCutoutPreferenceControllerTest {
51 private static final OverlayInfo ONE_DISABLED = createFakeOverlay("emulation.one", false);
52 private static final OverlayInfo ONE_ENABLED = createFakeOverlay("emulation.one", true);
53 private static final OverlayInfo TWO_DISABLED = createFakeOverlay("emulation.two", false);
54 private static final OverlayInfo TWO_ENABLED = createFakeOverlay("emulation.two", true);
57 private Context mContext;
59 private IOverlayManager mOverlayManager;
61 private PackageManager mPackageManager;
63 private ListPreference mPreference;
64 private EmulateDisplayCutoutPreferenceController mController;
67 public void setUp() throws Exception {
68 MockitoAnnotations.initMocks(this);
69 when(mContext.getSystemService(Context.OVERLAY_SERVICE)).thenReturn(mOverlayManager);
70 mockCurrentOverlays();
71 when(mPackageManager.getApplicationInfo(any(), anyInt()))
72 .thenThrow(PackageManager.NameNotFoundException.class);
73 mController = createController();
74 mController.setPreference(mPreference);
77 Object mockCurrentOverlays(OverlayInfo... overlays) {
79 return when(mOverlayManager.getOverlayInfosForTarget(eq("android"), anyInt()))
80 .thenReturn(Arrays.asList(overlays));
81 } catch (RemoteException re) {
82 return new ArrayList<OverlayInfo>();
87 public void isAvailable_true() throws Exception {
88 mockCurrentOverlays(ONE_DISABLED, TWO_DISABLED);
90 assertThat(createController().isAvailable()).isTrue();
94 public void isAvailable_false() throws Exception {
95 mockCurrentOverlays();
97 assertThat(createController().isAvailable()).isFalse();
101 public void onPreferenceChange_enable() throws Exception {
102 mockCurrentOverlays(ONE_DISABLED, TWO_DISABLED);
104 mController.onPreferenceChange(null, TWO_DISABLED.packageName);
106 verify(mOverlayManager)
107 .setEnabledExclusiveInCategory(eq(TWO_DISABLED.packageName), anyInt());
111 public void onPreferenceChange_disable() throws Exception {
112 mockCurrentOverlays(ONE_DISABLED, TWO_ENABLED);
114 mController.onPreferenceChange(null, "");
116 verify(mOverlayManager).setEnabled(eq(TWO_ENABLED.packageName), eq(false), anyInt());
120 public void updateState_enabled() throws Exception {
121 mockCurrentOverlays(ONE_DISABLED, TWO_ENABLED);
123 mController.updateState(null);
125 verify(mPreference).setValueIndex(2);
129 public void updateState_disabled() throws Exception {
130 mockCurrentOverlays(ONE_DISABLED, TWO_DISABLED);
132 mController.updateState(null);
134 verify(mPreference).setValueIndex(0);
138 public void onDeveloperOptionsSwitchDisabled() throws Exception {
139 mockCurrentOverlays(ONE_ENABLED, TWO_DISABLED);
140 final PreferenceScreen screen = mock(PreferenceScreen.class);
141 when(screen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
142 mController.displayPreference(screen);
144 mController.onDeveloperOptionsSwitchDisabled();
146 verify(mPreference).setEnabled(false);
147 verify(mOverlayManager).setEnabled(eq(ONE_ENABLED.packageName), eq(false), anyInt());
150 private EmulateDisplayCutoutPreferenceController createController() {
151 return new EmulateDisplayCutoutPreferenceController(mContext, mPackageManager,
155 private static OverlayInfo createFakeOverlay(String pkg, boolean enabled) {
156 final int state = (enabled) ? OverlayInfo.STATE_ENABLED : OverlayInfo.STATE_DISABLED;
158 return new OverlayInfo(pkg /* packageName */,
159 pkg + ".target" /* targetPackageName */,
160 DisplayCutout.EMULATION_OVERLAY_CATEGORY /* category */,
161 pkg + ".baseCodePath" /* baseCodePath */,
165 true /* isStatic */);