blob: f0997a68492b8466551e3931a37ea15e143f3ac8 [file] [log] [blame]
Yohei Yukawa5281b6b2018-10-15 07:38:25 +08001/*
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
17package android.content;
18
Andrii Kulian5877c7d2020-01-29 16:57:32 -080019import static android.view.Display.DEFAULT_DISPLAY;
20
Yohei Yukawa5281b6b2018-10-15 07:38:25 +080021import static org.junit.Assert.assertEquals;
22
23import android.app.ActivityThread;
Andrii Kulian5877c7d2020-01-29 16:57:32 -080024import android.hardware.display.DisplayManager;
Nate Myren7c491a52019-05-15 11:07:03 -070025import android.os.UserHandle;
Yohei Yukawa5281b6b2018-10-15 07:38:25 +080026
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090027import androidx.test.InstrumentationRegistry;
28import androidx.test.filters.SmallTest;
29import androidx.test.runner.AndroidJUnit4;
30
Yohei Yukawa5281b6b2018-10-15 07:38:25 +080031import org.junit.Test;
32import org.junit.runner.RunWith;
33
34@SmallTest
35@RunWith(AndroidJUnit4.class)
36public class ContextTest {
37 @Test
38 public void testDisplayIdForSystemContext() {
39 final Context systemContext =
40 ActivityThread.currentActivityThread().getSystemContext();
41
42 assertEquals(systemContext.getDisplay().getDisplayId(), systemContext.getDisplayId());
43 }
44
45 @Test
46 public void testDisplayIdForTestContext() {
47 final Context testContext =
48 InstrumentationRegistry.getInstrumentation().getTargetContext();
49
Andrii Kulian5877c7d2020-01-29 16:57:32 -080050 assertEquals(testContext.getDisplayNoVerify().getDisplayId(), testContext.getDisplayId());
Yohei Yukawa5281b6b2018-10-15 07:38:25 +080051 }
52
53 @Test
54 public void testDisplayIdForDefaultDisplayContext() {
55 final Context testContext =
56 InstrumentationRegistry.getInstrumentation().getTargetContext();
Andrii Kulian5877c7d2020-01-29 16:57:32 -080057 final DisplayManager dm = testContext.getSystemService(DisplayManager.class);
Yohei Yukawa5281b6b2018-10-15 07:38:25 +080058 final Context defaultDisplayContext =
Andrii Kulian5877c7d2020-01-29 16:57:32 -080059 testContext.createDisplayContext(dm.getDisplay(DEFAULT_DISPLAY));
Yohei Yukawa5281b6b2018-10-15 07:38:25 +080060
61 assertEquals(defaultDisplayContext.getDisplay().getDisplayId(),
62 defaultDisplayContext.getDisplayId());
63 }
Nate Myren7c491a52019-05-15 11:07:03 -070064
65 @Test(expected = NullPointerException.class)
66 public void testStartActivityAsUserNullIntentNullUser() {
67 final Context testContext =
68 InstrumentationRegistry.getInstrumentation().getTargetContext();
69 testContext.startActivityAsUser(null, null);
70 }
71
72 @Test(expected = NullPointerException.class)
73 public void testStartActivityAsUserNullIntentNonNullUser() {
74 final Context testContext =
75 InstrumentationRegistry.getInstrumentation().getTargetContext();
76 testContext.startActivityAsUser(null, new UserHandle(UserHandle.USER_ALL));
77 }
78
79 @Test(expected = NullPointerException.class)
80 public void testStartActivityAsUserNonNullIntentNullUser() {
81 final Context testContext =
82 InstrumentationRegistry.getInstrumentation().getTargetContext();
83 testContext.startActivityAsUser(new Intent(), null);
84 }
85
86 @Test(expected = RuntimeException.class)
87 public void testStartActivityAsUserNonNullIntentNonNullUser() {
88 final Context testContext =
89 InstrumentationRegistry.getInstrumentation().getTargetContext();
90 testContext.startActivityAsUser(new Intent(), new UserHandle(UserHandle.USER_ALL));
91 }
Yohei Yukawa5281b6b2018-10-15 07:38:25 +080092}