aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/test/java/com/vaadin/tests/components/window/WindowOrderTest.java
blob: 9e10c5bb95a2273193ae4951140284754341aa75 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.vaadin.tests.components.window;

import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.vaadin.tests.tb3.MultiBrowserTest;

/**
 * Test for window order position access.
 *
 * @author Vaadin Ltd
 */
public class WindowOrderTest extends MultiBrowserTest {

    @Test
    public void orderGetterTest() {
        openTestURL();

        checkPositionsAfterFirstWindowActivation();

        checkPositionsAfterActivationThirdFirstSecond();

        checkPositionsAfterDetachingThirdWindow();

        checkPositionsAfterNewWindowAttach();
    }

    private void checkPositionsAfterFirstWindowActivation() {
        // Bring the first window to front and check order positions of the
        // windows
        findElement(By.className("bring-to-front-first")).click();
        assertTrue(
                "The first window has wrong order position after bring first to front",
                hasOrder("window1", 2));
        assertTrue(
                "The first window position is incorrectly updated via UI listener after bring first to front",
                hasOrderInUi("window1", 2));
        assertTrue(
                "The second window has wrong order position after bring first to front",
                hasOrder("window2", 0));
        assertTrue(
                "The second window position is incorrectly updated via UI after bring first to front",
                hasOrderInUi("window2", 0));
        assertTrue(
                "The third window has wrong order position after bring first to front",
                hasOrder("window3", 1));
        assertTrue(
                "The third window position is incorrectly updated via UI after bring first to front",
                hasOrderInUi("window3", 1));
        assertTrue(
                "Last window is not attached and should have '-1' position, but hasn't.",
                lastWindowHasOrder(-1));
    }

    private void checkPositionsAfterActivationThirdFirstSecond() {
        // Bring third, first and second window at once (exactly in this order)
        // to front and check order positions of the
        // windows
        findElement(By.className("bring-to-front-all")).click();

        assertTrue(
                "The first window has wrong order position after bring all to front",
                hasOrder("window2", 2));
        assertTrue(
                "The first window position is incorrectly updated via UI after bring all to front",
                hasOrderInUi("window2", 2));
        assertTrue(
                "The second window has wrong order position after bring all to front",
                hasOrder("window1", 1));
        assertTrue(
                "The second window position is incorrectly updated via UI after bring all to front",
                hasOrderInUi("window1", 1));
        assertTrue(
                "The third window has wrong order position after bring all to front",
                hasOrder("window3", 0));
        assertTrue(
                "The third window position is incorrectly updated via UI after bring all to front",
                hasOrderInUi("window3", 0));
        assertTrue(
                "Last window is not attached and should have '-1' position, but hasn't.",
                lastWindowHasOrder(-1));
    }

    private void checkPositionsAfterDetachingThirdWindow() {
        // Detach third window and check order positions of the
        // windows
        findElement(By.className("detach-window")).click();

        assertTrue(
                "The first window has wrong order position after detach last window",
                hasOrder("window2", 1));
        assertTrue(
                "The first window position is incorrectly updated after detach last window",
                hasOrderInUi("window2", 1));
        assertTrue(
                "The second window has wrong order position after detach last window",
                hasOrder("window1", 0));
        assertTrue(
                "The second window position is incorrectly updated after detach last window",
                hasOrderInUi("window1", 0));
        WebElement thirdWindowInfo = findElement(By.className("w3-detached"));
        assertTrue("The third window has wrong order after detach",
                thirdWindowInfo.getAttribute("class").contains("w3--1"));
        assertTrue(
                "The third window position is incorrectly updated after detach last window",
                hasOrderInUi("window3", -1));
        assertTrue(
                "Last window is not attached and should have '-1' position, but hasn't.",
                lastWindowHasOrder(-1));
    }

    private void checkPositionsAfterNewWindowAttach() {
        // Attach new window and check order positions of the
        // windows
        findElement(By.className("add-window")).click();

        assertTrue(
                "The first window has wrong order position after add new window",
                hasOrder("window2", 1));
        assertTrue(
                "The first window position is incorrectly updated after add new window",
                hasOrderInUi("window2", 1));
        assertTrue(
                "The second window has wrong order position after add new window",
                hasOrder("window1", 0));
        assertTrue(
                "The second window position is incorrectly updated after add new window",
                hasOrderInUi("window1", 0));
        assertTrue(
                "The last window has wrong order position after add new window",
                hasOrder("window4", 2));
        assertTrue(
                "The last window position is incorrectly updated after add new window",
                hasOrderInUi("window4", 2));
    }

    private WebElement findElement(String styleName) {
        return findElement(By.className(styleName));
    }

    private boolean hasOrder(String window, int order) {
        WebElement win = findElement(window);
        WebElement content = win.findElement(By.className("v-label"));
        return content.getText().equals(String.valueOf(order)) && content
                .getAttribute("class").contains("event-order" + order);
    }

    private boolean hasOrderInUi(String window, int order) {
        WebElement uiLabel = findElement(By.className("ui-label"));
        return uiLabel.getAttribute("class").contains(window + '-' + order);
    }

    private boolean lastWindowHasOrder(int order) {
        WebElement info = findElement("info-label");
        String clazz = info.getAttribute("class");
        String style = "w4-" + order;
        boolean hasOrder = clazz.contains(style);
        if (!hasOrder) {
            return false;
        }
        clazz = clazz.replace(style, "");
        return !clazz.contains("w4");
    }

}