summaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/themes/CSSInjectTest.java
blob: 738d8fc6817b652fdd642a1297ee2944e5c6790e (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
package com.vaadin.tests.themes;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.UUID;

import com.vaadin.server.Page;
import com.vaadin.server.Page.Styles;
import com.vaadin.server.StreamResource;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.tests.components.TestBase;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextArea;

public class CSSInjectTest extends TestBase {

    @Override
    protected void setup() {

        final Styles stylesheet = Page.getCurrent().getStyles();

        // Inject some resources initially
        final StreamResource initialResource = new StreamResource(
                new StreamResource.StreamSource() {

                    @Override
                    public InputStream getStream() {
                        return new ByteArrayInputStream(
                                ".hello, .world { color:silver; }".getBytes());
                    }
                }, "mystyles-" + System.currentTimeMillis() + ".css");
        stylesheet.add(initialResource);

        Label hello = new Label(
                "<span class='hello'>Hello</span> <span class='world'>world</span>",
                ContentMode.HTML);
        addComponent(hello);

        final TextArea cssToInject = new TextArea();
        cssToInject.setImmediate(true);
        addComponent(cssToInject);

        Button inject = new Button("Inject!", new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                stylesheet.add(cssToInject.getValue());
                cssToInject.setValue("");
            }
        });
        addComponent(inject);

        Button injectRandom = new Button("Inject as resource!",
                new Button.ClickListener() {

                    @Override
                    public void buttonClick(ClickEvent event) {

                        final String css = cssToInject.getValue();

                        stylesheet.add(new StreamResource(
                                new StreamResource.StreamSource() {

                                    @Override
                                    public InputStream getStream() {
                                        return new ByteArrayInputStream(css
                                                .getBytes());
                                    }
                                }, UUID.randomUUID().toString() + ".css"));

                        cssToInject.setValue("");
                    }
                });
        addComponent(injectRandom);

        addComponent(new Button("Inject initial again!",
                new Button.ClickListener() {
                    @Override
                    public void buttonClick(ClickEvent event) {
                        stylesheet.add(initialResource);
                    }
                }));
    }

    @Override
    protected String getDescription() {
        return "Demonstrates how CSS injections can be used to theme the \"Hello world\" label below";
    }

    @Override
    protected Integer getTicketNumber() {
        return 5500;
    }

}