TaskSnapshotPersisterTestBase.java revision 35e3f53a30588b79e0309fdbeef29a8c18eef65d
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.content.res.Configuration.ORIENTATION_PORTRAIT;
20import static android.graphics.GraphicBuffer.USAGE_HW_TEXTURE;
21import static android.graphics.GraphicBuffer.USAGE_SW_READ_RARELY;
22
23import android.app.ActivityManager.TaskSnapshot;
24import android.content.pm.UserInfo;
25import android.graphics.Canvas;
26import android.graphics.Color;
27import android.graphics.GraphicBuffer;
28import android.graphics.PixelFormat;
29import android.graphics.Rect;
30import android.os.UserManager;
31import android.support.test.InstrumentationRegistry;
32
33import org.junit.After;
34import org.junit.AfterClass;
35import org.junit.Assert;
36import org.junit.Before;
37import org.junit.BeforeClass;
38
39import java.io.File;
40
41/**
42 * Base class for tests that use a {@link TaskSnapshotPersister}.
43 */
44class TaskSnapshotPersisterTestBase extends WindowTestsBase {
45
46    private static final String TEST_USER_NAME = "TaskSnapshotPersisterTest User";
47    private static final Rect TEST_INSETS = new Rect(10, 20, 30, 40);
48
49    TaskSnapshotPersister mPersister;
50    TaskSnapshotLoader mLoader;
51    static int sTestUserId;
52    static File sFilesDir;
53    private static UserManager sUserManager;
54
55    @BeforeClass
56    public static void setUpUser() {
57        sUserManager = UserManager.get(InstrumentationRegistry.getContext());
58        sTestUserId = createUser(TEST_USER_NAME, 0);
59        sFilesDir = InstrumentationRegistry.getContext().getFilesDir();
60    }
61
62    @AfterClass
63    public static void tearDownUser() {
64        removeUser(sTestUserId);
65    }
66
67    @Before
68    public void setUp() throws Exception {
69        super.setUp();
70        mPersister = new TaskSnapshotPersister(
71                userId -> sFilesDir);
72        mLoader = new TaskSnapshotLoader(mPersister);
73        mPersister.start();
74    }
75
76    @After
77    public void tearDown() throws Exception {
78        cleanDirectory();
79    }
80
81    private static int createUser(String name, int flags) {
82        UserInfo user = sUserManager.createUser(name, flags);
83        if (user == null) {
84            Assert.fail("Error while creating the test user: " + TEST_USER_NAME);
85        }
86        return user.id;
87    }
88
89    private static void removeUser(int userId) {
90        if (!sUserManager.removeUser(userId)) {
91            Assert.fail("Error while removing the test user: " + TEST_USER_NAME);
92        }
93    }
94
95    private void cleanDirectory() {
96        for (File file : new File(sFilesDir, "snapshots").listFiles()) {
97            if (!file.isDirectory()) {
98                file.delete();
99            }
100        }
101    }
102
103    TaskSnapshot createSnapshot() {
104        GraphicBuffer buffer = GraphicBuffer.create(100, 100, PixelFormat.RGBA_8888,
105                USAGE_HW_TEXTURE | USAGE_SW_READ_RARELY | USAGE_SW_READ_RARELY);
106        Canvas c = buffer.lockCanvas();
107        c.drawColor(Color.RED);
108        buffer.unlockCanvasAndPost(c);
109        return new TaskSnapshot(buffer, ORIENTATION_PORTRAIT, TEST_INSETS,
110                false /* reducedResolution */, 1f /* scale */);
111    }
112}
113