Search in sources :

Example 1 with ModelBuilder

use of com.android.tools.idea.uibuilder.fixtures.ModelBuilder 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 ModelBuilder

use of com.android.tools.idea.uibuilder.fixtures.ModelBuilder in project android by JetBrains.

the class NlModelTest method testRemoveFirstChild.

public void testRemoveFirstChild() throws Exception {
    ModelBuilder modelBuilder = createDefaultModelBuilder(false);
    NlModel model = modelBuilder.build();
    assertEquals("NlComponent{tag=<LinearLayout>, bounds=[0,0:1000x1000, instance=0}\n" + "    NlComponent{tag=<TextView>, bounds=[100,100:100x100, instance=1}\n" + "    NlComponent{tag=<Button>, bounds=[100,200:100x100, instance=2}", myTreeDumper.toTree(model.getComponents()));
    // Remove first child
    ComponentDescriptor parent = modelBuilder.findByPath(LINEAR_LAYOUT);
    assertThat(parent).isNotNull();
    parent.removeChild(modelBuilder.findByPath(LINEAR_LAYOUT, TEXT_VIEW));
    modelBuilder.updateModel(model, false);
    assertEquals("NlComponent{tag=<LinearLayout>, bounds=[0,0:1000x1000, instance=0}\n" + "    NlComponent{tag=<Button>, bounds=[100,200:100x100, instance=2}", myTreeDumper.toTree(model.getComponents()));
}
Also used : ModelBuilder(com.android.tools.idea.uibuilder.fixtures.ModelBuilder) ComponentDescriptor(com.android.tools.idea.uibuilder.fixtures.ComponentDescriptor)

Example 3 with ModelBuilder

use of com.android.tools.idea.uibuilder.fixtures.ModelBuilder in project android by JetBrains.

the class NlModelTest method testChangedPropertiesWithIds.

@SuppressWarnings("ConstantConditions")
public void testChangedPropertiesWithIds() throws Exception {
    boolean preserveXmlTags = false;
    // We include id's in the tags here since (due to attribute
    // changes between the two elements
    boolean includeIds = true;
    ModelBuilder modelBuilder = createDefaultModelBuilder(includeIds);
    NlModel model = modelBuilder.build();
    assertEquals("NlComponent{tag=<LinearLayout>, bounds=[0,0:1000x1000, instance=0}\n" + "    NlComponent{tag=<TextView>, bounds=[100,100:100x100, instance=1}\n" + "    NlComponent{tag=<Button>, bounds=[100,200:100x100, instance=2}", myTreeDumper.toTree(model.getComponents()));
    // Change some attributes; this means that our finger print comparison (which
    // hashes all tag names and attributes) won't work
    ComponentDescriptor button = modelBuilder.findByPath(LINEAR_LAYOUT, BUTTON);
    assertThat(button).isNotNull();
    ComponentDescriptor textView = modelBuilder.findByPath(LINEAR_LAYOUT, TEXT_VIEW);
    assertThat(textView).isNotNull();
    button.withAttribute("style", "@style/Foo");
    textView.withAttribute("style", "@style/Foo");
    modelBuilder.updateModel(model, preserveXmlTags);
    // Everything should be identical
    assertEquals("NlComponent{tag=<LinearLayout>, bounds=[0,0:1000x1000, instance=0}\n" + "    NlComponent{tag=<TextView>, bounds=[100,100:100x100, instance=1}\n" + "    NlComponent{tag=<Button>, bounds=[100,200:100x100, instance=2}", myTreeDumper.toTree(model.getComponents()));
}
Also used : ModelBuilder(com.android.tools.idea.uibuilder.fixtures.ModelBuilder) ComponentDescriptor(com.android.tools.idea.uibuilder.fixtures.ComponentDescriptor)

Example 4 with ModelBuilder

use of com.android.tools.idea.uibuilder.fixtures.ModelBuilder in project android by JetBrains.

the class NlModelTest method testAddChild.

public void testAddChild() throws Exception {
    ModelBuilder modelBuilder = createDefaultModelBuilder(false);
    NlModel model = modelBuilder.build();
    assertEquals("NlComponent{tag=<LinearLayout>, bounds=[0,0:1000x1000, instance=0}\n" + "    NlComponent{tag=<TextView>, bounds=[100,100:100x100, instance=1}\n" + "    NlComponent{tag=<Button>, bounds=[100,200:100x100, instance=2}", myTreeDumper.toTree(model.getComponents()));
    // Add child
    ComponentDescriptor parent = modelBuilder.findByPath(LINEAR_LAYOUT);
    assertThat(parent).isNotNull();
    parent.addChild(component(EDIT_TEXT).withBounds(100, 100, 100, 100).width("100dp").height("100dp"), null);
    modelBuilder.updateModel(model, false);
    assertEquals("NlComponent{tag=<LinearLayout>, bounds=[0,0:1000x1000, instance=0}\n" + "    NlComponent{tag=<TextView>, bounds=[100,100:100x100, instance=1}\n" + "    NlComponent{tag=<Button>, bounds=[100,200:100x100, instance=2}\n" + "    NlComponent{tag=<EditText>, bounds=[100,100:100x100, instance=3}", myTreeDumper.toTree(model.getComponents()));
}
Also used : ModelBuilder(com.android.tools.idea.uibuilder.fixtures.ModelBuilder) ComponentDescriptor(com.android.tools.idea.uibuilder.fixtures.ComponentDescriptor)

Example 5 with ModelBuilder

use of com.android.tools.idea.uibuilder.fixtures.ModelBuilder in project android by JetBrains.

the class NlModelTest method testTransposeChildren.

public void testTransposeChildren() throws Exception {
    ModelBuilder modelBuilder = createDefaultModelBuilder(false);
    NlModel model = modelBuilder.build();
    assertEquals("NlComponent{tag=<LinearLayout>, bounds=[0,0:1000x1000, instance=0}\n" + "    NlComponent{tag=<TextView>, bounds=[100,100:100x100, instance=1}\n" + "    NlComponent{tag=<Button>, bounds=[100,200:100x100, instance=2}", myTreeDumper.toTree(model.getComponents()));
    // Remove last child
    ComponentDescriptor parent = modelBuilder.findByPath(LINEAR_LAYOUT);
    ComponentDescriptor button = modelBuilder.findByPath(LINEAR_LAYOUT, BUTTON);
    ComponentDescriptor textView = modelBuilder.findByPath(LINEAR_LAYOUT, TEXT_VIEW);
    assertThat(parent).isNotNull();
    parent.children(button, textView);
    modelBuilder.updateModel(model, false);
    assertEquals("NlComponent{tag=<LinearLayout>, bounds=[0,0:1000x1000, instance=0}\n" + "    NlComponent{tag=<Button>, bounds=[100,200:100x100, instance=2}\n" + "    NlComponent{tag=<TextView>, bounds=[100,100:100x100, instance=1}", myTreeDumper.toTree(model.getComponents()));
}
Also used : ModelBuilder(com.android.tools.idea.uibuilder.fixtures.ModelBuilder) ComponentDescriptor(com.android.tools.idea.uibuilder.fixtures.ComponentDescriptor)

Aggregations

ModelBuilder (com.android.tools.idea.uibuilder.fixtures.ModelBuilder)29 NlModel (com.android.tools.idea.uibuilder.model.NlModel)19 ComponentDescriptor (com.android.tools.idea.uibuilder.fixtures.ComponentDescriptor)12 NlComponent (com.android.tools.idea.uibuilder.model.NlComponent)6 NotNull (org.jetbrains.annotations.NotNull)6 NlProperty (com.android.tools.idea.uibuilder.property.NlProperty)5 NlPropertyItem (com.android.tools.idea.uibuilder.property.NlPropertyItem)5 AttributeDefinition (org.jetbrains.android.dom.attrs.AttributeDefinition)5 ABSOLUTE_LAYOUT (com.android.SdkConstants.ABSOLUTE_LAYOUT)1 ViewInfo (com.android.ide.common.rendering.api.ViewInfo)1 Configuration (com.android.tools.idea.configurations.Configuration)1 BuildSettings (com.android.tools.idea.gradle.project.BuildSettings)1 BuildMode (com.android.tools.idea.gradle.util.BuildMode)1 TagSnapshot (com.android.tools.idea.rendering.TagSnapshot)1 LayoutTestCase (com.android.tools.idea.uibuilder.LayoutTestCase)1 IdeEventQueue (com.intellij.ide.IdeEventQueue)1 Document (com.intellij.openapi.editor.Document)1 Project (com.intellij.openapi.project.Project)1 Disposer (com.intellij.openapi.util.Disposer)1 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)1