aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/test/java/com/vaadin/tests/elements/tabsheet/TabSheetElementTabWithoutCaptionTest.java
blob: 06d08137afb8eb69e8d42448e3153700db53c1c5 (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
package com.vaadin.tests.elements.tabsheet;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.Arrays;
import java.util.List;

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

import com.vaadin.testbench.elements.TabSheetElement;
import com.vaadin.tests.tb3.MultiBrowserTest;

public class TabSheetElementTabWithoutCaptionTest extends MultiBrowserTest {

    @Before
    public void init() {
        openTestURL("theme=reindeer");
    }

    @Test
    public void openTabByCaption() {
        for (int i = 1; i <= 5; i++) {
            String caption = (i != 3 ? "Tab " + i : null);
            $(TabSheetElement.class).first().openTab(caption);
            checkSelectedTab(caption);
        }
    }

    @Test
    public void getCaptions() {
        List<String> expectedCaptions = Arrays.asList(
                new String[] { "Tab 1", "Tab 2", null, "Tab 4", "Tab 5" });
        List<String> actualCaptions = $(TabSheetElement.class).first()
                .getTabCaptions();
        assertEquals("Unexpected tab captions", expectedCaptions,
                actualCaptions);
    }

    @Test
    public void closeTabByCaption() {
        for (int i = 1; i <= 5; i++) {
            String caption = (i != 3 ? "Tab " + i : null);
            $(TabSheetElement.class).first().closeTab(caption);
            checkTabClosed(caption);
        }
    }

    @Test
    public void openTabByIndex() {
        int maxIndex = $(TabSheetElement.class).get(1).getTabCount();
        for (int i = 0; i < maxIndex; i++) {
            $(TabSheetElement.class).all().get(1).openTab(i);
            checkIconTabOpen(i);
        }
    }

    @Test
    public void closeTabByIndex() {
        $(TabSheetElement.class).get(1).closeTab(0);
        int numTabs = $(TabSheetElement.class).get(1)
                .findElements(By.className("v-tabsheet-tabitemcell")).size();
        assertEquals("The number of open tabs is incorrect", 4, numTabs);
        $(TabSheetElement.class).get(1).closeTab(3);
        numTabs = $(TabSheetElement.class).get(1)
                .findElements(By.className("v-tabsheet-tabitemcell")).size();
        assertEquals("The number of open tabs is incorrect", 3, numTabs);
        $(TabSheetElement.class).get(1).closeTab(2);
        numTabs = $(TabSheetElement.class).get(1)
                .findElements(By.className("v-tabsheet-tabitemcell")).size();
        assertEquals("The number of open tabs is incorrect", 2, numTabs);
    }

    private void checkSelectedTab(String caption) {
        // Check that the currently selected tab has the given caption.
        WebElement elem = $(TabSheetElement.class).first().getWrappedElement();
        List<WebElement> openTabs = elem
                .findElements(By.className("v-tabsheet-tabitem-selected"));
        assertTrue(
                "Exactly one tab should be open, but there are "
                        + openTabs.size() + " open tabs.",
                openTabs.size() == 1);
        WebElement tab = openTabs.get(0);
        List<WebElement> openTabCaptionElements = tab
                .findElement(By.className("v-caption"))
                .findElements(By.className("v-captiontext"));
        if (!openTabCaptionElements.isEmpty()) {
            String openTabCaption = openTabCaptionElements.get(0).getText();
            assertEquals("Wrong tab is open.", caption, openTabCaption);
        } else {
            assertEquals("Wrong tab is open.", caption, null);
        }
    }

    private void checkTabClosed(String caption) {
        List<String> openTabs = $(TabSheetElement.class).first()
                .getTabCaptions();
        assertFalse(
                "The tab with caption " + caption
                        + " is present, although it should have been closed.",
                openTabs.contains(caption));
    }

    private void checkIconTabOpen(int index) {
        List<WebElement> tabs = $(TabSheetElement.class).get(1)
                .findElements(By.className("v-tabsheet-tabitemcell"));
        boolean tabsOpen = false;
        for (int i = 0; i < tabs.size(); i++) {
            WebElement tab = tabs.get(i);
            boolean isOpened = !tab
                    .findElements(By.className("v-tabsheet-tabitem-selected"))
                    .isEmpty();
            if (isOpened) {
                tabsOpen = true;
                assertEquals("The wrong tab is open.", index, i);
            }
        }
        if (!tabsOpen) {
            fail("There are no tabs open, but tab with index " + index
                    + " should be open.");
        }
    }
}