Skip to content

Commit

Permalink
Merge pull request #1 from agstack/feature/tests
Browse files Browse the repository at this point in the history
Feature/tests
  • Loading branch information
lucienshema authored Nov 26, 2024
2 parents 8c5ea3f + a888883 commit 8193fea
Show file tree
Hide file tree
Showing 121 changed files with 8,639 additions and 3,958 deletions.
84 changes: 84 additions & 0 deletions .idea/androidTestResultsUserPreferences.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 45 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ android {
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = '1.8'
jvmTarget = '11'
}
buildFeatures {
compose true
Expand Down Expand Up @@ -138,7 +138,10 @@ dependencies {
implementation 'com.google.firebase:firebase-crashlytics-buildtools:3.0.2'
implementation 'androidx.test:monitor:1.7.2'
implementation 'androidx.test.ext:junit-ktx:1.2.1'
implementation 'androidx.compose.ui:ui-test-junit4-android:1.7.5'
implementation 'com.google.android.gms:play-services-phenotype:17.0.0'
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.testng:testng:6.9.6'

// Paging
def paging_version = "3.3.4"
Expand Down Expand Up @@ -199,4 +202,43 @@ dependencies {
implementation "androidx.navigation:navigation-compose:2.4.0-alpha04"

implementation "org.jetbrains.kotlin:kotlin-stdlib:2.0.20-RC"



// Testing Dependencies

implementation 'androidx.navigation:navigation-testing:2.4.0-alpha04'
implementation 'androidx.test.ext:junit-ktx:1.2.1'

testImplementation "androidx.test:core:1.6.1"
testImplementation "org.mockito:mockito-core:5.7.0"
testImplementation "org.mockito.kotlin:mockito-kotlin:5.3.1"
testImplementation "io.mockk:mockk:1.13.7"
testImplementation "org.robolectric:robolectric:4.10.3"
testImplementation 'com.google.dagger:hilt-android-testing:2.48'
testImplementation 'androidx.compose.ui:ui-test-junit4-android:1.7.5'
testImplementation 'androidx.arch.core:core-testing:2.2.0'
testImplementation "com.google.truth:truth:1.1.3"
testImplementation "io.mockk:mockk:1.13.7"
testImplementation "io.mockk:mockk-android:1.13.5"
testImplementation "org.robolectric:robolectric:4.9"


androidTestImplementation "androidx.work:work-testing:2.7.1"
androidTestImplementation "com.google.truth:truth:1.1.3"
androidTestImplementation "androidx.work:work-testing:2.7.1"
androidTestImplementation "androidx.compose.ui:ui-test-junit4:1.7.5"
androidTestImplementation "androidx.arch.core:core-testing:2.2.0"
androidTestImplementation "io.mockk:mockk-android:1.13.5"
androidTestImplementation 'com.google.dagger:hilt-android-testing:2.48'

androidTestImplementation 'androidx.test.ext:junit:1.2.1'
// androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:1.7.5"

debugImplementation("androidx.compose.ui:ui-test-manifest:1.7.5")
//
// implementation("group:artifact") {
// exclude group: "org.junit.jupiter", module: "junit-jupiter-engine"
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.technoserve.farmcollector.ui.components

import org.junit.Assert.*

import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

class FakeMapViewModel : ViewModel() {
var coordinatesCleared = false

fun clearCoordinates() {
coordinatesCleared = true
}
}

@RunWith(AndroidJUnit4::class)
class KeepPolygonDialogIntegrationTest {

@get:Rule
val composeTestRule = createComposeRule()

@Test
fun testKeepPolygonDialogClearsCoordinatesAndCapturesNew() {
val fakeViewModel = FakeMapViewModel()

composeTestRule.setContent {
KeepPolygonDialog(
onDismiss = {},
onKeepExisting = {},
onCaptureNew = {
fakeViewModel.clearCoordinates()
}
)
}

// Click the "Capture New" button
composeTestRule.onNodeWithText("Capture New").performClick()

// Assert that coordinates were cleared
assert(fakeViewModel.coordinatesCleared)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package org.technoserve.farmcollector.ui.composes

import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.test.ext.junit.runners.AndroidJUnit4
import dagger.hilt.android.testing.HiltAndroidTest
import org.junit.Assert.*
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@HiltAndroidTest
@RunWith(AndroidJUnit4::class)
class AreaDialogTest {

@get:Rule
val composeTestRule = createComposeRule()

private val calculatedArea = 123.456789
private val enteredArea = 100.0
private val threshold = enteredArea * 0.30

@Test
fun dialogDisplaysCorrectTextAndButtons() {
composeTestRule.setContent {
AreaDialog(
showDialog = true,
onDismiss = {},
onConfirm = {},
calculatedArea = calculatedArea,
enteredArea = enteredArea
)
}

// Check dialog title
composeTestRule.onNodeWithText("Choose Area") // Replace with localized string if using resources
.assertExists()

// Check warning message visibility
val difference = Math.abs(calculatedArea - enteredArea)
if (difference > threshold) {
composeTestRule.onNodeWithText("Warning: Difference is $difference") // Replace with localized string
.assertExists()
} else {
composeTestRule.onNodeWithText("Warning: Difference is $difference") // Replace with localized string
.assertDoesNotExist()
}

// Verify buttons
composeTestRule.onNodeWithText("Cancel") // Replace with localized string
.assertExists()

composeTestRule.onNodeWithText("Calculated Area: 123.456789") // Replace with localized string
.assertExists()

composeTestRule.onNodeWithText("Entered Area: 100.00") // Replace with localized string
.assertExists()
}

@Test
fun confirmButtonCallsOnConfirmWithCalculatedAreaOption() {
var confirmedOption: String? = null

composeTestRule.setContent {
AreaDialog(
showDialog = true,
onDismiss = {},
onConfirm = { confirmedOption = it },
calculatedArea = calculatedArea,
enteredArea = enteredArea
)
}

// Click the calculated area button
composeTestRule.onNodeWithText("Calculated Area: 123.456789") // Replace with localized string
.performClick()

// Verify the onConfirm callback was called with the correct option
assertEquals(CALCULATED_AREA_OPTION, confirmedOption)
}

@Test
fun dismissButtonCallsOnDismiss() {
var dismissed = false

composeTestRule.setContent {
AreaDialog(
showDialog = true,
onDismiss = { dismissed = true },
onConfirm = {},
calculatedArea = calculatedArea,
enteredArea = enteredArea
)
}

// Click the dismiss button
composeTestRule.onNodeWithText("Cancel") // Replace with localized string
.performClick()

// Verify the onDismiss callback was called
assertTrue(dismissed)
}
}
Loading

0 comments on commit 8193fea

Please sign in to comment.