1 /*
2  * Copyright (C) 2022 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.graphics.Bitmap
20 import android.graphics.Rect
21 import androidx.test.runner.screenshot.Screenshot
22 import platform.test.screenshot.matchers.BitmapMatcher
23 import platform.test.screenshot.matchers.PixelPerfectMatcher
24 
25 interface ScreenshotAsserter {
assertGoldenImagenull26     fun assertGoldenImage(goldenId: String, areas: List<Rect>)
27     fun assertGoldenImage(goldenId: String)
28 }
29 
30 /**
31  * Factory to create [ScreenshotAsserter] instances.
32  *
33  * Prefer using this interface over of exposing [ScreenshotTestRule] - or its wrappers - directly.
34  */
35 interface ScreenshotAsserterFactory {
36     /** Creates a pre-configured [ScreenshotAsserter]. */
37     fun createScreenshotAsserter(
38         config: ScreenshotAsserterConfig = ScreenshotAsserterConfig()
39     ): ScreenshotAsserter
40 }
41 
42 /** Config options to configure new [ScreenshotAsserter] instances. */
43 data class ScreenshotAsserterConfig(
44     /**
45      * Strategy to compute whether two bitmaps are the same for the purpose of a screenshot golden
46      * tests
47      */
48     val matcher: BitmapMatcher = PixelPerfectMatcher(),
<lambda>null49     val beforeScreenshot: () -> Unit = {},
<lambda>null50     val afterScreenshot: () -> Unit = {},
51     /**
52      * The [Bitmap] produced by [captureStrategy] will be recycled immediately after assertions are
53      * completed. Therefore, do not retain references to created [Bitmap]s.
54      */
<lambda>null55     val captureStrategy: BitmapSupplier = { Screenshot.capture().bitmap }
56 )
57