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.

TableGeneratedRowsTest.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package com.vaadin.tests.components.table;
  2. import static org.junit.Assert.assertFalse;
  3. import static org.junit.Assert.assertTrue;
  4. import static org.junit.Assert.fail;
  5. import org.junit.Test;
  6. import org.openqa.selenium.By;
  7. import org.openqa.selenium.WebElement;
  8. import com.vaadin.testbench.TestBenchElement;
  9. import com.vaadin.testbench.elements.TableElement;
  10. import com.vaadin.tests.tb3.MultiBrowserTest;
  11. @SuppressWarnings("deprecation")
  12. public class TableGeneratedRowsTest extends MultiBrowserTest {
  13. @Override
  14. protected Class<?> getUIClass() {
  15. return Tables.class;
  16. }
  17. @Test
  18. public void changeRowGenerator() {
  19. openTestURL();
  20. TableElement table = $(TableElement.class).first();
  21. /* No row generator */
  22. TestBenchElement cell = table.getCell(4, 0);
  23. int tableWidth = table.getSize().getWidth();
  24. int cellWidth = cell.getSize().getWidth();
  25. int buffer = 40; // for paddings, borders, scrollbars, and bit extra
  26. int bufferedTableWidth = tableWidth - buffer;
  27. String text = cell.getText();
  28. assertFalse("Unexpected cell contents without applying row generator: "
  29. + text, text != null && text.startsWith("FOOBARBAZ"));
  30. assertLessThan(
  31. "Cell shouldn't be spanned without applying row generator: "
  32. + cellWidth + " isn't less than " + (tableWidth / 10),
  33. cellWidth, tableWidth / 10);
  34. /* Spanned row generator */
  35. selectMenuPath("Component", "Features", "Row generator",
  36. "Every fifth row, spanned");
  37. cell = table.getCell(4, 0);
  38. cellWidth = cell.getSize().getWidth();
  39. text = cell.getText();
  40. assertTrue(
  41. "Unexpected cell contents for spanned generated row: " + text,
  42. text != null && text.startsWith("FOOBARBAZ"));
  43. assertLessThanOrEqual(
  44. "Spanned cell isn't wide enough for spanned generated row: "
  45. + cellWidth + " isn't more than " + bufferedTableWidth,
  46. bufferedTableWidth, cellWidth);
  47. /* Non-spanned row generator */
  48. selectMenuPath("Component", "Features", "Row generator",
  49. "Every tenth row, no spanning");
  50. cell = table.getCell(4, 0);
  51. cellWidth = cell.getSize().getWidth();
  52. text = cell.getText();
  53. assertFalse(
  54. "Unexpected cell contents after replacing spanned row generator: "
  55. + text,
  56. text != null && text.startsWith("FOOBARBAZ"));
  57. assertLessThan(
  58. "Cell shouldn't be spanned anymore after replacing spanned row generator: "
  59. + cellWidth + " isn't less than " + (tableWidth / 10),
  60. cellWidth, tableWidth / 10);
  61. cell = table.getCell(9, 0);
  62. cellWidth = cell.getSize().getWidth();
  63. text = cell.getText();
  64. assertTrue("Unexpected cell contents for non-spanned generated row: "
  65. + text, text != null && text.startsWith("FOO0"));
  66. assertLessThan(
  67. "Unexpected cell width for non-spanned generated row: "
  68. + cellWidth + " isn't less than " + (tableWidth / 10),
  69. cellWidth, tableWidth / 10);
  70. /* Spanned and html formatted row generator */
  71. selectMenuPath("Component", "Features", "Row generator",
  72. "Every eight row, spanned, html formatted");
  73. cell = table.getCell(9, 0);
  74. cellWidth = cell.getSize().getWidth();
  75. text = cell.getText();
  76. assertFalse(
  77. "Unexpected cell contents after replacing non-spanned row generator: "
  78. + text,
  79. text != null && text.startsWith("FOO0"));
  80. assertLessThan(
  81. "Unexpected cell width after replacing non-spanned row generator: "
  82. + cellWidth + " isn't less than " + (tableWidth / 10),
  83. cellWidth, tableWidth / 10);
  84. cell = table.getCell(7, 0);
  85. cellWidth = cell.getSize().getWidth();
  86. text = cell.getText();
  87. assertTrue("Unexpected contents for spanned and html formatted cell: "
  88. + text, text != null && text.startsWith("FOO BAR BAZ"));
  89. assertLessThanOrEqual(
  90. "Spanned and html formatted cell isn't wide enough: "
  91. + cellWidth + " isn't more than " + bufferedTableWidth,
  92. bufferedTableWidth, cellWidth);
  93. WebElement bazSpan = cell.findElement(By.tagName("span"));
  94. if (bazSpan == null) {
  95. fail("Unexpected styling: no span found");
  96. } else {
  97. String bazStyle = bazSpan.getAttribute("style");
  98. assertTrue("Unexpected styling: " + bazStyle,
  99. bazStyle != null && bazStyle.contains("color: red"));
  100. }
  101. /* Spanned row generator for all rows */
  102. selectMenuPath("Component", "Features", "Row generator",
  103. "Every row, spanned");
  104. for (int i = 0; i < 10; ++i) {
  105. cell = table.getCell(i, 0);
  106. cellWidth = cell.getSize().getWidth();
  107. text = cell.getText();
  108. assertTrue(
  109. "Unexpected cell contents with every row spanned: " + text,
  110. text != null && text.startsWith("SPANNED"));
  111. assertLessThanOrEqual(
  112. "Spanned cell isn't wide enough with every row spanned: "
  113. + cellWidth + " isn't more than "
  114. + bufferedTableWidth,
  115. bufferedTableWidth, cellWidth);
  116. }
  117. /* No row generator */
  118. selectMenuPath("Component", "Features", "Row generator", "None");
  119. for (int i = 0; i < 10; ++i) {
  120. cell = table.getCell(i, 0);
  121. cellWidth = cell.getSize().getWidth();
  122. text = cell.getText();
  123. assertFalse(
  124. "Unexpected cell contents without row generator: " + text,
  125. text != null && text.startsWith("SPANNED"));
  126. assertLessThan(
  127. "Unexpected cell width without row generator: " + cellWidth
  128. + " isn't less than " + (tableWidth / 10),
  129. cellWidth, tableWidth / 10);
  130. }
  131. }
  132. }