TaskSnapshotPersisterLoaderTest.java revision e163126d2e27ce1fbd5c74279dfb5d05c97b8392
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.Rect; 28import android.os.SystemClock; 29import android.platform.test.annotations.Presubmit; 30import android.support.test.filters.MediumTest; 31import android.support.test.runner.AndroidJUnit4; 32import android.util.ArraySet; 33 34import com.android.internal.util.Predicate; 35import com.android.server.wm.TaskSnapshotPersister.RemoveObsoleteFilesQueueItem; 36 37import org.junit.Test; 38import org.junit.runner.RunWith; 39 40import java.io.File; 41 42/** 43 * Test class for {@link TaskSnapshotPersister} and {@link TaskSnapshotLoader} 44 * 45 * runtest frameworks-services -c com.android.server.wm.TaskSnapshotPersisterLoaderTest 46 */ 47@MediumTest 48@Presubmit 49@RunWith(AndroidJUnit4.class) 50public class TaskSnapshotPersisterLoaderTest extends TaskSnapshotPersisterTestBase { 51 52 private static final Rect TEST_INSETS = new Rect(10, 20, 30, 40); 53 54 @Test 55 public void testPersistAndLoadSnapshot() { 56 mPersister.persistSnapshot(1 , mTestUserId, createSnapshot()); 57 mPersister.waitForQueueEmpty(); 58 final File[] files = new File[] { new File(sFilesDir.getPath() + "/snapshots/1.proto"), 59 new File(sFilesDir.getPath() + "/snapshots/1.jpg"), 60 new File(sFilesDir.getPath() + "/snapshots/1_reduced.jpg")}; 61 assertTrueForFiles(files, File::exists, " must exist"); 62 final TaskSnapshot snapshot = mLoader.loadTask(1, mTestUserId, false /* reduced */); 63 assertNotNull(snapshot); 64 assertEquals(TEST_INSETS, snapshot.getContentInsets()); 65 assertNotNull(snapshot.getSnapshot()); 66 assertEquals(Configuration.ORIENTATION_PORTRAIT, snapshot.getOrientation()); 67 } 68 69 private void assertTrueForFiles(File[] files, Predicate<File> predicate, String message) { 70 for (File file : files) { 71 assertTrue(file.getName() + message, predicate.apply(file)); 72 } 73 } 74 75 @Test 76 public void testTaskRemovedFromRecents() { 77 mPersister.persistSnapshot(1, mTestUserId, createSnapshot()); 78 mPersister.onTaskRemovedFromRecents(1, mTestUserId); 79 mPersister.waitForQueueEmpty(); 80 assertFalse(new File(sFilesDir.getPath() + "/snapshots/1.proto").exists()); 81 assertFalse(new File(sFilesDir.getPath() + "/snapshots/1.jpg").exists()); 82 assertFalse(new File(sFilesDir.getPath() + "/snapshots/1_reduced.jpg").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, mTestUserId, createSnapshot()); 92 mPersister.persistSnapshot(2, mTestUserId, createSnapshot()); 93 mPersister.persistSnapshot(3, mTestUserId, createSnapshot()); 94 mPersister.persistSnapshot(4, mTestUserId, createSnapshot()); 95 mPersister.persistSnapshot(5, mTestUserId, createSnapshot()); 96 mPersister.persistSnapshot(6, mTestUserId, 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.jpg")); 109 assertEquals(12, removeObsoleteFilesQueueItem.getTaskId("12.proto")); 110 assertEquals(1, removeObsoleteFilesQueueItem.getTaskId("1.jpg")); 111 assertEquals(1, removeObsoleteFilesQueueItem.getTaskId("1_reduced.jpg")); 112 } 113 114 @Test 115 public void testRemoveObsoleteFiles() { 116 mPersister.persistSnapshot(1, mTestUserId, createSnapshot()); 117 mPersister.persistSnapshot(2, mTestUserId, createSnapshot()); 118 final ArraySet<Integer> taskIds = new ArraySet<>(); 119 taskIds.add(1); 120 mPersister.removeObsoleteFiles(taskIds, new int[] { mTestUserId }); 121 mPersister.waitForQueueEmpty(); 122 final File[] existsFiles = new File[] { 123 new File(sFilesDir.getPath() + "/snapshots/1.proto"), 124 new File(sFilesDir.getPath() + "/snapshots/1.jpg"), 125 new File(sFilesDir.getPath() + "/snapshots/1_reduced.jpg") }; 126 final File[] nonExistsFiles = new File[] { 127 new File(sFilesDir.getPath() + "/snapshots/2.proto"), 128 new File(sFilesDir.getPath() + "/snapshots/2.jpg"), 129 new File(sFilesDir.getPath() + "/snapshots/2_reduced.jpg")}; 130 assertTrueForFiles(existsFiles, File::exists, " must exist"); 131 assertTrueForFiles(nonExistsFiles, file -> !file.exists(), " must not exist"); 132 } 133 134 @Test 135 public void testRemoveObsoleteFiles_addedOneInTheMeantime() { 136 mPersister.persistSnapshot(1, mTestUserId, createSnapshot()); 137 final ArraySet<Integer> taskIds = new ArraySet<>(); 138 taskIds.add(1); 139 mPersister.removeObsoleteFiles(taskIds, new int[] { mTestUserId }); 140 mPersister.persistSnapshot(2, mTestUserId, createSnapshot()); 141 mPersister.waitForQueueEmpty(); 142 final File[] existsFiles = new File[] { 143 new File(sFilesDir.getPath() + "/snapshots/1.proto"), 144 new File(sFilesDir.getPath() + "/snapshots/1.jpg"), 145 new File(sFilesDir.getPath() + "/snapshots/1_reduced.jpg"), 146 new File(sFilesDir.getPath() + "/snapshots/2.proto"), 147 new File(sFilesDir.getPath() + "/snapshots/2.jpg"), 148 new File(sFilesDir.getPath() + "/snapshots/2_reduced.jpg")}; 149 assertTrueForFiles(existsFiles, File::exists, " must exist"); 150 } 151} 152