1 /* 2 * Copyright (C) 2017 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.app.cts; 18 19 import static android.content.Context.ACTIVITY_SERVICE; 20 21 import static org.junit.Assert.assertEquals; 22 import static org.junit.Assert.assertNotNull; 23 import static org.junit.Assert.fail; 24 25 import android.app.Activity; 26 import android.app.ActivityManager; 27 import android.app.ActivityManager.RecentTaskInfo; 28 import android.app.ActivityManager.TaskDescription; 29 import android.app.stubs.MockActivity; 30 import android.graphics.Bitmap; 31 import android.graphics.Color; 32 import android.os.SystemClock; 33 import android.platform.test.annotations.Presubmit; 34 35 import androidx.test.rule.ActivityTestRule; 36 import androidx.test.runner.AndroidJUnit4; 37 38 import org.junit.Rule; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 42 import java.util.List; 43 import java.util.function.BooleanSupplier; 44 45 /** 46 * Build & Run: atest android.app.cts.TaskDescriptionTest 47 */ 48 @RunWith(AndroidJUnit4.class) 49 @Presubmit 50 public class TaskDescriptionTest { 51 private static final String TEST_LABEL = "test-label"; 52 private static final int TEST_NO_DATA = 0; 53 private static final int TEST_RES_DATA = 777; 54 private static final int TEST_COLOR = Color.RED; 55 private static final int NULL_COLOR = 0; 56 private static final int WAIT_TIMEOUT_MS = 1000; 57 private static final int WAIT_RETRIES = 5; 58 59 @Rule 60 public ActivityTestRule<MockActivity> mTaskDescriptionActivity = 61 new ActivityTestRule<>(MockActivity.class, 62 false /* initialTouchMode */, false /* launchActivity */); 63 64 @Test testBitmapConstructor()65 public void testBitmapConstructor() throws Exception { 66 final Activity activity = mTaskDescriptionActivity.launchActivity(null); 67 final Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); 68 bitmap.eraseColor(0); 69 activity.setTaskDescription(new TaskDescription(TEST_LABEL, bitmap, TEST_COLOR)); 70 assertTaskDescription(activity, TEST_LABEL, TEST_NO_DATA, bitmap); 71 72 activity.setTaskDescription(new TaskDescription(TEST_LABEL, bitmap)); 73 assertTaskDescription(activity, TEST_LABEL, TEST_NO_DATA, bitmap); 74 } 75 76 @Test testResourceConstructor()77 public void testResourceConstructor() throws Exception { 78 final Activity activity = mTaskDescriptionActivity.launchActivity(null); 79 activity.setTaskDescription(new TaskDescription(TEST_LABEL, TEST_RES_DATA, TEST_COLOR)); 80 assertTaskDescription(activity, TEST_LABEL, TEST_RES_DATA, null); 81 82 activity.setTaskDescription(new TaskDescription(TEST_LABEL, TEST_RES_DATA)); 83 assertTaskDescription(activity, TEST_LABEL, TEST_RES_DATA, null); 84 } 85 86 @Test testLabelConstructor()87 public void testLabelConstructor() throws Exception { 88 final Activity activity = mTaskDescriptionActivity.launchActivity(null); 89 activity.setTaskDescription(new TaskDescription(TEST_LABEL)); 90 assertTaskDescription(activity, TEST_LABEL, TEST_NO_DATA, null); 91 } 92 93 @Test testEmptyConstructor()94 public void testEmptyConstructor() throws Exception { 95 final Activity activity = mTaskDescriptionActivity.launchActivity(null); 96 activity.setTaskDescription(new TaskDescription()); 97 assertTaskDescription(activity, null, TEST_NO_DATA, null); 98 } 99 100 @Test testBuilder()101 public void testBuilder() { 102 final Activity activity = mTaskDescriptionActivity.launchActivity(null); 103 final TaskDescription td = new TaskDescription.Builder() 104 .setLabel(TEST_LABEL) 105 .setIcon(TEST_RES_DATA) 106 .setPrimaryColor(TEST_COLOR) 107 .setBackgroundColor(TEST_COLOR) 108 .setStatusBarColor(TEST_COLOR) 109 .setNavigationBarColor(TEST_COLOR) 110 .build(); 111 activity.setTaskDescription(td); 112 assertTaskDescription(activity, TEST_LABEL, TEST_RES_DATA, null, TEST_COLOR, TEST_COLOR, 113 TEST_COLOR, TEST_COLOR); 114 } 115 assertTaskDescription(Activity activity, String label, int resId, Bitmap bitmap)116 private void assertTaskDescription(Activity activity, String label, int resId, Bitmap bitmap) { 117 assertTaskDescription(activity, label, resId, bitmap, NULL_COLOR, NULL_COLOR, NULL_COLOR, 118 NULL_COLOR); 119 } 120 assertTaskDescription(Activity activity, String label, int resId, Bitmap bitmap, int primaryColor, int backgroundColor, int statusBarColor, int navigationBarColor)121 private void assertTaskDescription(Activity activity, String label, int resId, Bitmap bitmap, 122 int primaryColor, int backgroundColor, int statusBarColor, int navigationBarColor) { 123 final ActivityManager am = (ActivityManager) activity.getSystemService(ACTIVITY_SERVICE); 124 List<RecentTaskInfo> recentsTasks = am.getRecentTasks(1 /* maxNum */, 0 /* flags */); 125 if (!recentsTasks.isEmpty()) { 126 final RecentTaskInfo info = recentsTasks.get(0); 127 if (activity.getTaskId() == info.id) { 128 final TaskDescription td = info.taskDescription; 129 assertNotNull(td); 130 if (bitmap != null) { 131 // TaskPersister at the worst case scenario waits 3 secs (PRE_TASK_DELAY_MS) to 132 // write the image to disk if its write time has ended 133 waitFor("TaskDescription's icon is null", () -> td.getIcon() != null); 134 waitFor("TaskDescription's icon filename is null", 135 () -> td.getIconFilename() != null); 136 } else { 137 waitFor("TaskDescription's icon is not null", () -> td.getIcon() == null); 138 waitFor("TaskDescription's icon filename is not null", 139 () -> td.getIconFilename() == null); 140 } 141 142 assertEquals(resId, td.getIconResource()); 143 assertEquals(label, td.getLabel()); 144 if (primaryColor != NULL_COLOR) { 145 assertEquals(primaryColor, td.getPrimaryColor()); 146 } 147 if (backgroundColor != NULL_COLOR) { 148 assertEquals(backgroundColor, td.getBackgroundColor()); 149 } 150 if (statusBarColor != NULL_COLOR) { 151 assertEquals(statusBarColor, td.getStatusBarColor()); 152 } 153 if (navigationBarColor != NULL_COLOR) { 154 assertEquals(navigationBarColor, td.getNavigationBarColor()); 155 } 156 return; 157 } 158 } 159 fail("Did not find activity (id=" + activity.getTaskId() + ") in recent tasks list"); 160 } 161 waitFor(String message, BooleanSupplier waitCondition)162 private void waitFor(String message, BooleanSupplier waitCondition) { 163 for (int retry = 0; retry < WAIT_RETRIES; retry++) { 164 if (waitCondition.getAsBoolean()) { 165 return; 166 } 167 SystemClock.sleep(WAIT_TIMEOUT_MS); 168 } 169 fail(message); 170 } 171 } 172