Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ android {
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
testInstrumentationRunner = "com.bober.notesapp.HiltTestRunner"
}

packaging {
resources {
excludes += "META-INF/LICENSE.md"
excludes += "META-INF/LICENSE-notice.md"
excludes += "META-INF/LICENSE*"
excludes += "META-INF/NOTICE*"
}
}

buildTypes {
Expand Down

This file was deleted.

18 changes: 18 additions & 0 deletions app/src/androidTest/java/com/bober/notesapp/HiltTestRunner.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.bober.notesapp

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


class HiltTestRunner: AndroidJUnitRunner() {

override fun newApplication(
cl: ClassLoader?,
className: String?,
context: Context?
): Application? {
return super.newApplication(cl, HiltTestApplication::class.java.name, context)
}
}
34 changes: 34 additions & 0 deletions app/src/androidTest/java/com/bober/notesapp/di/AppModuleTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.bober.notesapp.di

import android.app.Application
import androidx.room.Room
import com.bober.notesapp.data.local.NoteDatabase
import com.bober.notesapp.data.repository.FakeNoteRepository
import com.bober.notesapp.data.repository.NoteRepositoryImpl
import com.bober.notesapp.domain.repository.NoteRepository
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object AppModuleTest {

@Provides
@Singleton
fun provideNoteDatabase(app: Application): NoteDatabase {
return Room.inMemoryDatabaseBuilder(
app,
NoteDatabase::class.java
).build()
}

@Provides
@Singleton
fun provideNoteRepository(db : NoteDatabase): NoteRepository{
return NoteRepositoryImpl(db.noteDao())
// return FakeNoteRepository()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.bober.notesapp.presentation.notes

import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertIsNotSelected
import androidx.compose.ui.test.assertIsSelected
import androidx.compose.ui.test.filterToOne
import androidx.compose.ui.test.hasClickAction
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onChildren
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import com.bober.notesapp.core.util.TestTags
import com.bober.notesapp.di.AppModule
import com.bober.notesapp.presentation.MainActivity
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import dagger.hilt.android.testing.UninstallModules
import org.junit.Before
import org.junit.Rule
import org.junit.Test

@HiltAndroidTest
@UninstallModules(AppModule::class)
class NoteScreenTest {

@get:Rule(order = 0)
val hiltRule = HiltAndroidRule(this)

@get:Rule(order = 1)
val composeRule = createAndroidComposeRule<MainActivity>()

@Before
fun setUp(){

hiltRule.inject()
}

@Test
fun clickToggleOrderSection_isVisible() {
composeRule.onNodeWithTag(TestTags.ORDER_SECTION).assertDoesNotExist()
composeRule.onNodeWithContentDescription("Sort").performClick()
composeRule.onNodeWithTag(TestTags.ORDER_SECTION).assertIsDisplayed()
}

@Test
fun clickToggleOrderSectionAndChangeToTitle_titleIsClicked() {
composeRule.onNodeWithTag(TestTags.ORDER_SECTION).assertDoesNotExist()
composeRule.onNodeWithContentDescription("Sort").performClick()

val titleRadioButton = composeRule
.onNodeWithTag(TestTags.TITLE_RADIO_BUTTON, useUnmergedTree = true)
.onChildren()
.filterToOne(hasClickAction())

titleRadioButton.assertIsNotSelected()
composeRule.onNodeWithText("Title").assertIsDisplayed()
titleRadioButton.performClick()
titleRadioButton.assertIsSelected()
}

@Test
fun clickAddNote_navigatesToAddEditNoteScreen() {
composeRule.onNodeWithText("Your note").assertIsDisplayed()
composeRule.onNodeWithContentDescription("Add Note").performClick()
composeRule.onNodeWithText("Enter title...").assertIsDisplayed()
}
}
7 changes: 7 additions & 0 deletions app/src/main/java/com/bober/notesapp/core/util/TestTags.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.bober.notesapp.core.util

object TestTags {

const val ORDER_SECTION = "ORDER_SECTION"
const val TITLE_RADIO_BUTTON = "TITLE_RADIO_BUTTON"
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import com.bober.notesapp.core.util.TestTags
import com.bober.notesapp.domain.model.Note
import com.bober.notesapp.presentation.notes.components.NoteItem
import com.bober.notesapp.presentation.notes.components.OrderSection
Expand Down Expand Up @@ -115,7 +117,8 @@ fun NoteScreenContent(
OrderSection(
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 16.dp),
.padding(vertical = 16.dp)
.testTag(TestTags.ORDER_SECTION),
noteOrder = state.noteOrder,
onOrderChange = {
onEvent(NotesEvent.Order(it))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ fun NoteItem(
) {
Icon(
imageVector = Icons.Default.Delete,
contentDescription = "Delete note",
contentDescription = "Delete note ${note.title}",
tint = Color.Black
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.unit.dp
import com.bober.notesapp.core.util.TestTags
import com.bober.notesapp.domain.util.NoteOrder
import com.bober.notesapp.domain.util.OrderType

Expand All @@ -29,7 +31,8 @@ fun OrderSection(
selected = noteOrder is NoteOrder.Title,
onSelect = {
onOrderChange(NoteOrder.Title(noteOrder.orderType))
}
},
Modifier.testTag(TestTags.TITLE_RADIO_BUTTON)
)
Spacer(modifier = Modifier.width(8.dp))
DefaultRadioButton(
Expand Down
Loading