aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/layouts/layouttester/LayoutTesterApplicationTest.java
blob: 5d8a6e0e7afb438d2052ec047f11a400b6a35469 (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
/*
 * Copyright 2000-2014 Vaadin Ltd.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.vaadin.tests.layouts.layouttester;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.Select;

import com.vaadin.tests.tb3.MultiBrowserTest;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.VerticalLayout;

public class LayoutTesterApplicationTest extends MultiBrowserTest {

    @Override
    protected DesiredCapabilities getDesiredCapabilities() {
        DesiredCapabilities cap = new DesiredCapabilities(
                super.getDesiredCapabilities());
        cap.setCapability("nativeEvents", false);

        return cap;
    }

    Map<String, Integer> numberOfSubTests = new HashMap<String, Integer>();
    private Set<String> tableOrIconsTests = new HashSet<String>();

    {
        numberOfSubTests.put("getExpandRatiosTests", 3);
        numberOfSubTests.put("getLayoutSizingTests", 4);
        numberOfSubTests.put("getComponentAddReplaceMoveTests", 4);
        numberOfSubTests.put("getMarginSpacingTests", 4);
        numberOfSubTests.put("getComponentSizingTests", 3);

        tableOrIconsTests.add("getComponentSizingTests");
        tableOrIconsTests.add("getExpandRatiosTests");
        tableOrIconsTests.add("getLayoutSizingTests");
        tableOrIconsTests.add("getMarginSpacingTests");
        tableOrIconsTests.add("getIconsTests");

    }

    @Test
    public void verticalLayout() throws Exception {
        openTestURL();
        runTest(VerticalLayout.class);
    }

    @Test
    public void horizontalLayout() throws Exception {
        openTestURL();
        runTest(HorizontalLayout.class);
    }

    @Test
    public void gridLayout() throws Exception {
        numberOfSubTests.put("getComponentAddReplaceMoveTests", 6);
        numberOfSubTests.put("getComponentSizingTests", 4);
        numberOfSubTests.put("getExpandRatiosTests", 4);

        openTestURL();
        runTest(GridLayout.class);
    }

    private void runTest(Class<?> layoutClass) throws Exception {
        new Select(vaadinElementById("layoutSelect").findElement(
                By.xpath("select")))
                .selectByVisibleText(layoutClass.toString());

        for (String subTest : LayoutTesterApplication.layoutGetters) {
            compareScreen(subTest);
            Integer subTests = numberOfSubTests.get(subTest);
            if (subTests != null) {
                for (int i = 1; i <= subTests; i++) {
                    clickAndCompareScreen(subTest, "testButton" + i);
                }
            }

            hitButton("nextButton");
        }

    }

    /**
     * Clicks the button with the given id and compares the result to a
     * screenshot named 'screenshotPrefix'-buttonCaption.
     * 
     * @param screenshotPrefix
     * @param buttonId
     * @throws Exception
     */
    private void clickAndCompareScreen(String screenshotPrefix, String buttonId)
            throws Exception {
        hitButton(buttonId);

        if (needsDelayToStabilize(screenshotPrefix)) {
            // Table does some extra layout phase and TestBench does not always
            // take this into account, grabbing screenshots before the layout
            // phase is done (see #12866).
            sleep(500);
        }
        compareScreen(screenshotPrefix + "-"
                + sanitize(driver.findElement(By.id(buttonId)).getText()));
    }

    private boolean needsDelayToStabilize(String screenshotPrefix) {
        return tableOrIconsTests.contains(screenshotPrefix);
    }

    private String sanitize(String text) {
        return text.replace("%", "pct").replaceAll("[^a-zA-Z0-9]", "-");
    }
}