Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

GridColumnWidthsWithoutDataTest.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright 2000-2014 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.tests.tb3.SingleBrowserTest;
  26. public class GridColumnWidthsWithoutDataTest extends SingleBrowserTest {
  27. @Test
  28. public void testWidthsWhenAddingDataBack() {
  29. openTestURL();
  30. GridElement grid = $(GridElement.class).first();
  31. int[] baseWidths = getColWidths(grid);
  32. Assert.assertEquals("Sanity check", 2, baseWidths.length);
  33. Assert.assertTrue("Columns should not have equal width",
  34. Math.abs(baseWidths[0] - baseWidths[1]) > 2);
  35. removeData();
  36. assertSameWidths(baseWidths, getColWidths(grid));
  37. addData();
  38. assertSameWidths(baseWidths, getColWidths(grid));
  39. }
  40. @Test
  41. public void testWidthsWhenInitiallyEmpty() {
  42. setDebug(true);
  43. openTestURL();
  44. $(ButtonElement.class).caption("Recreate without data").first().click();
  45. GridElement grid = $(GridElement.class).first();
  46. int[] baseWidths = getColWidths(grid);
  47. Assert.assertEquals("Sanity check", 2, baseWidths.length);
  48. Assert.assertTrue("Columns should have roughly equal width",
  49. Math.abs(baseWidths[0] - baseWidths[1]) < 10);
  50. Assert.assertTrue("Columns should not have default widths",
  51. baseWidths[0] > 140);
  52. Assert.assertTrue("Columns should not have default widths",
  53. baseWidths[1] > 140);
  54. addData();
  55. assertSameWidths(baseWidths, getColWidths(grid));
  56. Assert.assertFalse("Notification was present",
  57. isElementPresent(NotificationElement.class));
  58. }
  59. @Test
  60. public void testMultiSelectWidths() {
  61. setDebug(true);
  62. openTestURL();
  63. $(NativeSelectElement.class).caption("Selection mode").first()
  64. .selectByText("Multi");
  65. GridElement grid = $(GridElement.class).first();
  66. int sum = sumUsedWidths(grid);
  67. // 295 instead of 300 to avoid rounding issues
  68. Assert.assertTrue("Only " + sum + " out of 300px was used", sum > 295);
  69. $(ButtonElement.class).caption("Recreate without data").first().click();
  70. grid = $(GridElement.class).first();
  71. sum = sumUsedWidths(grid);
  72. // 295 instead of 300 to avoid rounding issues
  73. Assert.assertTrue("Only " + sum + " out of 300px was used", sum > 295);
  74. }
  75. private int sumUsedWidths(GridElement grid) {
  76. int sum = 0;
  77. for (int i : getColWidths(grid)) {
  78. sum += i;
  79. }
  80. return sum;
  81. }
  82. private static void assertSameWidths(int[] expected, int[] actual) {
  83. Assert.assertEquals("Arrays have differing lengths", expected.length,
  84. actual.length);
  85. for (int i = 0; i < expected.length; i++) {
  86. if (Math.abs(expected[i] - actual[i]) > 1) {
  87. Assert.fail("Differing sizes at index " + i + ". Expected "
  88. + expected[i] + " but got " + actual[i]);
  89. }
  90. }
  91. }
  92. private void removeData() {
  93. $(ButtonElement.class).caption("Remove data").first().click();
  94. }
  95. private void addData() {
  96. $(ButtonElement.class).caption("Add data").first().click();
  97. }
  98. private int[] getColWidths(GridElement grid) {
  99. List<GridCellElement> headerCells = grid.getHeaderCells(0);
  100. int[] widths = new int[headerCells.size()];
  101. for (int i = 0; i < widths.length; i++) {
  102. widths[i] = headerCells.get(i).getSize().getWidth();
  103. }
  104. return widths;
  105. }
  106. }