Search in sources :

Example 1 with TagSnapshot

use of com.android.tools.idea.rendering.TagSnapshot in project android by JetBrains.

the class NlModelTest method testMoveInHierarchyWithWrongXmlTags.

public void testMoveInHierarchyWithWrongXmlTags() throws Exception {
    ModelBuilder modelBuilder = model("linear.xml", component(LINEAR_LAYOUT).withBounds(0, 0, 1000, 1000).matchParentWidth().matchParentHeight().withAttribute(ANDROID_URI, ATTR_ORIENTATION, VALUE_VERTICAL).children(component(FRAME_LAYOUT).withBounds(100, 100, 100, 100).width("100dp").height("100dp").children(component(BUTTON).withBounds(100, 100, 100, 100).width("100dp").height("100dp"))));
    NlModel model = modelBuilder.build();
    assertEquals("NlComponent{tag=<LinearLayout>, bounds=[0,0:1000x1000, instance=0}\n" + "    NlComponent{tag=<FrameLayout>, bounds=[100,100:100x100, instance=1}\n" + "        NlComponent{tag=<Button>, bounds=[100,100:100x100, instance=2}", myTreeDumper.toTree(model.getComponents()));
    XmlTag originalRoot = model.getFile().getRootTag();
    assertThat(originalRoot).isNotNull();
    XmlTag originalFrameLayout = originalRoot.getSubTags()[0];
    final Project project = model.getProject();
    WriteCommandAction.runWriteCommandAction(project, () -> {
        PsiDocumentManager manager = PsiDocumentManager.getInstance(project);
        Document document = manager.getDocument(model.getFile());
        assertThat(document).isNotNull();
        document.setText("<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" + "    android:layout_width=\"match_parent\"\n" + "    android:layout_height=\"match_parent\"\n" + "    android:orientation=\"vertical\">\n" + "    <Button\n" + "        android:layout_width=\"100dp\"\n" + "        android:layout_height=\"100dp\"\n" + "        android:text=\"Button\" />\n" + "    <FrameLayout\n" + "        android:layout_width=\"100dp\"\n" + "        android:layout_height=\"100dp\">\n" + "\n" + "    </FrameLayout>\n" + "\n" + "</LinearLayout>");
        manager.commitAllDocuments();
    });
    // Manually construct the view hierarchy
    // Assert that component identity is preserved
    List<ViewInfo> views = Lists.newArrayList();
    XmlTag newRoot = model.getFile().getRootTag();
    assertThat(newRoot).isNotNull();
    XmlTag[] newRootSubTags = newRoot.getSubTags();
    XmlTag newButton = newRootSubTags[0];
    assertThat(originalRoot).isSameAs(newRoot);
    assertThat(originalFrameLayout).isSameAs(newButton);
    TagSnapshot snapshot = TagSnapshot.createTagSnapshot(newRoot);
    ViewInfo viewInfo = new ViewInfo("android.widget.LinearLayout", snapshot, 0, 0, 500, 500);
    views.add(viewInfo);
    ViewInfo buttonInfo = new ViewInfo("android.widget.Button", snapshot.children.get(0), 0, 0, 500, 500);
    buttonInfo.setChildren(Collections.emptyList());
    ViewInfo frameViewInfo = new ViewInfo("android.widget.TextView", snapshot.children.get(1), 0, 0, 300, 300);
    frameViewInfo.setChildren(Collections.emptyList());
    viewInfo.setChildren(Arrays.asList(buttonInfo, frameViewInfo));
    model.updateHierarchy(newRoot, views);
    assertEquals("NlComponent{tag=<LinearLayout>, bounds=[0,0:500x500, instance=3}\n" + // since before the reparse instance=1 was associated with a <FrameLayout> !
    "    NlComponent{tag=<Button>, bounds=[0,0:500x500, instance=4}\n" + "    NlComponent{tag=<FrameLayout>, bounds=[0,0:300x300, instance=5}", myTreeDumper.toTree(model.getComponents()));
}
Also used : ModelBuilder(com.android.tools.idea.uibuilder.fixtures.ModelBuilder) Project(com.intellij.openapi.project.Project) TagSnapshot(com.android.tools.idea.rendering.TagSnapshot) Document(com.intellij.openapi.editor.Document) XmlTag(com.intellij.psi.xml.XmlTag) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) ViewInfo(com.android.ide.common.rendering.api.ViewInfo)

Example 2 with TagSnapshot

use of com.android.tools.idea.rendering.TagSnapshot in project android by JetBrains.

the class ComponentDescriptor method createViewInfo.

@NotNull
public ViewInfo createViewInfo(@Nullable ComponentDescriptor parent, @NotNull XmlTag tag) {
    int left = myX;
    int top = myY;
    if (parent != null) {
        left -= parent.myX;
        top -= parent.myY;
    }
    int right = left + myWidth;
    int bottom = top + myHeight;
    TagSnapshot snapshot = TagSnapshot.createTagSnapshotWithoutChildren(tag);
    TestViewInfo viewInfo = new TestViewInfo(myTagName, snapshot, left, top, right, bottom, myViewObject, myLayoutParamsObject);
    viewInfo.setExtendedInfo((int) (0.8 * (bottom - top)), 0, 0, 0, 0);
    if (myViewType != null) {
        viewInfo.setViewType(myViewType);
    }
    List<ViewInfo> childList = Lists.newArrayList();
    XmlTag[] subTags = tag.getSubTags();
    assertEquals(subTags.length, myChildren.length);
    for (int i = 0; i < subTags.length; i++) {
        ComponentDescriptor childDescriptor = myChildren[i];
        XmlTag childTag = subTags[i];
        childList.add(childDescriptor.createViewInfo(this, childTag));
    }
    viewInfo.setChildren(childList);
    return viewInfo;
}
Also used : TagSnapshot(com.android.tools.idea.rendering.TagSnapshot) ViewInfo(com.android.ide.common.rendering.api.ViewInfo) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ViewInfo (com.android.ide.common.rendering.api.ViewInfo)2 TagSnapshot (com.android.tools.idea.rendering.TagSnapshot)2 XmlTag (com.intellij.psi.xml.XmlTag)2 ModelBuilder (com.android.tools.idea.uibuilder.fixtures.ModelBuilder)1 Document (com.intellij.openapi.editor.Document)1 Project (com.intellij.openapi.project.Project)1 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)1 NotNull (org.jetbrains.annotations.NotNull)1