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.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN; 20 import static android.content.res.Configuration.ORIENTATION_PORTRAIT; 21 import static android.graphics.GraphicBuffer.USAGE_HW_TEXTURE; 22 import static android.graphics.GraphicBuffer.USAGE_SW_READ_RARELY; 23 24 import android.app.ActivityManager.TaskSnapshot; 25 import android.graphics.Canvas; 26 import android.graphics.Color; 27 import android.graphics.GraphicBuffer; 28 import android.graphics.PixelFormat; 29 import android.graphics.Rect; 30 import android.os.UserManager; 31 import android.support.test.InstrumentationRegistry; 32 33 import org.junit.After; 34 import org.junit.Before; 35 import org.junit.BeforeClass; 36 37 import java.io.File; 38 39 /** 40 * Base class for tests that use a {@link TaskSnapshotPersister}. 41 */ 42 class TaskSnapshotPersisterTestBase extends WindowTestsBase { 43 44 private static final Rect TEST_INSETS = new Rect(10, 20, 30, 40); 45 46 TaskSnapshotPersister mPersister; 47 TaskSnapshotLoader mLoader; 48 int mTestUserId; 49 static File sFilesDir; 50 51 @BeforeClass setUpUser()52 public static void setUpUser() { 53 sFilesDir = InstrumentationRegistry.getContext().getFilesDir(); 54 } 55 56 @Before setUp()57 public void setUp() throws Exception { 58 super.setUp(); 59 final UserManager um = UserManager.get(InstrumentationRegistry.getContext()); 60 mTestUserId = um.getUserHandle(); 61 mPersister = new TaskSnapshotPersister(userId -> sFilesDir); 62 mLoader = new TaskSnapshotLoader(mPersister); 63 mPersister.start(); 64 } 65 66 @After tearDown()67 public void tearDown() throws Exception { 68 cleanDirectory(); 69 } 70 cleanDirectory()71 private void cleanDirectory() { 72 final File[] files = new File(sFilesDir, "snapshots").listFiles(); 73 if (files == null) { 74 return; 75 } 76 for (File file : files) { 77 if (!file.isDirectory()) { 78 file.delete(); 79 } 80 } 81 } 82 createSnapshot()83 TaskSnapshot createSnapshot() { 84 return createSnapshot(1f /* scale */); 85 } 86 createSnapshot(float scale)87 TaskSnapshot createSnapshot(float scale) { 88 return new TaskSnapshotBuilder() 89 .setScale(scale) 90 .build(); 91 } 92 93 /** 94 * Builds a TaskSnapshot. 95 */ 96 class TaskSnapshotBuilder { 97 98 private float mScale = 1f; 99 private boolean mIsRealSnapshot = true; 100 private boolean mIsTranslucent = false; 101 private int mWindowingMode = WINDOWING_MODE_FULLSCREEN; 102 private int mSystemUiVisibility = 0; 103 setScale(float scale)104 public TaskSnapshotBuilder setScale(float scale) { 105 mScale = scale; 106 return this; 107 } 108 setIsRealSnapshot(boolean isRealSnapshot)109 public TaskSnapshotBuilder setIsRealSnapshot(boolean isRealSnapshot) { 110 mIsRealSnapshot = isRealSnapshot; 111 return this; 112 } 113 setIsTranslucent(boolean isTranslucent)114 public TaskSnapshotBuilder setIsTranslucent(boolean isTranslucent) { 115 mIsTranslucent = isTranslucent; 116 return this; 117 } 118 setWindowingMode(int windowingMode)119 public TaskSnapshotBuilder setWindowingMode(int windowingMode) { 120 mWindowingMode = windowingMode; 121 return this; 122 } 123 setSystemUiVisibility(int systemUiVisibility)124 public TaskSnapshotBuilder setSystemUiVisibility(int systemUiVisibility) { 125 mSystemUiVisibility = systemUiVisibility; 126 return this; 127 } 128 build()129 public TaskSnapshot build() { 130 final GraphicBuffer buffer = GraphicBuffer.create(100, 100, PixelFormat.RGBA_8888, 131 USAGE_HW_TEXTURE | USAGE_SW_READ_RARELY | USAGE_SW_READ_RARELY); 132 Canvas c = buffer.lockCanvas(); 133 c.drawColor(Color.RED); 134 buffer.unlockCanvasAndPost(c); 135 return new TaskSnapshot(buffer, ORIENTATION_PORTRAIT, TEST_INSETS, 136 mScale < 1f /* reducedResolution */, mScale, mIsRealSnapshot, mWindowingMode, 137 mSystemUiVisibility, mIsTranslucent); 138 } 139 140 } 141 } 142