Skip to content

Commit 4c200a0

Browse files
authored
Configurable scenario supplier (#301)
* Updated extension with a configurable scenario supplier * Added a function which only takes a ScenarioSupplier in addition to the one which takes a Class and ScenarioSupplier * Added / updated javadoc * Updated the API surface file
1 parent 10b5d03 commit 4c200a0

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

instrumentation/compose/api/compose.api

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public final class de/mannodermaus/junit5/compose/AndroidComposeExtension : de/m
1313
}
1414

1515
public final class de/mannodermaus/junit5/compose/AndroidComposeExtensionKt {
16-
public static final fun createAndroidComposeExtension (Ljava/lang/Class;)Lde/mannodermaus/junit5/compose/AndroidComposeExtension;
16+
public static final fun createAndroidComposeExtension (Ljava/lang/Class;Lkotlin/jvm/functions/Function0;)Lde/mannodermaus/junit5/compose/AndroidComposeExtension;
17+
public static synthetic fun createAndroidComposeExtension$default (Ljava/lang/Class;Lkotlin/jvm/functions/Function0;ILjava/lang/Object;)Lde/mannodermaus/junit5/compose/AndroidComposeExtension;
1718
public static final fun createComposeExtension ()Lde/mannodermaus/junit5/compose/ComposeExtension;
1819
}
1920

instrumentation/compose/src/main/java/de/mannodermaus/junit5/compose/AndroidComposeExtension.kt

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,26 @@ public inline fun <reified A : ComponentActivity> createAndroidComposeExtension(
5151
* for field injection. Prefer this over [createComposeExtension] if your tests require a custom Activity.
5252
* This is usually the case for tests where the Compose content is set by that Activity, instead of
5353
* via [ComposeContext.setContent], provided through the extension. Make sure that you add the provided
54-
* Activity to your app's manifest file.
54+
* Activity to your app's manifest file. This variant allows you to provide a custom [ActivityScenario]
55+
* which is useful in cases where you may want to launch an activity with a custom intent, for example.
5556
*/
5657
@ExperimentalTestApi
57-
public fun <A : ComponentActivity> createAndroidComposeExtension(
58-
activityClass: Class<A>
59-
): AndroidComposeExtension<A> {
60-
return AndroidComposeExtension(
61-
scenarioSupplier = {
62-
ActivityScenario.launch(activityClass)
63-
}
64-
)
58+
public inline fun <reified A : ComponentActivity> createAndroidComposeExtension(noinline scenarioSupplier: () -> ActivityScenario<A>): AndroidComposeExtension<A> {
59+
return createAndroidComposeExtension(A::class.java, scenarioSupplier)
60+
}
61+
62+
/**
63+
* Factory method to provide a JUnit 5 extension for Compose using its [RegisterExtension] API
64+
* for field injection. Prefer this over [createComposeExtension] if your tests require a custom Activity.
65+
* This is usually the case for tests where the Compose content is set by that Activity, instead of
66+
* via [ComposeContext.setContent], provided through the extension. Make sure that you add the provided
67+
* Activity to your app's manifest file. You may also provide an optional [ActivityScenario] supplier
68+
* which is useful in cases where you may want to launch an activity with a custom intent,for example.
69+
*/
70+
@ExperimentalTestApi
71+
public fun <A : ComponentActivity> createAndroidComposeExtension(activityClass: Class<A>,
72+
scenarioSupplier: () -> ActivityScenario<A> = { ActivityScenario.launch(activityClass) }): AndroidComposeExtension<A> {
73+
return AndroidComposeExtension(scenarioSupplier)
6574
}
6675

6776
/**

0 commit comments

Comments
 (0)