aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/test/java/com/vaadin/tests/components/textarea/TextAreaSetRowsTest.java
blob: d2f4f139b598bb97c6ec9199ce26ae8f67a0aede (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.vaadin.tests.components.textarea;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import com.vaadin.testbench.elements.ButtonElement;
import com.vaadin.testbench.elements.PanelElement;
import com.vaadin.testbench.elements.TextAreaElement;
import com.vaadin.tests.tb3.MultiBrowserTest;

public class TextAreaSetRowsTest extends MultiBrowserTest {
    private double rowHeight;
    private int scrollbarHeight;
    private int bordersAndPadding = 14; // fixed by Valo theme

    @Test
    public void testSetRows() {
        openTestURL();
        waitUntilLoadingIndicatorNotVisible();

        // calculate scrollbar height for comparison
        PanelElement panel = $(PanelElement.class).first();
        int panelHeight = panel.getSize().getHeight();
        // add scrollbar
        $(ButtonElement.class).caption(TextAreaSetRows.SCROLLB).first().click();
        waitUntilLoadingIndicatorNotVisible();
        scrollbarHeight = panel.getSize().getHeight() - panelHeight;
        assertGreater("Unexpected comparison scrollbar height", scrollbarHeight,
                0);

        TextAreaElement textArea = $(TextAreaElement.class).first();
        int height5 = textArea.getSize().getHeight();
        // calculate height of a single row
        rowHeight = (height5 - bordersAndPadding) / 5d;
        assertEquals("Unexpected initial height,", getExpected(5), height5, 1);

        $(ButtonElement.class).caption(TextAreaSetRows.ROWS_0).first().click();
        waitUntilLoadingIndicatorNotVisible();
        int height0 = textArea.getSize().getHeight();
        assertEquals("Unexpected 0 rows height,", getExpected(0), height0, 1);

        $(ButtonElement.class).caption(TextAreaSetRows.ROWS_4).first().click();
        waitUntilLoadingIndicatorNotVisible();
        int height4 = textArea.getSize().getHeight();
        assertEquals("Unexpected 4 rows height,", getExpected(4), height4, 1);

        $(ButtonElement.class).caption(TextAreaSetRows.ROWS_2).first().click();
        waitUntilLoadingIndicatorNotVisible();
        int height2 = textArea.getSize().getHeight();
        assertEquals("Unexpected 2 rows height,", getExpected(2), height2, 1);

        $(ButtonElement.class).caption(TextAreaSetRows.ROWS_1).first().click();
        waitUntilLoadingIndicatorNotVisible();
        int height1 = textArea.getSize().getHeight();
        assertEquals("Unexpected 1 rows height,", getExpected(1), height1, 1);

        assertEquals("Height mismatch for 0 and 1 rows", height0, height1, 1);

        // set fixed height to 0 (does not affect borders and padding)
        $(ButtonElement.class).caption(TextAreaSetRows.HEIGHT0).first().click();
        waitUntilLoadingIndicatorNotVisible();
        int heightFixed = textArea.getSize().getHeight();
        assertEquals("Unexpected fixed height,", bordersAndPadding, heightFixed,
                1);

        // remove fixed height, should return to height by rows
        $(ButtonElement.class).caption(TextAreaSetRows.HEIGHTR).first().click();
        waitUntilLoadingIndicatorNotVisible();
        int heightReset = textArea.getSize().getHeight();
        assertEquals("Unexpected 1 rows height,", height1, heightReset, 1);

        $(ButtonElement.class).caption(TextAreaSetRows.ROWS_3).first().click();
        waitUntilLoadingIndicatorNotVisible();
        int height3 = textArea.getSize().getHeight();
        assertEquals("Unexpected 3 rows height,", getExpected(3), height3, 1);

        // toggle off word wrap
        $(ButtonElement.class).caption(TextAreaSetRows.WWRAP).first().click();
        waitUntilLoadingIndicatorNotVisible();
        int newHeight3 = textArea.getSize().getHeight();
        // expected height to increase even without a scrollbar
        assertGreater("Unexpected 3 rows height without word wrap (short),",
                newHeight3, height3);

        // trigger horizontal scroll bar
        $(ButtonElement.class).caption(TextAreaSetRows.LONGS).first().click();
        waitUntilLoadingIndicatorNotVisible();
        // height should not have changed
        assertEquals("Unexpected 3 rows height without word wrap (long),",
                newHeight3, textArea.getSize().getHeight(), 1);

        // switch to longer contents with no breaks
        $(ButtonElement.class).caption(TextAreaSetRows.LONGN).first().click();
        waitUntilLoadingIndicatorNotVisible();
        // height should not have changed
        assertEquals(
                "Unexpected 3 rows height without word wrap (long without breaks),",
                newHeight3, textArea.getSize().getHeight(), 1);

        // ensure that the height difference to original matches a scrollbar
        // height, use a Panel's scrollbar as a comparison
        assertEquals("Unexpected textarea scrollbar height,", scrollbarHeight,
                newHeight3 - height3, 1);

        // toggle word wrap back on
        $(ButtonElement.class).caption(TextAreaSetRows.WWRAP).first().click();
        waitUntilLoadingIndicatorNotVisible();
        // height should have reverted to what it was before removing wrap
        assertEquals(
                "Unexpected 3 rows height with word wrap (long without breaks),",
                height3, textArea.getSize().getHeight(), 1);
    }

    /**
     * Calculates the expected height when horizontal scrollbar isn't possible.
     *
     * @param rows
     *            how many rows are displayed
     * @return expected text area height
     */
    private double getExpected(int rows) {
        // minimum row count is one
        return bordersAndPadding + (Math.max(1, rows) * rowHeight);
    }

}