/
ModalDialogIT.java
152 lines (125 loc) · 5.44 KB
/
ModalDialogIT.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
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
package com.vaadin.flow.uitest.ui;
import com.vaadin.flow.component.html.testbench.DivElement;
import com.vaadin.flow.component.html.testbench.NativeButtonElement;
import com.vaadin.flow.testutil.ChromeBrowserTest;
import com.vaadin.testbench.TestBenchElement;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
public class ModalDialogIT extends ChromeBrowserTest {
private TestBenchElement eventLog;
private TestBenchElement modalDialogButton;
private TestBenchElement modelessDialogButton;
@Before
public void init() {
open();
eventLog = $(DivElement.class).id(ModalDialogView.EVENT_LOG);
modalDialogButton = $(NativeButtonElement.class)
.id(ModalDialogView.OPEN_MODAL_BUTTON);
modelessDialogButton = $(NativeButtonElement.class)
.id(ModalDialogView.OPEN_MODELESS_BUTTON);
}
// #7799
@Test
public void modalDialogOpened_sameShortcutsListeningOnUi_noShortcutTriggered() {
pressShortcutKey(
$(NativeButtonElement.class).id(ModalDialogView.UI_BUTTON));
validateLatestShortcutEvent(0, ModalDialogView.UI_BUTTON);
openDialog(modalDialogButton);
pressShortcutKey(getDialogInput());
// no event occurred
validateLatestShortcutEvent(0, ModalDialogView.UI_BUTTON);
listenToButtonShortcutOnUI();
pressShortcutKey(getDialogInput());
// no event occurred since shortcut is listened on ui which is inert
validateLatestShortcutEvent(0, ModalDialogView.UI_BUTTON);
closeDialog();
pressShortcutKey(
$(NativeButtonElement.class).id(ModalDialogView.UI_BUTTON));
validateLatestShortcutEvent(1, ModalDialogView.UI_BUTTON);
}
@Test
public void modalDialogOpened_sameShortcutListeningOnUiAndDialog_onlyDialogShortcutExecuted() {
pressShortcutKey(
$(NativeButtonElement.class).id(ModalDialogView.UI_BUTTON));
validateLatestShortcutEvent(0, ModalDialogView.UI_BUTTON);
openDialog(modalDialogButton);
pressShortcutKey(getDialogInput());
// no event occurred
validateLatestShortcutEvent(0, ModalDialogView.UI_BUTTON);
listenToButtonShortcutOnDialog();
pressShortcutKey(getDialogInput());
validateLatestShortcutEvent(1, ModalDialogView.DIALOG_BUTTON);
closeDialog();
pressShortcutKey(
$(NativeButtonElement.class).id(ModalDialogView.UI_BUTTON));
validateLatestShortcutEvent(2, ModalDialogView.UI_BUTTON);
}
@Test
public void modelessDialogOpened_sharesShortcutWithUI_bothExecuted() {
pressShortcutKey(
$(NativeButtonElement.class).id(ModalDialogView.UI_BUTTON));
validateLatestShortcutEvent(0, ModalDialogView.UI_BUTTON);
openDialog(modelessDialogButton);
listenToButtonShortcutOnUI();
pressShortcutKey(getDialogInput());
validateShortcutEvent(1, 1, ModalDialogView.UI_BUTTON);
validateLatestShortcutEvent(2, ModalDialogView.DIALOG_BUTTON);
closeDialog();
pressShortcutKey(
$(NativeButtonElement.class).id(ModalDialogView.UI_BUTTON));
validateLatestShortcutEvent(3, ModalDialogView.UI_BUTTON);
}
@Test
public void modelessDialogOpened_sameShortcutListeningOnUiAndDialog_bothExecuted() {
pressShortcutKey(
$(NativeButtonElement.class).id(ModalDialogView.UI_BUTTON));
validateLatestShortcutEvent(0, ModalDialogView.UI_BUTTON);
openDialog(modelessDialogButton);
listenToButtonShortcutOnDialog();
pressShortcutKey(getDialogInput());
validateLatestShortcutEvent(1, ModalDialogView.DIALOG_BUTTON);
pressShortcutKey(getDialogInput());
validateLatestShortcutEvent(2, ModalDialogView.DIALOG_BUTTON);
closeDialog();
pressShortcutKey(
$(NativeButtonElement.class).id(ModalDialogView.UI_BUTTON));
validateLatestShortcutEvent(3, ModalDialogView.UI_BUTTON);
}
private void openDialog(WebElement modalDialogButton) {
modalDialogButton.click();
}
private void closeDialog() {
$(NativeButtonElement.class).id(ModalDialogView.DIALOG_CLOSE_BUTTON)
.click();
}
private void validateLatestShortcutEvent(int eventCounter,
String eventSourceId) {
validateShortcutEvent(0, eventCounter, eventSourceId);
}
private void validateShortcutEvent(int indexFromTop, int eventCounter,
String eventSourceId) {
final WebElement latestEvent = eventLog.findElements(By.tagName("div"))
.get(indexFromTop);
Assert.assertEquals("Invalid latest event",
eventCounter + "-" + eventSourceId, latestEvent.getText());
}
private void pressShortcutKey(TestBenchElement elementToFocus) {
elementToFocus.focus();
elementToFocus.sendKeys("x");
}
private TestBenchElement getDialogInput() {
return $(DivElement.class).id(ModalDialogView.DIALOG).$("input")
.first();
}
private void listenToButtonShortcutOnUI() {
$(NativeButtonElement.class).id(ModalDialogView.LISTEN_ON_UI_BUTTON)
.click();
}
private void listenToButtonShortcutOnDialog() {
$(NativeButtonElement.class).id(ModalDialogView.LISTEN_ON_DIALOG_BUTTON)
.click();
}
}