Donuts/app/src/androidTest/java/com/example/cupcake/test/CupcakeOrderScreenTest.kt
2023-05-09 17:14:45 -04:00

154 lines
5.1 KiB
Kotlin

/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.cupcake.test
import androidx.activity.ComponentActivity
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertIsEnabled
import androidx.compose.ui.test.assertIsNotEnabled
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import com.example.cupcake.R
import com.example.cupcake.data.DataSource
import com.example.cupcake.data.OrderUiState
import com.example.cupcake.ui.OrderSummaryScreen
import com.example.cupcake.ui.SelectOptionScreen
import com.example.cupcake.ui.StartOrderScreen
import org.junit.Rule
import org.junit.Test
class CupcakeOrderScreenTest {
/**
* Note: To access to an empty activity, the code uses ComponentActivity instead of
* MainActivity.
*/
@get:Rule
val composeTestRule = createAndroidComposeRule<ComponentActivity>()
private val fakeOrderUiState = OrderUiState(
quantity = 6,
flavor = "Vanilla",
date = "Wed Jul 21",
price = "$100",
pickupOptions = listOf()
)
/**
* When quantity options are provided to StartOrderScreen, the options are displayed on the
* screen.
*/
@Test
fun startOrderScreen_verifyContent() {
// When StartOrderScreen is loaded
composeTestRule.setContent {
StartOrderScreen(
quantityOptions = DataSource.quantityOptions,
onNextButtonClicked = {}
)
}
// Then all the options are displayed on the screen.
DataSource.quantityOptions.forEach {
composeTestRule.onNodeWithStringId(it.first).assertIsDisplayed()
}
}
/**
* When list of options and subtotal are provided to SelectOptionScreen,the options and subtotal
* are displayed on the screen and the next button is disabled.
*/
@Test
fun selectOptionScreen_verifyContent() {
// Given list of options
val flavours = listOf("Vanilla", "Chocolate", "Hazelnut", "Cookie", "Mango")
// And sub total
val subTotal = "$100"
// When SelectOptionScreen is loaded
composeTestRule.setContent {
SelectOptionScreen(subtotal = subTotal, options = flavours)
}
// Then all the options are displayed on the screen.
flavours.forEach { flavour ->
composeTestRule.onNodeWithText(flavour).assertIsDisplayed()
}
// And then the subtotal is displayed correctly.
composeTestRule.onNodeWithText(
composeTestRule.activity.getString(
R.string.subtotal_price,
subTotal
)
).assertIsDisplayed()
// And then the next button is disabled
composeTestRule.onNodeWithStringId(R.string.next).assertIsNotEnabled()
}
/**
* When list of options and subtotal are provided to SelectOptionScreen, and one of the option
* is selected, then the next button is enabled.
*/
@Test
fun selectOptionScreen_optionSelected_NextButtonEnabled() {
// Given list of options
val flavours = listOf("Vanilla", "Chocolate", "Hazelnut", "Cookie", "Mango")
// And sub total
val subTotal = "$100"
// When SelectOptionScreen is loaded
composeTestRule.setContent {
SelectOptionScreen(subtotal = subTotal, options = flavours)
}
// And one option is selected
composeTestRule.onNodeWithText("Vanilla").performClick()
// Then the next button is disabled
composeTestRule.onNodeWithStringId(R.string.next).assertIsEnabled()
}
/**
* When a OrderUiState is provided to Summary Screen, then the flavor, date and subtotal is
* displayed on the screen.
*/
@Test
fun summaryScreen_verifyContentDisplay() {
// When Summary Screen is loaded
composeTestRule.setContent {
OrderSummaryScreen(
orderUiState = fakeOrderUiState,
onCancelButtonClicked = {},
onSendButtonClicked = { _, _ -> },
)
}
// Then the UI is updated correctly.
composeTestRule.onNodeWithText(fakeOrderUiState.flavor).assertIsDisplayed()
composeTestRule.onNodeWithText(fakeOrderUiState.date).assertIsDisplayed()
composeTestRule.onNodeWithText(
composeTestRule.activity.getString(
R.string.subtotal_price,
fakeOrderUiState.price
)
)
}
}