Skip to content

Commit

Permalink
Merge pull request #2 from omer358/dev
Browse files Browse the repository at this point in the history
write Instrumentation Tests for navigation and database
  • Loading branch information
omer358 authored Jun 13, 2024
2 parents b87f692 + a406b5f commit 5e6991f
Show file tree
Hide file tree
Showing 19 changed files with 934 additions and 124 deletions.
36 changes: 36 additions & 0 deletions .idea/androidTestResultsUserPreferences.xml

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

10 changes: 8 additions & 2 deletions .idea/deploymentTargetSelector.xml

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

2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

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

26 changes: 22 additions & 4 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.jetbrains.kotlin.android)
alias(libs.plugins.compose.compiler)

id("com.google.devtools.ksp")

}

android {
Expand Down Expand Up @@ -40,7 +44,7 @@ android {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.1"
kotlinCompilerExtensionVersion = "1.5.14"
}
packaging {
resources {
Expand All @@ -50,9 +54,6 @@ android {
}

dependencies {
val lifecycle_version = "2.8.1"
val arch_version = "2.2.0"
val room_version = "2.6.1"

implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
Expand All @@ -67,19 +68,36 @@ dependencies {
androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.ui.test.junit4)
testImplementation(libs.androidx.core.testing)
debugImplementation(libs.androidx.ui.tooling)
debugImplementation(libs.androidx.ui.test.manifest)

androidTestImplementation(libs.truth)
androidTestImplementation(libs.core.testing)


implementation(libs.androidx.room.runtime)
annotationProcessor(libs.androidx.room.compiler)
implementation(libs.androidx.room.ktx)
testImplementation(libs.androidx.room.room.testing)
ksp(libs.androidx.room.compiler)



// Google font
implementation(libs.androidx.ui.text.google.fonts)

implementation(libs.androidx.junit.ktx)

// ViewModel utilities for Compose
implementation(libs.androidx.lifecycle.viewmodel.compose)

// Splash screen
implementation(libs.androidx.core.splashscreen)

// Navigation Compose
implementation(libs.androidx.navigation.compose)
androidTestImplementation(libs.androidx.navigation.testing)


}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package com.example.rememberme.data

import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.example.rememberme.data.database.People
import com.example.rememberme.data.database.PeopleDatabase
import com.example.remindme.database.PeopleDao
import kotlinx.coroutines.runBlocking
import org.junit.After
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
@SmallTest
class PeopleDatabaseTest {

private lateinit var database: PeopleDatabase
private lateinit var peopleDao: PeopleDao

@Before
fun initDb() {
database = Room.inMemoryDatabaseBuilder(
ApplicationProvider.getApplicationContext(),
PeopleDatabase::class.java
).allowMainThreadQueries().build()

peopleDao = database.peopleDao
}

@After
fun closeDb() {
if (this::database.isInitialized) {
database.close()
}
}

@Test
fun insertAndGetPerson() = runBlocking {
val person = People(
firstName = "John",
secondName = "Doe",
place = "Park",
time = "2023-06-13 10:00",
note = "Met at the park",
gender = "Male",
avatar = 1
)

peopleDao.insert(person)

val retrievedPerson = peopleDao.getAll()[0]
Assert.assertEquals(retrievedPerson.firstName, "John")
Assert.assertEquals(retrievedPerson.secondName, "Doe")
}

@Test
fun updatePerson() = runBlocking {
val person = People(
firstName = "John",
secondName = "Doe",
place = "Park",
time = "2023-06-13 10:00",
note = "Met at the park",
gender = "Male",
avatar = 1
)

peopleDao.insert(person)

val retrievedPerson = peopleDao.getAll()[0]
retrievedPerson.firstName = "Jane"
peopleDao.update(retrievedPerson)

val updatedPerson = peopleDao.getAll()[0]
Assert.assertEquals(updatedPerson.firstName, "Jane")
}

@Test
fun deletePerson() = runBlocking {
val person = People(
firstName = "John",
secondName = "Doe",
place = "Park",
time = "2023-06-13 10:00",
note = "Met at the park",
gender = "Male",
avatar = 1
)

peopleDao.insert(person)

val retrievedPerson = peopleDao.getAll()[0]
peopleDao.removePerson(retrievedPerson.id)

val people = peopleDao.getAll()
Assert.assertTrue(people.isEmpty())
}

@Test
fun clearAllPeople() = runBlocking {
val person1 = People(
firstName = "John",
secondName = "Doe",
place = "Park",
time = "2023-06-13 10:00",
note = "Met at the park",
gender = "Male",
avatar = 1
)

val person2 = People(
firstName = "Jane",
secondName = "Doe",
place = "Mall",
time = "2023-06-13 12:00",
note = "Met at the mall",
gender = "Female",
avatar = 2
)

peopleDao.insertAll(person1, person2)

peopleDao.clear()
val people = peopleDao.getAll()
Assert.assertTrue(people.isEmpty())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.example.rememberme

import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.example.rememberme.presentation.navgraph.NavGraph
import com.example.rememberme.presentation.navgraph.Routes
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class NavGraphTest {

@get:Rule
val composeTestRule = createComposeRule()

@Test
fun testNavigationToOnBoardingScreen() {
composeTestRule.setContent {
NavGraph(startDestination = Routes.AppStartNavigation.route)
}

composeTestRule.onNodeWithText("OnBoarding Screen").assertIsDisplayed()
}

@Test
fun testNavigationToPeopleScreen() {
composeTestRule.setContent {
NavGraph(startDestination = Routes.PeopleNavigation.route)
}

composeTestRule.onNodeWithTag("PeopleListScreen").assertIsDisplayed()
}
}
22 changes: 5 additions & 17 deletions app/src/main/java/com/example/rememberme/RememberMeApp.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package com.example.rememberme

import android.content.res.Configuration.UI_MODE_NIGHT_YES
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import com.example.rememberme.presentation.people.PeopleScreen
import com.example.rememberme.presentation.navgraph.NavGraph
import com.example.rememberme.presentation.navgraph.Routes
import com.example.rememberme.ui.theme.RememberMeTheme

@OptIn(ExperimentalMaterial3Api::class)
Expand All @@ -21,23 +18,14 @@ fun RememberMeApp() {
Surface(
modifier = Modifier.fillMaxSize(),
) {
Scaffold(
topBar = {
TopAppBar(
title = {
Text(stringResource(id = R.string.app_name))
}
)
}
) {
PeopleScreen(modifier = Modifier.padding(it))
}
NavGraph(startDestination = Routes.PeopleNavigation.route)
}
}
}


@Preview(showBackground = true)
@Preview(uiMode = UI_MODE_NIGHT_YES)
@Composable
fun RememberMeAppPreview() {
RememberMeApp()
Expand Down
Loading

0 comments on commit 5e6991f

Please sign in to comment.