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