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.

GridColumnWidthsWithoutDataTest.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package com.vaadin.tests.components.grid;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.assertFalse;
  4. import static org.junit.Assert.assertTrue;
  5. import static org.junit.Assert.fail;
  6. import java.util.List;
  7. import org.junit.Test;
  8. import com.vaadin.testbench.elements.ButtonElement;
  9. import com.vaadin.testbench.elements.GridElement;
  10. import com.vaadin.testbench.elements.GridElement.GridCellElement;
  11. import com.vaadin.testbench.elements.NativeSelectElement;
  12. import com.vaadin.testbench.elements.NotificationElement;
  13. import com.vaadin.testbench.parallel.TestCategory;
  14. import com.vaadin.tests.tb3.SingleBrowserTest;
  15. @TestCategory("grid")
  16. public class GridColumnWidthsWithoutDataTest extends SingleBrowserTest {
  17. @Test
  18. public void testWidthsWhenAddingDataBack() {
  19. openTestURL();
  20. GridElement grid = $(GridElement.class).first();
  21. int[] baseWidths = getColWidths(grid);
  22. assertEquals("Sanity check", 2, baseWidths.length);
  23. assertTrue("Columns should not have equal width",
  24. Math.abs(baseWidths[0] - baseWidths[1]) > 2);
  25. removeData();
  26. assertSameWidths(baseWidths, getColWidths(grid));
  27. addData();
  28. assertSameWidths(baseWidths, getColWidths(grid));
  29. }
  30. @Test
  31. public void testWidthsWhenInitiallyEmpty() {
  32. setDebug(true);
  33. openTestURL();
  34. $(ButtonElement.class).caption("Recreate without data").first().click();
  35. GridElement grid = $(GridElement.class).first();
  36. int[] baseWidths = getColWidths(grid);
  37. assertEquals("Sanity check", 2, baseWidths.length);
  38. assertTrue("Columns should have roughly equal width",
  39. Math.abs(baseWidths[0] - baseWidths[1]) < 10);
  40. assertTrue("Columns should not have default widths",
  41. baseWidths[0] > 140);
  42. assertTrue("Columns should not have default widths",
  43. baseWidths[1] > 140);
  44. addData();
  45. assertSameWidths(baseWidths, getColWidths(grid));
  46. assertFalse("Notification was present",
  47. isElementPresent(NotificationElement.class));
  48. }
  49. @Test
  50. public void testMultiSelectWidths() {
  51. setDebug(true);
  52. openTestURL();
  53. $(NativeSelectElement.class).caption("Selection mode").first()
  54. .selectByText("MULTI");
  55. GridElement grid = $(GridElement.class).first();
  56. int sum = sumUsedWidths(grid);
  57. // 295 instead of 300 to avoid rounding issues
  58. assertTrue("Only " + sum + " out of 300px was used", sum > 295);
  59. $(ButtonElement.class).caption("Recreate without data").first().click();
  60. grid = $(GridElement.class).first();
  61. sum = sumUsedWidths(grid);
  62. // 295 instead of 300 to avoid rounding issues
  63. assertTrue("Only " + sum + " out of 300px was used", sum > 295);
  64. }
  65. private int sumUsedWidths(GridElement grid) {
  66. int sum = 0;
  67. for (int i : getColWidths(grid)) {
  68. sum += i;
  69. }
  70. return sum;
  71. }
  72. private static void assertSameWidths(int[] expected, int[] actual) {
  73. assertEquals("Arrays have differing lengths", expected.length,
  74. actual.length);
  75. for (int i = 0; i < expected.length; i++) {
  76. if (Math.abs(expected[i] - actual[i]) > 1) {
  77. fail("Differing sizes at index " + i + ". Expected "
  78. + expected[i] + " but got " + actual[i]);
  79. }
  80. }
  81. }
  82. private void removeData() {
  83. $(ButtonElement.class).caption("Remove data").first().click();
  84. }
  85. private void addData() {
  86. $(ButtonElement.class).caption("Add data").first().click();
  87. }
  88. private int[] getColWidths(GridElement grid) {
  89. List<GridCellElement> headerCells = grid.getHeaderCells(0);
  90. int[] widths = new int[headerCells.size()];
  91. for (int i = 0; i < widths.length; i++) {
  92. widths[i] = headerCells.get(i).getSize().getWidth();
  93. }
  94. return widths;
  95. }
  96. }