aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/test/java/com/vaadin/tests/components/radiobuttongroup/RadioButtonGroupTest.java
blob: e027310ec99db6890193dfe7c1f7150fe634301a (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package com.vaadin.tests.components.radiobuttongroup;

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

import java.util.List;

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

import com.vaadin.icons.VaadinIcons;
import com.vaadin.testbench.By;
import com.vaadin.testbench.TestBenchElement;
import com.vaadin.testbench.elements.RadioButtonGroupElement;
import com.vaadin.tests.components.radiobutton.RadioButtonGroupTestUI;
import com.vaadin.tests.tb3.MultiBrowserTest;

/**
 * Test for RadioButtonGroup
 *
 * @author Vaadin Ltd
 * @since 8.0
 */
public class RadioButtonGroupTest extends MultiBrowserTest {

    @Before
    public void setUp() throws Exception {
        openTestURL();
    }

    @Test
    public void initialLoad_containsCorrectItems() {
        assertItems(20);
    }

    @Test
    public void initialItems_reduceItemCount_containsCorrectItems() {
        selectMenuPath("Component", "Data provider", "Items", "5");
        assertItems(5);
    }

    @Test
    public void disabled_reduceItemCount_containsCorrectItems() {
        selectMenuPath("Component", "State", "Enabled");
        selectMenuPath("Component", "Data provider", "Items", "5");
        assertItems(5);
    }

    @Test
    public void initialItems_increaseItemCount_containsCorrectItems() {
        selectMenuPath("Component", "Data provider", "Items", "100");
        assertItems(100);
    }

    @Test
    public void disabled_increaseItemCountWithinPushRows_containsCorrectItems() {
        selectMenuPath("Component", "Data provider", "Items", "5");
        selectMenuPath("Component", "State", "Enabled");
        selectMenuPath("Component", "Data provider", "Items", "20");
        assertItems(20);
    }

    @Test
    public void disabled_increaseItemCountBeyondPushRows_containsCorrectItems() {
        selectMenuPath("Component", "Data provider", "Items", "5");
        selectMenuPath("Component", "State", "Enabled");
        selectMenuPath("Component", "Data provider", "Items", "100");
        assertItems(100);
    }

    @Test
    public void clickToSelect() {
        selectMenuPath("Component", "Listeners", "Selection listener");

        getSelect().selectByText("Item 4");
        assertEquals("1. Selected: Optional[Item 4]", getLogRow(0));

        getSelect().selectByText("Item 2");
        assertEquals("2. Selected: Optional[Item 2]", getLogRow(0));

        getSelect().selectByText("Item 4");
        assertEquals("3. Selected: Optional[Item 4]", getLogRow(0));
    }

    @Test
    public void disabled_clickToSelect() {
        selectMenuPath("Component", "State", "Enabled");

        assertTrue(getSelect().findElements(By.tagName("input")).stream()
                .allMatch(element -> element.getAttribute("disabled") != null));

        selectMenuPath("Component", "Listeners", "Selection listener");

        String lastLogRow = getLogRow(0);

        getSelect().selectByText("Item 4");
        assertEquals(lastLogRow, getLogRow(0));

        getSelect().selectByText("Item 2");
        assertEquals(lastLogRow, getLogRow(0));

        getSelect().selectByText("Item 4");
        assertEquals(lastLogRow, getLogRow(0));
    }

    @Test // #9258
    public void disabled_correctClassNamesApplied() {
        openTestURL("theme=valo");
        selectMenuPath("Component", "State", "Enabled");

        List<WebElement> options = getSelect().findElements(By.tagName("span"));
        assertTrue(options.size() > 0);
        options.stream().map(element -> element.getAttribute("className"))
                .forEach(className -> verifyRadioButtonDisabledClassNames(
                        className, true));

        selectMenuPath("Component", "State", "Enabled");

        options = getSelect().findElements(By.tagName("span"));
        assertTrue(options.size() > 0);
        options.stream().map(element -> element.getAttribute("className"))
                .forEach(className -> verifyRadioButtonDisabledClassNames(
                        className, false));
    }

    @Test // #9258
    public void itemDisabledWithEnabledProvider_correctClassNamesApplied() {
        openTestURL("theme=valo");

        List<WebElement> options = getSelect().findElements(By.tagName("span"));

        assertTrue(options.size() > 0);
        options.stream().map(element -> element.getAttribute("className"))
                .forEach(cs -> verifyRadioButtonDisabledClassNames(cs, false));

        selectMenuPath("Component", "Item Enabled Provider",
                "Item Enabled Provider", "Disable Item 0");

        String className = getSelect().findElements(By.tagName("span")).get(0)
                .getAttribute("className");
        verifyRadioButtonDisabledClassNames(className, true);

        selectMenuPath("Component", "Item Enabled Provider",
                "Item Enabled Provider", "Disable Item 3");

        className = getSelect().findElements(By.tagName("span")).get(0)
                .getAttribute("className");
        verifyRadioButtonDisabledClassNames(className, false);

        className = getSelect().findElements(By.tagName("span")).get(3)
                .getAttribute("className");
        verifyRadioButtonDisabledClassNames(className, true);

        selectMenuPath("Component", "State", "Enabled");

        options = getSelect().findElements(By.tagName("span"));

        assertTrue(options.size() > 0);
        options.stream().map(element -> element.getAttribute("className"))
                .forEach(cs -> verifyRadioButtonDisabledClassNames(cs, true));

        selectMenuPath("Component", "Item Enabled Provider",
                "Item Enabled Provider", "Disable Item 5");

        options = getSelect().findElements(By.tagName("span"));

        assertTrue(options.size() > 0);
        options.stream().map(element -> element.getAttribute("className"))
                .forEach(cs -> verifyRadioButtonDisabledClassNames(cs, true));

        selectMenuPath("Component", "State", "Enabled");

        options = getSelect().findElements(By.tagName("span"));
        className = options.remove(5).getAttribute("className");

        assertTrue(options.size() > 0);
        options.stream().map(element -> element.getAttribute("className"))
                .forEach(cs -> verifyRadioButtonDisabledClassNames(cs, false));
        verifyRadioButtonDisabledClassNames(className, true);
    }

    @Test // #3387
    public void shouldApplySelectedClassToSelectedItems() {
        openTestURL("theme=valo");
        selectMenuPath("Component", "Selection", "Toggle Item 5");

        String className = getSelect().findElements(By.tagName("span")).get(5)
                .getAttribute("className");
        assertTrue("No v-select-option-selected class, was " + className,
                className.contains("v-select-option-selected"));

        getSelect().selectByText("Item 5");
        className = getSelect().findElements(By.tagName("span")).get(5)
                .getAttribute("className");
        assertTrue("No v-select-option-selected class, was " + className,
                className.contains("v-select-option-selected"));

        getSelect().selectByText("Item 10");
        List<WebElement> options = getSelect().findElements(By.tagName("span"));
        className = options.get(5).getAttribute("className");
        assertFalse("Extra v-select-option-selected class, was " + className,
                className.contains("v-select-option-selected"));
        className = options.get(10).getAttribute("className");
        assertTrue("No v-select-option-selected class, was " + className,
                className.contains("v-select-option-selected"));

        selectMenuPath("Component", "Selection", "Toggle Item 10");
        className = getSelect().findElements(By.tagName("span")).get(10)
                .getAttribute("className");
        assertFalse("Extra v-select-option-selected class, was " + className,
                className.contains("v-select-option-selected"));
    }

    @Test
    public void itemIconGenerator() {
        selectMenuPath("Component", "Item Icon Generator",
                "Use Item Icon Generator");
        assertItemsSuffices(20);

        List<WebElement> icons = getSelect()
                .findElements(By.cssSelector(".v-select-optiongroup .v-icon"));

        assertEquals(20, icons.size());

        for (int i = 0; i < icons.size(); i++) {
            assertEquals(VaadinIcons.values()[i + 1].getCodepoint(),
                    icons.get(i).getText().charAt(0));
        }
    }

    @Test
    public void clickToSelect_reenable() {
        selectMenuPath("Component", "State", "Enabled");
        selectMenuPath("Component", "Listeners", "Selection listener");

        getSelect().selectByText("Item 4");

        selectMenuPath("Component", "State", "Enabled");

        getSelect().selectByText("Item 5");
        assertEquals("3. Selected: Optional[Item 5]", getLogRow(0));

        getSelect().selectByText("Item 2");
        assertEquals("4. Selected: Optional[Item 2]", getLogRow(0));

        getSelect().selectByText("Item 4");
        assertEquals("5. Selected: Optional[Item 4]", getLogRow(0));
    }

    @Test
    public void itemCaptionGenerator() {
        selectMenuPath("Component", "Item Caption Generator",
                "Item Caption Generator", "Custom Caption Generator");
        assertItems(20, " Caption");
    }

    @Test
    public void nullItemCaptionGenerator() {
        selectMenuPath("Component", "Item Caption Generator",
                "Item Caption Generator", "Null Caption Generator");
        for (String text : getSelect().getOptions()) {
            assertEquals("", text);
        }
    }

    @Test
    public void selectProgramatically() {
        selectMenuPath("Component", "Listeners", "Selection listener");

        selectMenuPath("Component", "Selection", "Toggle Item 5");
        assertEquals("2. Selected: Optional[Item 5]", getLogRow(0));
        assertSelected("Item 5");

        selectMenuPath("Component", "Selection", "Toggle Item 1");
        assertEquals("4. Selected: Optional[Item 1]", getLogRow(0));
        // DOM order
        assertSelected("Item 1");

        selectMenuPath("Component", "Selection", "Toggle Item 5");
        assertEquals("6. Selected: Optional[Item 5]", getLogRow(0));
        assertSelected("Item 5");
    }

    @Test
    public void testItemDescriptionGenerators() {
        TestBenchElement label;

        selectMenuPath("Component", "Item Description Generator",
                "Item Description Generator", "Default Description Generator");

        label = (TestBenchElement) findElements(By.tagName("label")).get(5);
        label.showTooltip();
        assertEquals("Tooltip should contain the same text as caption",
                label.getText(), getTooltipElement().getText());

        selectMenuPath("Component", "Item Description Generator",
                "Item Description Generator", "Custom Description Generator");

        label = (TestBenchElement) findElements(By.tagName("label")).get(5);
        label.showTooltip();
        assertEquals("Tooltip should contain caption + ' Description'",
                label.getText() + " Description",
                getTooltipElement().getText());
    }

    private void assertSelected(String expectedSelection) {
        assertEquals(expectedSelection, getSelect().getValue());
    }

    @Override
    protected Class<?> getUIClass() {
        return RadioButtonGroupTestUI.class;
    }

    protected RadioButtonGroupElement getSelect() {
        return $(RadioButtonGroupElement.class).first();
    }

    protected void assertItems(int count) {
        assertItems(count, "");
    }

    protected void assertItems(int count, String suffix) {
        int i = 0;
        for (String text : getSelect().getOptions()) {
            assertEquals("Item " + i + suffix, text);
            i++;
        }
        assertEquals("Number of items", count, i);
    }

    protected void assertItemsSuffices(int count) {
        int i = 0;
        for (String text : getSelect().getOptions()) {
            assertTrue(text.endsWith("Item " + i));
            i++;
        }
        assertEquals("Number of items", count, i);
    }

    // needed to make tooltips work in IE tests
    @Override
    protected boolean requireWindowFocusForIE() {
        return true;
    }

    private static void verifyRadioButtonDisabledClassNames(String className,
            boolean disabled) {
        assertEquals(
                disabled ? "No"
                        : "Extra" + " v-radiobutton-disabled class, was "
                                + className,
                disabled, className.contains("v-radiobutton-disabled"));
        assertEquals(
                disabled ? "No"
                        : "Extra" + " v-disabled class, was " + className,
                disabled, className.contains("v-disabled"));
    }
}