/* * 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() 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 flavors = listOf("Vanilla", "Chocolate", "Hazelnut", "Cookie", "Mango") // And subtotal val subtotal = "$100" // When SelectOptionScreen is loaded composeTestRule.setContent { SelectOptionScreen(subtotal = subtotal, options = flavors) } // Then all the options are displayed on the screen. flavors.forEach { flavor -> composeTestRule.onNodeWithText(flavor).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 ) ).assertIsDisplayed() } }