Skip to content

Instantly share code, notes, and snippets.

View ferPrieto's full-sized avatar
💪
I'm just driven

Fernando Prieto Moyano ferPrieto

💪
I'm just driven
View GitHub Profile
internal fun launchesScreenRobot(
composeTestRule: AndroidComposeTestRule<ActivityScenarioRule<EntryPointActivity>, EntryPointActivity>,
func: LaunchesScreenRobot.() -> Unit
) = LaunchesScreenRobot(composeTestRule).also { func }
internal open class LaunchesScreenRobot constructor(
private val composeTestRule: AndroidComposeTestRule<ActivityScenarioRule<EntryPointActivity>, EntryPointActivity>
) {
// Robot definitions and functions
}
@ferPrieto
ferPrieto / LaunchesScreenKtTest.kt
Created January 13, 2022 21:19
For this demonstration, the dispatcher initialisation and content setup (composeTestRule.setContent) was omitted for simplicity
@Test
fun elementsVisibilityAfterOpeningTheScreen() {
launchesScreenRobot(composeTestRule) {
clickOnLaunchesTab()
initialElementsShowed()
}
}
@Test
fun elementsVisibilityAfterOpeningTheScreen() {
@ferPrieto
ferPrieto / LaunchesScreenTest.kt
Last active January 5, 2022 00:20
This is a simplified demonstration of a UI Test in Isolation where only one of the tabs is initialised with the desired use case
@Test
@InternalCoroutinesApi
fun elementsVisibilityAfterTwoItemsRetrieved() {
composeTestRule.apply {
setContent {
SpaceXTheme {
LaunchesScreen(
state = LaunchesContract.State(
listOf(
LaunchUiModel(
@ferPrieto
ferPrieto / LaunchesScreenKtTest.kt
Last active January 5, 2022 00:18
This is the simplified test using MockWebServer
@ExperimentalMaterialApi
@RunWith(AndroidJUnit4::class)
@HiltAndroidTest
class LaunchesScreenKtTest : BaseScreenTest() {
@Test
@InternalCoroutinesApi
fun visibleItemsCountAfterOpeningTheScreen() {
mockWebServer.dispatcher = SuccessDispatcher()
setMainContent()
@ferPrieto
ferPrieto / BaseTest.kt
Created January 4, 2022 17:11
These are the minimum initialisation needed in order to use MockWebServer in your tests
val mockWebServer by lazy { MockWebServer() }
@Before
fun setUp() {
mockWebServer.start(BuildConfig.PORT)
}
@After
fun teardown() {
mockWebServer.shutdown()
@ferPrieto
ferPrieto / NetworkModule.kt
Created January 4, 2022 16:53
This is the simplified version of the NetworkModule, which will be replaced by the FakeNetworkModule in the Tests
@InstallIn(SingletonComponent::class)
@Module
open class NetworkModule {
open fun getBaseUrl () ="https://api.spacexdata.com/v3/"
@Provides
@BaseUrl
fun provideBaseUrl() = getBaseUrl ()
@ferPrieto
ferPrieto / FakeNetworkModule.kt
Created January 4, 2022 16:50
This is the FakeNetworkModule class used for testing purpose that replaces the original one (NetworkModule) defined in production
@Module
@TestInstallIn(
components = [SingletonComponent::class],
replaces = [NetworkModule::class]
)
class FakeNetworkModule : NetworkModule() {
override fun getBaseUrl() = "http://127.0.0.1:${BuildConfig.PORT}"
}
class MockTestRunner : AndroidJUnitRunner() {
override fun onCreate(arguments: Bundle?) {
StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder().permitAll().build())
super.onCreate(arguments)
}
override fun newApplication(
cl: ClassLoader?,
className: String?,
context: Context?
android {
defaultConfig {
applicationId = "prieto.fernando.sample"
testInstrumentationRunner = "prieto.fernando.sample.webmock.MockTestRunner"
}
buildFeatures {
compose = true
viewBinding = true
}
// rest of configuration
@ferPrieto
ferPrieto / MainScreenTest.kt
Created January 4, 2022 12:13
This is an example of how to identify a node by Semantics: ContentDescription
private val dialogFilterButton by lazy {
composeTestRule.onNodeWithContentDescription("Filter Button")
}
@Test
fun elementsVisibilityAfterOpeningTheMainScreen() {
setMainContent()
dialogFilterButton.assertIsDisplayed()
}