You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestLayoutPerformance.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package com.vaadin.tests.layouts;
  2. import com.vaadin.shared.ui.ContentMode;
  3. import com.vaadin.tests.components.TestBase;
  4. import com.vaadin.ui.AbstractComponent;
  5. import com.vaadin.ui.Button;
  6. import com.vaadin.ui.CheckBox;
  7. import com.vaadin.ui.Component;
  8. import com.vaadin.ui.CssLayout;
  9. import com.vaadin.ui.GridLayout;
  10. import com.vaadin.ui.Label;
  11. import com.vaadin.ui.Layout;
  12. import com.vaadin.ui.VerticalLayout;
  13. import com.vaadin.v7.ui.NativeSelect;
  14. import com.vaadin.v7.ui.TextField;
  15. public class TestLayoutPerformance extends TestBase {
  16. private NativeSelect ns;
  17. private int i;
  18. private NativeSelect ns2;
  19. private VerticalLayout testarea = new VerticalLayout();
  20. @Override
  21. protected String getDescription() {
  22. return "Test app to test simple rendering to various layouts.";
  23. }
  24. @Override
  25. protected Integer getTicketNumber() {
  26. return null;
  27. }
  28. @Override
  29. protected void setup() {
  30. Label label = new Label("<h1>CssLayout performance test.</h1>",
  31. ContentMode.HTML);
  32. getLayout().addComponent(label);
  33. label = new Label(
  34. "<em>Hint</em>. Use debug dialog to measure rendering times TODO: extend with size settings (to both layout and content).",
  35. ContentMode.HTML);
  36. getLayout().addComponent(label);
  37. ns = new NativeSelect("Select component to test");
  38. ns.addItem(CssLayout.class);
  39. ns.addItem(GridLayout.class);
  40. ns.addItem(VerticalLayout.class);
  41. ns.setNullSelectionAllowed(false);
  42. ns.setValue(CssLayout.class);
  43. ns2 = new NativeSelect("Select component to render inside layout.");
  44. ns2.addItem(Label.class);
  45. ns2.addItem(Button.class);
  46. ns2.setNullSelectionAllowed(false);
  47. ns2.setValue(Label.class);
  48. final TextField n = new TextField("Number of components");
  49. n.setValue("1000");
  50. final CheckBox cb = new CheckBox("Generate captions", false);
  51. Button b = new Button("Render component");
  52. b.addClickListener(event -> {
  53. int components = Integer.parseInt(n.getValue());
  54. Layout layout = getCurrentLayout();
  55. for (int i = 0; i < components; i++) {
  56. Component component = newTestComponent();
  57. if (cb.getValue()) {
  58. component.setCaption("caption " + i);
  59. }
  60. layout.addComponent(component);
  61. }
  62. testarea.removeAllComponents();
  63. testarea.addComponent(layout);
  64. });
  65. getLayout().addComponent(ns);
  66. getLayout().addComponent(ns2);
  67. getLayout().addComponent(n);
  68. getLayout().addComponent(cb);
  69. getLayout().addComponent(b);
  70. getLayout().addComponent(testarea);
  71. }
  72. private Layout getCurrentLayout() {
  73. Class<?> value = (Class<?>) ns.getValue();
  74. if (value == GridLayout.class) {
  75. return new GridLayout(10, 1);
  76. }
  77. try {
  78. return (Layout) value.newInstance();
  79. } catch (InstantiationException | IllegalAccessException e) {
  80. e.printStackTrace();
  81. }
  82. return null;
  83. }
  84. private Component newTestComponent() {
  85. Class<?> componentClass = (Class<?>) ns2.getValue();
  86. AbstractComponent newInstance = null;
  87. try {
  88. newInstance = (AbstractComponent) componentClass.newInstance();
  89. } catch (InstantiationException | IllegalAccessException e) {
  90. e.printStackTrace();
  91. }
  92. if (componentClass == Label.class) {
  93. ((Label) newInstance).setValue("Test l " + (i++));
  94. ((Label) newInstance).setSizeUndefined();
  95. } else {
  96. newInstance.setCaption("Test l " + (i++));
  97. }
  98. return newInstance;
  99. }
  100. }