Yuncheol Heo | b9554e1 | 2019-10-08 15:51:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.car.apitest; |
| 18 | |
| 19 | import static android.view.Display.DEFAULT_DISPLAY; |
| 20 | import static android.view.Display.INVALID_DISPLAY; |
Yuncheol Heo | b9554e1 | 2019-10-08 15:51:49 -0700 | [diff] [blame] | 21 | |
| 22 | import static com.google.common.truth.Truth.assertThat; |
| 23 | |
| 24 | import static org.junit.Assume.assumeTrue; |
| 25 | import static org.testng.Assert.assertThrows; |
| 26 | |
Jun | 9e5b2cf | 2019-12-18 16:24:47 -0800 | [diff] [blame] | 27 | import androidx.test.filters.FlakyTest; |
Antonio Kantek | 57bbf27 | 2020-05-01 11:31:16 -0700 | [diff] [blame] | 28 | import androidx.test.filters.MediumTest; |
Jun | 9e5b2cf | 2019-12-18 16:24:47 -0800 | [diff] [blame] | 29 | |
Yuncheol Heo | b9554e1 | 2019-10-08 15:51:49 -0700 | [diff] [blame] | 30 | import org.junit.Test; |
| 31 | import org.junit.runner.RunWith; |
| 32 | import org.junit.runners.JUnit4; |
Yuncheol Heo | b9554e1 | 2019-10-08 15:51:49 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Build/Install/Run: |
| 36 | * atest AndroidCarApiTest:CarActivityViewDisplayIdTest |
| 37 | */ |
| 38 | @RunWith(JUnit4.class) |
Yuncheol Heo | b9554e1 | 2019-10-08 15:51:49 -0700 | [diff] [blame] | 39 | @MediumTest |
Antonio Kantek | 57bbf27 | 2020-05-01 11:31:16 -0700 | [diff] [blame] | 40 | public class CarActivityViewDisplayIdTest extends CarActivityViewDisplayIdTestBase { |
Yuncheol Heo | b9554e1 | 2019-10-08 15:51:49 -0700 | [diff] [blame] | 41 | private static final String CAR_LAUNCHER_PKG_NAME = "com.android.car.carlauncher"; |
Yuncheol Heo | b9554e1 | 2019-10-08 15:51:49 -0700 | [diff] [blame] | 42 | private static final int NONEXISTENT_DISPLAY_ID = Integer.MAX_VALUE; |
Yuncheol Heo | b9554e1 | 2019-10-08 15:51:49 -0700 | [diff] [blame] | 43 | |
| 44 | @Test |
Jun | 9e5b2cf | 2019-12-18 16:24:47 -0800 | [diff] [blame] | 45 | @FlakyTest |
Yuncheol Heo | b9554e1 | 2019-10-08 15:51:49 -0700 | [diff] [blame] | 46 | public void testSingleActivityView() throws Exception { |
| 47 | ActivityViewTestActivity activity = startActivityViewTestActivity(DEFAULT_DISPLAY); |
| 48 | activity.waitForActivityViewReady(); |
| 49 | int virtualDisplayId = activity.getActivityView().getVirtualDisplayId(); |
| 50 | |
| 51 | startTestActivity(ActivityInActivityView.class, virtualDisplayId); |
| 52 | |
| 53 | assertThat(getMappedPhysicalDisplayOfVirtualDisplay(virtualDisplayId)) |
| 54 | .isEqualTo(DEFAULT_DISPLAY); |
| 55 | assertThat(getMappedPhysicalDisplayOfVirtualDisplay(DEFAULT_DISPLAY)) |
| 56 | .isEqualTo(INVALID_DISPLAY); |
| 57 | |
| 58 | activity.finish(); |
| 59 | activity.waitForActivityViewDestroyed(); |
| 60 | |
| 61 | assertThat(getMappedPhysicalDisplayOfVirtualDisplay(virtualDisplayId)) |
| 62 | .isEqualTo(INVALID_DISPLAY); |
| 63 | assertThat(getMappedPhysicalDisplayOfVirtualDisplay(DEFAULT_DISPLAY)) |
| 64 | .isEqualTo(INVALID_DISPLAY); |
| 65 | } |
| 66 | |
| 67 | @Test |
Jun | 9e5b2cf | 2019-12-18 16:24:47 -0800 | [diff] [blame] | 68 | @FlakyTest |
Yuncheol Heo | b9554e1 | 2019-10-08 15:51:49 -0700 | [diff] [blame] | 69 | public void testDoubleActivityView() throws Exception { |
| 70 | ActivityViewTestActivity activity1 = startActivityViewTestActivity(DEFAULT_DISPLAY); |
| 71 | activity1.waitForActivityViewReady(); |
| 72 | int virtualDisplayId1 = activity1.getActivityView().getVirtualDisplayId(); |
| 73 | |
| 74 | ActivityViewTestActivity activity2 = startActivityViewTestActivity(virtualDisplayId1); |
| 75 | activity2.waitForActivityViewReady(); |
| 76 | int virtualDisplayId2 = activity2.getActivityView().getVirtualDisplayId(); |
| 77 | |
| 78 | startTestActivity(ActivityInActivityView.class, virtualDisplayId2); |
| 79 | |
| 80 | assertThat(virtualDisplayId1).isNotEqualTo(virtualDisplayId2); |
| 81 | assertThat(getMappedPhysicalDisplayOfVirtualDisplay(virtualDisplayId1)) |
| 82 | .isEqualTo(DEFAULT_DISPLAY); |
| 83 | assertThat(getMappedPhysicalDisplayOfVirtualDisplay(virtualDisplayId2)) |
| 84 | .isEqualTo(DEFAULT_DISPLAY); |
| 85 | assertThat(getMappedPhysicalDisplayOfVirtualDisplay(DEFAULT_DISPLAY)) |
| 86 | .isEqualTo(INVALID_DISPLAY); |
| 87 | |
| 88 | activity2.finish(); |
| 89 | activity1.finish(); |
| 90 | |
| 91 | activity2.waitForActivityViewDestroyed(); |
| 92 | activity1.waitForActivityViewDestroyed(); |
| 93 | |
| 94 | assertThat(getMappedPhysicalDisplayOfVirtualDisplay(virtualDisplayId1)) |
| 95 | .isEqualTo(INVALID_DISPLAY); |
| 96 | assertThat(getMappedPhysicalDisplayOfVirtualDisplay(virtualDisplayId2)) |
| 97 | .isEqualTo(INVALID_DISPLAY); |
| 98 | assertThat(getMappedPhysicalDisplayOfVirtualDisplay(DEFAULT_DISPLAY)) |
| 99 | .isEqualTo(INVALID_DISPLAY); |
| 100 | } |
| 101 | |
| 102 | @Test |
Jun | 9e5b2cf | 2019-12-18 16:24:47 -0800 | [diff] [blame] | 103 | @FlakyTest |
Yuncheol Heo | b9554e1 | 2019-10-08 15:51:49 -0700 | [diff] [blame] | 104 | public void testThrowsExceptionOnReportingNonExistingDisplay() throws Exception { |
| 105 | ActivityViewTestActivity activity = startActivityViewTestActivity(DEFAULT_DISPLAY); |
| 106 | activity.waitForActivityViewReady(); |
| 107 | int virtualDisplayId = activity.getActivityView().getVirtualDisplayId(); |
| 108 | |
| 109 | // This will pass since the test owns the display. |
| 110 | mCarUxRestrictionsManager.reportVirtualDisplayToPhysicalDisplay(virtualDisplayId, |
| 111 | NONEXISTENT_DISPLAY_ID); |
| 112 | |
| 113 | assertThat(getMappedPhysicalDisplayOfVirtualDisplay(virtualDisplayId)) |
| 114 | .isEqualTo(NONEXISTENT_DISPLAY_ID); |
| 115 | |
| 116 | activity.finish(); |
| 117 | activity.waitForActivityViewDestroyed(); |
| 118 | |
| 119 | // Now the display was released, so expect to throw an Exception. |
| 120 | assertThrows( |
| 121 | java.lang.IllegalArgumentException.class, |
| 122 | () -> mCarUxRestrictionsManager.reportVirtualDisplayToPhysicalDisplay( |
| 123 | virtualDisplayId, NONEXISTENT_DISPLAY_ID)); |
| 124 | } |
| 125 | |
| 126 | // TODO(b/143353546): Make the following tests not to rely on CarLauncher. |
| 127 | @Test |
Jun | 9e5b2cf | 2019-12-18 16:24:47 -0800 | [diff] [blame] | 128 | @FlakyTest |
Yuncheol Heo | b9554e1 | 2019-10-08 15:51:49 -0700 | [diff] [blame] | 129 | public void testThrowsExceptionOnReportingNonOwningDisplay() throws Exception { |
Yuncheol Heo | 667de7c | 2019-11-19 10:19:22 -0800 | [diff] [blame] | 130 | int displayIdOfCarLauncher = waitForActivityViewDisplayReady(CAR_LAUNCHER_PKG_NAME); |
Yuncheol Heo | b9554e1 | 2019-10-08 15:51:49 -0700 | [diff] [blame] | 131 | assumeTrue(INVALID_DISPLAY != displayIdOfCarLauncher); |
| 132 | |
| 133 | // CarLauncher owns the display, so expect to throw an Exception. |
| 134 | assertThrows( |
| 135 | java.lang.SecurityException.class, |
| 136 | () -> mCarUxRestrictionsManager.reportVirtualDisplayToPhysicalDisplay( |
| 137 | displayIdOfCarLauncher, DEFAULT_DISPLAY + 1)); |
Yuncheol Heo | 667de7c | 2019-11-19 10:19:22 -0800 | [diff] [blame] | 138 | |
Yuncheol Heo | b9554e1 | 2019-10-08 15:51:49 -0700 | [diff] [blame] | 139 | } |
Yuncheol Heo | b9554e1 | 2019-10-08 15:51:49 -0700 | [diff] [blame] | 140 | } |