Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not assert child of KLazyListItemNode #81

Closed
kongvorawut opened this issue Mar 23, 2024 · 2 comments
Closed

Can not assert child of KLazyListItemNode #81

kongvorawut opened this issue Mar 23, 2024 · 2 comments

Comments

@kongvorawut
Copy link

kongvorawut commented Mar 23, 2024

A KLazyListItemNode can have children like title and subtitle text, but can't to verify title node and subtitle node because nodes are merged.

Workaround: set useUnmergedTree is true

class KItem(
        semanticsNode: SemanticsNode,
        semanticsProvider: SemanticsNodeInteractionsProvider,
): KLazyListItemNode<KItem>(semanticsNode, semanticsProvider) {
      val title: KNode = child {
          useUnmergedTree = true   // add this
          hasTestTag("title")
      }
      val subtitle: KNode = child {
          useUnmergedTree = true   // add this
          hasTestTag("subtitle")
      }
}

Is there a way to make this code easier to use?

@EvgenyMeltsaykin
Copy link

Hi!

You can override the KNode constructor like in this issue

@Vacxe Can you explain why the useUnmergedTree flag has a default value of false? Perhaps it's worth pointing this out in the documentation and workaround examples?

@Vacxe
Copy link
Member

Vacxe commented Mar 25, 2024

@EvgenyMeltsaykin sure mate, actually the answer is quite simple:
Kakao/KakaoCompose it's a DSL wrapper over Espresso library, with amount of extra custom matchers and assertions. We trying DO NOT change any default Espresso behavior. So, lets jump in to Espresso code

androidx.compose.ui.test.SemanticsNodeInteractionsProvider

package androidx.compose.ui.test

import androidx.compose.ui.test.internal.JvmDefaultWithCompatibility

/**
 * Provides the main entry point into testing.
 *
 * Typically implemented by a test rule.
 */
@JvmDefaultWithCompatibility
interface SemanticsNodeInteractionsProvider {
    /**
     * Finds a semantics node that matches the given condition.
     *
     * Any subsequent operation on its result will expect exactly one element found (unless
     * [SemanticsNodeInteraction.assertDoesNotExist] is used) and will throw [AssertionError] if
     * none or more than one element is found.
     *
     * For usage patterns and semantics concepts see [SemanticsNodeInteraction]
     *
     * @param matcher Matcher used for filtering
     * @param useUnmergedTree Find within merged composables like Buttons.
     * @see onAllNodes to work with multiple elements
     */
    fun onNode(
        matcher: SemanticsMatcher,
        useUnmergedTree: Boolean = false
    ): SemanticsNodeInteraction

    /**
     * Finds all semantics nodes that match the given condition.
     *
     * If you are working with elements that are not supposed to occur multiple times use [onNode]
     * instead.
     *
     * For usage patterns and semantics concepts see [SemanticsNodeInteraction]
     *
     * @param matcher Matcher used for filtering.
     * @param useUnmergedTree Find within merged composables like Buttons.
     * @see onNode
     */
    fun onAllNodes(
        matcher: SemanticsMatcher,
        useUnmergedTree: Boolean = false
    ): SemanticsNodeInteractionCollection
}

useUnmergedTree: Boolean = false - default Espresso behavior

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants