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.BLACK; 55 private static final int WAIT_TIMEOUT_MS = 1000; 56 private static final int WAIT_RETRIES = 5; 57 58 @Rule 59 public ActivityTestRule<MockActivity> mTaskDescriptionActivity = 60 new ActivityTestRule<>(MockActivity.class, 61 false /* initialTouchMode */, false /* launchActivity */); 62 63 @Test 64 public void testBitmapConstructor() throws Exception { 65 final Activity activity = mTaskDescriptionActivity.launchActivity(null); 66 final Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); 67 bitmap.eraseColor(0); 68 activity.setTaskDescription(new TaskDescription(TEST_LABEL, bitmap, TEST_COLOR)); 69 assertTaskDescription(activity, TEST_LABEL, TEST_NO_DATA); 70 } 71 72 @Test 73 public void testResourceConstructor() throws Exception { 74 final Activity activity = mTaskDescriptionActivity.launchActivity(null); 75 activity.setTaskDescription(new TaskDescription(TEST_LABEL, TEST_RES_DATA, TEST_COLOR)); 76 assertTaskDescription(activity, TEST_LABEL, TEST_RES_DATA); 77 } 78 79 private void assertTaskDescription(Activity activity, String label, int resId) { 80 final ActivityManager am = (ActivityManager) activity.getSystemService(ACTIVITY_SERVICE); 81 List<RecentTaskInfo> recentsTasks = am.getRecentTasks(1 /* maxNum */, 0 /* flags */); 82 if (!recentsTasks.isEmpty()) { 83 final RecentTaskInfo info = recentsTasks.get(0); 84 if (activity.getTaskId() == info.id) { 85 final TaskDescription td = info.taskDescription; 86 assertNotNull(td); 87 if (resId == TEST_NO_DATA) { 88 // TaskPersister at the worst case scenario waits 3 secs (PRE_TASK_DELAY_MS) to 89 // write the image to disk if its write time has ended 90 waitFor("TaskDescription's icon is null", () -> td.getIcon() != null); 91 waitFor("TaskDescription's icon filename is null", 92 () -> td.getIconFilename() != null); 93 } else { 94 waitFor("TaskDescription's icon is not null", () -> td.getIcon() == null); 95 waitFor("TaskDescription's icon filename is not null", 96 () -> td.getIconFilename() == null); 97 } 98 99 assertEquals(resId, td.getIconResource()); 100 assertEquals(label, td.getLabel()); 101 return; 102 } 103 } 104 fail("Did not find activity (id=" + activity.getTaskId() + ") in recent tasks list"); 105 } 106 107 private void waitFor(String message, BooleanSupplier waitCondition) { 108 for (int retry = 0; retry < WAIT_RETRIES; retry++) { 109 if (waitCondition.getAsBoolean()) { 110 return; 111 } 112 SystemClock.sleep(WAIT_TIMEOUT_MS); 113 } 114 fail(message); 115 } 116 } 117