|
| 1 | +package io.sentry.compose.viewhierarchy; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNull; |
| 5 | + |
| 6 | +import androidx.compose.runtime.collection.MutableVector; |
| 7 | +import androidx.compose.ui.layout.LayoutCoordinates; |
| 8 | +import androidx.compose.ui.layout.ModifierInfo; |
| 9 | +import androidx.compose.ui.node.LayoutNode; |
| 10 | +import androidx.compose.ui.node.Owner; |
| 11 | +import androidx.compose.ui.semantics.SemanticsConfiguration; |
| 12 | +import androidx.compose.ui.semantics.SemanticsModifier; |
| 13 | +import androidx.compose.ui.semantics.SemanticsPropertyKey; |
| 14 | +import io.sentry.internal.viewhierarchy.ViewHierarchyExporter; |
| 15 | +import io.sentry.protocol.ViewHierarchyNode; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.List; |
| 18 | +import kotlin.jvm.functions.Function2; |
| 19 | +import org.jetbrains.annotations.NotNull; |
| 20 | +import org.jetbrains.annotations.Nullable; |
| 21 | +import org.junit.Test; |
| 22 | +import org.mockito.Mockito; |
| 23 | + |
| 24 | +public class ComposeViewHierarchyExporterTest { |
| 25 | + |
| 26 | + @Test |
| 27 | + public void testComposeViewHierarchyExport() { |
| 28 | + final ViewHierarchyNode rootVhNode = new ViewHierarchyNode(); |
| 29 | + |
| 30 | + final LayoutNode childA = mockLayoutNode(true, "childA", 10, 20); |
| 31 | + final LayoutNode childB = mockLayoutNode(true, null, 10, 20); |
| 32 | + final LayoutNode childC = mockLayoutNode(false, null, 10, 20); |
| 33 | + final LayoutNode parent = mockLayoutNode(true, "root", 30, 40, childA, childB, childC); |
| 34 | + |
| 35 | + final Owner node = Mockito.mock(Owner.class); |
| 36 | + Mockito.when(node.getRoot()).thenReturn(parent); |
| 37 | + |
| 38 | + final ViewHierarchyExporter exporter = new ComposeViewHierarchyExporter(); |
| 39 | + exporter.export(rootVhNode, node); |
| 40 | + |
| 41 | + assertEquals(1, rootVhNode.getChildren().size()); |
| 42 | + final ViewHierarchyNode parentVhNode = rootVhNode.getChildren().get(0); |
| 43 | + |
| 44 | + assertEquals("root", parentVhNode.getTag()); |
| 45 | + assertEquals(30.0, parentVhNode.getWidth().doubleValue(), 0.001); |
| 46 | + assertEquals(40.0, parentVhNode.getHeight().doubleValue(), 0.001); |
| 47 | + |
| 48 | + // ensure not placed elements (childC) are not part of the view hierarchy |
| 49 | + assertEquals(2, parentVhNode.getChildren().size()); |
| 50 | + |
| 51 | + final ViewHierarchyNode childAVhNode = parentVhNode.getChildren().get(0); |
| 52 | + assertEquals("childA", childAVhNode.getTag()); |
| 53 | + assertEquals(10.0, childAVhNode.getWidth().doubleValue(), 0.001); |
| 54 | + assertEquals(20.0, childAVhNode.getHeight().doubleValue(), 0.001); |
| 55 | + assertNull(childAVhNode.getChildren()); |
| 56 | + |
| 57 | + final ViewHierarchyNode childBVhNode = parentVhNode.getChildren().get(1); |
| 58 | + assertNull(childBVhNode.getTag()); |
| 59 | + } |
| 60 | + |
| 61 | + private static LayoutNode mockLayoutNode( |
| 62 | + final boolean isPlaced, |
| 63 | + final @Nullable String tag, |
| 64 | + final int width, |
| 65 | + final int height, |
| 66 | + LayoutNode... children) { |
| 67 | + final LayoutNode nodeA = Mockito.mock(LayoutNode.class); |
| 68 | + Mockito.when(nodeA.isPlaced()).thenReturn(isPlaced); |
| 69 | + Mockito.when((nodeA.getWidth())).thenReturn(width); |
| 70 | + Mockito.when((nodeA.getHeight())).thenReturn(height); |
| 71 | + |
| 72 | + final ModifierInfo modifierInfo = Mockito.mock(ModifierInfo.class); |
| 73 | + Mockito.when(modifierInfo.getModifier()) |
| 74 | + .thenReturn( |
| 75 | + new SemanticsModifier() { |
| 76 | + @NotNull |
| 77 | + @Override |
| 78 | + public SemanticsConfiguration getSemanticsConfiguration() { |
| 79 | + final SemanticsConfiguration config = new SemanticsConfiguration(); |
| 80 | + config.set( |
| 81 | + new SemanticsPropertyKey<>( |
| 82 | + "SentryTag", |
| 83 | + new Function2<String, String, String>() { |
| 84 | + @Override |
| 85 | + public String invoke(String s, String s2) { |
| 86 | + return s; |
| 87 | + } |
| 88 | + }), |
| 89 | + tag); |
| 90 | + return config; |
| 91 | + } |
| 92 | + }); |
| 93 | + final List<ModifierInfo> modifierInfoList = new ArrayList<>(); |
| 94 | + modifierInfoList.add(modifierInfo); |
| 95 | + Mockito.when((nodeA.getModifierInfo())).thenReturn(modifierInfoList); |
| 96 | + |
| 97 | + Mockito.when((nodeA.getZSortedChildren())) |
| 98 | + .thenReturn(new MutableVector<>(children, children.length)); |
| 99 | + |
| 100 | + final LayoutCoordinates coordinates = Mockito.mock(LayoutCoordinates.class); |
| 101 | + Mockito.when(nodeA.getCoordinates()).thenReturn(coordinates); |
| 102 | + return nodeA; |
| 103 | + } |
| 104 | +} |
0 commit comments