0
0
mirror of https://github.com/ankidroid/Anki-Android.git synced 2024-09-20 03:52:15 +02:00

#13283 Resolve @NeedsTest Issues for flagToDisplay in Reviewer.kt (#14291)

* refactor: separate logic from dependencies by extracting easy to test class FlagToDisplay
* test: cover FLAG_NONE case
* test: Resolve @NeedsTest("is hidden if flag is on app bar")
* test: Resolve @NeedsTest("is not hidden if flag is not on app bar")
* test: Resolve @NeedsTest("is not hidden if flag is on app bar and fullscreen is enabled")
* test: Remove redundant test, as already covered by other tests
* refactor: simplify now tested logic
* test: remove @RunWith to fix Roboelectric Exception
* refactor: add more meaning to field by renaming to actualFlag
* refactor: revert accidental auto formatting
* refactor: add own copyright

---------

Co-authored-by: Paul Tietz <pati@aprixon.de>
This commit is contained in:
taulpi 2023-08-25 16:33:34 +02:00 committed by GitHub
parent 7e28aa18a0
commit a54c63dc30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 13 deletions

View File

@ -0,0 +1,33 @@
/****************************************************************************************
* Copyright (c) 2023 Paul Tietz <tietz.paul@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify it under *
* the terms of the GNU General Public License as published by the Free Software *
* Foundation; either version 3 of the License, or (at your option) any later *
* version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT ANY *
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
* PARTICULAR PURPOSE. See the GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License along with *
* this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************************/
package com.ichi2.anki
import com.ichi2.anki.reviewer.CardMarker
class FlagToDisplay(
private val actualFlag: Int,
private val isOnAppBar: Boolean,
private val isFullscreen: Boolean
) {
fun get(): Int {
return when {
!isOnAppBar -> actualFlag
isFullscreen -> actualFlag
else -> CardMarker.FLAG_NONE
}
}
}

View File

@ -187,21 +187,13 @@ open class Reviewer :
}
}
@NeedsTest("is hidden if flag is on app bar")
@NeedsTest("is not hidden if flag is not on app bar")
@NeedsTest("is not hidden if flag is on app bar and fullscreen is enabled")
protected val flagToDisplay: Int
get() {
val actualValue = currentCard!!.userFlag()
if (actualValue == CardMarker.FLAG_NONE) {
return CardMarker.FLAG_NONE
}
val shownAsToolbarButton = mActionButtons.findMenuItem(ActionButtons.RES_FLAG)?.isActionButton == true
return if (shownAsToolbarButton && !mPrefFullscreenReview) {
CardMarker.FLAG_NONE
} else {
actualValue
}
return FlagToDisplay(
currentCard!!.userFlag(),
mActionButtons.findMenuItem(ActionButtons.RES_FLAG)?.isActionButton ?: true,
mPrefFullscreenReview
).get()
}
override fun recreateWebView() {

View File

@ -0,0 +1,41 @@
//noinspection MissingCopyrightHeader #8659
package com.ichi2.anki
import com.ichi2.anki.reviewer.CardMarker.Companion.FLAG_BLUE
import com.ichi2.anki.reviewer.CardMarker.Companion.FLAG_GREEN
import com.ichi2.anki.reviewer.CardMarker.Companion.FLAG_NONE
import com.ichi2.anki.reviewer.CardMarker.Companion.FLAG_ORANGE
import com.ichi2.anki.reviewer.CardMarker.Companion.FLAG_PINK
import com.ichi2.anki.reviewer.CardMarker.Companion.FLAG_PURPLE
import com.ichi2.anki.reviewer.CardMarker.Companion.FLAG_RED
import com.ichi2.anki.reviewer.CardMarker.Companion.FLAG_TURQUOISE
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
class FlagToDisplayTest {
@ParameterizedTest
@ValueSource(ints = [FLAG_NONE, FLAG_RED, FLAG_ORANGE, FLAG_GREEN, FLAG_BLUE, FLAG_PINK, FLAG_TURQUOISE, FLAG_PURPLE])
fun `is hidden if flag is on app bar and fullscreen is disabled`(actualFlag: Int) {
assertEquals(FLAG_NONE, FlagToDisplay(actualFlag, isOnAppBar = true, isFullscreen = false).get())
}
@ParameterizedTest
@ValueSource(ints = [FLAG_NONE, FLAG_RED, FLAG_ORANGE, FLAG_GREEN, FLAG_BLUE, FLAG_PINK, FLAG_TURQUOISE, FLAG_PURPLE])
fun `is not hidden if flag is not on app bar and fullscreen is disabled`(actualFlag: Int) {
assertEquals(actualFlag, FlagToDisplay(actualFlag, isOnAppBar = false, isFullscreen = false).get())
}
@ParameterizedTest
@ValueSource(ints = [FLAG_NONE, FLAG_RED, FLAG_ORANGE, FLAG_GREEN, FLAG_BLUE, FLAG_PINK, FLAG_TURQUOISE, FLAG_PURPLE])
fun `is not hidden if flag is not on app bar and fullscreen is enabled`(actualFlag: Int) {
assertEquals(actualFlag, FlagToDisplay(actualFlag, isOnAppBar = false, isFullscreen = true).get())
}
@ParameterizedTest
@ValueSource(ints = [FLAG_NONE, FLAG_RED, FLAG_ORANGE, FLAG_GREEN, FLAG_BLUE, FLAG_PINK, FLAG_TURQUOISE, FLAG_PURPLE])
fun `is not hidden if flag is on app bar and fullscreen is enabled`(actualFlag: Int) {
assertEquals(actualFlag, FlagToDisplay(actualFlag, isOnAppBar = true, isFullscreen = true).get())
}
}