Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 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 |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 14 | * limitations under the License. |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package com.android.server.wm; |
| 18 | |
Winson Chung | a4fa8d5 | 2018-04-20 15:54:51 -0700 | [diff] [blame] | 19 | import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN; |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 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 | |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 24 | import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; |
| 25 | |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 26 | import android.app.ActivityManager.TaskSnapshot; |
Winson Chung | 48b2565 | 2018-10-22 14:04:30 -0700 | [diff] [blame] | 27 | import android.content.ComponentName; |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 28 | import android.graphics.Canvas; |
| 29 | import android.graphics.Color; |
Peiyong Lin | 21e499a | 2019-04-03 16:37:46 -0700 | [diff] [blame^] | 30 | import android.graphics.ColorSpace; |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 31 | import android.graphics.GraphicBuffer; |
| 32 | import android.graphics.PixelFormat; |
| 33 | import android.graphics.Rect; |
| 34 | import android.os.UserManager; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 35 | |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 36 | import org.junit.After; |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 37 | import org.junit.Before; |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 38 | |
| 39 | import java.io.File; |
| 40 | |
| 41 | /** |
| 42 | * Base class for tests that use a {@link TaskSnapshotPersister}. |
| 43 | */ |
| 44 | class TaskSnapshotPersisterTestBase extends WindowTestsBase { |
| 45 | |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 46 | private static final Rect TEST_INSETS = new Rect(10, 20, 30, 40); |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 47 | static final File FILES_DIR = getInstrumentation().getTargetContext().getFilesDir(); |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 48 | |
| 49 | TaskSnapshotPersister mPersister; |
| 50 | TaskSnapshotLoader mLoader; |
Wale Ogunwale | e163126 | 2017-04-28 15:11:16 -0700 | [diff] [blame] | 51 | int mTestUserId; |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 52 | |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 53 | @Before |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 54 | public void setUp() { |
| 55 | final UserManager um = UserManager.get(getInstrumentation().getTargetContext()); |
Wale Ogunwale | e163126 | 2017-04-28 15:11:16 -0700 | [diff] [blame] | 56 | mTestUserId = um.getUserHandle(); |
Kevin | 716a9db | 2018-12-18 16:53:39 -0800 | [diff] [blame] | 57 | mPersister = new TaskSnapshotPersister(mWm, userId -> FILES_DIR); |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 58 | mLoader = new TaskSnapshotLoader(mPersister); |
| 59 | mPersister.start(); |
| 60 | } |
| 61 | |
| 62 | @After |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 63 | public void tearDown() { |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 64 | cleanDirectory(); |
| 65 | } |
| 66 | |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 67 | private void cleanDirectory() { |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 68 | final File[] files = new File(FILES_DIR, "snapshots").listFiles(); |
Wale Ogunwale | 68e882c | 2017-05-03 07:54:23 -0700 | [diff] [blame] | 69 | if (files == null) { |
| 70 | return; |
| 71 | } |
| 72 | for (File file : files) { |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 73 | if (!file.isDirectory()) { |
| 74 | file.delete(); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | TaskSnapshot createSnapshot() { |
Matthew Ng | 3531dd0 | 2017-08-23 16:41:57 -0700 | [diff] [blame] | 80 | return createSnapshot(1f /* scale */); |
| 81 | } |
| 82 | |
| 83 | TaskSnapshot createSnapshot(float scale) { |
Winson Chung | 173020c | 2018-05-04 15:36:47 -0700 | [diff] [blame] | 84 | return new TaskSnapshotBuilder() |
| 85 | .setScale(scale) |
| 86 | .build(); |
Winson Chung | f3e412e | 2018-03-08 11:07:40 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Winson Chung | 173020c | 2018-05-04 15:36:47 -0700 | [diff] [blame] | 89 | /** |
| 90 | * Builds a TaskSnapshot. |
| 91 | */ |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 92 | static class TaskSnapshotBuilder { |
Winson Chung | 173020c | 2018-05-04 15:36:47 -0700 | [diff] [blame] | 93 | |
| 94 | private float mScale = 1f; |
| 95 | private boolean mIsRealSnapshot = true; |
| 96 | private boolean mIsTranslucent = false; |
| 97 | private int mWindowingMode = WINDOWING_MODE_FULLSCREEN; |
| 98 | private int mSystemUiVisibility = 0; |
| 99 | |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 100 | TaskSnapshotBuilder setScale(float scale) { |
Winson Chung | 173020c | 2018-05-04 15:36:47 -0700 | [diff] [blame] | 101 | mScale = scale; |
| 102 | return this; |
| 103 | } |
| 104 | |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 105 | TaskSnapshotBuilder setIsRealSnapshot(boolean isRealSnapshot) { |
Winson Chung | 173020c | 2018-05-04 15:36:47 -0700 | [diff] [blame] | 106 | mIsRealSnapshot = isRealSnapshot; |
| 107 | return this; |
| 108 | } |
| 109 | |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 110 | TaskSnapshotBuilder setIsTranslucent(boolean isTranslucent) { |
Winson Chung | 173020c | 2018-05-04 15:36:47 -0700 | [diff] [blame] | 111 | mIsTranslucent = isTranslucent; |
| 112 | return this; |
| 113 | } |
| 114 | |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 115 | TaskSnapshotBuilder setWindowingMode(int windowingMode) { |
Winson Chung | 173020c | 2018-05-04 15:36:47 -0700 | [diff] [blame] | 116 | mWindowingMode = windowingMode; |
| 117 | return this; |
| 118 | } |
| 119 | |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 120 | TaskSnapshotBuilder setSystemUiVisibility(int systemUiVisibility) { |
Winson Chung | 173020c | 2018-05-04 15:36:47 -0700 | [diff] [blame] | 121 | mSystemUiVisibility = systemUiVisibility; |
| 122 | return this; |
| 123 | } |
| 124 | |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 125 | TaskSnapshot build() { |
Winson Chung | 173020c | 2018-05-04 15:36:47 -0700 | [diff] [blame] | 126 | final GraphicBuffer buffer = GraphicBuffer.create(100, 100, PixelFormat.RGBA_8888, |
| 127 | USAGE_HW_TEXTURE | USAGE_SW_READ_RARELY | USAGE_SW_READ_RARELY); |
| 128 | Canvas c = buffer.lockCanvas(); |
| 129 | c.drawColor(Color.RED); |
| 130 | buffer.unlockCanvasAndPost(c); |
Peiyong Lin | 21e499a | 2019-04-03 16:37:46 -0700 | [diff] [blame^] | 131 | return new TaskSnapshot(new ComponentName("", ""), buffer, |
| 132 | ColorSpace.get(ColorSpace.Named.SRGB), ORIENTATION_PORTRAIT, TEST_INSETS, |
| 133 | mScale < 1f /* reducedResolution */, mScale, mIsRealSnapshot, |
Winson Chung | 48b2565 | 2018-10-22 14:04:30 -0700 | [diff] [blame] | 134 | mWindowingMode, mSystemUiVisibility, mIsTranslucent); |
Winson Chung | 173020c | 2018-05-04 15:36:47 -0700 | [diff] [blame] | 135 | } |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 136 | } |
| 137 | } |