aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/resources/ResourceDownload.java
blob: e323f5285b0eb9f2f412f59746c2bbfdfbce156c (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
package com.vaadin.tests.resources;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import com.vaadin.server.StreamResource;
import com.vaadin.server.StreamResource.StreamSource;
import com.vaadin.tests.components.TestBase;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;

public class ResourceDownload extends TestBase {

    @Override
    public void setup() {

        Button b = new Button("Download (_new)", new ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                download("_new");
            }
        });
        addComponent(b);

        b = new Button("Download (_blank)", new ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                download("_blank");
            }
        });
        addComponent(b);

        b = new Button("Download ()", new ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                download("");
            }
        });
        addComponent(b);

        b = new Button("Download (_top)", new ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                download("_top");
            }
        });
        addComponent(b);

        b = new Button("Test", new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                ResourceDownload.this.getMainWindow().showNotification(
                        "Still working");
            }

        });
        addComponent(b);

    }

    protected void download(String target) {
        String filename = "filename";
        StreamResource streamResource = new StreamResource(new StreamSource() {

            @Override
            public InputStream getStream() {
                try {
                    return new FileInputStream("FIXME C:/temp/file.xls");
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    return null;
                }
            }
        }, filename + ".xls");
        streamResource.setCacheTime(5000); // no cache (<=0) does not work with
        // IE8
        streamResource.setMIMEType("application/x-msexcel");

        getMainWindow().open(streamResource, target);

    }

    @Override
    protected String getDescription() {
        return "Downloading with target _new should work, aswell as with target _blank and _top.";
    }

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