1 /* 2 * Copyright (C) 2024 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 * http://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 package com.google.jetpackcamera.utils 17 18 import android.content.Context 19 import androidx.annotation.StringRes 20 import androidx.compose.ui.test.SemanticsMatcher 21 import androidx.compose.ui.test.SemanticsNodeInteraction 22 import androidx.compose.ui.test.SemanticsNodeInteractionsProvider 23 import androidx.compose.ui.test.onNodeWithContentDescription 24 import androidx.compose.ui.test.onNodeWithText 25 import androidx.compose.ui.test.printToString 26 import androidx.test.core.app.ApplicationProvider 27 import org.junit.AssumptionViolatedException 28 29 /** 30 * Allows use of testRule.onNodeWithText that uses an integer string resource 31 * rather than a [String] directly. 32 */ SemanticsNodeInteractionsProvidernull33fun SemanticsNodeInteractionsProvider.onNodeWithText( 34 @StringRes strRes: Int 35 ): SemanticsNodeInteraction = onNodeWithText( 36 text = getResString(strRes) 37 ) 38 39 /** 40 * Allows use of testRule.onNodeWithContentDescription that uses an integer string resource 41 * rather than a [String] directly. 42 */ 43 fun SemanticsNodeInteractionsProvider.onNodeWithContentDescription( 44 @StringRes strRes: Int 45 ): SemanticsNodeInteraction = onNodeWithContentDescription( 46 label = getResString(strRes) 47 ) 48 49 /** 50 * Fetch a string resources from a [SemanticsNodeInteractionsProvider] context. 51 */ 52 fun SemanticsNodeInteractionsProvider.getResString(@StringRes strRes: Int): String { 53 return ApplicationProvider.getApplicationContext<Context>().getString(strRes) 54 } 55 56 /** 57 * Assumes that the provided [matcher] is satisfied for this node. 58 * 59 * This is equivalent to [SemanticsNodeInteraction.assert()], but will skip the test rather than 60 * fail the test. 61 * 62 * @param matcher Matcher to verify. 63 * @param messagePrefixOnError Prefix to be put in front of an error that gets thrown in case this 64 * assert fails. This can be helpful in situations where this assert fails as part of a bigger 65 * operation that used this assert as a precondition check. 66 * 67 * @throws AssumptionViolatedException if the matcher does not match or the node can no 68 * longer be found 69 */ SemanticsNodeInteractionnull70fun SemanticsNodeInteraction.assume( 71 matcher: SemanticsMatcher, 72 messagePrefixOnError: (() -> String)? = null 73 ): SemanticsNodeInteraction { 74 var errorMessageOnFail = "Failed to assume the following: (${matcher.description})" 75 if (messagePrefixOnError != null) { 76 errorMessageOnFail = messagePrefixOnError() + "\n" + errorMessageOnFail 77 } 78 val node = fetchSemanticsNode(errorMessageOnFail) 79 if (!matcher.matches(node)) { 80 throw AssumptionViolatedException( 81 buildGeneralErrorMessage(errorMessageOnFail, this) 82 ) 83 } 84 return this 85 } 86 buildGeneralErrorMessagenull87internal fun buildGeneralErrorMessage( 88 errorMessage: String, 89 nodeInteraction: SemanticsNodeInteraction 90 ): String { 91 val sb = StringBuilder() 92 93 sb.appendLine(errorMessage) 94 95 sb.appendLine("Semantics of the node:") 96 sb.appendLine(nodeInteraction.printToString()) 97 98 return sb.toString() 99 } 100