Skip to content

Commit

Permalink
#17 in progress - first espresso tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mjureczko committed Sep 20, 2024
1 parent b3400cf commit f432a18
Show file tree
Hide file tree
Showing 18 changed files with 120 additions and 13 deletions.
35 changes: 29 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ android {
applicationId "pl.marianjureczko.poszukiwacz"
minSdkVersion 23
targetSdkVersion 34
versionCode 7
versionName "0.7"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
versionCode 8
versionName "0.8"
testInstrumentationRunner "pl.marianjureczko.poszukiwacz.CustomTestRunner"
manifestPlaceholders = [facebookToken: FACEBOOK_TOKEN]
javaCompileOptions {
annotationProcessorOptions {
// Ensure these options are set for androidTest as well
arguments += ["dagger.hilt.android.internal.disableAndroidSuperclassValidation": "true"]
}
}
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
Expand Down Expand Up @@ -44,6 +50,10 @@ android {
resValue "string", "photo_ad", "ca-app-pub-3940256099942544/6300978111"
}
}
kapt {
correctErrorTypes = true
showProcessorStats = true
}
flavorDimensions "assets", "mode"
productFlavors {
classic {
Expand Down Expand Up @@ -73,7 +83,13 @@ android {
setIgnore(true)
}
}

sourceSets {
androidTest {
manifest.srcFile 'src/androidTest/AndroidManifest.xml'
java.srcDirs = ['src/androidTest/java']
res.srcDirs = ['src/androidTest/res']
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
Expand Down Expand Up @@ -125,7 +141,8 @@ dependencies {
implementation "androidx.navigation:navigation-compose:2.4.2"
implementation "androidx.window:window-java:1.1.0"
implementation "com.google.dagger:hilt-android:${hilt_version}"
kapt "com.google.dagger:hilt-compiler:${hilt_version}"
implementation 'com.google.ar:core:1.45.0'
kapt "com.google.dagger:hilt-android-compiler:${hilt_version}"
implementation 'androidx.hilt:hilt-navigation-compose:1.1.0-alpha01'
implementation 'com.google.accompanist:accompanist-permissions:0.30.1'
implementation 'commons-io:commons-io:2.11.0'
Expand All @@ -152,11 +169,17 @@ dependencies {

androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test:core:1.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation "androidx.test.espresso:espresso-intents:3.5.1"
androidTestImplementation group: 'org.mockito', name: 'mockito-core', version: '4.4.0'
androidTestImplementation 'org.mockito:mockito-inline:2.13.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
androidTestImplementation 'androidx.test:rules:1.5.0'
androidTestImplementation 'androidx.test:runner:1.5.2'
androidTestImplementation "com.google.dagger:hilt-android-testing:${hilt_version}"
kaptAndroidTest "com.google.dagger:hilt-android-compiler:${hilt_version}"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"
}

configurations {
Expand Down
Binary file added app/src/androidTest/assets/map_footer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/androidTest/assets/summary.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package pl.marianjureczko.poszukiwacz

import android.app.Application
import android.content.Context
import androidx.test.runner.AndroidJUnitRunner
import dagger.hilt.android.testing.HiltTestApplication

class CustomTestRunner : AndroidJUnitRunner() {

override fun newApplication(cl: ClassLoader?, name: String?, context: Context?): Application {
return super.newApplication(cl, HiltTestApplication::class.java.name, context)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package pl.marianjureczko.poszukiwacz

import androidx.compose.ui.test.assertCountEquals
import androidx.compose.ui.test.assertTextContains
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onAllNodesWithContentDescription
import androidx.test.rule.GrantPermissionRule
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 pl.marianjureczko.poszukiwacz.activity.main.MainActivity

//@UninstallModules(SingletonModule::class)
@HiltAndroidTest
//@Config(application = HiltTestApplication::class)
class MyInstrumentedTest {


@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(
android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.CAMERA
)

@Before
fun init() {
hiltRule.inject()
}

@Test
fun test() {
//given
// composeRule.waitForIdle()
val text = composeRule.onAllNodesWithContentDescription("TEST", false, false, true)
.assertCountEquals(1)
.get(0)

//then
text.assertTextContains("WARNING", substring = true)


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
Expand Down Expand Up @@ -52,7 +54,8 @@ fun MainScreenBody(goToSearching: GoToSearching) {
Text(
text = state.messages[state.messageIndex].text,
color = colorResource(R.color.colorPrimaryVariant),
textAlign = TextAlign.Justify
textAlign = TextAlign.Justify,
modifier = Modifier.semantics { contentDescription = "TEST" }
)
Row(
modifier = Modifier
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pl.marianjureczko.poszukiwacz">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ val FACEBOOK_ROUTE = "$FACEBOOK_PATH"
/**
* Routes creation and selection activity
*/
//TODO t: check https://developer.android.com/build/build-variants and Product Flavours
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
private val TAG = javaClass.simpleName
Expand Down Expand Up @@ -98,7 +97,7 @@ class MainActivity : ComponentActivity() {
}

@Composable
private fun ComposeRoot(onClickGuide: GoToGuide) {
fun ComposeRoot(onClickGuide: GoToGuide) {
val navController = rememberNavController()
val goToFacebook: GoToFacebook = FacebookHelper.createFacebookCallback(navController)
val goToCommemorative: GoToCommemorative = { treasureId -> navController.navigate("$COMMEMORATIVE_PATH/$treasureId") }
Expand All @@ -112,7 +111,6 @@ private fun ComposeRoot(onClickGuide: GoToGuide) {
composable(
route = SEARCHING_ROUTE,
arguments = listOf(navArgument(PARAMETER_ROUTE_NAME) { type = NavType.StringType }),
// deepLinks = listOf(navDeepLink { uriPattern = "www.restaurantsapp.details.com/{restaurant_id}" }),
) {
SearchingScreen(
navController = navController,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,24 @@ import androidx.compose.ui.viewinterop.AndroidView
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdSize
import com.google.android.gms.ads.AdView
import com.google.android.gms.ads.MobileAds
import com.google.android.gms.ads.RequestConfiguration
import pl.marianjureczko.poszukiwacz.R

@Composable
fun AdvertBanner(){
AndroidView(
modifier = Modifier.fillMaxWidth(),
factory = { context ->
MobileAds.setRequestConfiguration(
MobileAds.getRequestConfiguration()
.toBuilder()
.setTagForChildDirectedTreatment(RequestConfiguration.TAG_FOR_CHILD_DIRECTED_TREATMENT_TRUE)
.setTagForUnderAgeOfConsent(RequestConfiguration.TAG_FOR_UNDER_AGE_OF_CONSENT_TRUE)
.setMaxAdContentRating(RequestConfiguration.MAX_AD_CONTENT_RATING_G)
.build()
)

AdView(context).apply {
setAdSize(AdSize.BANNER)
adUnitId = context.resources.getString(R.string.main_ad)
Expand Down
10 changes: 9 additions & 1 deletion docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,20 @@ In order to refer to one of the variant, for instance to execute unit tests, one
```
$ ./gradlew testCustomDebugUnitTest
```
The above line will execute tests from the "custom" flavors as well as from the "unflavoured" source set, i.e. src/test.


# Source sets

For each flavor there has been created a source set with the same name as the flavor.
There are also dedicated test source sets with the `Test` postfix, i.e. `<flavor>Test`
There are also dedicated test source sets with the `test` prefix, i.e. `test<flavor>`
Note that the source sets contains not only source code but also resources and assets.
The pattern is not followed for the instrumented UI tests.
For those tests, the source set directory name has and `androidTest` prefix followed by all flavors required to have a vaild build variant, e.g. `androidTestKalinowiceCustomDebug`.
In order to execute such tests you need and a running emulator, and then execute:
```
$ ./gradlew connectedKalinowiceCustomDebugAndroidTest
```

# Releasing

Expand Down

0 comments on commit f432a18

Please sign in to comment.