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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.components.grid;
  17. import java.util.List;
  18. import org.junit.Assert;
  19. import org.junit.Test;
  20. import com.vaadin.testbench.elements.ButtonElement;
  21. import com.vaadin.testbench.elements.GridElement;
  22. import com.vaadin.testbench.elements.GridElement.GridCellElement;
  23. import com.vaadin.testbench.elements.NativeSelectElement;
  24. import com.vaadin.testbench.elements.NotificationElement;
  25. import com.vaadin.testbench.parallel.TestCategory;
  26. import com.vaadin.tests.tb3.SingleBrowserTest;
  27. @TestCategory("grid")
  28. public class GridColumnWidthsWithoutDataTest extends SingleBrowserTest {
  29. @Test
  30. public void testWidthsWhenAddingDataBack() {
  31. openTestURL();
  32. GridElement grid = $(LegacyGridElement.class).first();
  33. int[] baseWidths = getColWidths(grid);
  34. Assert.assertEquals("Sanity check", 2, baseWidths.length);
  35. Assert.assertTrue("Columns should not have equal width",
  36. Math.abs(baseWidths[0] - baseWidths[1]) > 2);
  37. removeData();
  38. assertSameWidths(baseWidths, getColWidths(grid));
  39. addData();
  40. assertSameWidths(baseWidths, getColWidths(grid));
  41. }
  42. @Test
  43. public void testWidthsWhenInitiallyEmpty() {
  44. setDebug(true);
  45. openTestURL();
  46. $(ButtonElement.class).caption("Recreate without data").first().click();
  47. GridElement grid = $(LegacyGridElement.class).first();
  48. int[] baseWidths = getColWidths(grid);
  49. Assert.assertEquals("Sanity check", 2, baseWidths.length);
  50. Assert.assertTrue("Columns should have roughly equal width",
  51. Math.abs(baseWidths[0] - baseWidths[1]) < 10);
  52. Assert.assertTrue("Columns should not have default widths",
  53. baseWidths[0] > 140);
  54. Assert.assertTrue("Columns should not have default widths",
  55. baseWidths[1] > 140);
  56. addData();
  57. assertSameWidths(baseWidths, getColWidths(grid));
  58. Assert.assertFalse("Notification was present",
  59. isElementPresent(NotificationElement.class));
  60. }
  61. @Test
  62. public void testMultiSelectWidths() {
  63. setDebug(true);
  64. openTestURL();
  65. $(NativeSelectElement.class).caption("Selection mode").first()
  66. .selectByText("Multi");
  67. GridElement grid = $(LegacyGridElement.class).first();
  68. int sum = sumUsedWidths(grid);
  69. // 295 instead of 300 to avoid rounding issues
  70. Assert.assertTrue("Only " + sum + " out of 300px was used", sum > 295);
  71. $(ButtonElement.class).caption("Recreate without data").first().click();
  72. grid = $(LegacyGridElement.class).first();
  73. sum = sumUsedWidths(grid);
  74. // 295 instead of 300 to avoid rounding issues
  75. Assert.assertTrue("Only " + sum + " out of 300px was used", sum > 295);
  76. }
  77. private int sumUsedWidths(GridElement grid) {
  78. int sum = 0;
  79. for (int i : getColWidths(grid)) {
  80. sum += i;
  81. }
  82. return sum;
  83. }
  84. private static void assertSameWidths(int[] expected, int[] actual) {
  85. Assert.assertEquals("Arrays have differing lengths", expected.length,
  86. actual.length);
  87. for (int i = 0; i < expected.length; i++) {
  88. if (Math.abs(expected[i] - actual[i]) > 1) {
  89. Assert.fail("Differing sizes at index " + i + ". Expected "
  90. + expected[i] + " but got " + actual[i]);
  91. }
  92. }
  93. }
  94. private void removeData() {
  95. $(ButtonElement.class).caption("Remove data").first().click();
  96. }
  97. private void addData() {
  98. $(ButtonElement.class).caption("Add data").first().click();
  99. }
  100. private int[] getColWidths(GridElement grid) {
  101. List<GridCellElement> headerCells = grid.getHeaderCells(0);
  102. int[] widths = new int[headerCells.size()];
  103. for (int i = 0; i < widths.length; i++) {
  104. widths[i] = headerCells.get(i).getSize().getWidth();
  105. }
  106. return widths;
  107. }
  108. }