OSDN Git Service

Remove OverlayManagerWrapper class out from Settings
[android-x86/packages-apps-Settings.git] / tests / robotests / src / com / android / settings / development / EmulateDisplayCutoutPreferenceControllerTest.java
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.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;
36
37 import com.android.settings.testutils.SettingsRobolectricTestRunner;
38
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;
44
45 import java.util.ArrayList;
46 import java.util.Arrays;
47
48 @RunWith(SettingsRobolectricTestRunner.class)
49 public class EmulateDisplayCutoutPreferenceControllerTest {
50
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);
55
56     @Mock
57     private Context mContext;
58     @Mock
59     private IOverlayManager mOverlayManager;
60     @Mock
61     private PackageManager mPackageManager;
62     @Mock
63     private ListPreference mPreference;
64     private EmulateDisplayCutoutPreferenceController mController;
65
66     @Before
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);
75     }
76
77     Object mockCurrentOverlays(OverlayInfo... overlays) {
78         try {
79             return when(mOverlayManager.getOverlayInfosForTarget(eq("android"), anyInt()))
80                 .thenReturn(Arrays.asList(overlays));
81         } catch (RemoteException re) {
82             return new ArrayList<OverlayInfo>();
83         }
84     }
85
86     @Test
87     public void isAvailable_true() throws Exception {
88         mockCurrentOverlays(ONE_DISABLED, TWO_DISABLED);
89
90         assertThat(createController().isAvailable()).isTrue();
91     }
92
93     @Test
94     public void isAvailable_false() throws Exception {
95         mockCurrentOverlays();
96
97         assertThat(createController().isAvailable()).isFalse();
98     }
99
100     @Test
101     public void onPreferenceChange_enable() throws Exception {
102         mockCurrentOverlays(ONE_DISABLED, TWO_DISABLED);
103
104         mController.onPreferenceChange(null, TWO_DISABLED.packageName);
105
106         verify(mOverlayManager)
107             .setEnabledExclusiveInCategory(eq(TWO_DISABLED.packageName), anyInt());
108     }
109
110     @Test
111     public void onPreferenceChange_disable() throws Exception {
112         mockCurrentOverlays(ONE_DISABLED, TWO_ENABLED);
113
114         mController.onPreferenceChange(null, "");
115
116         verify(mOverlayManager).setEnabled(eq(TWO_ENABLED.packageName), eq(false), anyInt());
117     }
118
119     @Test
120     public void updateState_enabled() throws Exception {
121         mockCurrentOverlays(ONE_DISABLED, TWO_ENABLED);
122
123         mController.updateState(null);
124
125         verify(mPreference).setValueIndex(2);
126     }
127
128     @Test
129     public void updateState_disabled() throws Exception {
130         mockCurrentOverlays(ONE_DISABLED, TWO_DISABLED);
131
132         mController.updateState(null);
133
134         verify(mPreference).setValueIndex(0);
135     }
136
137     @Test
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);
143
144         mController.onDeveloperOptionsSwitchDisabled();
145
146         verify(mPreference).setEnabled(false);
147         verify(mOverlayManager).setEnabled(eq(ONE_ENABLED.packageName), eq(false), anyInt());
148     }
149
150     private EmulateDisplayCutoutPreferenceController createController() {
151         return new EmulateDisplayCutoutPreferenceController(mContext, mPackageManager,
152                 mOverlayManager);
153     }
154
155     private static OverlayInfo createFakeOverlay(String pkg, boolean enabled) {
156         final int state = (enabled) ? OverlayInfo.STATE_ENABLED : OverlayInfo.STATE_DISABLED;
157
158         return new OverlayInfo(pkg /* packageName */,
159                 pkg + ".target" /* targetPackageName */,
160                 DisplayCutout.EMULATION_OVERLAY_CATEGORY /* category */,
161                 pkg + ".baseCodePath" /* baseCodePath */,
162                 state /* state */,
163                 0 /* userId */,
164                 0 /* priority */,
165                 true /* isStatic */);
166     }
167 }