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()));
}
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()));
}
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()));
}
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()));
}
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()));
}
Aggregations