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 android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
20import static junit.framework.Assert.assertNotNull;
21import static junit.framework.Assert.assertNull;
22
23import android.platform.test.annotations.Presubmit;
24import android.support.test.filters.SmallTest;
25import android.support.test.runner.AndroidJUnit4;
26
27import org.junit.Before;
28import org.junit.Test;
29import org.junit.runner.RunWith;
30
31/**
32 * Test class for {@link TaskSnapshotCache}.
33 *
34 * runtest frameworks-services -c com.android.server.wm.TaskSnapshotCacheTest
35 */
36@SmallTest
37@Presubmit
38@RunWith(AndroidJUnit4.class)
39public class TaskSnapshotCacheTest extends TaskSnapshotPersisterTestBase {
40
41    private TaskSnapshotCache mCache;
42
43    @Before
44    public void setUp() throws Exception {
45        super.setUp();
46        mCache = new TaskSnapshotCache(sWm, mLoader);
47    }
48
49    @Test
50    public void testAppRemoved() throws Exception {
51        final WindowState window = createWindow(null, FIRST_APPLICATION_WINDOW, "window");
52        mCache.putSnapshot(window.getTask(), createSnapshot());
53        assertNotNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */,
54                false /* restoreFromDisk */, false /* reducedResolution */));
55        mCache.onAppRemoved(window.mAppToken);
56        assertNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */,
57                false /* restoreFromDisk */, false /* reducedResolution */));
58    }
59
60    @Test
61    public void testAppDied() throws Exception {
62        final WindowState window = createWindow(null, FIRST_APPLICATION_WINDOW, "window");
63        mCache.putSnapshot(window.getTask(), createSnapshot());
64        assertNotNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */,
65                false /* restoreFromDisk */, false /* reducedResolution */));
66        mCache.onAppDied(window.mAppToken);
67        assertNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */,
68                false /* restoreFromDisk */, false /* reducedResolution */));
69    }
70
71    @Test
72    public void testTaskRemoved() throws Exception {
73        final WindowState window = createWindow(null, FIRST_APPLICATION_WINDOW, "window");
74        mCache.putSnapshot(window.getTask(), createSnapshot());
75        assertNotNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */,
76                false /* restoreFromDisk */, false /* reducedResolution */));
77        mCache.onTaskRemoved(window.getTask().mTaskId);
78        assertNull(mCache.getSnapshot(window.getTask().mTaskId, 0 /* userId */,
79                false /* restoreFromDisk */, false /* reducedResolution */));
80    }
81
82    @Test
83    public void testReduced_notCached() throws Exception {
84        final WindowState window = createWindow(null, FIRST_APPLICATION_WINDOW, "window");
85        mPersister.persistSnapshot(window.getTask().mTaskId, sWm.mCurrentUserId, createSnapshot());
86        mPersister.waitForQueueEmpty();
87        assertNull(mCache.getSnapshot(window.getTask().mTaskId, sWm.mCurrentUserId,
88                false /* restoreFromDisk */, false /* reducedResolution */));
89
90        // Load it from disk
91        assertNotNull(mCache.getSnapshot(window.getTask().mTaskId, sWm.mCurrentUserId,
92                true /* restoreFromDisk */, true /* reducedResolution */));
93
94        // Make sure it's not in the cache now.
95        assertNull(mCache.getSnapshot(window.getTask().mTaskId, sWm.mCurrentUserId,
96                false /* restoreFromDisk */, false /* reducedResolution */));
97    }
98
99    @Test
100    public void testRestoreFromDisk() throws Exception {
101        final WindowState window = createWindow(null, FIRST_APPLICATION_WINDOW, "window");
102        mPersister.persistSnapshot(window.getTask().mTaskId, sWm.mCurrentUserId, createSnapshot());
103        mPersister.waitForQueueEmpty();
104        assertNull(mCache.getSnapshot(window.getTask().mTaskId, sWm.mCurrentUserId,
105                false /* restoreFromDisk */, false /* reducedResolution */));
106
107        // Load it from disk
108        assertNotNull(mCache.getSnapshot(window.getTask().mTaskId, sWm.mCurrentUserId,
109                true /* restoreFromDisk */, false /* reducedResolution */));
110    }
111}
112