• 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 org.junit.Assert.assertEquals;
20 
21 import android.app.ActivityThread;
22 import android.os.UserHandle;
23 import android.view.WindowManager;
24 
25 import androidx.test.InstrumentationRegistry;
26 import androidx.test.filters.SmallTest;
27 import androidx.test.runner.AndroidJUnit4;
28 
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 
32 @SmallTest
33 @RunWith(AndroidJUnit4.class)
34 public class ContextTest {
35     @Test
testDisplayIdForSystemContext()36     public void testDisplayIdForSystemContext() {
37         final Context systemContext =
38                 ActivityThread.currentActivityThread().getSystemContext();
39 
40         assertEquals(systemContext.getDisplay().getDisplayId(), systemContext.getDisplayId());
41     }
42 
43     @Test
testDisplayIdForTestContext()44     public void testDisplayIdForTestContext() {
45         final Context testContext =
46                 InstrumentationRegistry.getInstrumentation().getTargetContext();
47 
48         assertEquals(testContext.getDisplay().getDisplayId(), testContext.getDisplayId());
49     }
50 
51     @Test
testDisplayIdForDefaultDisplayContext()52     public void testDisplayIdForDefaultDisplayContext() {
53         final Context testContext =
54                 InstrumentationRegistry.getInstrumentation().getTargetContext();
55         final WindowManager wm = testContext.getSystemService(WindowManager.class);
56         final Context defaultDisplayContext =
57                 testContext.createDisplayContext(wm.getDefaultDisplay());
58 
59         assertEquals(defaultDisplayContext.getDisplay().getDisplayId(),
60                 defaultDisplayContext.getDisplayId());
61     }
62 
63     @Test(expected = NullPointerException.class)
testStartActivityAsUserNullIntentNullUser()64     public void testStartActivityAsUserNullIntentNullUser() {
65         final Context testContext =
66                 InstrumentationRegistry.getInstrumentation().getTargetContext();
67         testContext.startActivityAsUser(null, null);
68     }
69 
70     @Test(expected = NullPointerException.class)
testStartActivityAsUserNullIntentNonNullUser()71     public void testStartActivityAsUserNullIntentNonNullUser() {
72         final Context testContext =
73                 InstrumentationRegistry.getInstrumentation().getTargetContext();
74         testContext.startActivityAsUser(null, new UserHandle(UserHandle.USER_ALL));
75     }
76 
77     @Test(expected = NullPointerException.class)
testStartActivityAsUserNonNullIntentNullUser()78     public void testStartActivityAsUserNonNullIntentNullUser() {
79         final Context testContext =
80                 InstrumentationRegistry.getInstrumentation().getTargetContext();
81         testContext.startActivityAsUser(new Intent(), null);
82     }
83 
84     @Test(expected = RuntimeException.class)
testStartActivityAsUserNonNullIntentNonNullUser()85     public void testStartActivityAsUserNonNullIntentNonNullUser() {
86         final Context testContext =
87                 InstrumentationRegistry.getInstrumentation().getTargetContext();
88         testContext.startActivityAsUser(new Intent(), new UserHandle(UserHandle.USER_ALL));
89     }
90 }
91