|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2021 Shopify Inc. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | +package com.shopify.testify |
| 25 | + |
| 26 | +import android.app.Activity |
| 27 | +import android.graphics.Bitmap |
| 28 | +import androidx.compose.runtime.Composable |
| 29 | +import androidx.compose.ui.platform.ComposeView |
| 30 | +import androidx.test.espresso.Espresso |
| 31 | +import com.shopify.testify.compose.R |
| 32 | +import com.shopify.testify.internal.disposeComposition |
| 33 | +import org.junit.Assert.assertTrue |
| 34 | +import java.util.concurrent.CountDownLatch |
| 35 | +import java.util.concurrent.TimeUnit |
| 36 | + |
| 37 | +/** |
| 38 | + * Helper extension of [ScreenshotRule] which simplifies testing [Composable] functions. |
| 39 | + */ |
| 40 | +open class ComposableScreenshotRule( |
| 41 | + var exactness: Float = 0.9f |
| 42 | +) : ScreenshotRule<ComposableTestActivity>( |
| 43 | + ComposableTestActivity::class.java, |
| 44 | + launchActivity = false, |
| 45 | +) { |
| 46 | + lateinit var composeFunction: @Composable () -> Unit |
| 47 | + |
| 48 | + open fun onCleanUp(activity: Activity) { |
| 49 | + activity.disposeComposition() |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Set a screenshot view provider to capture only the @Composable bounds |
| 54 | + */ |
| 55 | + override fun beforeAssertSame() { |
| 56 | + super.beforeAssertSame() |
| 57 | + TestifyFeatures.PixelCopyCapture.setEnabled(true) |
| 58 | + setExactness(exactness) |
| 59 | + setScreenshotViewProvider { |
| 60 | + it.getChildAt(0) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Render the composable function after the activity has loaded. |
| 66 | + */ |
| 67 | + override fun afterActivityLaunched() { |
| 68 | + Espresso.onIdle() |
| 69 | + val latch = CountDownLatch(1) |
| 70 | + activity.runOnUiThread { |
| 71 | + val composeView = activity.findViewById<ComposeView>(R.id.compose_container) |
| 72 | + composeView.setContent { |
| 73 | + composeFunction() |
| 74 | + latch.countDown() |
| 75 | + } |
| 76 | + } |
| 77 | + assertTrue(latch.await(COMPOSE_TIMEOUT_SECONDS, TimeUnit.SECONDS)) |
| 78 | + Espresso.onIdle() |
| 79 | + super.afterActivityLaunched() |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Proactively dispose of any compositions after the screenshot has been taken. |
| 84 | + */ |
| 85 | + override fun afterScreenshot(activity: Activity, currentBitmap: Bitmap?) { |
| 86 | + super.afterScreenshot(activity, currentBitmap) |
| 87 | + onCleanUp(activity) |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Used to provide a @Composable function to be rendered in the screenshot. |
| 92 | + */ |
| 93 | + fun setCompose(composable: @Composable () -> Unit): ComposableScreenshotRule { |
| 94 | + composeFunction = composable |
| 95 | + return this |
| 96 | + } |
| 97 | + |
| 98 | + companion object { |
| 99 | + private const val COMPOSE_TIMEOUT_SECONDS: Long = 15 |
| 100 | + } |
| 101 | +} |
0 commit comments