aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/components/upload/TestUploadAndDisableOnSuccess.java
blob: 516ca2d7c93e9577252ed38ca3a31ef822402b36 (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
package com.vaadin.tests.components.upload;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import com.vaadin.tests.components.ComponentTestCase;
import com.vaadin.ui.Component;
import com.vaadin.ui.Label;
import com.vaadin.ui.Upload;
import com.vaadin.ui.Upload.FinishedEvent;
import com.vaadin.ui.Upload.Receiver;
import com.vaadin.ui.Upload.StartedEvent;

public class TestUploadAndDisableOnSuccess extends ComponentTestCase<Upload>
        implements Receiver {

    @Override
    protected String getDescription() {
        return "If upload is detached and attached during upload, the client side componenent never receives information that the upload has finished. Second update will not be successful.";
    }

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

    @Override
    protected Class<Upload> getTestClass() {
        return Upload.class;
    }

    int counter = 0;
    private Label l;

    @Override
    protected void initializeComponents() {

        final Label labe = new Label();

        addComponent(labe);

        final Upload u;
        u = new Upload(null, this);
        u.setImmediate(true);
        addTestComponent(u);

        l = new Label(getUploadcount());
        addComponent(l);

        u.addListener(new Upload.StartedListener() {

            @Override
            public void uploadStarted(StartedEvent event) {
                /*
                 * Remove component before upload from the same vertical layout.
                 * Causes upload to be detached/attached -> upload loses it
                 * target iframes onload listener -> puts VUpload inappropriate
                 * state.
                 */
                getLayout().removeComponent(labe);
            }
        });

        u.addListener(new Upload.FinishedListener() {
            @Override
            public void uploadFinished(FinishedEvent event) {
                getMainWindow().showNotification("Done");
                l.setValue(getUploadcount());
            }
        });

    }

    private String getUploadcount() {
        return counter++ + " uploads";
    }

    @Override
    protected List<Component> createActions() {
        List<Component> actions = new ArrayList<Component>();
        actions.add(createButtonAction("Toggle Enabled",
                new Command<Upload, Boolean>() {

                    @Override
                    public void execute(Upload c, Boolean value, Object data) {
                        c.setEnabled(!c.isEnabled());
                    }
                }));

        return actions;
    }

    @Override
    public OutputStream receiveUpload(String filename, String MIMEType) {
        // sleep to ensure change before upload is complete
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new ByteArrayOutputStream();
    }

}