aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/components/grid/GridBasicFeaturesTest.java
blob: 73bef67c326bacec8ed30e7a3051057ebd0f8f2b (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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
 * Copyright 2000-2013 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.components.grid;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.List;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

import com.vaadin.tests.tb3.MultiBrowserTest;

/**
 * 
 * @since
 * @author Vaadin Ltd
 */
public class GridBasicFeaturesTest extends MultiBrowserTest {

    @Test
    public void testColumnHeaderCaptions() throws Exception {
        openTestURL();

        // Column headers should be visible
        List<WebElement> cells = getGridHeaderRowCells();
        assertEquals(10, cells.size());
        assertEquals("Column0", cells.get(0).getText());
        assertEquals("Column1", cells.get(1).getText());
        assertEquals("Column2", cells.get(2).getText());
    }

    @Test
    public void testColumnFooterCaptions() throws Exception {
        openTestURL();

        // footer row should by default be hidden
        List<WebElement> cells = getGridFooterRowCells();
        assertEquals(0, cells.size());

        // Open footer row
        selectMenuPath("Component", "Footers", "Visible");

        // Footers should now be visible
        cells = getGridFooterRowCells();
        assertEquals(10, cells.size());
        assertEquals("Footer 0", cells.get(0).getText());
        assertEquals("Footer 1", cells.get(1).getText());
        assertEquals("Footer 2", cells.get(2).getText());
    }

    @Test
    public void testColumnGroupHeaders() throws Exception {
        openTestURL();

        // Hide column headers for this test
        selectMenuPath("Component", "Headers", "Visible");

        List<WebElement> cells = getGridHeaderRowCells();

        // header row should be empty
        assertEquals(0, cells.size());

        // add a group row
        selectMenuPath("Component", "Column groups", "Add group row");

        // Empty group row cells should be present
        cells = getGridHeaderRowCells();
        assertEquals(10, cells.size());

        // Group columns 0 & 1
        selectMenuPath("Component", "Column groups", "Column group row 1",
                "Group Column 0 & 1");

        cells = getGridHeaderRowCells();
        assertEquals("Column 0 & 1", cells.get(0).getText());
    }

    @Test
    public void testColumnGroupFooters() throws Exception {
        openTestURL();

        // add a group row
        selectMenuPath("Component", "Column groups", "Add group row");

        // Set footer visible
        selectMenuPath("Component", "Column groups", "Column group row 1",
                "Footer Visible");

        // Group columns 0 & 1
        selectMenuPath("Component", "Column groups", "Column group row 1",
                "Group Column 0 & 1");

        List<WebElement> cells = getGridFooterRowCells();
        assertEquals("Column 0 & 1", cells.get(0).getText());
    }

    @Test
    public void testGroupingSameColumnsOnRowThrowsException() throws Exception {
        openTestURL();

        // add a group row
        selectMenuPath("Component", "Column groups", "Add group row");

        // Group columns 0 & 1
        selectMenuPath("Component", "Column groups", "Column group row 1",
                "Group Column 0 & 1");

        // Group columns 1 & 2 shoud fail
        selectMenuPath("Component", "Column groups", "Column group row 1",
                "Group Column 1 & 2");

        assertTrue(getLogRow(0)
                .contains(
                        "Exception occured, java.lang.IllegalArgumentExceptionColumn Column1 already belongs to another group."));
    }

    @Test
    public void testHidingColumn() throws Exception {
        openTestURL();

        // Column 0 should be visible
        List<WebElement> cells = getGridHeaderRowCells();
        assertEquals("Column0", cells.get(0).getText());

        // Hide column 0
        selectMenuPath("Component", "Columns", "Column0", "Visible");

        // Column 1 should now be the first cell
        cells = getGridHeaderRowCells();
        assertEquals("Column1", cells.get(0).getText());
    }

    @Test
    public void testRemovingColumn() throws Exception {
        openTestURL();

        // Column 0 should be visible
        List<WebElement> cells = getGridHeaderRowCells();
        assertEquals("Column0", cells.get(0).getText());

        // Hide column 0
        selectMenuPath("Component", "Columns", "Column0", "Remove");

        // Column 1 should now be the first cell
        cells = getGridHeaderRowCells();
        assertEquals("Column1", cells.get(0).getText());
    }

    @Test
    public void testDataLoadingAfterRowRemoval() throws Exception {
        openTestURL();

        // Remove columns 2,3,4
        selectMenuPath("Component", "Columns", "Column2", "Remove");
        selectMenuPath("Component", "Columns", "Column3", "Remove");
        selectMenuPath("Component", "Columns", "Column4", "Remove");

        // Scroll so new data is lazy loaded
        scrollGridVerticallyTo(1000);

        // Let lazy loading do its job
        sleep(1000);

        // Check that row is loaded
        assertThat(getBodyCellByRowAndColumn(11, 1).getText(), not("..."));
    }

    @Test
    public void testFreezingColumn() throws Exception {
        openTestURL();

        // Freeze column 2
        selectMenuPath("Component", "Columns", "Column2", "Freeze");

        WebElement cell = getBodyCellByRowAndColumn(1, 1);
        assertTrue(cell.getAttribute("class").contains("frozen"));

        cell = getBodyCellByRowAndColumn(1, 2);
        assertTrue(cell.getAttribute("class").contains("frozen"));
    }

    @Test
    public void testInitialColumnWidths() throws Exception {
        openTestURL();

        WebElement cell = getBodyCellByRowAndColumn(1, 1);
        assertEquals(100, cell.getSize().getWidth());

        cell = getBodyCellByRowAndColumn(1, 2);
        assertEquals(150, cell.getSize().getWidth());

        cell = getBodyCellByRowAndColumn(1, 3);
        assertEquals(200, cell.getSize().getWidth());
    }

    @Test
    public void testColumnWidths() throws Exception {
        openTestURL();

        // Default column width is 100px
        WebElement cell = getBodyCellByRowAndColumn(1, 1);
        assertEquals(100, cell.getSize().getWidth());

        // Set first column to be 200px wide
        selectMenuPath("Component", "Columns", "Column0", "Column0 Width",
                "200px");

        cell = getBodyCellByRowAndColumn(1, 1);
        assertEquals(200, cell.getSize().getWidth());

        // Set second column to be 150px wide
        selectMenuPath("Component", "Columns", "Column1", "Column1 Width",
                "150px");
        cell = getBodyCellByRowAndColumn(1, 2);
        assertEquals(150, cell.getSize().getWidth());

        // Set first column to be auto sized (defaults to 100px currently)
        selectMenuPath("Component", "Columns", "Column0", "Column0 Width",
                "Auto");

        cell = getBodyCellByRowAndColumn(1, 1);
        assertEquals(100, cell.getSize().getWidth());
    }

    @Test
    public void testPrimaryStyleNames() throws Exception {
        openTestURL();

        // v-grid is default primary style namea
        assertPrimaryStylename("v-grid");

        selectMenuPath("Component", "State", "Primary style name",
                "v-escalator");
        assertPrimaryStylename("v-escalator");

        selectMenuPath("Component", "State", "Primary style name", "my-grid");
        assertPrimaryStylename("my-grid");

        selectMenuPath("Component", "State", "Primary style name", "v-grid");
        assertPrimaryStylename("v-grid");
    }

    /**
     * Test that the current view is updated when a server-side container change
     * occurs (without scrolling back and forth)
     */
    @Test
    public void testItemSetChangeEvent() throws Exception {
        openTestURL();

        final By newRow = By.xpath("//td[text()='newcell: 0']");

        assertTrue("Unexpected initial state", !elementIsFound(newRow));

        selectMenuPath("Component", "Body rows", "Add first row");
        assertTrue("Add row failed", elementIsFound(newRow));

        selectMenuPath("Component", "Body rows", "Remove first row");
        assertTrue("Remove row failed", !elementIsFound(newRow));
    }

    /**
     * Test that the current view is updated when a property's value is reflect
     * to the client, when the value is modified server-side.
     */
    @Test
    public void testPropertyValueChangeEvent() throws Exception {
        openTestURL();

        assertEquals("Unexpected cell initial state", "(0, 0)",
                getBodyCellByRowAndColumn(1, 1).getText());

        selectMenuPath("Component", "Body rows",
                "Modify first row (getItemProperty)");
        assertEquals("(First) modification with getItemProperty failed",
                "modified: 0", getBodyCellByRowAndColumn(1, 1).getText());

        selectMenuPath("Component", "Body rows",
                "Modify first row (getContainerProperty)");
        assertEquals("(Second) modification with getItemProperty failed",
                "modified: Column0", getBodyCellByRowAndColumn(1, 1).getText());
    }

    private boolean elementIsFound(By locator) {
        try {
            return driver.findElement(locator) != null;
        } catch (NoSuchElementException e) {
            return false;
        }
    }

    private void assertPrimaryStylename(String stylename) {
        assertTrue(getGridElement().getAttribute("class").contains(stylename));

        String tableWrapperStyleName = getTableWrapper().getAttribute("class");
        assertTrue(tableWrapperStyleName.contains(stylename + "-tablewrapper"));

        String hscrollStyleName = getHorizontalScroller().getAttribute("class");
        assertTrue(hscrollStyleName.contains(stylename + "-scroller"));
        assertTrue(hscrollStyleName
                .contains(stylename + "-scroller-horizontal"));

        String vscrollStyleName = getVerticalScroller().getAttribute("class");
        assertTrue(vscrollStyleName.contains(stylename + "-scroller"));
        assertTrue(vscrollStyleName.contains(stylename + "-scroller-vertical"));
    }

    private WebElement getBodyCellByRowAndColumn(int row, int column) {
        return getDriver().findElement(
                By.xpath("//div[@id='testComponent']//tbody/tr[" + row
                        + "]/td[" + column + "]"));
    }

    private void selectSubMenu(String menuCaption) {
        selectMenu(menuCaption);
        new Actions(getDriver()).moveByOffset(100, 0).build().perform();
    }

    private void selectMenu(String menuCaption) {
        getDriver().findElement(
                By.xpath("//span[text() = '" + menuCaption + "']")).click();
    }

    private void selectMenuPath(String... menuCaptions) {
        selectMenu(menuCaptions[0]);
        for (int i = 1; i < menuCaptions.length; i++) {
            selectSubMenu(menuCaptions[i]);
        }
    }

    private WebElement getVerticalScroller() {
        return getDriver().findElement(
                By.xpath("//div[@id='testComponent']/div[1]"));
    }

    private WebElement getHorizontalScroller() {
        return getDriver().findElement(
                By.xpath("//div[@id='testComponent']/div[2]"));
    }

    private WebElement getTableWrapper() {
        return getDriver().findElement(
                By.xpath("//div[@id='testComponent']/div[3]"));
    }

    private WebElement getGridElement() {
        return getDriver().findElement(By.id("testComponent"));
    }

    private List<WebElement> getGridHeaderRowCells() {
        return getDriver().findElements(
                By.xpath("//div[@id='testComponent']//thead//th"));
    }

    private List<WebElement> getGridFooterRowCells() {
        return getDriver().findElements(
                By.xpath("//div[@id='testComponent']//tfoot//td"));
    }

    private void scrollGridVerticallyTo(double px) {
        executeScript("arguments[0].scrollTop = " + px,
                getGridVerticalScrollbar());
    }

    private Object executeScript(String script, WebElement element) {
        @SuppressWarnings("hiding")
        final WebDriver driver = getDriver();
        if (driver instanceof JavascriptExecutor) {
            final JavascriptExecutor je = (JavascriptExecutor) driver;
            return je.executeScript(script, element);
        } else {
            throw new IllegalStateException("current driver "
                    + getDriver().getClass().getName() + " is not a "
                    + JavascriptExecutor.class.getSimpleName());
        }
    }

    private WebElement getGridVerticalScrollbar() {
        return getDriver()
                .findElement(
                        By.xpath("//div[contains(@class, \"v-grid-scroller-vertical\")]"));
    }
}