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.

TextAreaSetRowsTest.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package com.vaadin.tests.components.textarea;
  2. import static org.junit.Assert.assertEquals;
  3. import org.junit.Test;
  4. import com.vaadin.testbench.elements.ButtonElement;
  5. import com.vaadin.testbench.elements.PanelElement;
  6. import com.vaadin.testbench.elements.TextAreaElement;
  7. import com.vaadin.tests.tb3.MultiBrowserTest;
  8. public class TextAreaSetRowsTest extends MultiBrowserTest {
  9. private double rowHeight;
  10. private int scrollbarHeight;
  11. private int bordersAndPadding = 14; // fixed by Valo theme
  12. @Test
  13. public void testSetRows() {
  14. openTestURL();
  15. waitUntilLoadingIndicatorNotVisible();
  16. // calculate scrollbar height for comparison
  17. PanelElement panel = $(PanelElement.class).first();
  18. int panelHeight = panel.getSize().getHeight();
  19. // add scrollbar
  20. $(ButtonElement.class).caption(TextAreaSetRows.SCROLLB).first().click();
  21. waitUntilLoadingIndicatorNotVisible();
  22. scrollbarHeight = panel.getSize().getHeight() - panelHeight;
  23. assertGreater("Unexpected comparison scrollbar height", scrollbarHeight,
  24. 0);
  25. TextAreaElement textArea = $(TextAreaElement.class).first();
  26. int height5 = textArea.getSize().getHeight();
  27. // calculate height of a single row
  28. rowHeight = (height5 - bordersAndPadding) / 5d;
  29. assertEquals("Unexpected initial height,", getExpected(5), height5, 1);
  30. $(ButtonElement.class).caption(TextAreaSetRows.ROWS_0).first().click();
  31. waitUntilLoadingIndicatorNotVisible();
  32. int height0 = textArea.getSize().getHeight();
  33. assertEquals("Unexpected 0 rows height,", getExpected(0), height0, 1);
  34. $(ButtonElement.class).caption(TextAreaSetRows.ROWS_4).first().click();
  35. waitUntilLoadingIndicatorNotVisible();
  36. int height4 = textArea.getSize().getHeight();
  37. assertEquals("Unexpected 4 rows height,", getExpected(4), height4, 1);
  38. $(ButtonElement.class).caption(TextAreaSetRows.ROWS_2).first().click();
  39. waitUntilLoadingIndicatorNotVisible();
  40. int height2 = textArea.getSize().getHeight();
  41. assertEquals("Unexpected 2 rows height,", getExpected(2), height2, 1);
  42. $(ButtonElement.class).caption(TextAreaSetRows.ROWS_1).first().click();
  43. waitUntilLoadingIndicatorNotVisible();
  44. int height1 = textArea.getSize().getHeight();
  45. assertEquals("Unexpected 1 rows height,", getExpected(1), height1, 1);
  46. assertEquals("Height mismatch for 0 and 1 rows", height0, height1, 1);
  47. // set fixed height to 0 (does not affect borders and padding)
  48. $(ButtonElement.class).caption(TextAreaSetRows.HEIGHT0).first().click();
  49. waitUntilLoadingIndicatorNotVisible();
  50. int heightFixed = textArea.getSize().getHeight();
  51. assertEquals("Unexpected fixed height,", bordersAndPadding, heightFixed,
  52. 1);
  53. // remove fixed height, should return to height by rows
  54. $(ButtonElement.class).caption(TextAreaSetRows.HEIGHTR).first().click();
  55. waitUntilLoadingIndicatorNotVisible();
  56. int heightReset = textArea.getSize().getHeight();
  57. assertEquals("Unexpected 1 rows height,", height1, heightReset, 1);
  58. $(ButtonElement.class).caption(TextAreaSetRows.ROWS_3).first().click();
  59. waitUntilLoadingIndicatorNotVisible();
  60. int height3 = textArea.getSize().getHeight();
  61. assertEquals("Unexpected 3 rows height,", getExpected(3), height3, 1);
  62. // toggle off word wrap
  63. $(ButtonElement.class).caption(TextAreaSetRows.WWRAP).first().click();
  64. waitUntilLoadingIndicatorNotVisible();
  65. int newHeight3 = textArea.getSize().getHeight();
  66. // expected height to increase even without a scrollbar
  67. assertGreater("Unexpected 3 rows height without word wrap (short),",
  68. newHeight3, height3);
  69. // trigger horizontal scroll bar
  70. $(ButtonElement.class).caption(TextAreaSetRows.LONGS).first().click();
  71. waitUntilLoadingIndicatorNotVisible();
  72. // height should not have changed
  73. assertEquals("Unexpected 3 rows height without word wrap (long),",
  74. newHeight3, textArea.getSize().getHeight(), 1);
  75. // switch to longer contents with no breaks
  76. $(ButtonElement.class).caption(TextAreaSetRows.LONGN).first().click();
  77. waitUntilLoadingIndicatorNotVisible();
  78. // height should not have changed
  79. assertEquals(
  80. "Unexpected 3 rows height without word wrap (long without breaks),",
  81. newHeight3, textArea.getSize().getHeight(), 1);
  82. // ensure that the height difference to original matches a scrollbar
  83. // height, use a Panel's scrollbar as a comparison
  84. assertEquals("Unexpected textarea scrollbar height,", scrollbarHeight,
  85. newHeight3 - height3, 1);
  86. // toggle word wrap back on
  87. $(ButtonElement.class).caption(TextAreaSetRows.WWRAP).first().click();
  88. waitUntilLoadingIndicatorNotVisible();
  89. // height should have reverted to what it was before removing wrap
  90. assertEquals(
  91. "Unexpected 3 rows height with word wrap (long without breaks),",
  92. height3, textArea.getSize().getHeight(), 1);
  93. }
  94. /**
  95. * Calculates the expected height when horizontal scrollbar isn't possible.
  96. *
  97. * @param rows
  98. * how many rows are displayed
  99. * @return expected text area height
  100. */
  101. private double getExpected(int rows) {
  102. // minimum row count is one
  103. return bordersAndPadding + (Math.max(1, rows) * rowHeight);
  104. }
  105. }