Yohei Yukawa | 5281b6b | 2018-10-15 07:38:25 +0800 | [diff] [blame] | 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 | |
Andrii Kulian | 5877c7d | 2020-01-29 16:57:32 -0800 | [diff] [blame^] | 19 | import static android.view.Display.DEFAULT_DISPLAY; |
| 20 | |
Yohei Yukawa | 5281b6b | 2018-10-15 07:38:25 +0800 | [diff] [blame] | 21 | import static org.junit.Assert.assertEquals; |
| 22 | |
| 23 | import android.app.ActivityThread; |
Andrii Kulian | 5877c7d | 2020-01-29 16:57:32 -0800 | [diff] [blame^] | 24 | import android.hardware.display.DisplayManager; |
Nate Myren | 7c491a5 | 2019-05-15 11:07:03 -0700 | [diff] [blame] | 25 | import android.os.UserHandle; |
Yohei Yukawa | 5281b6b | 2018-10-15 07:38:25 +0800 | [diff] [blame] | 26 | |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 27 | import androidx.test.InstrumentationRegistry; |
| 28 | import androidx.test.filters.SmallTest; |
| 29 | import androidx.test.runner.AndroidJUnit4; |
| 30 | |
Yohei Yukawa | 5281b6b | 2018-10-15 07:38:25 +0800 | [diff] [blame] | 31 | import org.junit.Test; |
| 32 | import org.junit.runner.RunWith; |
| 33 | |
| 34 | @SmallTest |
| 35 | @RunWith(AndroidJUnit4.class) |
| 36 | public 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 Kulian | 5877c7d | 2020-01-29 16:57:32 -0800 | [diff] [blame^] | 50 | assertEquals(testContext.getDisplayNoVerify().getDisplayId(), testContext.getDisplayId()); |
Yohei Yukawa | 5281b6b | 2018-10-15 07:38:25 +0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | @Test |
| 54 | public void testDisplayIdForDefaultDisplayContext() { |
| 55 | final Context testContext = |
| 56 | InstrumentationRegistry.getInstrumentation().getTargetContext(); |
Andrii Kulian | 5877c7d | 2020-01-29 16:57:32 -0800 | [diff] [blame^] | 57 | final DisplayManager dm = testContext.getSystemService(DisplayManager.class); |
Yohei Yukawa | 5281b6b | 2018-10-15 07:38:25 +0800 | [diff] [blame] | 58 | final Context defaultDisplayContext = |
Andrii Kulian | 5877c7d | 2020-01-29 16:57:32 -0800 | [diff] [blame^] | 59 | testContext.createDisplayContext(dm.getDisplay(DEFAULT_DISPLAY)); |
Yohei Yukawa | 5281b6b | 2018-10-15 07:38:25 +0800 | [diff] [blame] | 60 | |
| 61 | assertEquals(defaultDisplayContext.getDisplay().getDisplayId(), |
| 62 | defaultDisplayContext.getDisplayId()); |
| 63 | } |
Nate Myren | 7c491a5 | 2019-05-15 11:07:03 -0700 | [diff] [blame] | 64 | |
| 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 Yukawa | 5281b6b | 2018-10-15 07:38:25 +0800 | [diff] [blame] | 92 | } |