• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.content;
18 
19 import static android.content.Context.DEVICE_ID_DEFAULT;
20 import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY;
21 import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
22 import static android.view.Display.DEFAULT_DISPLAY;
23 import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
24 
25 import static com.google.common.truth.Truth.assertThat;
26 import static com.google.common.truth.Truth.assertWithMessage;
27 
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertFalse;
30 import static org.junit.Assert.assertNotNull;
31 import static org.junit.Assert.assertTrue;
32 
33 import android.app.ActivityThread;
34 import android.content.res.Configuration;
35 import android.graphics.PixelFormat;
36 import android.hardware.display.DisplayManager;
37 import android.hardware.display.VirtualDisplay;
38 import android.media.ImageReader;
39 import android.os.Looper;
40 import android.os.UserHandle;
41 import android.platform.test.annotations.DisableFlags;
42 import android.platform.test.annotations.DisabledOnRavenwood;
43 import android.platform.test.annotations.EnableFlags;
44 import android.platform.test.annotations.Presubmit;
45 import android.platform.test.flag.junit.SetFlagsRule;
46 import android.platform.test.ravenwood.RavenwoodRule;
47 import android.view.Display;
48 import android.window.WindowTokenClient;
49 
50 import androidx.test.core.app.ApplicationProvider;
51 import androidx.test.ext.junit.runners.AndroidJUnit4;
52 import androidx.test.filters.SmallTest;
53 import androidx.test.platform.app.InstrumentationRegistry;
54 
55 import com.android.window.flags.Flags;
56 
57 import org.junit.Rule;
58 import org.junit.Test;
59 import org.junit.runner.RunWith;
60 
61 /**
62  *  Build/Install/Run:
63  *   atest FrameworksCoreTests:ContextTest
64  */
65 @Presubmit
66 @SmallTest
67 @RunWith(AndroidJUnit4.class)
68 public class ContextTest {
69     @Rule
70     public final RavenwoodRule mRavenwood = new RavenwoodRule.Builder().build();
71 
72     @Rule
73     public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
74 
75     @Test
testInstrumentationContext()76     public void testInstrumentationContext() {
77         // Confirm that we have a valid Context
78         assertNotNull(InstrumentationRegistry.getInstrumentation().getContext());
79     }
80 
81     @Test
testInstrumentationTargetContext()82     public void testInstrumentationTargetContext() {
83         // Confirm that we have a valid Context
84         assertNotNull(InstrumentationRegistry.getInstrumentation().getTargetContext());
85     }
86 
87     @Test
88     @DisabledOnRavenwood(blockedBy = Context.class)
testDisplayIdForSystemContext()89     public void testDisplayIdForSystemContext() {
90         final Context systemContext =
91                 ActivityThread.currentActivityThread().getSystemContext();
92 
93         assertEquals(systemContext.getDisplay().getDisplayId(), systemContext.getDisplayId());
94     }
95 
96     @Test
97     @DisabledOnRavenwood(blockedBy = Context.class)
testDisplayIdForSystemUiContext()98     public void testDisplayIdForSystemUiContext() {
99         final Context systemUiContext =
100                 ActivityThread.currentActivityThread().getSystemUiContext();
101 
102         assertEquals(systemUiContext.getDisplay().getDisplayId(), systemUiContext.getDisplayId());
103     }
104 
105     @Test
106     @DisabledOnRavenwood(blockedBy = Context.class)
testDisplayIdForTestContext()107     public void testDisplayIdForTestContext() {
108         final Context testContext =
109                 InstrumentationRegistry.getInstrumentation().getTargetContext();
110 
111         assertEquals(testContext.getDisplayNoVerify().getDisplayId(), testContext.getDisplayId());
112     }
113 
114     @Test
115     @DisabledOnRavenwood(blockedBy = Context.class)
testDisplayIdForDefaultDisplayContext()116     public void testDisplayIdForDefaultDisplayContext() {
117         final Context testContext =
118                 InstrumentationRegistry.getInstrumentation().getTargetContext();
119         final DisplayManager dm = testContext.getSystemService(DisplayManager.class);
120         final Context defaultDisplayContext =
121                 testContext.createDisplayContext(dm.getDisplay(DEFAULT_DISPLAY));
122 
123         assertEquals(defaultDisplayContext.getDisplay().getDisplayId(),
124                 defaultDisplayContext.getDisplayId());
125     }
126 
127     @Test(expected = NullPointerException.class)
128     @DisabledOnRavenwood(blockedBy = Context.class)
testStartActivityAsUserNullIntentNullUser()129     public void testStartActivityAsUserNullIntentNullUser() {
130         final Context testContext =
131                 InstrumentationRegistry.getInstrumentation().getTargetContext();
132         testContext.startActivityAsUser(null, null);
133     }
134 
135     @Test(expected = NullPointerException.class)
136     @DisabledOnRavenwood(blockedBy = Context.class)
testStartActivityAsUserNullIntentNonNullUser()137     public void testStartActivityAsUserNullIntentNonNullUser() {
138         final Context testContext =
139                 InstrumentationRegistry.getInstrumentation().getTargetContext();
140         testContext.startActivityAsUser(null, new UserHandle(UserHandle.USER_ALL));
141     }
142 
143     @Test(expected = NullPointerException.class)
144     @DisabledOnRavenwood(blockedBy = Context.class)
testStartActivityAsUserNonNullIntentNullUser()145     public void testStartActivityAsUserNonNullIntentNullUser() {
146         final Context testContext =
147                 InstrumentationRegistry.getInstrumentation().getTargetContext();
148         testContext.startActivityAsUser(new Intent(), null);
149     }
150 
151     @Test(expected = RuntimeException.class)
152     @DisabledOnRavenwood(blockedBy = Context.class)
testStartActivityAsUserNonNullIntentNonNullUser()153     public void testStartActivityAsUserNonNullIntentNonNullUser() {
154         final Context testContext =
155                 InstrumentationRegistry.getInstrumentation().getTargetContext();
156         testContext.startActivityAsUser(new Intent(), new UserHandle(UserHandle.USER_ALL));
157     }
158 
159     @Test
160     @DisabledOnRavenwood(blockedBy = Context.class)
testIsUiContext_appContext_returnsFalse()161     public void testIsUiContext_appContext_returnsFalse() {
162         final Context appContext = ApplicationProvider.getApplicationContext();
163 
164         assertThat(appContext.isUiContext()).isFalse();
165     }
166 
167     @Test
168     @DisabledOnRavenwood(blockedBy = Context.class)
testIsUiContext_systemContext_returnsTrue()169     public void testIsUiContext_systemContext_returnsTrue() {
170         final Context systemContext =
171                 ActivityThread.currentActivityThread().getSystemContext();
172 
173         assertThat(systemContext.isUiContext()).isTrue();
174     }
175 
176     @Test
177     @DisabledOnRavenwood(blockedBy = Context.class)
testIsUiContext_systemUiContext_returnsTrue()178     public void testIsUiContext_systemUiContext_returnsTrue() {
179         final Context systemUiContext =
180                 ActivityThread.currentActivityThread().getSystemUiContext();
181 
182         assertThat(systemUiContext.isUiContext()).isTrue();
183     }
184 
185     @Test
186     @DisabledOnRavenwood(blockedBy = Context.class)
testGetDisplayFromDisplayContextDerivedContextOnPrimaryDisplay()187     public void testGetDisplayFromDisplayContextDerivedContextOnPrimaryDisplay() {
188         verifyGetDisplayFromDisplayContextDerivedContext(false /* onSecondaryDisplay */);
189     }
190 
191     @Test
192     @DisabledOnRavenwood(blockedBy = Context.class)
testGetDisplayFromDisplayContextDerivedContextOnSecondaryDisplay()193     public void testGetDisplayFromDisplayContextDerivedContextOnSecondaryDisplay() {
194         verifyGetDisplayFromDisplayContextDerivedContext(true /* onSecondaryDisplay */);
195     }
196 
verifyGetDisplayFromDisplayContextDerivedContext( boolean onSecondaryDisplay)197     private static void verifyGetDisplayFromDisplayContextDerivedContext(
198             boolean onSecondaryDisplay) {
199         final Context appContext = ApplicationProvider.getApplicationContext();
200         final DisplayManager displayManager = appContext.getSystemService(DisplayManager.class);
201         final Display display;
202         if (onSecondaryDisplay) {
203             display = getSecondaryDisplay(displayManager);
204         } else {
205             display = displayManager.getDisplay(DEFAULT_DISPLAY);
206         }
207         final Context context = appContext.createDisplayContext(display)
208                 .createConfigurationContext(new Configuration());
209         assertEquals(display, context.getDisplay());
210     }
211 
getSecondaryDisplay(DisplayManager displayManager)212     private static Display getSecondaryDisplay(DisplayManager displayManager) {
213         final int width = 800;
214         final int height = 480;
215         final int density = 160;
216         ImageReader reader = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888,
217                 2 /* maxImages */);
218         VirtualDisplay virtualDisplay = displayManager.createVirtualDisplay(
219                 ContextTest.class.getName(), width, height, density, reader.getSurface(),
220                 VIRTUAL_DISPLAY_FLAG_PUBLIC | VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY);
221         return virtualDisplay.getDisplay();
222     }
223 
224     @Test
225     @DisabledOnRavenwood(blockedBy = Context.class)
testIsUiContext_ContextWrapper()226     public void testIsUiContext_ContextWrapper() {
227         ContextWrapper wrapper = new ContextWrapper(null /* base */);
228 
229         assertFalse(wrapper.isUiContext());
230 
231         wrapper = new ContextWrapper(createUiContext());
232 
233         assertTrue(wrapper.isUiContext());
234     }
235 
236     @Test
237     @DisabledOnRavenwood(blockedBy = Context.class)
testIsUiContext_UiContextDerivedContext()238     public void testIsUiContext_UiContextDerivedContext() {
239         final Context uiContext = createUiContext();
240         Context context = uiContext.createAttributionContext(null /* attributionTag */);
241 
242         assertTrue(context.isUiContext());
243 
244         context = uiContext.createConfigurationContext(new Configuration());
245 
246         assertTrue(context.isUiContext());
247     }
248 
249     @Test
250     @DisabledOnRavenwood(blockedBy = Context.class)
testIsUiContext_UiContextDerivedDisplayContext()251     public void testIsUiContext_UiContextDerivedDisplayContext() {
252         final Context uiContext = createUiContext();
253         final Display secondaryDisplay =
254                 getSecondaryDisplay(uiContext.getSystemService(DisplayManager.class));
255         final Context context = uiContext.createDisplayContext(secondaryDisplay);
256 
257         assertFalse(context.isUiContext());
258     }
259 
260     @Test
261     @DisabledOnRavenwood(blockedBy = Context.class)
testDeviceIdForSystemContext()262     public void testDeviceIdForSystemContext() {
263         final Context systemContext =
264                 ActivityThread.currentActivityThread().getSystemContext();
265 
266         assertEquals(systemContext.getDeviceId(), DEVICE_ID_DEFAULT);
267     }
268 
269     @Test
270     @DisabledOnRavenwood(blockedBy = Context.class)
testDeviceIdForSystemUiContext()271     public void testDeviceIdForSystemUiContext() {
272         final Context systemUiContext =
273                 ActivityThread.currentActivityThread().getSystemUiContext();
274 
275         assertEquals(systemUiContext.getDeviceId(), DEVICE_ID_DEFAULT);
276     }
277 
278     @Test
279     @DisabledOnRavenwood(blockedBy = Context.class)
testDeviceIdForTestContext()280     public void testDeviceIdForTestContext() {
281         final Context testContext =
282                 InstrumentationRegistry.getInstrumentation().getTargetContext();
283 
284         assertEquals(testContext.getDeviceId(), DEVICE_ID_DEFAULT);
285     }
286 
createUiContext()287     private Context createUiContext() {
288         final Context appContext = ApplicationProvider.getApplicationContext();
289         final DisplayManager displayManager = appContext.getSystemService(DisplayManager.class);
290         final Display display = displayManager.getDisplay(DEFAULT_DISPLAY);
291         return appContext.createDisplayContext(display)
292                 .createWindowContext(TYPE_APPLICATION_OVERLAY, null /* options */);
293     }
294 
295     @Test
296     @DisabledOnRavenwood(blockedBy = Context.class)
297     @DisableFlags(Flags.FLAG_TRACK_SYSTEM_UI_CONTEXT_BEFORE_WMS)
testSysUiContextRegisterComponentCallbacks_disableFlag()298     public void testSysUiContextRegisterComponentCallbacks_disableFlag() {
299         Looper.prepare();
300 
301         // Use createSystemActivityThreadForTesting to initialize
302         // systemUiContext#getApplicationContext.
303         final Context systemUiContext = ActivityThread.createSystemActivityThreadForTesting()
304                 .getSystemUiContext();
305         final TestComponentCallbacks2 callbacks = new TestComponentCallbacks2();
306         systemUiContext.registerComponentCallbacks(callbacks);
307 
308         final WindowTokenClient windowTokenClient =
309                 (WindowTokenClient) systemUiContext.getWindowContextToken();
310         windowTokenClient.onConfigurationChanged(Configuration.EMPTY, DEFAULT_DISPLAY);
311 
312         assertWithMessage("ComponentCallbacks should delegate to the app Context "
313                 + "if the flag is disabled.").that(callbacks.mConfiguration).isNull();
314     }
315 
316     @Test
317     @DisabledOnRavenwood(blockedBy = Context.class)
318     @EnableFlags(Flags.FLAG_TRACK_SYSTEM_UI_CONTEXT_BEFORE_WMS)
testSysUiContextRegisterComponentCallbacks_enableFlag()319     public void testSysUiContextRegisterComponentCallbacks_enableFlag() {
320         final Context systemUiContext = ActivityThread.currentActivityThread()
321                 .createSystemUiContextForTesting(DEFAULT_DISPLAY);
322         final TestComponentCallbacks2 callbacks = new TestComponentCallbacks2();
323         final Configuration config = Configuration.EMPTY;
324 
325         systemUiContext.registerComponentCallbacks(callbacks);
326 
327         final WindowTokenClient windowTokenClient =
328                 (WindowTokenClient) systemUiContext.getWindowContextToken();
329         windowTokenClient.onConfigurationChanged(config, DEFAULT_DISPLAY);
330 
331         assertWithMessage("ComponentCallbacks should delegate to SystemUiContext "
332                 + "if the flag is enabled.").that(callbacks.mConfiguration).isEqualTo(config);
333     }
334 }
335