Skip to content

Commit

Permalink
Don't use MainActivity for screenshot tests (#1046)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maria Neumayer authored May 24, 2022
1 parent ffdb7a5 commit 2960305
Show file tree
Hide file tree
Showing 20 changed files with 26 additions and 12 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,20 @@ import androidx.activity.compose.setContent
import androidx.annotation.ColorRes
import androidx.annotation.Dimension
import androidx.annotation.Dimension.DP
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ProvidedValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
import androidx.test.core.app.ActivityScenario
import androidx.test.platform.app.InstrumentationRegistry
import com.facebook.testing.screenshot.Screenshot
import com.facebook.testing.screenshot.ViewHelpers
import com.facebook.testing.screenshot.internal.TestNameDetector
import net.skyscanner.backpack.demo.MainActivity
import net.skyscanner.backpack.demo.compose.BackpackPreview
import org.hamcrest.Matchers
import org.junit.Assume
Expand Down Expand Up @@ -109,7 +110,7 @@ open class BpkSnapshotTest {
tags.joinToString(separator = "_", prefix = getScreenshotName() + ".") { it.toString() }
}

ActivityScenario.launch(MainActivity::class.java).use { scenario ->
ActivityScenario.launch(AppCompatActivity::class.java).use { scenario ->
scenario.onActivity { activity ->
with(activity) {
setContent {
Expand All @@ -121,7 +122,7 @@ open class BpkSnapshotTest {
)
}

val view = (window.decorView as ViewGroup).getChildAt(0)
val view = window.decorView.findComposeView()

ViewHelpers.setupView(view)
.setExactWidthDp(size.width)
Expand All @@ -136,6 +137,22 @@ open class BpkSnapshotTest {
}
}

private fun View.findComposeView(): ComposeView? {
if (this !is ViewGroup) return null
for (i in 0..childCount) {
val child = getChildAt(i)
if (child is ComposeView) {
return child
} else if (child is ViewGroup) {
val view = child.findComposeView()
if (view != null) {
return view
}
}
}
return null
}

inner class AsyncSnapshot(private val testClass: String, private val testName: String) {
fun record(view: View) {
Screenshot.snap(wrapMeasuredViewWithBackground(view))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import net.skyscanner.backpack.calendar.presenter.CurrentDateProvider
import net.skyscanner.backpack.calendar.presenter.HighlightedDaysAdapter
import net.skyscanner.backpack.calendar.presenter.MonthFooterAdapter
import net.skyscanner.backpack.calendar.presenter.SelectionType
import net.skyscanner.backpack.demo.MainActivity
import net.skyscanner.backpack.demo.data.multiColoredExampleCalendarColoring
import org.hamcrest.CoreMatchers
import org.junit.Before
Expand Down Expand Up @@ -85,8 +84,8 @@ class BpkCalendarTest : BpkSnapshotTest() {
private lateinit var activity: AppCompatActivity

@get:Rule
var activityRule: ActivityTestRule<MainActivity> =
ActivityTestRule(MainActivity::class.java)
var activityRule: ActivityTestRule<AppCompatActivity> =
ActivityTestRule(AppCompatActivity::class.java)

@Before
fun setup() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import kotlinx.coroutines.test.TestCoroutineDispatcher
import net.skyscanner.backpack.BpkSnapshotTest
import net.skyscanner.backpack.calendar2.data.CalendarDispatchers
import net.skyscanner.backpack.calendar2.extension.toIterable
import net.skyscanner.backpack.demo.MainActivity
import net.skyscanner.backpack.demo.R
import net.skyscanner.backpack.util.InternalBackpackApi
import org.junit.Before
Expand All @@ -56,8 +55,8 @@ class BpkCalendarTest : BpkSnapshotTest() {
private lateinit var activity: AppCompatActivity

@get:Rule
var activityRule: ActivityTestRule<MainActivity> =
ActivityTestRule(MainActivity::class.java)
var activityRule: ActivityTestRule<AppCompatActivity> =
ActivityTestRule(AppCompatActivity::class.java)

@OptIn(InternalBackpackApi::class, ExperimentalCoroutinesApi::class)
@Before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.rule.ActivityTestRule
import net.skyscanner.backpack.BpkSnapshotTest
import net.skyscanner.backpack.demo.MainActivity
import net.skyscanner.backpack.demo.R
import net.skyscanner.backpack.util.unsafeLazy
import org.hamcrest.Matcher
Expand All @@ -45,8 +44,8 @@ class BpkSnackbarTests : BpkSnapshotTest() {
private lateinit var activity: AppCompatActivity

@get:Rule
var activityRule: ActivityTestRule<MainActivity> =
ActivityTestRule(MainActivity::class.java)
var activityRule: ActivityTestRule<AppCompatActivity> =
ActivityTestRule(AppCompatActivity::class.java)

@Before
fun setUp() {
Expand Down

0 comments on commit 2960305

Please sign in to comment.