-
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.
- espresso tests for main screen
- Loading branch information
Showing
30 changed files
with
403 additions
and
178 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
53 changes: 53 additions & 0 deletions
53
app/src/androidTest/java/pl/marianjureczko/poszukiwacz/AbstractUITest.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,53 @@ | ||
package pl.marianjureczko.poszukiwacz | ||
|
||
import android.Manifest | ||
import androidx.compose.ui.test.SemanticsNodeInteraction | ||
import androidx.compose.ui.test.assertIsDisplayed | ||
import androidx.compose.ui.test.junit4.createAndroidComposeRule | ||
import androidx.compose.ui.test.onNodeWithContentDescription | ||
import androidx.compose.ui.test.onNodeWithTag | ||
import androidx.compose.ui.test.performClick | ||
import androidx.test.platform.app.InstrumentationRegistry | ||
import androidx.test.rule.GrantPermissionRule | ||
import dagger.hilt.android.testing.HiltAndroidRule | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import pl.marianjureczko.poszukiwacz.activity.main.MainActivity | ||
|
||
abstract class AbstractUITest { | ||
@get:Rule(order = 0) | ||
val hiltRule = HiltAndroidRule(this) | ||
|
||
@get:Rule(order = 1) | ||
val composeRule = createAndroidComposeRule<MainActivity>() | ||
|
||
@get:Rule(order = 3) | ||
val permissionRule: GrantPermissionRule = GrantPermissionRule.grant( | ||
Manifest.permission.ACCESS_FINE_LOCATION, | ||
Manifest.permission.CAMERA, | ||
Manifest.permission.RECORD_AUDIO | ||
) | ||
val context = InstrumentationRegistry.getInstrumentation().targetContext | ||
|
||
@Before | ||
fun init() { | ||
hiltRule.inject() | ||
} | ||
|
||
fun performClick(contentDescription: String) { | ||
composeRule | ||
.onNodeWithContentDescription(contentDescription) | ||
.assertExists() | ||
.performClick() | ||
composeRule.waitForIdle() | ||
} | ||
|
||
fun getNode(contentDescription: String): SemanticsNodeInteraction { | ||
return composeRule.onNodeWithContentDescription(contentDescription) | ||
} | ||
|
||
fun assertImageIsDisplayed(drawableId: Int) { | ||
composeRule.onNodeWithTag(drawableId.toString()) | ||
.assertIsDisplayed() | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
app/src/androidTest/java/pl/marianjureczko/poszukiwacz/CustomArrangersDetector.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,35 @@ | ||
package pl.marianjureczko.poszukiwacz | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry | ||
import dalvik.system.DexFile | ||
import org.junit.Test | ||
|
||
|
||
class CustomArrangersDetector { | ||
|
||
@Test | ||
fun findArrangerConstructors() { | ||
val testClasses = getTestApkClasses() | ||
val customArrangers = convertToComaSeparatedCustomArrangers(testClasses) | ||
println("### $customArrangers ###") | ||
} | ||
|
||
private fun convertToComaSeparatedCustomArrangers(classes: List<String>): String { | ||
return classes | ||
.filter { c -> c.endsWith("Arranger") && !c.endsWith(".Arranger") && !c.endsWith(".CustomArranger") } | ||
.joinToString(separator = ",") | ||
} | ||
|
||
private fun getTestApkClasses(): List<String> { | ||
try { | ||
val testApkPath: String = InstrumentationRegistry.getInstrumentation() | ||
.getContext() | ||
.getApplicationInfo() | ||
.sourceDir | ||
val dexFile = DexFile(testApkPath) | ||
return dexFile.entries().asSequence().toList() | ||
} catch (e: Exception) { | ||
throw RuntimeException("Failed to retrieve test APK classes", e) | ||
} | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
107 changes: 107 additions & 0 deletions
107
app/src/androidTestClassic/java/pl/marianjureczko/poszukiwacz/MainScreenTest.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,107 @@ | ||
package pl.marianjureczko.poszukiwacz | ||
|
||
import androidx.compose.ui.test.SemanticsNodeInteraction | ||
import androidx.compose.ui.test.assertTextEquals | ||
import androidx.compose.ui.test.performClick | ||
import androidx.compose.ui.test.performTextInput | ||
import dagger.hilt.android.testing.HiltAndroidTest | ||
import dagger.hilt.android.testing.UninstallModules | ||
import org.junit.After | ||
import org.junit.Test | ||
import pl.marianjureczko.poszukiwacz.model.Route | ||
import pl.marianjureczko.poszukiwacz.model.RouteArranger | ||
import pl.marianjureczko.poszukiwacz.screen.main.CONFIRM_ROUTE_NAME_BUTTON | ||
import pl.marianjureczko.poszukiwacz.screen.main.DELETE_ROUTE_BUTTON | ||
import pl.marianjureczko.poszukiwacz.screen.main.EDIT_ROUTE_BUTTON | ||
import pl.marianjureczko.poszukiwacz.screen.main.ENTER_ROUTE_NAME_TITLE | ||
import pl.marianjureczko.poszukiwacz.screen.main.NEW_ROUTE_BUTTON | ||
import pl.marianjureczko.poszukiwacz.screen.main.ROUTE_NAME_TEXT_EDIT | ||
import pl.marianjureczko.poszukiwacz.screen.treasureseditor.TREASURE_ITEM_ROW | ||
import pl.marianjureczko.poszukiwacz.screen.treasureseditor.TREASURE_ITEM_TEXT | ||
import pl.marianjureczko.poszukiwacz.shared.di.PortsModule | ||
import pl.marianjureczko.poszukiwacz.ui.components.TOPBAR_SCREEN_TITLE | ||
import pl.marianjureczko.poszukiwacz.ui.components.YES_BUTTON | ||
import java.time.LocalDateTime | ||
import java.time.ZoneOffset | ||
|
||
|
||
@UninstallModules(PortsModule::class) | ||
@HiltAndroidTest | ||
class MainScreenTest : UiTest() { | ||
|
||
var route: Route = getRouteFromStorage() | ||
|
||
@After | ||
fun restoreRoute() { | ||
if(TestPortsModule.storage.routes.isEmpty()) { | ||
val newRoute = RouteArranger.routeWithoutTipFiles() | ||
TestPortsModule.storage.routes[newRoute.name] = newRoute | ||
} | ||
route = getRouteFromStorage() | ||
} | ||
|
||
@Test | ||
fun shouldGoToTreasureEditorScreen_whenCreatingNewRoute() { | ||
//given | ||
val button: SemanticsNodeInteraction = getNode(NEW_ROUTE_BUTTON) | ||
|
||
//then | ||
button.assertTextEquals(context.getString(R.string.new_route_button)) | ||
button.performClick() | ||
composeRule.waitForIdle() | ||
|
||
val dialogTitle = getNode(ENTER_ROUTE_NAME_TITLE) | ||
dialogTitle.assertTextEquals(context.getString(R.string.route_name_prompt)) | ||
|
||
val nameInput = getNode(ROUTE_NAME_TEXT_EDIT) | ||
val routeName = "TEST_" + LocalDateTime.now().toEpochSecond(ZoneOffset.UTC) | ||
nameInput.assertExists() | ||
.performTextInput(routeName) | ||
composeRule.waitForIdle() | ||
|
||
performClick(CONFIRM_ROUTE_NAME_BUTTON) | ||
|
||
getNode(TOPBAR_SCREEN_TITLE) | ||
.assertTextEquals("${context.getString(R.string.route)} $routeName") | ||
getNode(TREASURE_ITEM_ROW) | ||
.assertDoesNotExist() | ||
} | ||
|
||
@Test | ||
fun shouldGoToTreasureEditorScreen_whenClickOnEditButton() { | ||
//given | ||
// | ||
// composeRule.setContent { | ||
// // Your composable function that renders the screen | ||
// MainScreen { }() | ||
// } | ||
composeRule.waitForIdle() | ||
|
||
//when | ||
performClick(EDIT_ROUTE_BUTTON + route.name) | ||
|
||
//then | ||
val screenTitle: SemanticsNodeInteraction = getNode(TOPBAR_SCREEN_TITLE) | ||
screenTitle.assertTextEquals("${context.getString(R.string.route)} ${route.name}") | ||
route.treasures.forEach { treasure -> | ||
val treasureText: SemanticsNodeInteraction = getNode("$TREASURE_ITEM_TEXT ${treasure.id}") | ||
treasureText.assertTextEquals(treasure.prettyName()) | ||
} | ||
} | ||
|
||
@Test | ||
fun shouldRemoveRoute_whenClickingRemoveButton() { | ||
//given | ||
composeRule.waitForIdle() | ||
|
||
//when | ||
performClick(DELETE_ROUTE_BUTTON + route.name) | ||
performClick(YES_BUTTON) | ||
|
||
//then | ||
getNode(EDIT_ROUTE_BUTTON + route.name) | ||
.assertDoesNotExist() | ||
} | ||
|
||
private fun getRouteFromStorage() = TestPortsModule.storage.routes.values.first() | ||
} |
46 changes: 46 additions & 0 deletions
46
app/src/androidTestClassic/java/pl/marianjureczko/poszukiwacz/TestPortsModule.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,46 @@ | ||
package pl.marianjureczko.poszukiwacz | ||
|
||
import com.google.android.gms.location.FusedLocationProviderClient | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import org.mockito.Mockito.mock | ||
import pl.marianjureczko.poszukiwacz.shared.port.CameraPort | ||
import pl.marianjureczko.poszukiwacz.shared.port.LocationPort | ||
import pl.marianjureczko.poszukiwacz.shared.port.StorageHelper | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object TestPortsModule { | ||
|
||
val storage = TestStoragePort() | ||
val locationClient = mock<FusedLocationProviderClient>() | ||
val location = mock<LocationPort>() | ||
val camera = mock<CameraPort>() | ||
|
||
@Singleton | ||
@Provides | ||
fun fusedLocationClient(): FusedLocationProviderClient { | ||
return locationClient | ||
} | ||
|
||
@Singleton | ||
@Provides | ||
fun locationPort(): LocationPort { | ||
return location | ||
} | ||
|
||
@Singleton | ||
@Provides | ||
fun photoPort(): CameraPort { | ||
return camera | ||
} | ||
|
||
@Singleton | ||
@Provides | ||
fun storageHelper(): StorageHelper { | ||
return storage | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
app/src/androidTestClassic/java/pl/marianjureczko/poszukiwacz/UiTest.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,5 @@ | ||
package pl.marianjureczko.poszukiwacz | ||
|
||
open class UiTest: AbstractUITest() { | ||
|
||
} |
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
47 changes: 1 addition & 46 deletions
47
app/src/androidTestKalinowiceCustomDebug/java/pl/marianjureczko/poszukiwacz/UiTest.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
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
Oops, something went wrong.