1 /*
<lambda>null2 * Copyright (C) 2023 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 platform.test.screenshot
18
19 import android.app.Activity
20 import android.app.Dialog
21 import android.graphics.Bitmap
22 import androidx.test.ext.junit.rules.ActivityScenarioRule
23 import java.util.concurrent.TimeUnit
24 import platform.test.screenshot.matchers.BitmapMatcher
25
26 fun <A : Activity> dialogScreenshotTest(
27 activityRule: ActivityScenarioRule<A>,
28 bitmapDiffer: BitmapDiffer,
29 matcher: BitmapMatcher,
30 goldenIdentifier: String,
31 waitForIdle: () -> Unit = {},
32 dialogProvider: (A) -> Dialog,
33 ) {
34 var dialog: Dialog? = null
activitynull35 activityRule.scenario.onActivity { activity ->
36 dialog =
37 dialogProvider(activity).apply {
38 val window = checkNotNull(window)
39
40 // Make sure that the dialog draws full screen and fits the whole display
41 // instead of the system bars.
42 window.setDecorFitsSystemWindows(false)
43
44 // Disable enter/exit animations.
45 create()
46 window.setWindowAnimations(0)
47
48 // Elevation/shadows is not deterministic when doing hardware rendering, so we
49 // disable it for any view in the hierarchy.
50 window.decorView.removeElevationRecursively()
51
52 // Show the dialog.
53 show()
54 }
55 }
56
57 waitForIdle()
58
59 try {
60 val bitmap = dialog?.toBitmap() ?: error("dialog is null")
61
62 bitmapDiffer.assertBitmapAgainstGolden(
63 bitmap,
64 goldenIdentifier,
65 matcher,
66 )
67 } finally {
68 dialog?.dismiss()
69 }
70 }
71
toBitmapnull72 private fun Dialog.toBitmap(): Bitmap {
73 val window = checkNotNull(window)
74 return window.decorView.captureToBitmapAsync().get(10, TimeUnit.SECONDS)
75 ?: error("timeout while trying to capture view to bitmap for window")
76 }
77