| 1 |
/* |
| 2 |
* Copyright (C) 2020 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 |
package com.android.test |
| 17 |
|
| 18 |
import android.annotation.ColorInt |
| 19 |
import android.content.Context |
| 20 |
import android.content.Intent |
| 21 |
import android.graphics.Rect |
| 22 |
import android.server.wm.WindowManagerState.getLogicalDisplaySize |
| 23 |
import android.view.cts.surfacevalidator.CapturedActivity |
| 24 |
import android.view.cts.surfacevalidator.ISurfaceValidatorTestCase |
| 25 |
import android.view.cts.surfacevalidator.PixelChecker |
| 26 |
import android.view.cts.surfacevalidator.RectChecker |
| 27 |
import android.widget.FrameLayout |
| 28 |
import androidx.test.rule.ActivityTestRule |
| 29 |
import org.junit.After |
| 30 |
import org.junit.Before |
| 31 |
import org.junit.Rule |
| 32 |
import java.util.concurrent.CountDownLatch |
| 33 |
|
| 34 |
open class ScreenRecordTestBase(useBlastAdapter: Boolean) : |
| 35 |
SurfaceViewBufferTestBase(useBlastAdapter) { |
| 36 |
@get:Rule |
| 37 |
var mActivityRule = ActivityTestRule(MainActivity::class.java) |
| 38 |
|
| 39 |
private lateinit var mActivity: MainActivity |
| 40 |
|
| 41 |
@Before |
| 42 |
override fun setup() { |
| 43 |
super.setup() |
| 44 |
mActivity = mActivityRule.launchActivity(Intent()) |
| 45 |
lateinit var surfaceReadyLatch: CountDownLatch |
| 46 |
runOnUiThread { |
| 47 |
it.dismissPermissionDialog() |
| 48 |
it.setLogicalDisplaySize(getLogicalDisplaySize()) |
| 49 |
surfaceReadyLatch = it.addSurfaceView(defaultBufferSize) |
| 50 |
} |
| 51 |
surfaceReadyLatch.await() |
| 52 |
// sleep to finish animations |
| 53 |
instrumentation.waitForIdleSync() |
| 54 |
} |
| 55 |
|
| 56 |
@After |
| 57 |
override fun teardown() { |
| 58 |
super.teardown() |
| 59 |
mActivityRule.finishActivity() |
| 60 |
} |
| 61 |
|
| 62 |
fun runOnUiThread(predicate: (it: MainActivity) -> Unit) { |
| 63 |
mActivityRule.runOnUiThread { |
| 64 |
predicate(mActivity) |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
fun withScreenRecording( |
| 69 |
boundsToCheck: Rect, |
| 70 |
@ColorInt color: Int, |
| 71 |
predicate: (it: MainActivity) -> Unit |
| 72 |
): CapturedActivity.TestResult { |
| 73 |
val testCase = object : ISurfaceValidatorTestCase { |
| 74 |
override fun getChecker(): PixelChecker = RectChecker(boundsToCheck, color) |
| 75 |
override fun start(context: Context, parent: FrameLayout) { |
| 76 |
predicate(mActivity) |
| 77 |
} |
| 78 |
override fun end() { /* do nothing */ } |
| 79 |
} |
| 80 |
|
| 81 |
return mActivity.runTest(testCase) |
| 82 |
} |
| 83 |
} |