Skip to content

Commit

Permalink
more unit + integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ahudson20 committed Apr 24, 2024
1 parent f9bf360 commit bcd89ff
Show file tree
Hide file tree
Showing 8 changed files with 423 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.app.whakaara.onboarding

import androidx.compose.ui.test.assertHasClickAction
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import com.app.whakaara.ui.onboarding.DisableBatteryOptimizationOnboarding
import com.app.whakaara.ui.theme.WhakaaraTheme
import org.junit.Rule
import org.junit.Test

class DisableBatteryOptimizationOnboardingTest {
@get:Rule
val composeTestRule = createComposeRule()

@Test
fun shouldDisplayCorrectData(): Unit = with(composeTestRule) {
// Given + When
setContent {
WhakaaraTheme {
DisableBatteryOptimizationOnboarding()
}
}

// Then
onNodeWithText("Battery").assertIsDisplayed()
onNodeWithText("This app requires unrestricted battery settings, otherwise certain functionality may not work as expected!").assertIsDisplayed()
onNodeWithText("Disable battery optimization").assertIsDisplayed().assertHasClickAction()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.app.whakaara.onboarding

import androidx.compose.material3.SnackbarHostState
import androidx.compose.ui.test.assertHasClickAction
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import com.app.whakaara.ui.onboarding.NotificationsOnboarding
import com.app.whakaara.ui.theme.WhakaaraTheme
import org.junit.Rule
import org.junit.Test

class NotificationsOnboardingTest {
@get:Rule
val composeTestRule = createComposeRule()

@Test
fun shouldDisplayCorrectData(): Unit = with(composeTestRule) {
// Given + When
setContent {
WhakaaraTheme {
NotificationsOnboarding(
snackbarHostState = SnackbarHostState()
)
}
}

// Then
onNodeWithText("Notifications").assertIsDisplayed()
onNodeWithText("This app requires permission to send you notifications!").assertIsDisplayed()
onNodeWithText("Enable notifications").assertIsDisplayed().assertHasClickAction()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.app.whakaara.onboarding

import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithText
import com.app.whakaara.ui.onboarding.WelcomeOnboarding
import com.app.whakaara.ui.theme.WhakaaraTheme
import org.junit.Rule
import org.junit.Test

class WelcomeOnboardingTest {
@get:Rule
val composeTestRule = createComposeRule()

@Test
fun shouldDisplayCorrectData(): Unit = with(composeTestRule) {
// Given + When
setContent {
WhakaaraTheme {
WelcomeOnboarding()
}
}

// Then
onNodeWithText("Widget").assertIsDisplayed()
onNodeWithText("This screen will guide you through the most important setup steps").assertIsDisplayed()
onNodeWithContentDescription("whakaara app icon").assertIsDisplayed()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.app.whakaara.onboarding

import androidx.compose.ui.test.assertHasClickAction
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import com.app.whakaara.ui.onboarding.WidgetOnboarding
import com.app.whakaara.ui.theme.WhakaaraTheme
import org.junit.Rule
import org.junit.Test

class WidgetOnboardingTest {
@get:Rule
val composeTestRule = createComposeRule()

@Test
fun shouldDisplayCorrectData(): Unit = with(composeTestRule) {
// Given + When
setContent {
WhakaaraTheme {
WidgetOnboarding()
}
}

// Then
onNodeWithText("Widget").assertIsDisplayed()
onNodeWithText("This app supports displaying your next scheduled alarm in a widget").assertIsDisplayed()
onNodeWithText("Try adding it to the home screen and using it!").assertIsDisplayed()
onNodeWithText("Create widget").assertIsDisplayed().assertHasClickAction()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fun WelcomeOnboarding(
) {
Image(
painterResource(id = R.drawable.translucent),
contentDescription = stringResource(id = R.string.delete_icon_content_description)
contentDescription = stringResource(id = R.string.onboarding_welcome_icon_description)
)
}
Spacer(modifier = Modifier.height(space20))
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
<string name="onboarding_sub_text">This app requires permission to send you notifications!</string>
<string name="onboarding_welcome_title">Welcome to whakaara</string>
<string name="onboarding_welcome_sub_text">This screen will guide you through the most important setup steps</string>
<string name="onboarding_welcome_icon_description">whakaara app icon</string>

<string name="notification_timer_play_action_label">Resume</string>
<string name="notification_timer_pause_action_label">Pause</string>
Expand Down
217 changes: 217 additions & 0 deletions app/src/test/java/com/app/whakaara/logic/StopwatchManagerTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
package com.app.whakaara.logic

import android.app.Application
import android.app.NotificationManager
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.core.app.NotificationCompat
import app.cash.turbine.test
import com.app.whakaara.data.datastore.PreferencesDataStore
import com.app.whakaara.state.Lap
import com.app.whakaara.state.StopwatchState
import io.mockk.Runs
import io.mockk.coEvery
import io.mockk.just
import io.mockk.mockk
import io.mockk.slot
import io.mockk.verify
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestRule

@OptIn(ExperimentalCoroutinesApi::class)
class StopwatchManagerTest {
@Rule
@JvmField
var rule: TestRule = InstantTaskExecutorRule()

private val testDispatcher = StandardTestDispatcher()

private lateinit var stopwatchManagerWrapper: StopwatchManagerWrapper
private lateinit var app: Application
private lateinit var notificationManager: NotificationManager
private lateinit var stopwatchNotificationBuilder: NotificationCompat.Builder
private lateinit var coroutineScope: CoroutineScope
private lateinit var preferencesDatastore: PreferencesDataStore
private val managedCoroutineScope = TestScope(testDispatcher)
private val id = 400

@Before
fun setUp() {
Dispatchers.setMain(testDispatcher)
app = mockk()
notificationManager = mockk()
stopwatchNotificationBuilder = mockk()
coroutineScope = mockk()
preferencesDatastore = mockk()
stopwatchManagerWrapper = StopwatchManagerWrapper(app, notificationManager, stopwatchNotificationBuilder, managedCoroutineScope, preferencesDatastore)
}

@After
fun tearDown() {
Dispatchers.resetMain()
}

@Test
fun `pause stopwatch`() = runTest {
// Given
val notificationId = slot<Int>()
coEvery { notificationManager.cancel(any()) } just Runs

// When
stopwatchManagerWrapper.pauseStopwatch()

// Then
verify(exactly = 1) { notificationManager.cancel(capture(notificationId)) }
assertEquals(id, notificationId.captured)
stopwatchManagerWrapper.stopwatchState.test {
val state = awaitItem()
with(state) {
assertEquals(false, isActive)
assertEquals(true, isPaused)
}
}
}

@Test
fun `reset stopwatch`() = runTest {
// Given
val notificationId = slot<Int>()
coEvery { notificationManager.cancel(any()) } just Runs

// When
stopwatchManagerWrapper.resetStopwatch()

// Then
verify(exactly = 1) { notificationManager.cancel(capture(notificationId)) }
assertEquals(id, notificationId.captured)
stopwatchManagerWrapper.stopwatchState.test {
val state = awaitItem()
with(state) {
assertEquals(0L, timeMillis)
assertEquals(0L, lastTimeStamp)
assertEquals("00:00:00:000", formattedTime)
assertEquals(false, isActive)
assertEquals(true, isStart)
assertEquals(false, isPaused)
assert(lapList.isEmpty())
}
}
}

@Test
fun `lap stopwatch empty list`() = runTest {
// Given
val current = 123123L
stopwatchManagerWrapper.stopwatchState.value = StopwatchState(
timeMillis = current
)

// When
stopwatchManagerWrapper.lapStopwatch()

// Then
stopwatchManagerWrapper.stopwatchState.test {
val state = awaitItem()
with(state) {
assert(lapList.isNotEmpty())
assertEquals(1, lapList.size)
assertEquals(current, lapList.first().diff)
assertEquals(current, lapList.first().time)
}
}
}

@Test
fun `lap stopwatch not empty list`() = runTest {
// Given
val current = 123123L
stopwatchManagerWrapper.stopwatchState.value = StopwatchState(
timeMillis = current,
lapList = mutableListOf(
Lap(
time = 123L,
diff = 123L
),
Lap(
time = 456L,
diff = 456L
)
)
)

// When
stopwatchManagerWrapper.lapStopwatch()

// Then
stopwatchManagerWrapper.stopwatchState.test {
val state = awaitItem()
with(state) {
assert(lapList.isNotEmpty())
assertEquals(3, lapList.size)
assertEquals(123123, lapList.last().time)
assertEquals(122667, lapList.last().diff)
}
}
}

@Test
fun `recreate stopwatch paused`() = runTest {
// Given
val pausedState = StopwatchState(
isPaused = true,
isActive = false,
isStart = false,
timeMillis = 112233L,
formattedTime = "11:22:33:444",
lapList = mutableListOf(
Lap(
time = 420L,
diff = 420L
)
)
)

// When
stopwatchManagerWrapper.recreateStopwatchPaused(state = pausedState)

// Then
stopwatchManagerWrapper.stopwatchState.test {
val state = awaitItem()
with(state) {
assertEquals(true, isPaused)
assertEquals(false, isActive)
assertEquals(false, isStart)
assertEquals(112233L, timeMillis)
assertEquals("11:22:33:444", formattedTime)
assertEquals(1, lapList.size)
assertEquals(420L, lapList.first().time)
assertEquals(420L, lapList.first().diff)
}
}
}

@Test
fun `cancel notification`() = runTest {
// Given
val notificationId = slot<Int>()
coEvery { notificationManager.cancel(any()) } just Runs

// When
stopwatchManagerWrapper.cancelNotification()

// Then
verify(exactly = 1) { notificationManager.cancel(capture(notificationId)) }
assertEquals(id, notificationId.captured)
}
}
Loading

0 comments on commit bcd89ff

Please sign in to comment.