Skip to content

Commit 088327f

Browse files
committed
add new test
1 parent 0fa8805 commit 088327f

2 files changed

Lines changed: 97 additions & 20 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2026 David Allison <davidallisongithub@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify it under
5+
* the terms of the GNU General Public License as published by the Free Software
6+
* Foundation; either version 3 of the License, or (at your option) any later
7+
* version.
8+
*
9+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
10+
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
11+
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License along with
14+
* this program. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
package com.ichi2.anki
17+
18+
import android.view.Gravity
19+
import android.view.View
20+
import android.view.ViewGroup
21+
import android.widget.FrameLayout
22+
import androidx.core.graphics.Insets
23+
import androidx.core.view.ViewCompat
24+
import androidx.core.view.WindowInsetsCompat
25+
import androidx.test.ext.junit.runners.AndroidJUnit4
26+
import org.junit.Test
27+
import org.junit.runner.RunWith
28+
29+
/**
30+
* Screenshot tests for [CardBrowser]
31+
*
32+
* `./gradlew :AnkiDroid:verifyRoborazziPlayDebug -Pscreenshot --tests "com.ichi2.anki.CardBrowserScreenshotTest"`
33+
*/
34+
@RunWith(AndroidJUnit4::class)
35+
class CardBrowserScreenshotTest : ScreenshotTest() {
36+
@Test
37+
fun cardBrowserWith30Notes() =
38+
withCardBrowser(noteCount = 50) { browser ->
39+
// Robolectric reports zero system-bar insets by default. Inject realistic ones
40+
// so the app's edge-to-edge layout responds as it would on a real device.
41+
val density = browser.resources.displayMetrics.density
42+
val statusBarPx = (24 * density).toInt()
43+
val navBarPx = (48 * density).toInt()
44+
val insets =
45+
WindowInsetsCompat
46+
.Builder()
47+
.setInsets(WindowInsetsCompat.Type.statusBars(), Insets.of(0, statusBarPx, 0, 0))
48+
.setInsets(WindowInsetsCompat.Type.navigationBars(), Insets.of(0, 0, 0, navBarPx))
49+
.build()
50+
ViewCompat.dispatchApplyWindowInsets(browser.window.decorView, insets)
51+
52+
// overlay a translucent band where the nav bar would sit
53+
// to see if content is drawn underneath it
54+
val decor = browser.window.decorView as ViewGroup
55+
val navBarOverlay =
56+
View(browser).apply {
57+
setBackgroundColor(0x80000000.toInt())
58+
}
59+
decor.addView(
60+
navBarOverlay,
61+
FrameLayout.LayoutParams(
62+
FrameLayout.LayoutParams.MATCH_PARENT,
63+
navBarPx,
64+
Gravity.BOTTOM,
65+
),
66+
)
67+
68+
captureScreen("30_notes")
69+
}
70+
}

AnkiDroid/src/test/java/com/ichi2/anki/CardBrowserTest.kt

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -974,26 +974,6 @@ class CardBrowserTest : RobolectricTest() {
974974
advanceRobolectricLooper()
975975
}
976976

977-
/** Returns an instance of [CardBrowser] containing [noteCount] notes */
978-
private fun getBrowserWithNotes(
979-
noteCount: Int,
980-
reversed: Boolean = false,
981-
): CardBrowser {
982-
ensureCollectionLoadIsSynchronous()
983-
if (reversed) {
984-
for (i in 0 until noteCount) {
985-
addBasicAndReversedNote(i.toString(), "back")
986-
}
987-
} else {
988-
for (i in 0 until noteCount) {
989-
addBasicNote(i.toString(), "back")
990-
}
991-
}
992-
return super.startRegularActivity<CardBrowser>(Intent()).also {
993-
advanceRobolectricLooper() // may be a fix for flaky tests
994-
}
995-
}
996-
997977
private val browserWithNoNewCards: CardBrowser
998978
get() = getBrowserWithNotes(0)
999979

@@ -2132,3 +2112,30 @@ suspend fun CardBrowser.selectAll() {
21322112

21332113
val CardBrowser.menu: Menu
21342114
get() = if (this.useSearchView) cardBrowserFragment.searchBar!!.menu else shadowOf(this).optionsMenu!!
2115+
2116+
/** Returns an instance of [CardBrowser] containing [noteCount] notes */
2117+
context(test: RobolectricTest)
2118+
fun getBrowserWithNotes(
2119+
noteCount: Int,
2120+
reversed: Boolean = false,
2121+
): CardBrowser {
2122+
test.ensureCollectionLoadIsSynchronous()
2123+
if (reversed) {
2124+
for (i in 0 until noteCount) {
2125+
test.addBasicAndReversedNote(i.toString(), "back")
2126+
}
2127+
} else {
2128+
for (i in 0 until noteCount) {
2129+
test.addBasicNote(i.toString(), "back")
2130+
}
2131+
}
2132+
return test.startRegularActivity<CardBrowser>(Intent()).also {
2133+
advanceRobolectricLooper() // may be a fix for flaky tests
2134+
}
2135+
}
2136+
2137+
context(test: RobolectricTest)
2138+
fun withCardBrowser(
2139+
noteCount: Int,
2140+
block: (CardBrowser) -> Unit,
2141+
) = block(getBrowserWithNotes(noteCount))

0 commit comments

Comments
 (0)