aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/test/java/com/vaadin/tests/components/grid/GridComponentRendererTest.java
blob: e8cfc63ec5ec2e7d6c17b0bb8d2c6fc991a32f1c (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
package com.vaadin.tests.components.grid;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

import org.junit.Before;
import org.junit.Test;

import com.vaadin.data.provider.DataProvider;
import com.vaadin.server.VaadinSession;
import com.vaadin.tests.data.bean.Person;
import com.vaadin.tests.util.AlwaysLockedVaadinSession;
import com.vaadin.tests.util.MockUI;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Label;

/**
 * Test to validate clean detaching in Grid with ComponentRenderer.
 */
public class GridComponentRendererTest {

    private static final Person PERSON = Person.createTestPerson1();
    private Grid<Person> grid;
    private List<Person> backend;
    private DataProvider<Person, ?> dataProvider;
    private Label testComponent;
    private Label oldComponent;

    @Before
    public void setUp() {
        VaadinSession.setCurrent(new AlwaysLockedVaadinSession(null));
        backend = new ArrayList<>();
        backend.add(PERSON);
        dataProvider = DataProvider.ofCollection(backend);
        grid = new Grid<>();
        grid.setDataProvider(dataProvider);
        grid.addComponentColumn(p -> {
            oldComponent = testComponent;
            testComponent = new Label();
            return testComponent;
        });
        new MockUI() {
            @Override
            public Future<Void> access(Runnable runnable) {
                runnable.run();
                return null;
            };
        }.setContent(grid);
    }

    @Test
    public void testComponentChangeOnRefresh() {
        generateDataForClient(true);
        dataProvider.refreshItem(PERSON);
        generateDataForClient(false);
        assertNotNull("Old component should exist.", oldComponent);
    }

    @Test
    public void testComponentChangeOnSelection() {
        generateDataForClient(true);
        grid.select(PERSON);
        generateDataForClient(false);
        assertNotNull("Old component should exist.", oldComponent);
    }

    @Test
    public void testComponentChangeOnDataProviderChange() {
        generateDataForClient(true);
        grid.setItems(PERSON);
        assertEquals("Test component was not detached on DataProvider change.",
                null, testComponent.getParent());
    }

    private void generateDataForClient(boolean initial) {
        grid.getDataCommunicator().beforeClientResponse(initial);
        if (testComponent != null) {
            assertEquals("New component was not attached.", grid,
                    testComponent.getParent());
        }
        if (oldComponent != null) {
            assertEquals("Old component was not detached.", null,
                    oldComponent.getParent());
        }
    }
}