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.removeObsoleteFiles(new ArraySet<>(), new int[] { mTestUserId });
94        mPersister.removeObsoleteFiles(new ArraySet<>(), new int[] { mTestUserId });
95        mPersister.removeObsoleteFiles(new ArraySet<>(), new int[] { mTestUserId });
96        mPersister.removeObsoleteFiles(new ArraySet<>(), new int[] { mTestUserId });
97        mPersister.waitForQueueEmpty();
98        assertTrue(SystemClock.elapsedRealtime() - ms > 500);
99    }
100
101    /**
102     * Tests that too many store write queue items are being purged.
103     */
104    @Test
105    public void testPurging() {
106        mPersister.persistSnapshot(100, mTestUserId, createSnapshot());
107        mPersister.waitForQueueEmpty();
108        mPersister.setPaused(true);
109        mPersister.persistSnapshot(1, mTestUserId, createSnapshot());
110        mPersister.removeObsoleteFiles(new ArraySet<>(), new int[] { mTestUserId });
111        mPersister.persistSnapshot(2, mTestUserId, createSnapshot());
112        mPersister.persistSnapshot(3, mTestUserId, createSnapshot());
113        mPersister.persistSnapshot(4, mTestUserId, createSnapshot());
114        mPersister.setPaused(false);
115        mPersister.waitForQueueEmpty();
116
117        // Make sure 1,2 were purged but removeObsoleteFiles wasn't.
118        final File[] existsFiles = new File[] {
119                new File(sFilesDir.getPath() + "/snapshots/3.proto"),
120                new File(sFilesDir.getPath() + "/snapshots/4.proto")};
121        final File[] nonExistsFiles = new File[] {
122                new File(sFilesDir.getPath() + "/snapshots/100.proto"),
123                new File(sFilesDir.getPath() + "/snapshots/1.proto"),
124                new File(sFilesDir.getPath() + "/snapshots/1.proto")};
125        assertTrueForFiles(existsFiles, File::exists, " must exist");
126        assertTrueForFiles(nonExistsFiles, file -> !file.exists(), " must not exist");
127    }
128
129    @Test
130    public void testGetTaskId() {
131        RemoveObsoleteFilesQueueItem removeObsoleteFilesQueueItem =
132                mPersister.new RemoveObsoleteFilesQueueItem(new ArraySet<>(), new int[] {});
133        assertEquals(-1, removeObsoleteFilesQueueItem.getTaskId("blablablulp"));
134        assertEquals(-1, removeObsoleteFilesQueueItem.getTaskId("nothing.err"));
135        assertEquals(-1, removeObsoleteFilesQueueItem.getTaskId("/invalid/"));
136        assertEquals(12, removeObsoleteFilesQueueItem.getTaskId("12.jpg"));
137        assertEquals(12, removeObsoleteFilesQueueItem.getTaskId("12.proto"));
138        assertEquals(1, removeObsoleteFilesQueueItem.getTaskId("1.jpg"));
139        assertEquals(1, removeObsoleteFilesQueueItem.getTaskId("1_reduced.jpg"));
140    }
141
142    @Test
143    public void testRemoveObsoleteFiles() {
144        mPersister.persistSnapshot(1, mTestUserId, createSnapshot());
145        mPersister.persistSnapshot(2, mTestUserId, createSnapshot());
146        final ArraySet<Integer> taskIds = new ArraySet<>();
147        taskIds.add(1);
148        mPersister.removeObsoleteFiles(taskIds, new int[] { mTestUserId });
149        mPersister.waitForQueueEmpty();
150        final File[] existsFiles = new File[] {
151                new File(sFilesDir.getPath() + "/snapshots/1.proto"),
152                new File(sFilesDir.getPath() + "/snapshots/1.jpg"),
153                new File(sFilesDir.getPath() + "/snapshots/1_reduced.jpg") };
154        final File[] nonExistsFiles = new File[] {
155                new File(sFilesDir.getPath() + "/snapshots/2.proto"),
156                new File(sFilesDir.getPath() + "/snapshots/2.jpg"),
157                new File(sFilesDir.getPath() + "/snapshots/2_reduced.jpg")};
158        assertTrueForFiles(existsFiles, File::exists, " must exist");
159        assertTrueForFiles(nonExistsFiles, file -> !file.exists(), " must not exist");
160    }
161
162    @Test
163    public void testRemoveObsoleteFiles_addedOneInTheMeantime() {
164        mPersister.persistSnapshot(1, mTestUserId, createSnapshot());
165        final ArraySet<Integer> taskIds = new ArraySet<>();
166        taskIds.add(1);
167        mPersister.removeObsoleteFiles(taskIds, new int[] { mTestUserId });
168        mPersister.persistSnapshot(2, mTestUserId, createSnapshot());
169        mPersister.waitForQueueEmpty();
170        final File[] existsFiles = new File[] {
171                new File(sFilesDir.getPath() + "/snapshots/1.proto"),
172                new File(sFilesDir.getPath() + "/snapshots/1.jpg"),
173                new File(sFilesDir.getPath() + "/snapshots/1_reduced.jpg"),
174                new File(sFilesDir.getPath() + "/snapshots/2.proto"),
175                new File(sFilesDir.getPath() + "/snapshots/2.jpg"),
176                new File(sFilesDir.getPath() + "/snapshots/2_reduced.jpg")};
177        assertTrueForFiles(existsFiles, File::exists, " must exist");
178    }
179}
180