summaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/push/EnableDisablePush.java
blob: 50dab436678e2d0b6598210f34c8d106a27033e3 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package com.vaadin.tests.push;

import static org.junit.Assert.assertEquals;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

import org.junit.Test;
import org.openqa.selenium.WebElement;

import com.vaadin.server.VaadinRequest;
import com.vaadin.shared.communication.PushMode;
import com.vaadin.tests.components.AbstractTestUI;
import com.vaadin.tests.tb3.MultiBrowserTest;
import com.vaadin.tests.util.Log;
import com.vaadin.ui.Button;
import com.vaadin.ui.UIDetachedException;

public class EnableDisablePush extends AbstractTestUI {

    public static class EnableDisablePushTest extends MultiBrowserTest {
        @Test
        public void testEnablePushWhenUsingPolling() throws Exception {
            openTestURL();

            assertEquals("1. Push enabled", getLogRow(0));

            getDisablePushButton().click();
            assertEquals("3. Push disabled", getLogRow(0));

            getEnablePollButton().click();
            assertEquals("5. Poll enabled", getLogRow(0));

            getEnablePushButton().click();
            assertEquals("7. Push enabled", getLogRow(0));

            getDisablePollButton().click();
            assertEquals("9. Poll disabled", getLogRow(0));

            getDisablePushButtonAndReenableFromBackground().click();
            Thread.sleep(2500);
            assertEquals("16. Polling disabled, push enabled", getLogRow(0));

            getDisablePushButton().click();
            assertEquals("18. Push disabled", getLogRow(0));
        }

        private WebElement getDisablePushButton() {
            return vaadinElement("/VVerticalLayout[0]/Slot[1]/VVerticalLayout[0]/Slot[0]/VButton[0]");
        }

        private WebElement getEnablePushButton() {
            return vaadinElement("/VVerticalLayout[0]/Slot[1]/VVerticalLayout[0]/Slot[1]/VButton[0]");
        }

        private WebElement getDisablePollButton() {
            return vaadinElement("/VVerticalLayout[0]/Slot[1]/VVerticalLayout[0]/Slot[2]/VButton[0]");
        }

        private WebElement getEnablePollButton() {
            return vaadinElement("/VVerticalLayout[0]/Slot[1]/VVerticalLayout[0]/Slot[3]/VButton[0]");
        }

        private WebElement getDisablePushButtonAndReenableFromBackground() {
            return vaadinElement("/VVerticalLayout[0]/Slot[1]/VVerticalLayout[0]/Slot[4]/VButton[0]");
        }

    }

    private int c = 0;

    private Log log = new Log(15);

    private final Timer timer = new Timer(true);

    private final class CounterTask extends TimerTask {

        @Override
        public void run() {

            try {
                while (true) {
                    TimeUnit.MILLISECONDS.sleep(500);

                    access(new Runnable() {
                        @Override
                        public void run() {
                            log.log("Counter = " + c++);
                            if (c == 3) {
                                log.log("Disabling polling, enabling push");
                                getPushConfiguration().setPushMode(
                                        PushMode.AUTOMATIC);
                                setPollInterval(-1);
                                log.log("Polling disabled, push enabled");
                            }
                        }
                    });
                    if (c == 3) {
                        return;
                    }
                }
            } catch (InterruptedException e) {
            } catch (UIDetachedException e) {
            }
        }
    };

    @Override
    protected void setup(VaadinRequest request) {

        getPushConfiguration().setPushMode(PushMode.AUTOMATIC);
        log.log("Push enabled");

        addComponent(new Button("Disable push", new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                log.log("Disabling push");
                getPushConfiguration().setPushMode(PushMode.DISABLED);
                log.log("Push disabled");
            }
        }));

        addComponent(new Button("Enable push", new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                log.log("Enabling push");
                getPushConfiguration().setPushMode(PushMode.AUTOMATIC);
                log.log("Push enabled");
            }
        }));

        addComponent(new Button("Disable polling", new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                log.log("Disabling poll");
                setPollInterval(-1);
                log.log("Poll disabled");
            }
        }));

        addComponent(new Button("Enable polling", new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                log.log("Enabling poll");
                setPollInterval(1000);
                log.log("Poll enabled");
            }
        }));

        addComponent(new Button(
                "Disable push, re-enable from background thread",
                new Button.ClickListener() {
                    @Override
                    public void buttonClick(Button.ClickEvent event) {
                        log.log("Disabling push, enabling polling");
                        getPushConfiguration().setPushMode(PushMode.DISABLED);
                        setPollInterval(1000);
                        timer.schedule(new CounterTask(), new Date());
                        log.log("Push disabled, polling enabled");
                    }
                }));

        addComponent(log);
    }

    @Override
    protected String getTestDescription() {
        return "Test dynamically enablind and disabling push";
    }

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