TaskSnapshotPersisterLoaderTest.java revision 7361babf94baa985eaa8bd2e94fcb16f00670998
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 17package com.android.server.wm; 18 19import static org.junit.Assert.assertEquals; 20import static org.junit.Assert.assertFalse; 21import static org.junit.Assert.assertNotNull; 22import static org.junit.Assert.assertTrue; 23import static org.junit.Assert.fail; 24 25import android.app.ActivityManager.TaskSnapshot; 26import android.content.res.Configuration; 27import android.graphics.Bitmap; 28import android.graphics.Rect; 29import android.os.SystemClock; 30import android.platform.test.annotations.Presubmit; 31import android.support.test.filters.MediumTest; 32import android.support.test.runner.AndroidJUnit4; 33import android.util.ArraySet; 34 35import com.android.internal.util.Predicate; 36import com.android.server.wm.TaskSnapshotPersister.RemoveObsoleteFilesQueueItem; 37 38import org.junit.Test; 39import org.junit.runner.RunWith; 40 41import java.io.File; 42 43/** 44 * Test class for {@link TaskSnapshotPersister} and {@link TaskSnapshotLoader} 45 * 46 * runtest frameworks-services -c com.android.server.wm.TaskSnapshotPersisterLoaderTest 47 */ 48@MediumTest 49@Presubmit 50@RunWith(AndroidJUnit4.class) 51public class TaskSnapshotPersisterLoaderTest extends TaskSnapshotPersisterTestBase { 52 53 private static final String TEST_USER_NAME = "TaskSnapshotPersisterTest User"; 54 private static final Rect TEST_INSETS = new Rect(10, 20, 30, 40); 55 56 @Test 57 public void testPersistAndLoadSnapshot() { 58 mPersister.persistSnapshot(1 , sTestUserId, createSnapshot()); 59 mPersister.waitForQueueEmpty(); 60 final File[] files = new File[] { new File(sFilesDir.getPath() + "/snapshots/1.proto"), 61 new File(sFilesDir.getPath() + "/snapshots/1.png") }; 62 assertTrueForFiles(files, File::exists, " must exist"); 63 final TaskSnapshot snapshot = mLoader.loadTask(1, sTestUserId); 64 assertNotNull(snapshot); 65 assertEquals(TEST_INSETS, snapshot.getContentInsets()); 66 assertNotNull(snapshot.getSnapshot()); 67 assertEquals(Configuration.ORIENTATION_PORTRAIT, snapshot.getOrientation()); 68 } 69 70 private void assertTrueForFiles(File[] files, Predicate<File> predicate, String message) { 71 for (File file : files) { 72 assertTrue(file.getName() + message, predicate.apply(file)); 73 } 74 } 75 76 @Test 77 public void testTaskRemovedFromRecents() { 78 mPersister.persistSnapshot(1, sTestUserId, createSnapshot()); 79 mPersister.onTaskRemovedFromRecents(1, sTestUserId); 80 mPersister.waitForQueueEmpty(); 81 assertFalse(new File(sFilesDir.getPath() + "/snapshots/1.proto").exists()); 82 assertFalse(new File(sFilesDir.getPath() + "/snapshots/1.png").exists()); 83 } 84 85 /** 86 * Tests that persisting a couple of snapshots is being throttled. 87 */ 88 @Test 89 public void testThrottling() { 90 long ms = SystemClock.elapsedRealtime(); 91 mPersister.persistSnapshot(1, sTestUserId, createSnapshot()); 92 mPersister.persistSnapshot(2, sTestUserId, createSnapshot()); 93 mPersister.persistSnapshot(3, sTestUserId, createSnapshot()); 94 mPersister.persistSnapshot(4, sTestUserId, createSnapshot()); 95 mPersister.persistSnapshot(5, sTestUserId, createSnapshot()); 96 mPersister.persistSnapshot(6, sTestUserId, createSnapshot()); 97 mPersister.waitForQueueEmpty(); 98 assertTrue(SystemClock.elapsedRealtime() - ms > 500); 99 } 100 101 @Test 102 public void testGetTaskId() { 103 RemoveObsoleteFilesQueueItem removeObsoleteFilesQueueItem = 104 mPersister.new RemoveObsoleteFilesQueueItem(new ArraySet<>(), new int[] {}); 105 assertEquals(-1, removeObsoleteFilesQueueItem.getTaskId("blablablulp")); 106 assertEquals(-1, removeObsoleteFilesQueueItem.getTaskId("nothing.err")); 107 assertEquals(-1, removeObsoleteFilesQueueItem.getTaskId("/invalid/")); 108 assertEquals(12, removeObsoleteFilesQueueItem.getTaskId("12.png")); 109 assertEquals(12, removeObsoleteFilesQueueItem.getTaskId("12.proto")); 110 assertEquals(1, removeObsoleteFilesQueueItem.getTaskId("1.png")); 111 } 112 113 @Test 114 public void testRemoveObsoleteFiles() { 115 mPersister.persistSnapshot(1, sTestUserId, createSnapshot()); 116 mPersister.persistSnapshot(2, sTestUserId, createSnapshot()); 117 final ArraySet<Integer> taskIds = new ArraySet<>(); 118 taskIds.add(1); 119 mPersister.removeObsoleteFiles(taskIds, new int[] { sTestUserId }); 120 mPersister.waitForQueueEmpty(); 121 final File[] existsFiles = new File[] { 122 new File(sFilesDir.getPath() + "/snapshots/1.proto"), 123 new File(sFilesDir.getPath() + "/snapshots/1.png") }; 124 final File[] nonExistsFiles = new File[] { 125 new File(sFilesDir.getPath() + "/snapshots/2.proto"), 126 new File(sFilesDir.getPath() + "/snapshots/2.png") }; 127 assertTrueForFiles(existsFiles, File::exists, " must exist"); 128 assertTrueForFiles(nonExistsFiles, file -> !file.exists(), " must not exist"); 129 } 130 131 @Test 132 public void testRemoveObsoleteFiles_addedOneInTheMeantime() { 133 mPersister.persistSnapshot(1, sTestUserId, createSnapshot()); 134 final ArraySet<Integer> taskIds = new ArraySet<>(); 135 taskIds.add(1); 136 mPersister.removeObsoleteFiles(taskIds, new int[] { sTestUserId }); 137 mPersister.persistSnapshot(2, sTestUserId, createSnapshot()); 138 mPersister.waitForQueueEmpty(); 139 final File[] existsFiles = new File[] { 140 new File(sFilesDir.getPath() + "/snapshots/1.proto"), 141 new File(sFilesDir.getPath() + "/snapshots/1.png"), 142 new File(sFilesDir.getPath() + "/snapshots/2.proto"), 143 new File(sFilesDir.getPath() + "/snapshots/2.png") }; 144 assertTrueForFiles(existsFiles, File::exists, " must exist"); 145 } 146} 147