-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from manununhez/feature/52-add-ui-tests
feature/52-add-ui-tests
- Loading branch information
Showing
43 changed files
with
717 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
app/src/androidTest/kotlin/com/manuelnunez/apps/navigation/CustomTestRunner.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.manuelnunez.apps.navigation | ||
|
||
import android.app.Application | ||
import android.content.Context | ||
import androidx.test.runner.AndroidJUnitRunner | ||
import dagger.hilt.android.testing.HiltTestApplication | ||
|
||
// A custom runner to set up the instrumented application class for tests. | ||
class CustomTestRunner : AndroidJUnitRunner() { | ||
|
||
override fun newApplication(cl: ClassLoader?, name: String?, context: Context?): Application { | ||
return super.newApplication(cl, HiltTestApplication::class.java.name, context) | ||
} | ||
} |
161 changes: 161 additions & 0 deletions
161
app/src/androidTest/kotlin/com/manuelnunez/apps/navigation/MainNavigationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package com.manuelnunez.apps.navigation | ||
|
||
import androidx.annotation.StringRes | ||
import androidx.compose.ui.test.assertIsNotSelected | ||
import androidx.compose.ui.test.assertIsSelected | ||
import androidx.compose.ui.test.junit4.AndroidComposeTestRule | ||
import androidx.compose.ui.test.junit4.createAndroidComposeRule | ||
import androidx.compose.ui.test.onAllNodesWithTag | ||
import androidx.compose.ui.test.onFirst | ||
import androidx.compose.ui.test.onNodeWithContentDescription | ||
import androidx.compose.ui.test.performClick | ||
import androidx.test.ext.junit.rules.ActivityScenarioRule | ||
import com.manuelnunez.apps.MainActivity | ||
import com.manuelnunez.apps.core.ui.component.CustomCardAutomation | ||
import dagger.hilt.android.testing.HiltAndroidRule | ||
import dagger.hilt.android.testing.HiltAndroidTest | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import kotlin.properties.ReadOnlyProperty | ||
import com.manuelnunez.apps.core.ui.R as CoreUIR | ||
import com.manuelnunez.apps.features.detail.ui.R as DetailR | ||
import com.manuelnunez.apps.features.favorites.ui.R as FavorR | ||
import com.manuelnunez.apps.features.home.ui.R as HomeR | ||
|
||
@HiltAndroidTest | ||
class MainNavigationTest { | ||
private fun AndroidComposeTestRule<*, *>.stringResource(@StringRes resId: Int) = | ||
ReadOnlyProperty<Any, String> { _, _ -> activity.getString(resId) } | ||
|
||
@get:Rule(order = 0) val hiltRule = HiltAndroidRule(this) | ||
|
||
@get:Rule(order = 1) val composeTestRule = createAndroidComposeRule<MainActivity>() | ||
|
||
private val navigateUp by composeTestRule.stringResource(resId = CoreUIR.string.button_back) | ||
private val home by composeTestRule.stringResource(resId = RootScreen.HOME.contentDescription) | ||
private val favorite by | ||
composeTestRule.stringResource(resId = RootScreen.FAVORITES.contentDescription) | ||
private val details by | ||
composeTestRule.stringResource(resId = DetailR.string.section_details_title) | ||
private val featureHomeTitle by composeTestRule.stringResource(HomeR.string.section_feature) | ||
private val featureFavoriteTitle by | ||
composeTestRule.stringResource(FavorR.string.section_favorites) | ||
private val featureDetailFavoriteButton by | ||
composeTestRule.stringResource(DetailR.string.button_favorite) | ||
|
||
@Before | ||
fun init() { | ||
hiltRule.inject() | ||
} | ||
|
||
@Test | ||
fun firstTabScreen_startDestination_isHomeNotArrowShown() { | ||
composeTestRule.apply { | ||
// Tab Home selected | ||
onNodeWithContentDescription(home).assertIsSelected() | ||
onNodeWithContentDescription(favorite).assertIsNotSelected() | ||
|
||
// HomeTitle | ||
onNodeWithContentDescription(featureHomeTitle).assertExists() | ||
|
||
// GIVEN the user is on any of the top level destinations, THEN the Up arrow is not shown. | ||
onNodeWithContentDescription(navigateUp).assertDoesNotExist() | ||
} | ||
} | ||
|
||
@Test | ||
fun secondTabScreen_navigateFromHome_isFavoriteNotArrowShown() { | ||
composeTestRule.apply { | ||
// Navigate to tab favorites | ||
onNodeWithContentDescription(favorite).performClick().assertIsSelected() | ||
|
||
// Favorite title | ||
onNodeWithContentDescription(featureFavoriteTitle).assertExists() | ||
|
||
// Home tab not selected | ||
onNodeWithContentDescription(home).assertIsNotSelected() | ||
|
||
// GIVEN the user is on any of the top level destinations, THEN the Up arrow is not shown. | ||
onNodeWithContentDescription(navigateUp).assertDoesNotExist() | ||
} | ||
} | ||
|
||
@Test | ||
fun detailScreen_navigateFromHome_onClickAnyPhoto() { | ||
composeTestRule.apply { | ||
// Home tab selected | ||
onNodeWithContentDescription(home).assertIsSelected() | ||
|
||
// Click on any photo and navigate to details | ||
onAllNodesWithTag(CustomCardAutomation.IMAGE_CARD_PREFIX).onFirst().performClick() | ||
|
||
// We are in Details Screen | ||
onNodeWithContentDescription(details).performClick() | ||
|
||
// GIVEN the user is not on any top level destinations, THEN the Up arrow is shown. | ||
onNodeWithContentDescription(navigateUp).assertExists().performClick() | ||
|
||
// We are in HomeScreen again | ||
onNodeWithContentDescription(featureHomeTitle).assertExists() | ||
} | ||
} | ||
|
||
@Test | ||
fun detailScreen_navigateFromFavorite_onClickAnyPhoto() { | ||
composeTestRule.apply { | ||
// Home tab selected | ||
onNodeWithContentDescription(home).assertIsSelected() | ||
addItemToFavorites() | ||
|
||
// Move to tab favorites | ||
onNodeWithContentDescription(favorite).performClick() | ||
onNodeWithContentDescription(featureFavoriteTitle).assertExists() | ||
|
||
// Click on any photo and navigate to details | ||
onAllNodesWithTag(CustomCardAutomation.IMAGE_CARD_PREFIX).onFirst().performClick() | ||
|
||
// We are in Details Screen | ||
onNodeWithContentDescription(details).performClick() | ||
|
||
// GIVEN the user is not on any top level destinations, THEN the Up arrow is shown. | ||
onNodeWithContentDescription(navigateUp).assertExists().performClick() | ||
|
||
// We are in HomeScreen again | ||
onNodeWithContentDescription(featureFavoriteTitle).assertExists() | ||
} | ||
} | ||
|
||
@Test | ||
fun navigationBar_reselectTab_keepsState() { | ||
composeTestRule.apply { | ||
// Home tab selected | ||
onNodeWithContentDescription(home).assertIsSelected() | ||
|
||
// Click on any photo and navigate to details | ||
onAllNodesWithTag(CustomCardAutomation.IMAGE_CARD_PREFIX).onFirst().performClick() | ||
|
||
// We are in Details Screen | ||
onNodeWithContentDescription(details).performClick() | ||
|
||
// Move to tab favorites | ||
onNodeWithContentDescription(favorite).performClick() | ||
|
||
// Move back to home | ||
onNodeWithContentDescription(home).performClick() | ||
|
||
// We are stills in Details Screen | ||
onNodeWithContentDescription(details).performClick() | ||
} | ||
} | ||
|
||
private fun AndroidComposeTestRule<ActivityScenarioRule<MainActivity>, MainActivity> | ||
.addItemToFavorites() { | ||
// Click on any photo and navigate to details | ||
onAllNodesWithTag(CustomCardAutomation.IMAGE_CARD_PREFIX).onFirst().performClick() | ||
// Add to favorites | ||
onNodeWithContentDescription(featureDetailFavoriteButton).performClick() | ||
// Back | ||
onNodeWithContentDescription(navigateUp).assertExists().performClick() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
<resources> | ||
<string name="app_name">Purrfect Pics</string> | ||
<string name="home_destination_title">Home</string> | ||
<string name="home_destination_content_description">HomeTabItem</string> | ||
<string name="favorites_destination_title">Favorites</string> | ||
<string name="favorites_destination_content_description">FavoritesTabItem</string> | ||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
core/data/src/main/kotlin/com/manuelnunez/apps/core/data/di/TestDataModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.manuelnunez.apps.core.data.di | ||
|
||
import com.manuelnunez.apps.core.data.repository.fake.FakeDetailRepository | ||
import com.manuelnunez.apps.core.data.repository.fake.FakeFavoritesRepository | ||
import com.manuelnunez.apps.core.data.repository.fake.FakeHomeRepository | ||
import com.manuelnunez.apps.core.data.repository.fake.FakeSeeMoreRepository | ||
import com.manuelnunez.apps.features.detail.domain.repository.DetailRepository | ||
import com.manuelnunez.apps.features.favorites.domain.repository.FavoritesRepository | ||
import com.manuelnunez.apps.features.home.domain.repository.HomeRepository | ||
import com.manuelnunez.apps.features.seemore.domain.repository.SeeMoreRepository | ||
import dagger.Binds | ||
import dagger.Module | ||
import dagger.hilt.components.SingletonComponent | ||
import dagger.hilt.testing.TestInstallIn | ||
import javax.inject.Singleton | ||
|
||
/** | ||
* This Test Module is used as replace of the DataModule when testing is done. In case of the | ||
* MainNavigationTest, this avoid to call network and emit fake data instead. | ||
*/ | ||
@Module | ||
@TestInstallIn( | ||
components = [SingletonComponent::class], | ||
replaces = [DataModule::class], | ||
) | ||
internal interface TestDataModule { | ||
@Singleton | ||
@Binds | ||
abstract fun bindsHomeRepository(homeRepository: FakeHomeRepository): HomeRepository | ||
|
||
@Singleton | ||
@Binds | ||
abstract fun bindsSeeMoreRepository(seeMoreRepository: FakeSeeMoreRepository): SeeMoreRepository | ||
|
||
@Singleton | ||
@Binds | ||
abstract fun bindsDetailRepository(detailRepository: FakeDetailRepository): DetailRepository | ||
|
||
@Singleton | ||
@Binds | ||
abstract fun bindsFavoritesRepository( | ||
seeMoreRepository: FakeFavoritesRepository | ||
): FavoritesRepository | ||
} |
21 changes: 21 additions & 0 deletions
21
...ta/src/main/kotlin/com/manuelnunez/apps/core/data/repository/fake/FakeDetailRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.manuelnunez.apps.core.data.repository.fake | ||
|
||
import com.manuelnunez.apps.core.domain.model.Item | ||
import com.manuelnunez.apps.features.detail.domain.repository.DetailRepository | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flowOf | ||
import javax.inject.Inject | ||
|
||
class FakeDetailRepository @Inject constructor() : DetailRepository { | ||
private val items = mutableListOf<Item>() | ||
|
||
override suspend fun saveFavoriteItem(favoriteItem: Item) { | ||
items.add(favoriteItem) | ||
} | ||
|
||
override suspend fun removeFavoriteItem(favoriteItem: Item) { | ||
items.remove(favoriteItem) | ||
} | ||
|
||
override fun isItemFavorite(itemPhotoId: String): Flow<Boolean> = flowOf(true) | ||
} |
12 changes: 12 additions & 0 deletions
12
...src/main/kotlin/com/manuelnunez/apps/core/data/repository/fake/FakeFavoritesRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.manuelnunez.apps.core.data.repository.fake | ||
|
||
import com.manuelnunez.apps.core.domain.model.Item | ||
import com.manuelnunez.apps.core.domain.utils.mockItems | ||
import com.manuelnunez.apps.features.favorites.domain.repository.FavoritesRepository | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flowOf | ||
import javax.inject.Inject | ||
|
||
class FakeFavoritesRepository @Inject constructor() : FavoritesRepository { | ||
override fun getAllFavorites(): Flow<List<Item>> = flowOf(mockItems) | ||
} |
Oops, something went wrong.