aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/test/java/com/vaadin/tests/components/combobox/ComboBoxBackEndRequestsTest.java
blob: 73447a09c47f606afb2e777166a7df43d21172e2 (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
package com.vaadin.tests.components.combobox;

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

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

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

import com.vaadin.testbench.elements.ButtonElement;
import com.vaadin.testbench.elements.CheckBoxElement;
import com.vaadin.testbench.elements.ComboBoxElement;
import com.vaadin.tests.tb3.SingleBrowserTest;

public class ComboBoxBackEndRequestsTest extends SingleBrowserTest {

    @Before
    public void open() {
        openTestURL("?debug");
    }

    private void open(int pageLength, int items) {
        openTestURL(ComboBoxBackEndRequests.PAGE_LENGTH_REQUEST_PARAMETER + "="
                + pageLength + "&"
                + ComboBoxBackEndRequests.ITEMS_REQUEST_PARAMETER + "=" + items
                + "&restartApplication");
    }

    @Test
    public void testInitialLoad_onlySizeAndFirstItemsRequested() {
        verifyInitialLoadRequests();
    }

    @Test
    public void testOpeningDropDown_noRequests() {
        verifyInitialLoadRequests();

        clearLogs();
        openPopup();

        verifyNumberOrRequests(
                "opening drop down should not have caused requests", 0);

        // select something to close popup
        selectByClick("Item 2");

        verifyNumberOrRequests("selecting should have not caused requests", 0);

        openPopup();
        verifyNumberOrRequests(
                "opening drop down should not have caused requests", 0);
    }

    @Test
    public void testNoPaging_nullSelectionAllowed() {
        final int pageLength = 0;
        final int items = 20;
        open(pageLength, items);

        verifyInitialLoadRequests();

        clearLogs();

        openPopup();

        verifyNumberOrRequests("extra request expected", 1);
        // TODO with pageLength 0, the cache strategy forces to fetch the whole
        // set for opening popup
        verifyFetchRequest(0, 0, items, null);

        verifyPopupItems(true, 0, items - 1);

        // pop up status not shown for only one page!
    }

    @Test
    public void testNoPaging_nullSelectionDisallowed() {
        final int pageLength = 0;
        final int items = 20;
        open(pageLength, items);

        verifyInitialLoadRequests();

        clearLogs();

        triggerNullSelectionAllowed(false);

        openPopup();

        verifyNumberOrRequests("extra request expected", 1);
        // TODO the cache strategy forces to fetch the whole range, instead of
        // just the missing 40-50
        verifyFetchRequest(0, 0, items, null);

        verifyPopupItems(false, 0, items - 1);

        // pop up status not shown for only one page!
    }

    @Test
    public void testPagingWorks_nullSelectionAllowed_defaultSizes() {
        verifyPopupPages(ComboBoxBackEndRequests.DEFAULT_PAGE_LENGTH,
                ComboBoxBackEndRequests.DEFAULT_NUMBER_OF_ITEMS, true);
    }

    @Test
    public void testPagingWorks_nullSelectionDisallowed_defaultSizes() {
        triggerNullSelectionAllowed(false);

        verifyPopupPages(ComboBoxBackEndRequests.DEFAULT_PAGE_LENGTH,
                ComboBoxBackEndRequests.DEFAULT_NUMBER_OF_ITEMS, false);
    }

    @Test
    public void testInitialPage_pageLengthBiggerThanInitialCache() {
        // initial request is for 40 items
        final int pageLength = 50;
        final int items = 100;

        open(pageLength, items);

        verifyNumberOrRequests("three initial requests expected", 3);
        verifySizeRequest(0, null);
        verifyFetchRequest(1, 0, 40, null);
        verifyFetchRequest(2, 40, 60, null);

        clearLogs();

        verifyPopupPages(pageLength, items, true);

        // browsing through the pages should't have caused more requests
        verifyNumberOrRequests("no additional requests should have happened",
                0);
    }

    @Test
    public void testPagingWorks_nullSelectionAllowed_customSizes() {
        final int pageLength = 23;
        final int items = 333;
        open(pageLength, items);

        // with null selection allowed
        verifyPopupPages(pageLength, items, true);
    }

    @Test
    public void testPagingWorks_nullSelectionDisallowed_customSizes() {
        final int pageLength = 23;
        final int items = 333;
        open(pageLength, items);

        triggerNullSelectionAllowed(false);

        verifyPopupPages(pageLength, items, false);
    }

    @Test
    @Ignore("cache strategy is still broken for CB")
    public void testPaging_usesCachedData() {
        verifyInitialLoadRequests();
        clearLogs();
        openPopup();
        verifyNumberOrRequests(
                "opening drop down should not have caused requests", 0);

        nextPage();

        verifyNumberOrRequests("next page should have not caused more requests",
                0);
    }

    @Test
    public void testPaging_nullSelectionAllowed_correctNumberOfItemsShown() {
        verifyInitialLoadRequests();
        openPopup();

        verifyPopupItems(true, 0, 8);

        nextPage();

        verifyPopupItems(false, 9, 18);
    }

    @Test
    public void testPaging_nullSelectionDisallowed_correctNumberOfItemsShown() {
        verifyInitialLoadRequests();
        triggerNullSelectionAllowed(false);

        openPopup();

        verifyPopupItems(false, 0, 9);

        nextPage();

        verifyPopupItems(false, 10, 19);
    }

    private void triggerNullSelectionAllowed(boolean expectedValue) {
        $(CheckBoxElement.class).caption("emptySelectionAllowed").first()
                .click();
        assertEquals(expectedValue, $(CheckBoxElement.class)
                .caption("emptySelectionAllowed").first().isChecked());
    }

    private void clearLogs() {
        $(ButtonElement.class).caption("Clear logs").first().click();
        verifyNumberOrRequests("logs should have been cleared", 0);
    }

    private void selectByClick(String itemText) {
        $(ComboBoxElement.class).first().getPopupSuggestionElements().stream()
                .filter(element -> element.getText().equals(itemText))
                .findFirst().get().click();
        assertEquals("Wrong selected", itemText,
                $(ComboBoxElement.class).first().getValue());
    }

    private void nextPage() {
        assertTrue("no next page available",
                $(ComboBoxElement.class).first().openNextPage());
    }

    private void previousPage() {
        assertTrue("no previous page available",
                $(ComboBoxElement.class).first().openPrevPage());
    }

    private void openPopup() {
        ComboBoxElement element = $(ComboBoxElement.class).first();
        assertFalse("popup shouldn't be open", element.isPopupOpen());

        element.getSuggestionPopup();

        assertTrue("popup should be open", element.isPopupOpen());
    }

    private void verifyPopupPages(int pageSize, int totalItems,
            boolean nullSelectionAllowed) {
        final int numberOfPages = new BigDecimal(
                totalItems + (nullSelectionAllowed ? 1 : 0))
                        .divide(new BigDecimal(pageSize), RoundingMode.UP)
                        .intValue();
        openPopup();

        verifyPopupStatus(pageSize, 1, numberOfPages, totalItems,
                nullSelectionAllowed);

        for (int i = 2; i <= numberOfPages; i++) {
            nextPage();
            verifyPopupStatus(pageSize, i, numberOfPages, totalItems,
                    nullSelectionAllowed);
        }

        assertFalse("there should not be a next page",
                $(ComboBoxElement.class).first().openNextPage());

        if (numberOfPages < 2) {
            return;
        }
        for (int i = numberOfPages - 1; i > 0; i--) {
            previousPage();
            verifyPopupStatus(pageSize, i, numberOfPages, totalItems,
                    nullSelectionAllowed);
        }

        assertFalse("there should not be a previous page",
                $(ComboBoxElement.class).first().openPrevPage());
    }

    private void verifyPopupStatus(int pageSize, int currentPage,
            int numberOfPages, int totalItems, boolean nullSelectionAllowed) {
        int start;
        int end;

        if (currentPage == 1) { // first page
            start = 1;
            end = pageSize - (nullSelectionAllowed ? 1 : 0);
        } else if (currentPage == numberOfPages) { // last page
            final int lastPageSize = (totalItems
                    + (nullSelectionAllowed ? 1 : 0)) % pageSize;

            start = pageSize * (currentPage - 1)
                    + (nullSelectionAllowed ? 0 : 1);
            end = start + (lastPageSize == 0 ? pageSize : lastPageSize) - 1;
        } else { // all other
            start = pageSize * (currentPage - 1)
                    + (nullSelectionAllowed ? 0 : 1);
            end = pageSize * currentPage - (nullSelectionAllowed ? 1 : 0);
        }

        verifyPopupStatus(start, end, totalItems, currentPage, numberOfPages);
    }

    private void verifyPopupStatus(int start, int end, int totalItems,
            int currentPage, int numberOfPages) {
        WebElement element = findElement(By.className("v-filterselect-status"));
        assertEquals(
                "Wrong status text on popup page " + currentPage + "/"
                        + numberOfPages,
                start + "-" + end + "/" + totalItems, element.getText());
    }

    private void verifyPopupItems(boolean hasNullSelectionItem, int startIndex,
            int lastIndex) {
        List<String> popupSuggestions = $(ComboBoxElement.class).first()
                .getPopupSuggestions();
        if (hasNullSelectionItem) {
            String text = popupSuggestions.remove(0);
            assertEquals("nullSelectionItem should be visible on page", " ",
                    text);
        }
        assertEquals("invalid number of suggestions",
                lastIndex - startIndex + 1, popupSuggestions.size());
        for (int i = startIndex; i <= lastIndex; i++) {
            assertEquals("unexpected item", "Item " + i,
                    popupSuggestions.get(i - startIndex));
        }
    }

    private void verifyInitialLoadRequests() {
        verifyNumberOrRequests("two initial requests expected", 2);
        verifySizeRequest(0, null);
        verifyFetchRequest(1, 0, 40, null);
    }

    private WebElement verifyFetchRequest(int requestNumber, int startIndex,
            int size, String filter) {
        WebElement element = verifyRequest(requestNumber, "FETCH");
        // format is 1: FETCH 0 40 _filter_
        final String actual = element.getText();
        String expected = requestNumber + ": FETCH " + startIndex + " " + size
                + (filter == null ? "" : " " + filter);
        assertEquals("invalid fetch data", expected, actual);
        return element;

    }

    private WebElement verifySizeRequest(int index, String filter) {
        WebElement element = verifyRequest(index, "SIZE");
        if (filter != null) {
            // format is "0: SIZE 0 2147483647 _filter_
            String currentFilter = element.getText().split("2147483647")[1];
            assertEquals("Wrong filter", filter, currentFilter);
        }
        return element;
    }

    private WebElement verifyRequest(int index, String queryType) {
        WebElement element = findElement(By.id(queryType + "-" + index));
        return element;
    }

    private void verifyNumberOrRequests(String message, int numberOfRequests) {
        List<WebElement> logRows = getLogRows();
        assertEquals(message, numberOfRequests, logRows.size());
    }

    private List<WebElement> getLogRows() {
        return findElements(By.className("log"));
    }
}