1 /* 2 * Copyright (C) 2021 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 com.google.common.truth.Truth.assertThat; 20 21 import static org.junit.Assert.assertThrows; 22 23 import android.car.Car; 24 import android.car.app.CarActivityManager; 25 import android.content.ActivityNotFoundException; 26 import android.content.ComponentName; 27 import android.view.Display; 28 29 import androidx.test.filters.MediumTest; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 34 @MediumTest 35 public final class CarActivityManagerTest extends CarApiTestBase { 36 private static final String TAG = CarActivityManagerTest.class.getSimpleName(); 37 38 // Comes from android.window.DisplayAreaOrganizer.FEATURE_DEFAULT_TASK_CONTAINER 39 private static final int FEATURE_DEFAULT_TASK_CONTAINER = 1; 40 41 // Comes from android.window.DisplayAreaOrganizer.FEATURE_UNDEFINED 42 private static final int FEATURE_UNDEFINED = -1; 43 44 private CarActivityManager mCarActivityManager; 45 46 private final ComponentName mTestActivity = new ComponentName("test.pkg", "test.activity"); 47 48 @Before setUp()49 public void setUp() throws Exception { 50 mCarActivityManager = (CarActivityManager) getCar().getCarManager(Car.CAR_ACTIVITY_SERVICE); 51 assertThat(mCarActivityManager).isNotNull(); 52 } 53 54 @Test testSetPersistentActivity()55 public void testSetPersistentActivity() { 56 // Set 57 int retSet = mCarActivityManager.setPersistentActivity( 58 mTestActivity, Display.DEFAULT_DISPLAY, FEATURE_DEFAULT_TASK_CONTAINER); 59 assertThat(retSet).isEqualTo(CarActivityManager.RESULT_SUCCESS); 60 61 // Remove 62 int retRemove = mCarActivityManager.setPersistentActivity( 63 mTestActivity, Display.DEFAULT_DISPLAY, FEATURE_UNDEFINED); 64 assertThat(retRemove).isEqualTo(CarActivityManager.RESULT_SUCCESS); 65 } 66 67 @Test testSetPersistentActivity_throwsExceptionForInvalidDisplayId()68 public void testSetPersistentActivity_throwsExceptionForInvalidDisplayId() { 69 int invalidDisplayId = 999999990; 70 71 assertThrows(IllegalArgumentException.class, 72 () -> mCarActivityManager.setPersistentActivity( 73 mTestActivity, invalidDisplayId, FEATURE_DEFAULT_TASK_CONTAINER)); 74 } 75 76 @Test testSetPersistentActivity_throwsExceptionForInvalidFeatureId()77 public void testSetPersistentActivity_throwsExceptionForInvalidFeatureId() { 78 int unknownFeatureId = 999999990; 79 80 assertThrows(IllegalArgumentException.class, 81 () -> mCarActivityManager.setPersistentActivity( 82 mTestActivity, Display.DEFAULT_DISPLAY, unknownFeatureId)); 83 } 84 85 @Test testSetPersistentActivity_throwsExceptionForUnknownActivity()86 public void testSetPersistentActivity_throwsExceptionForUnknownActivity() { 87 // Tries to remove the Activity without registering it. 88 assertThrows(ActivityNotFoundException.class, 89 () -> mCarActivityManager.setPersistentActivity( 90 mTestActivity, Display.DEFAULT_DISPLAY, FEATURE_UNDEFINED)); 91 } 92 } 93