|
| 1 | +// SPDX-FileCopyrightText: 2026 David Allison <davidallisongithub@gmail.com> |
| 2 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 3 | +package com.ichi2.anki |
| 4 | + |
| 5 | +import android.app.Activity |
| 6 | +import android.view.Gravity |
| 7 | +import android.view.View |
| 8 | +import android.view.ViewGroup |
| 9 | +import android.widget.FrameLayout |
| 10 | +import androidx.core.content.edit |
| 11 | +import androidx.core.graphics.Insets |
| 12 | +import androidx.core.view.ViewCompat |
| 13 | +import androidx.core.view.WindowInsetsCompat |
| 14 | +import com.ichi2.anki.account.AccountActivity |
| 15 | +import com.ichi2.anki.instantnoteeditor.InstantNoteEditorActivity |
| 16 | +import com.ichi2.anki.multimedia.MultimediaActivity |
| 17 | +import com.ichi2.anki.preferences.PreferencesActivity |
| 18 | +import com.ichi2.anki.preferences.sharedPrefs |
| 19 | +import com.ichi2.anki.previewer.CardViewerActivity |
| 20 | +import com.ichi2.anki.utils.ConfigAwareSingleFragmentActivity |
| 21 | +import com.ichi2.testutils.ActivityList |
| 22 | +import com.ichi2.testutils.ActivityList.ActivityLaunchParam |
| 23 | +import com.ichi2.testutils.BackupManagerTestUtilities |
| 24 | +import com.ichi2.testutils.skipTest |
| 25 | +import com.ichi2.utils.dp |
| 26 | +import org.junit.After |
| 27 | +import org.junit.Before |
| 28 | +import org.junit.Test |
| 29 | +import org.junit.runner.RunWith |
| 30 | +import org.robolectric.ParameterizedRobolectricTestRunner |
| 31 | + |
| 32 | +/** |
| 33 | + * Captures a baseline screenshot for every activity declared in the manifest. |
| 34 | + * |
| 35 | + * @see ActivityList.allActivitiesAndIntents |
| 36 | + * |
| 37 | + * `./gradlew :AnkiDroid:verifyRoborazziPlayDebug -Pscreenshot --tests "com.ichi2.anki.AllActivitiesScreenshotTest"` |
| 38 | + * |
| 39 | + * TODO: Split each activity into its own per-class screenshot test |
| 40 | + */ |
| 41 | +@RunWith(ParameterizedRobolectricTestRunner::class) |
| 42 | +class AllActivitiesScreenshotTest : ScreenshotTest() { |
| 43 | + init { |
| 44 | + setPhoneQualifiers() |
| 45 | + } |
| 46 | + |
| 47 | + @ParameterizedRobolectricTestRunner.Parameter |
| 48 | + @JvmField |
| 49 | + var launcher: ActivityLaunchParam? = null |
| 50 | + |
| 51 | + // Used for display and the screenshot filename (e.g. "DeckPicker" or "DeckPicker_edgeToEdge") |
| 52 | + @ParameterizedRobolectricTestRunner.Parameter(1) |
| 53 | + @JvmField |
| 54 | + var displayName: String? = null |
| 55 | + |
| 56 | + @ParameterizedRobolectricTestRunner.Parameter(2) |
| 57 | + @JvmField |
| 58 | + var configure: (Activity.() -> Unit)? = null |
| 59 | + |
| 60 | + @Before |
| 61 | + override fun setUp() { |
| 62 | + // Same exclusions as ActivityStartupUnderBackupTest — onCreate fails standalone for these. |
| 63 | + notYetHandled(IntentHandler::class.java.simpleName, "Not working (or implemented) - inherits from Activity") |
| 64 | + notYetHandled(IntentHandler2::class.java.simpleName, "Not working (or implemented) - inherits from Activity") |
| 65 | + notYetHandled( |
| 66 | + PreferencesActivity::class.java.simpleName, |
| 67 | + "Not working (or implemented) - inherits from AppCompatPreferenceActivity", |
| 68 | + ) |
| 69 | + notYetHandled( |
| 70 | + SingleFragmentActivity::class.java.simpleName, |
| 71 | + "Implemented, but the test fails because the activity throws if a specific intent extra isn't set", |
| 72 | + ) |
| 73 | + notYetHandled(InstantNoteEditorActivity::class.java.simpleName, "Single instance activity so should be used") |
| 74 | + |
| 75 | + // Fragment-host activities: need a 'fragmentName' intent extra to render anything. |
| 76 | + // TODO: split these into per-class screenshot tests that pass a real fragment. |
| 77 | + notYetHandled(ConfigAwareSingleFragmentActivity::class.java.simpleName, "Needs 'fragmentName' intent extra") |
| 78 | + notYetHandled(CardViewerActivity::class.java.simpleName, "Needs 'fragmentName' intent extra") |
| 79 | + notYetHandled(MultimediaActivity::class.java.simpleName, "Needs 'fragmentName' intent extra") |
| 80 | + notYetHandled(AccountActivity::class.java.simpleName, "Needs 'fragmentName' intent extra") |
| 81 | + |
| 82 | + // TODO: split into a per-class test that creates a real note type before launching. |
| 83 | + notYetHandled(CardTemplateEditor::class.java.simpleName, "Needs a real note type in the collection") |
| 84 | + |
| 85 | + super.setUp() |
| 86 | + |
| 87 | + // Setup for DeckPicker |
| 88 | + ensureCollectionLoadIsSynchronous() |
| 89 | + setIntroductionSlidesShown(true) |
| 90 | + BackupManagerTestUtilities.setupSpaceForBackup(targetContext) |
| 91 | + // suppress the periodic 'backup your collection' prompt so the screenshot is just the activity |
| 92 | + targetContext.sharedPrefs().edit { putBoolean("backupPromptDisabled", true) } |
| 93 | + } |
| 94 | + |
| 95 | + @After |
| 96 | + fun tearDownBackup() { |
| 97 | + BackupManagerTestUtilities.reset() |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + fun screenshot() { |
| 102 | + val activity = |
| 103 | + startActivityNormallyOpenCollectionWithIntent( |
| 104 | + launcher!!.activity, |
| 105 | + launcher!!.buildIntent(targetContext), |
| 106 | + ) |
| 107 | + configure!!(activity) |
| 108 | + captureScreen(displayName!!) |
| 109 | + } |
| 110 | + |
| 111 | + private fun notYetHandled( |
| 112 | + activityName: String, |
| 113 | + reason: String, |
| 114 | + ) { |
| 115 | + if (launcher!!.simpleName == activityName) { |
| 116 | + skipTest("$activityName $reason") |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + companion object { |
| 121 | + private val regular: Activity.() -> Unit = {} |
| 122 | + private val edgeToEdge: Activity.() -> Unit = { simulateEdgeToEdge() } |
| 123 | + |
| 124 | + @ParameterizedRobolectricTestRunner.Parameters(name = "{1}") |
| 125 | + @JvmStatic |
| 126 | + fun initParameters(): Collection<Array<Any>> = |
| 127 | + ActivityList.allActivitiesAndIntents().flatMap { launcher -> |
| 128 | + listOf( |
| 129 | + arrayOf<Any>(launcher, launcher.simpleName, regular), |
| 130 | + arrayOf<Any>(launcher, "${launcher.simpleName}_edgeToEdge", edgeToEdge), |
| 131 | + ) |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * Inject realistic system bars for edge to edge. |
| 138 | + * |
| 139 | + * Mirrors the helper from `DeckPickerScreenshotTest` on the `edge-2-edge` branch. |
| 140 | + * |
| 141 | + * WARN: Does not match reality. There are issues with element placement and scrolling lists. [FAB in Deck Picker] |
| 142 | + */ |
| 143 | +private fun Activity.simulateEdgeToEdge() { |
| 144 | + val insets = |
| 145 | + WindowInsetsCompat |
| 146 | + .Builder() |
| 147 | + .setInsets(WindowInsetsCompat.Type.statusBars(), Insets.of(0, 24.dp.toPx(this), 0, 0)) |
| 148 | + .setInsets(WindowInsetsCompat.Type.navigationBars(), Insets.of(0, 0, 0, 48.dp.toPx(this))) |
| 149 | + .build() |
| 150 | + ViewCompat.dispatchApplyWindowInsets(window.decorView, insets) |
| 151 | + |
| 152 | + val decor = window.decorView as ViewGroup |
| 153 | + val navBarOverlay = |
| 154 | + View(this).apply { |
| 155 | + setBackgroundColor(0x80000000.toInt()) |
| 156 | + } |
| 157 | + decor.addView( |
| 158 | + navBarOverlay, |
| 159 | + FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, 48.dp.toPx(this), Gravity.BOTTOM), |
| 160 | + ) |
| 161 | +} |
0 commit comments