-
Notifications
You must be signed in to change notification settings - Fork 164
/
EventRpcHandlerTest.java
100 lines (86 loc) · 3.71 KB
/
EventRpcHandlerTest.java
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
/*
* Copyright 2000-2024 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.server.communication.rpc;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Assert;
import org.junit.Test;
import com.vaadin.flow.component.ComponentTest.TestComponent;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.dom.DomListenerRegistration;
import com.vaadin.flow.dom.Element;
import com.vaadin.flow.internal.StateNode;
import com.vaadin.flow.internal.nodefeature.InertData;
import com.vaadin.flow.shared.JsonConstants;
import elemental.json.Json;
import elemental.json.JsonObject;
public class EventRpcHandlerTest {
@Test
public void testElementEventNoData() throws Exception {
TestComponent c = new TestComponent();
Element element = c.getElement();
UI ui = new UI();
ui.add(c);
AtomicInteger invocations = new AtomicInteger(0);
element.addEventListener("test-event",
e -> invocations.incrementAndGet());
sendElementEvent(element, ui, "test-event", null);
Assert.assertEquals(1, invocations.get());
}
@Test
public void testElementEventData() throws Exception {
TestComponent c = new TestComponent();
Element element = c.getElement();
UI ui = new UI();
ui.add(c);
AtomicInteger invocationData = new AtomicInteger(0);
DomListenerRegistration domListenerRegistration = element
.addEventListener("test-event", e -> invocationData
.addAndGet((int) e.getEventData().getNumber("nr")));
JsonObject eventData = Json.createObject();
eventData.put("nr", 123);
sendElementEvent(element, ui, "test-event", eventData);
Assert.assertEquals(123, invocationData.get());
// Also verify inert stops the event and allowInert allows to bypass
invocationData.set(0);
eventData.put("nr", 124);
InertData inertData = element.getNode().getFeature(InertData.class);
inertData.setInertSelf(true);
inertData.generateChangesFromEmpty();
sendElementEvent(element, ui, "test-event", eventData);
Assert.assertEquals(0, invocationData.get());
// explicitly allow this event listener even when element is inert
domListenerRegistration.allowInert();
sendElementEvent(element, ui, "test-event", eventData);
Assert.assertEquals(124, invocationData.get());
}
private static JsonObject createElementEventInvocation(Element element,
String eventType, JsonObject eventData) {
StateNode node = element.getNode();
// Copied from ServerConnector
JsonObject message = Json.createObject();
message.put(JsonConstants.RPC_NODE, node.getId());
message.put(JsonConstants.RPC_EVENT_TYPE, eventType);
if (eventData != null) {
message.put(JsonConstants.RPC_EVENT_DATA, eventData);
}
return message;
}
private static void sendElementEvent(Element element, UI ui,
String eventType, JsonObject eventData) throws Exception {
new EventRpcHandler().handle(ui,
createElementEventInvocation(element, eventType, eventData));
}
}