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

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

import org.junit.Test;

import com.vaadin.data.TreeData;
import com.vaadin.data.provider.TreeDataProvider;
import com.vaadin.event.CollapseEvent;
import com.vaadin.event.CollapseEvent.CollapseListener;
import com.vaadin.event.ExpandEvent;
import com.vaadin.event.ExpandEvent.ExpandListener;
import com.vaadin.server.ThemeResource;
import com.vaadin.ui.Tree;

public class TreeTest {

    public static final String TEST_CAPTION = "test caption";
    public static final String TEST_DESCRIPTION = "test description";
    public static final String TEST_RESOURCE_ID = "nothing.gif";

    private static class TreeCollapseExpandListener
            implements ExpandListener<String>, CollapseListener<String> {

        private boolean collapsed = false;
        private boolean expanded = false;
        private final Tree<String> tree;

        public TreeCollapseExpandListener(Tree<String> tree) {
            this.tree = tree;
        }

        @Override
        public void itemCollapse(CollapseEvent<String> event) {
            assertEquals("Source component was incorrect", tree,
                    event.getComponent());
            assertFalse("Multiple collapse events", collapsed);
            collapsed = true;
        }

        @Override
        public void itemExpand(ExpandEvent<String> event) {
            assertEquals("Source component was incorrect", tree,
                    event.getComponent());
            assertFalse("Multiple expand events", expanded);
            expanded = true;
        }

        public boolean isExpanded() {
            return expanded;
        }

        public boolean isCollapsed() {
            return collapsed;
        }
    }

    @Test
    public void event_source_is_tree() {
        Tree<String> tree = new Tree<>();
        TreeData<String> treeData = new TreeData<>();
        treeData.addItem(null, "Foo");
        treeData.addItem("Foo", "Bar");
        treeData.addItem("Foo", "Baz");
        tree.setDataProvider(new TreeDataProvider<>(treeData));

        TreeCollapseExpandListener listener = new TreeCollapseExpandListener(
                tree);
        tree.addExpandListener(listener);
        tree.addCollapseListener(listener);

        assertFalse(listener.isExpanded());
        tree.expand("Foo");
        assertTrue("Item not expanded", tree.isExpanded("Foo"));
        assertTrue("Expand event not fired", listener.isExpanded());
        assertFalse(listener.isCollapsed());
        tree.collapse("Foo");
        assertFalse("Item not collapsed", tree.isExpanded("Foo"));
        assertTrue("Collapse event not fired", listener.isCollapsed());
    }

    @Test
    public void testComponentProperties() {
        Tree<String> tree = new Tree<>();
        tree.setCaption(TEST_CAPTION);
        tree.setDescription(TEST_DESCRIPTION);
        tree.setIcon(new ThemeResource(TEST_RESOURCE_ID));

        assertEquals(TEST_CAPTION, tree.getCaption());
        assertEquals(TEST_DESCRIPTION, tree.getDescription());
        assertEquals(TEST_RESOURCE_ID, tree.getIcon().toString());

        assertFalse(tree.isCaptionAsHtml());
        tree.setCaptionAsHtml(true);
        assertTrue(tree.isCaptionAsHtml());
    }
}