|
| 1 | +/* |
| 2 | + * Copyright 2020 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.jetnews |
| 18 | + |
| 19 | +import androidx.compose.material.ExperimentalMaterialApi |
| 20 | +import androidx.compose.material.SnackbarHostState |
| 21 | +import androidx.compose.material.rememberScaffoldState |
| 22 | +import androidx.compose.runtime.ExperimentalComposeApi |
| 23 | +import androidx.compose.runtime.snapshots.snapshotFlow |
| 24 | +import androidx.test.platform.app.InstrumentationRegistry |
| 25 | +import androidx.ui.test.assertIsDisplayed |
| 26 | +import androidx.ui.test.createComposeRule |
| 27 | +import androidx.ui.test.onNodeWithText |
| 28 | +import com.example.jetnews.ui.home.HomeScreen |
| 29 | +import com.example.jetnews.ui.state.UiState |
| 30 | +import kotlinx.coroutines.flow.filterNotNull |
| 31 | +import kotlinx.coroutines.flow.first |
| 32 | +import kotlinx.coroutines.runBlocking |
| 33 | +import org.junit.Rule |
| 34 | +import org.junit.Test |
| 35 | + |
| 36 | +/** |
| 37 | + * Checks that the Snackbar is shown when the HomeScreen data contains an error. |
| 38 | + */ |
| 39 | +class HomeScreenSnackbarTest { |
| 40 | + |
| 41 | + @get:Rule |
| 42 | + val composeTestRule = createComposeRule(disableTransitions = true) |
| 43 | + |
| 44 | + @OptIn( |
| 45 | + ExperimentalMaterialApi::class, |
| 46 | + ExperimentalComposeApi::class |
| 47 | + ) |
| 48 | + @Test |
| 49 | + fun postsContainError_snackbarShown() { |
| 50 | + val snackbarHostState = SnackbarHostState() |
| 51 | + composeTestRule.setContent { |
| 52 | + val scaffoldState = rememberScaffoldState(snackbarHostState = snackbarHostState) |
| 53 | + |
| 54 | + // When the Home screen receives data with an error |
| 55 | + HomeScreen( |
| 56 | + posts = UiState(exception = IllegalStateException()), |
| 57 | + favorites = emptySet(), |
| 58 | + onToggleFavorite = {}, |
| 59 | + onRefreshPosts = {}, |
| 60 | + onErrorDismiss = {}, |
| 61 | + navigateTo = {}, |
| 62 | + scaffoldState = scaffoldState |
| 63 | + ) |
| 64 | + } |
| 65 | + |
| 66 | + // Then the first message received in the Snackbar is an error message |
| 67 | + val snackbarText = InstrumentationRegistry.getInstrumentation() |
| 68 | + .targetContext.resources.getString(R.string.load_error) |
| 69 | + runBlocking { |
| 70 | + // snapshotFlow converts a State to a Kotlin Flow so we can observe it |
| 71 | + // wait for the first a non-null `currentSnackbarData` |
| 72 | + snapshotFlow { snackbarHostState.currentSnackbarData }.filterNotNull().first() |
| 73 | + composeTestRule.onNodeWithText(snackbarText, false, false).assertIsDisplayed() |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments