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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.example.lambda_bbb_events.model.repository.CampsiteRepository
import com.example.lambda_bbb_events.model.repository.UserRepository
import com.example.lambda_bbb_events.model.user.Entrant
import com.example.lambda_bbb_events.model.user.UserRole
import com.example.lambda_bbb_events.store.CampsiteStore
import com.example.lambda_bbb_events.model.campsite.CampsiteStore
import com.google.firebase.FirebaseApp
import com.google.firebase.firestore.FirebaseFirestore
import io.mockk.clearAllMocks
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package com.example.lambda_bbb_events.ui.account

import android.Manifest
import android.os.Build
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertIsNotSelected
import androidx.compose.ui.test.assertIsSelected
import androidx.compose.ui.test.junit4.v2.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performScrollTo
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.example.lambda_bbb_events.LambdaEventsApp
import com.example.lambda_bbb_events.controller.AppControllers
import com.example.lambda_bbb_events.controller.UserController
import com.example.lambda_bbb_events.model.repository.UserRepository
import com.example.lambda_bbb_events.model.user.Entrant
import io.mockk.clearAllMocks
import io.mockk.coVerify
import io.mockk.every
import io.mockk.mockk
import io.mockk.unmockkAll
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

/**
* Tests that an entrant can opt out of, and back in to, notifications from the notification
* settings screen (US 01.10.01).
*
* Notification permission is granted before each test so Android's permission prompt never
* covers the screen.
*/
@RunWith(AndroidJUnit4::class)
class NotificationOptOutUITest {

@get:Rule
val composeTestRule = createComposeRule()

private val controllers = mockk<AppControllers>(relaxed = true)
private val repository = mockk<UserRepository>(relaxed = true)

@Before
fun grantNotificationPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
val instrumentation = InstrumentationRegistry.getInstrumentation()
instrumentation.uiAutomation.grantRuntimePermission(
instrumentation.targetContext.packageName,
Manifest.permission.POST_NOTIFICATIONS
)
}
}

@After
fun cleanUp() {
unmockkAll()
clearAllMocks()
}

/**
* Starts the app signed in as an entrant and opens the notification settings screen.
*
* @param notificationsEnabled The entrant's saved notification choice.
* @return The signed-in entrant.
*/
private fun openNotificationSettings(notificationsEnabled: Boolean): Entrant {
val entrant = Entrant(
deviceId = "device-1",
name = "Ed",
email = "ed@example.com",
phone = "+1 7805551234",
notificationsEnabled = notificationsEnabled
)
every { repository.usersFullyLoaded } returns true
every { repository.getCachedUser("device-1") } returns entrant
every { controllers.users } returns UserController(repository, "device-1")
every { controllers.reservations.getReservationsForEntrant(any()) } returns emptyList()
every { controllers.waitlistEntries.getEntriesForEntrant(any()) } returns emptyList()
every { controllers.campsites.getCampsitesByOwner(any()) } returns emptyList()

composeTestRule.setContent {
LambdaEventsApp(controllers = controllers, darkTheme = false, onDarkThemeChange = {})
}

composeTestRule.onNodeWithText("Profile").performClick()
composeTestRule.onNodeWithText("Settings").performScrollTo().performClick()
composeTestRule.onNodeWithText("Notifications").performClick()
composeTestRule.onNodeWithTag("screen_notification_settings").assertIsDisplayed()
return entrant
}

@Test
fun showsEnabledForOptedInEntrant() {
openNotificationSettings(notificationsEnabled = true)

composeTestRule.onNodeWithText("Enable").assertIsSelected()
composeTestRule.onNodeWithText("Disable").assertIsNotSelected()
}

@Test
fun showsDisabledForOptedOutEntrant() {
openNotificationSettings(notificationsEnabled = false)

composeTestRule.onNodeWithText("Disable").assertIsSelected()
composeTestRule.onNodeWithText("Enable").assertIsNotSelected()
}

@Test
fun disablingSavesOptOut() {
val entrant = openNotificationSettings(notificationsEnabled = true)

composeTestRule.onNodeWithText("Disable").performClick()

composeTestRule.onNodeWithText("Disable").assertIsSelected()
coVerify(exactly = 1) { repository.setAllowNotifications(entrant, false) }
}

@Test
fun enablingSavesOptIn() {
val entrant = openNotificationSettings(notificationsEnabled = false)

composeTestRule.onNodeWithText("Enable").performClick()

composeTestRule.onNodeWithText("Enable").assertIsSelected()
coVerify(exactly = 1) { repository.setAllowNotifications(entrant, true) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package com.example.lambda_bbb_events.ui.campsites
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.semantics.SemanticsProperties
import androidx.compose.ui.test.click
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertIsNotSelected
import androidx.compose.ui.test.assertIsSelected
Expand Down Expand Up @@ -54,8 +57,8 @@ class FilterPageUITest {
showResultsCalled = false
// Remembers the filters like the real controller
every { campsiteController.filters } answers { filters }
every { campsiteController.applyFilters(any(), any(), any(), any(), any()) } answers {
filters = CampsiteFilters(arg(0), arg(1), arg(2), arg(3), arg(4))
every { campsiteController.applyFilters(any(), any(), any(), any(), any(), any()) } answers {
filters = CampsiteFilters(arg(0), arg(1), arg(2), arg(3), arg(4), arg(5))
emptyMap()
}
every { campsiteController.guestOptions } answers { guestOptions }
Expand Down Expand Up @@ -314,6 +317,7 @@ class FilterPageUITest {
fun ratingInputWorks() {
composeTestRule
.onNodeWithTag("ratingStar3")
.performScrollTo()
.performClick()

composeTestRule.onNodeWithTag("ratingStar1").assertIsSelected()
Expand All @@ -336,4 +340,84 @@ class FilterPageUITest {
assertTrue(showResultsCalled)
}
}

/** The text under the area map. */
private fun areaSummary(): String =
composeTestRule.onNodeWithTag("areaSummary")
.fetchSemanticsNode()
.config[SemanticsProperties.Text]
.joinToString("") { it.text }

/**
* Taps the middle of the area map until an area is placed, since the map takes a moment to
* load before it reports taps.
*/
private fun placeArea() {
composeTestRule.onNodeWithTag("areaMap").performScrollTo()
repeat(MAP_TAP_ATTEMPTS) {
composeTestRule.onNodeWithTag("areaMap").performTouchInput { click(center) }
composeTestRule.waitForIdle()
if (areaSummary().startsWith("Within")) return
Thread.sleep(MAP_TAP_WAIT_MS)
}
throw AssertionError("Tapping the map never placed an area")
}

@Test
fun areaStartsUnchosen() {
composeTestRule.onNodeWithTag("areaSummary").performScrollTo()

assertTrue(areaSummary().startsWith("Tap the map"))
}

@Test
fun tappingTheMapPlacesAnArea() {
placeArea()

composeTestRule.onNodeWithTag("areaRadiusHandle").assertIsDisplayed()
}

@Test
fun draggingTheHandleResizesTheArea() {
placeArea()
val before = areaSummary()

composeTestRule.onNodeWithTag("areaRadiusHandle").performTouchInput {
swipe(start = center, end = center + Offset(HANDLE_DRAG_PX, 0f), durationMillis = 500)
}
composeTestRule.waitForIdle()

assertTrue("Expected the radius to change from \"$before\"", areaSummary() != before)
}

@Test
fun resettingTheAreaClearsIt() {
placeArea()

composeTestRule.onNodeWithTag("areaResetButton").performScrollTo().performClick()

assertTrue(areaSummary().startsWith("Tap the map"))
}

@Test
fun showResultsAppliesTheArea() {
placeArea()

composeTestRule.onNodeWithTag("showResultsButton").performScrollTo().performClick()

composeTestRule.runOnIdle {
assertTrue(filters.area != null)
}
}

private companion object {
/** How many times [placeArea] taps before giving up. */
const val MAP_TAP_ATTEMPTS = 15

/** How long [placeArea] waits between taps for the map to load. */
const val MAP_TAP_WAIT_MS = 1000L

/** How far the handle is dragged outwards in the resize test. */
const val HANDLE_DRAG_PX = 150f
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@ class LocationSearchUITest {
composeTestRule.onNodeWithText("Jasper").assertIsDisplayed()
}

/** Where the top of the address field is, in pixels from the top of the screen's content. */
private fun addressFieldTop(): Float =
composeTestRule.onNodeWithTag("create_address").fetchSemanticsNode().boundsInRoot.top

@Test
fun suggestionsCoverWhatIsBelowInsteadOfPushingItDown() {
val before = addressFieldTop()

type("Lake")
waitForSuggestions()

assertEquals(before, addressFieldTop(), 0.5f)
}

@Test
fun choosingAPlaceFillsInTheLocation() {
form.postalCode = "X0X 0X0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package com.example.lambda_bbb_events.ui.navigation

import androidx.compose.ui.test.junit4.v2.createComposeRule
import androidx.compose.ui.test.onAllNodesWithText
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performImeAction
import androidx.compose.ui.test.performTextClearance
import androidx.compose.ui.test.performTextInput
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.example.lambda_bbb_events.controller.AppControllers
import com.example.lambda_bbb_events.controller.CampsiteController
import com.example.lambda_bbb_events.model.campsite.Campsite
import com.example.lambda_bbb_events.model.campsite.CampsiteStore
import com.example.lambda_bbb_events.view.ui.user_screens.explore.ExploreDefaultScreen
import com.example.lambda_bbb_events.view.ui.user_screens.home.HomeScreen
import io.mockk.clearAllMocks
import io.mockk.every
import io.mockk.mockk
import io.mockk.unmockkAll
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

/**
* Tests searching from the home screen, which opens the explore screen with the results, using
* a real [CampsiteController] over three campsites.
*/
@RunWith(AndroidJUnit4::class)
class HomeSearchUITest {
@get:Rule
val composeTestRule = createComposeRule()

private lateinit var navController: NavHostController

private val campsites = listOf(
Campsite(campsiteId = "lake", campsiteName = "Lakeside Retreat", address = "Banff"),
Campsite(campsiteId = "forest", campsiteName = "Forest Hideout", address = "Jasper"),
Campsite(campsiteId = "meadow", campsiteName = "Meadow Field", address = "Canmore")
).associateBy { it.campsiteId }.toMutableMap()

@Before
fun setup() {
val store = mockk<CampsiteStore>(relaxed = true)
every { store.campsites } returns campsites
val campsiteController = CampsiteController(store, scope = CoroutineScope(Dispatchers.Unconfined))
val controllers = mockk<AppControllers>(relaxed = true)
every { controllers.campsites } returns campsiteController

composeTestRule.setContent {
navController = rememberNavController()
NavHost(navController, startDestination = "home") {
composable("home") { HomeScreen(navController, controllers) }
composable("explore_default") { ExploreDefaultScreen(navController, controllers) }
}
}
}

@After
fun cleanUp() {
unmockkAll()
clearAllMocks()
}

/** Types [text] into the home search bar, replacing what was there, and presses search. */
private fun searchFromHome(text: String) {
val input = composeTestRule.onNodeWithTag("searchInput")
input.performTextClearance()
if (text.isNotEmpty()) {
input.performTextInput(text)
}
input.performImeAction()
composeTestRule.waitForIdle()
}

/** How many times [name] is shown on the current screen. */
private fun shown(name: String): Int =
composeTestRule.onAllNodesWithText(name).fetchSemanticsNodes().size

@Test
fun searchingFromHomeShowsMatchingCampsites() {
searchFromHome("Lake")

assertEquals("explore_default", navController.currentDestination?.route)
assertEquals(1, shown("Lakeside Retreat"))
assertEquals(0, shown("Forest Hideout"))
}

@Test
fun searchingNothingFromHomeAfterASearchShowsEveryCampsite() {
searchFromHome("Lake")
composeTestRule.runOnIdle { navController.popBackStack() }
composeTestRule.waitForIdle()

searchFromHome("")

assertEquals("explore_default", navController.currentDestination?.route)
assertEquals(1, shown("Forest Hideout"))
assertEquals(1, shown("Meadow Field"))
}

@Test
fun clearingTheHomeSearchBarThenSearchingShowsEveryCampsite() {
val input = composeTestRule.onNodeWithTag("searchInput")
input.performTextInput("Lake")
input.performTextClearance()
input.performImeAction()
composeTestRule.waitForIdle()

assertEquals("explore_default", navController.currentDestination?.route)
assertEquals(1, shown("Forest Hideout"))
}
}
Loading
Loading