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
|
/*
* Copyright 2000-2014 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.tests.contextclick;
import static org.junit.Assert.assertEquals;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import com.vaadin.testbench.elements.AbstractComponentElement;
import com.vaadin.testbench.elements.ButtonElement;
import com.vaadin.testbench.parallel.TestCategory;
import com.vaadin.tests.tb3.MultiBrowserTest;
@TestCategory("contextclick")
public abstract class AbstractContextClickTest extends MultiBrowserTest {
private Pattern defaultLog = Pattern
.compile("[0-9]+. ContextClickEvent: [(]([0-9]+), ([0-9]+)[)]");
@Override
protected boolean useNativeEventsForIE() {
return false;
}
@Override
public List<DesiredCapabilities> getBrowsersToTest() {
return getBrowsersSupportingContextMenu();
}
@Before
public void setUp() {
openTestURL();
}
@Test
public void testDefaultListener() {
addOrRemoveDefaultListener();
assertDefaultContextClickListener(1);
}
protected void assertNoContextClickHandler() {
AbstractComponentElement component = $(AbstractComponentElement.class)
.id("testComponent");
String log = getLogRow(0);
contextClick(component);
assertEquals("Log entry without a context click listener.", log,
getLogRow(0));
}
protected void assertDefaultContextClickListener(int index) {
AbstractComponentElement component = $(AbstractComponentElement.class)
.id("testComponent");
contextClick(component);
Point l = component.getLocation();
Matcher matcher = defaultLog.matcher(getLogRow(0));
Assert.assertTrue(
"Log row content did not match default listener output: "
+ getLogRow(0), matcher.find());
int xCoord = Integer.parseInt(matcher.group(1));
int yCoord = Integer.parseInt(matcher.group(2));
int xExpected = l.getX() + 10;
int yExpected = l.getY() + 10;
Assert.assertTrue(
"X Coordinate differs too much from expected. Expected: "
+ xExpected + ", actual: " + xCoord,
Math.abs(xExpected - xCoord) <= 1);
Assert.assertTrue(
"Y Coordinate differs too much from expected. Expected: "
+ yExpected + ", actual: " + yCoord,
Math.abs(yExpected - yCoord) <= 1);
}
protected void addOrRemoveDefaultListener() {
$(ButtonElement.class).caption("Add/Remove default listener").first()
.click();
}
protected void addOrRemoveTypedListener() {
$(ButtonElement.class).caption("Add/Remove typed listener").first()
.click();
}
/**
* Performs a context click on given element at coordinates 10, 10 followed
* by a regular click. This prevents browser context menu from blocking
* future operations.
*
* @param e
* web element
*/
protected void contextClick(WebElement e) {
contextClick(e, 10, 10);
}
/**
* Performs a context click on given element at given coordinates followed
* by a regular click. This prevents browser context menu from blocking
* future operations.
*
* @param e
* web element
* @param xCoord
* x coordinate
* @param yCoord
* y coordinate
*/
protected void contextClick(WebElement e, int xCoord, int yCoord) {
new Actions(getDriver()).moveToElement(e, xCoord, yCoord)
.contextClick().perform();
new Actions(getDriver()).moveToElement(e, xCoord - 5, yCoord - 5)
.click().perform();
}
}
|