Skip to content

Commit

Permalink
migrate from @MethodSource to @CsvSource when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
mvmike committed Jan 12, 2024
1 parent 74c71ee commit 1d4ede7
Show file tree
Hide file tree
Showing 16 changed files with 906 additions and 1,843 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import io.mockk.verify
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource
import org.junit.jupiter.params.provider.CsvSource
import java.time.DayOfWeek
import java.util.concurrent.TimeoutException
import kotlin.system.measureTimeMillis
Expand Down Expand Up @@ -91,8 +91,21 @@ internal class RedrawWidgetUseCaseTest : BaseTest() {
}

@ParameterizedTest
@MethodSource("getWidgetIdsWithDrawingConfigurations")
fun shouldRedrawWidgetRegardlessOfBinderProxyTransactionTimeout(testProperties: RedrawWidgetUseCaseTestProperties) {
@CsvSource(
"1,32,DARK,20,33,MONDAY,false",
"5,100,LIGHT,5,33,SUNDAY,false",
"7,0,DARK,100,34,THURSDAY,true",
"14,50,DARK,32,35,MONDAY,true"
)
fun shouldRedrawWidgetRegardlessOfBinderProxyTransactionTimeout(
appWidgetId: Int,
textSizePercentage: Int,
widgetTheme: Theme,
transparencyPercentage: Int,
runtimeSDK: Int,
firstDayOfWeek: DayOfWeek,
shouldHaveFirstWeekLocalPreferenceEnabled: Boolean
) {
mockkObject(
ActionableView.ConfigurationIcon,
ActionableView.MonthAndYearHeader,
Expand All @@ -110,114 +123,53 @@ internal class RedrawWidgetUseCaseTest : BaseTest() {
justRun { ActionableView.ConfigurationIcon.addListener(context, any()) }
justRun { ActionableView.MonthAndYearHeader.addListener(context, any()) }

mockWidgetTextSize(testProperties.textSize)
mockWidgetTheme(testProperties.widgetTheme)
mockWidgetTransparency(testProperties.transparency)
val textSize = TextSize(textSizePercentage)
val transparency = Transparency(transparencyPercentage)
mockWidgetTextSize(textSize)
mockWidgetTheme(widgetTheme)
mockWidgetTransparency(transparency)

mockGetRuntimeSDK(testProperties.runtimeSDK)
if (testProperties.shouldHaveFirstWeekLocalPreferenceEnabled) {
mockGetSystemFirstDayOfWeek(testProperties.firstDayOfWeek)
mockGetRuntimeSDK(runtimeSDK)
if (shouldHaveFirstWeekLocalPreferenceEnabled) {
mockGetSystemFirstDayOfWeek(firstDayOfWeek)
} else {
mockFirstDayOfWeek(testProperties.firstDayOfWeek)
mockFirstDayOfWeek(firstDayOfWeek)
}

justRun {
LayoutService.draw(
context = context,
widgetRemoteView = any(),
widgetTheme = testProperties.widgetTheme,
transparency = testProperties.transparency
)
}
justRun {
MonthAndYearHeaderService.draw(
context = context,
widgetRemoteView = any(),
textSize = testProperties.textSize,
widgetTheme = testProperties.widgetTheme
)
}
justRun {
DaysHeaderService.draw(
context = context,
widgetRemoteView = any(),
firstDayOfWeek = testProperties.firstDayOfWeek,
widgetTheme = testProperties.widgetTheme,
transparency = testProperties.transparency,
textSize = testProperties.textSize
)
}
justRun {
DaysService.draw(
context = context,
widgetRemoteView = any(),
firstDayOfWeek = testProperties.firstDayOfWeek,
widgetTheme = testProperties.widgetTheme,
transparency = testProperties.transparency,
textSize = testProperties.textSize
)
}
justRun { LayoutService.draw(context, any(), widgetTheme, transparency) }
justRun { MonthAndYearHeaderService.draw(context, any(), textSize, widgetTheme) }
justRun { DaysHeaderService.draw(context, any(), firstDayOfWeek, widgetTheme, transparency, textSize) }
justRun { DaysService.draw(context, any(), firstDayOfWeek, widgetTheme, transparency, textSize) }

val binderProxyTransactionTimeoutInMillis = 1000L
every { appWidgetManager.updateAppWidget(testProperties.appWidgetId, any()) } answers {
every { appWidgetManager.updateAppWidget(appWidgetId, any()) } answers {
Thread.sleep(binderProxyTransactionTimeoutInMillis)
throw TimeoutException("android.os.BinderProxy.transactNative timeout")
}

val executionTime = measureTimeMillis {
RedrawWidgetUseCase.execute(context, appWidgetManager, testProperties.appWidgetId)
RedrawWidgetUseCase.execute(context, appWidgetManager, appWidgetId)
}

assertThat(executionTime).isLessThan(binderProxyTransactionTimeoutInMillis)
verifyWidgetTextSize()
verifyWidgetTheme()
verifyWidgetTransparency()
verifyGetRuntimeSDK()
when (testProperties.shouldHaveFirstWeekLocalPreferenceEnabled) {
when (shouldHaveFirstWeekLocalPreferenceEnabled) {
true -> verifyGetSystemFirstDayOfWeek()
else -> verifyFirstDayOfWeek()
}
verify { context.packageName }
verify { ActionableView.ConfigurationIcon.addListener(context, any()) }
verify { ActionableView.MonthAndYearHeader.addListener(context, any()) }
verify {
LayoutService.draw(
context = context,
widgetRemoteView = any(),
widgetTheme = testProperties.widgetTheme,
transparency = testProperties.transparency
)
}
verify {
MonthAndYearHeaderService.draw(
context = context,
widgetRemoteView = any(),
textSize = testProperties.textSize,
widgetTheme = testProperties.widgetTheme
)
}
verify {
DaysHeaderService.draw(
context = context,
widgetRemoteView = any(),
firstDayOfWeek = testProperties.firstDayOfWeek,
widgetTheme = testProperties.widgetTheme,
transparency = testProperties.transparency,
textSize = testProperties.textSize
)
}
verify {
DaysService.draw(
context = context,
widgetRemoteView = any(),
firstDayOfWeek = testProperties.firstDayOfWeek,
widgetTheme = testProperties.widgetTheme,
transparency = testProperties.transparency,
textSize = testProperties.textSize
)
}

verify { appWidgetManager.updateAppWidget(testProperties.appWidgetId, any()) }
verify { LayoutService.draw(context, any(), widgetTheme, transparency) }
verify { MonthAndYearHeaderService.draw(context, any(), textSize, widgetTheme) }
verify { DaysHeaderService.draw(context, any(), firstDayOfWeek, widgetTheme, transparency, textSize) }
verify { DaysService.draw(context, any(), firstDayOfWeek, widgetTheme, transparency, textSize) }

verify { appWidgetManager.updateAppWidget(appWidgetId, any()) }

confirmVerified(
appWidgetManager,
Expand All @@ -227,21 +179,4 @@ internal class RedrawWidgetUseCaseTest : BaseTest() {
DaysService
)
}

private fun getWidgetIdsWithDrawingConfigurations() = listOf(
RedrawWidgetUseCaseTestProperties(1, TextSize(32), Theme.DARK, Transparency(20), 33, DayOfWeek.MONDAY, false),
RedrawWidgetUseCaseTestProperties(5, TextSize(100), Theme.LIGHT, Transparency(5), 33, DayOfWeek.SUNDAY, false),
RedrawWidgetUseCaseTestProperties(7, TextSize(0), Theme.DARK, Transparency(100), 34, DayOfWeek.THURSDAY, true),
RedrawWidgetUseCaseTestProperties(14, TextSize(50), Theme.DARK, Transparency(32), 35, DayOfWeek.MONDAY, true)
)

internal data class RedrawWidgetUseCaseTestProperties(
val appWidgetId: Int,
val textSize: TextSize,
val widgetTheme: Theme,
val transparency: Transparency,
val runtimeSDK: Int,
val firstDayOfWeek: DayOfWeek,
val shouldHaveFirstWeekLocalPreferenceEnabled: Boolean
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import io.mockk.mockkObject
import io.mockk.verify
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments.of
import org.junit.jupiter.params.provider.MethodSource
import org.junit.jupiter.params.provider.CsvSource
import org.junit.jupiter.params.provider.NullSource
import org.junit.jupiter.params.provider.ValueSource
import java.time.Instant.ofEpochSecond
Expand Down Expand Up @@ -147,7 +146,14 @@ internal class ProcessIntentUseCaseTest : BaseTest() {
}

@ParameterizedTest
@MethodSource("getMincalCalendarIntentActionAndExpectedExtraInstantAndStartTimeInstant")
@ValueSource(
strings = [
"$CELL_DAY_CLICK_ACTION.1675786154",
"$CELL_DAY_CLICK_ACTION.1671349586",
"$CELL_DAY_CLICK_ACTION.1624298458",
"$CELL_DAY_CLICK_ACTION.1434887405"
]
)
fun shouldLaunchCalendarActivityOnTodayAndRedrawWidget(action: String) {
mockIntent(action)
mockIsReadCalendarPermitted(true)
Expand All @@ -172,7 +178,12 @@ internal class ProcessIntentUseCaseTest : BaseTest() {
}

@ParameterizedTest
@MethodSource("getMincalCalendarIntentActionAndExpectedExtraInstantAndStartTimeInstant")
@CsvSource(
"$CELL_DAY_CLICK_ACTION.1675886154,1675886154,1675863134",
"$CELL_DAY_CLICK_ACTION.1671249586,1671249586,1671283934",
"$CELL_DAY_CLICK_ACTION.1624398458,1624398458,1624455134",
"$CELL_DAY_CLICK_ACTION.1434987405,1434987405,1434979934"
)
fun shouldLaunchCalendarActivityOnIntentExtraAndRedrawWidget_whenIntentAndPermissionsGiven(
action: String,
extraInstantEpochSeconds: Long,
Expand Down Expand Up @@ -204,11 +215,4 @@ internal class ProcessIntentUseCaseTest : BaseTest() {
verify { RedrawWidgetUseCase.execute(context) }
verify { AutoUpdate.set(context) }
}

private fun getMincalCalendarIntentActionAndExpectedExtraInstantAndStartTimeInstant() = listOf(
of("$CELL_DAY_CLICK_ACTION.1675886154", 1675886154, 1675863134),
of("$CELL_DAY_CLICK_ACTION.1671249586", 1671249586, 1671283934),
of("$CELL_DAY_CLICK_ACTION.1624398458", 1624398458, 1624455134),
of("$CELL_DAY_CLICK_ACTION.1434987405", 1434987405, 1434979934)
)
}
Loading

0 comments on commit 1d4ede7

Please sign in to comment.