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.graphics.Canvas; 25import android.graphics.Color; 26import android.graphics.GraphicBuffer; 27import android.graphics.PixelFormat; 28import android.graphics.Rect; 29import android.os.UserManager; 30import android.support.test.InstrumentationRegistry; 31 32import org.junit.After; 33import org.junit.Before; 34import org.junit.BeforeClass; 35 36import java.io.File; 37 38/** 39 * Base class for tests that use a {@link TaskSnapshotPersister}. 40 */ 41class 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 final File[] files = new File(sFilesDir, "snapshots").listFiles(); 72 if (files == null) { 73 return; 74 } 75 for (File file : files) { 76 if (!file.isDirectory()) { 77 file.delete(); 78 } 79 } 80 } 81 82 TaskSnapshot createSnapshot() { 83 return createSnapshot(1f /* scale */); 84 } 85 86 TaskSnapshot createSnapshot(float scale) { 87 final GraphicBuffer buffer = GraphicBuffer.create(100, 100, PixelFormat.RGBA_8888, 88 USAGE_HW_TEXTURE | USAGE_SW_READ_RARELY | USAGE_SW_READ_RARELY); 89 Canvas c = buffer.lockCanvas(); 90 c.drawColor(Color.RED); 91 buffer.unlockCanvasAndPost(c); 92 return new TaskSnapshot(buffer, ORIENTATION_PORTRAIT, TEST_INSETS, 93 scale < 1f /* reducedResolution */, scale); 94 } 95} 96