OSDN Git Service

Don't try to create user for task snapshot tests
[android-x86/frameworks-base.git] / services / tests / servicestests / src / com / android / server / wm / TaskSnapshotPersisterTestBase.java
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
17 package com.android.server.wm;
18
19 import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
20 import static android.graphics.GraphicBuffer.USAGE_HW_TEXTURE;
21 import static android.graphics.GraphicBuffer.USAGE_SW_READ_RARELY;
22
23 import android.app.ActivityManager.TaskSnapshot;
24 import android.graphics.Canvas;
25 import android.graphics.Color;
26 import android.graphics.GraphicBuffer;
27 import android.graphics.PixelFormat;
28 import android.graphics.Rect;
29 import android.os.UserManager;
30 import android.support.test.InstrumentationRegistry;
31
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.BeforeClass;
35
36 import java.io.File;
37
38 /**
39  * Base class for tests that use a {@link TaskSnapshotPersister}.
40  */
41 class TaskSnapshotPersisterTestBase extends WindowTestsBase {
42
43     private static final Rect TEST_INSETS = new Rect(10, 20, 30, 40);
44
45     TaskSnapshotPersister mPersister;
46     TaskSnapshotLoader mLoader;
47     int mTestUserId;
48     static File sFilesDir;
49
50     @BeforeClass
51     public static void setUpUser() {
52         sFilesDir = InstrumentationRegistry.getContext().getFilesDir();
53     }
54
55     @Before
56     public void setUp() throws Exception {
57         super.setUp();
58         final UserManager um = UserManager.get(InstrumentationRegistry.getContext());
59         mTestUserId = um.getUserHandle();
60         mPersister = new TaskSnapshotPersister(userId -> sFilesDir);
61         mLoader = new TaskSnapshotLoader(mPersister);
62         mPersister.start();
63     }
64
65     @After
66     public void tearDown() throws Exception {
67         cleanDirectory();
68     }
69
70     private void cleanDirectory() {
71         for (File file : new File(sFilesDir, "snapshots").listFiles()) {
72             if (!file.isDirectory()) {
73                 file.delete();
74             }
75         }
76     }
77
78     TaskSnapshot createSnapshot() {
79         final GraphicBuffer buffer = GraphicBuffer.create(100, 100, PixelFormat.RGBA_8888,
80                 USAGE_HW_TEXTURE | USAGE_SW_READ_RARELY | USAGE_SW_READ_RARELY);
81         Canvas c = buffer.lockCanvas();
82         c.drawColor(Color.RED);
83         buffer.unlockCanvasAndPost(c);
84         return new TaskSnapshot(buffer, ORIENTATION_PORTRAIT, TEST_INSETS,
85                 false /* reducedResolution */, 1f /* scale */);
86     }
87 }