1 /* 2 * Copyright 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 com.android.input.screenshot 18 19 import android.graphics.Bitmap 20 import android.graphics.Canvas 21 import android.os.Build 22 import android.view.View 23 import platform.test.screenshot.matchers.MSSIMMatcher 24 import platform.test.screenshot.matchers.PixelPerfectMatcher 25 26 /** Draw this [View] into a [Bitmap]. */ 27 // TODO(b/195673633): Remove this once Compose screenshot tests use hardware rendering for their 28 // tests. Viewnull29fun View.drawIntoBitmap(): Bitmap { 30 val bitmap = 31 Bitmap.createBitmap( 32 measuredWidth, 33 measuredHeight, 34 Bitmap.Config.ARGB_8888, 35 ) 36 val canvas = Canvas(bitmap) 37 draw(canvas) 38 return bitmap 39 } 40 41 /** 42 * The [BitmapMatcher][platform.test.screenshot.matchers.BitmapMatcher] that should be used for 43 * screenshot *unit* tests. 44 */ 45 val UnitTestBitmapMatcher = 46 if (Build.CPU_ABI == "x86_64") { 47 // Different CPU architectures can sometimes end up rendering differently, so we can't do 48 // pixel-perfect matching on different architectures using the same golden. Given that our 49 // presubmits are run on cf_x86_64_phone, our goldens should be perfectly matched on the 50 // x86_64 architecture and use the Structural Similarity Index on others. 51 // TODO(b/237511747): Run our screenshot presubmit tests on arm64 instead so that we can 52 // do pixel perfect matching both at presubmit time and at development time with actual 53 // devices. 54 PixelPerfectMatcher() 55 } else { 56 MSSIMMatcher() 57 } 58 59 /** 60 * The [BitmapMatcher][platform.test.screenshot.matchers.BitmapMatcher] that should be used for 61 * screenshot *unit* tests. 62 * 63 * We use the Structural Similarity Index for integration tests because they usually contain 64 * additional information and noise that shouldn't break the test. 65 */ 66 val IntegrationTestBitmapMatcher = MSSIMMatcher()