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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
/*
* 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.components.grid;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import com.vaadin.testbench.TestBenchElement;
import com.vaadin.testbench.elements.ButtonElement;
import com.vaadin.testbench.elements.CheckBoxElement;
import com.vaadin.testbench.elements.GridElement.GridRowElement;
import com.vaadin.testbench.elements.TextFieldElement;
import com.vaadin.testbench.parallel.BrowserUtil;
import com.vaadin.testbench.parallel.TestCategory;
import com.vaadin.tests.components.grid.basicfeatures.element.CustomGridElement;
import com.vaadin.tests.tb3.MultiBrowserTest;
@TestCategory("grid")
public class GridDetailsLocationTest extends MultiBrowserTest {
private static final int detailsDefaultHeight = 51;
private static final int detailsDefinedHeight = 33;
private static final int detailsDefinedHeightIE8 = 31;
private static class Param {
private final int rowIndex;
private final boolean useGenerator;
private final boolean scrollFirstToBottom;
public Param(int rowIndex, boolean useGenerator,
boolean scrollFirstToBottom) {
this.rowIndex = rowIndex;
this.useGenerator = useGenerator;
this.scrollFirstToBottom = scrollFirstToBottom;
}
public int getRowIndex() {
return rowIndex;
}
public boolean useGenerator() {
return useGenerator;
}
public boolean scrollFirstToBottom() {
return scrollFirstToBottom;
}
@Override
public String toString() {
return "Param [rowIndex=" + getRowIndex() + ", useGenerator="
+ useGenerator() + ", scrollFirstToBottom="
+ scrollFirstToBottom() + "]";
}
}
public static Collection<Param> parameters() {
List<Param> data = new ArrayList<Param>();
int[] params = new int[] { 0, 500, 999 };
for (int rowIndex : params) {
data.add(new Param(rowIndex, false, false));
data.add(new Param(rowIndex, true, false));
data.add(new Param(rowIndex, false, true));
data.add(new Param(rowIndex, true, true));
}
return data;
}
@Before
public void setUp() {
setDebug(true);
}
@Test
public void toggleAndScroll() throws Throwable {
for (Param param : parameters()) {
try {
openTestURL();
useGenerator(param.useGenerator());
scrollToBottom(param.scrollFirstToBottom());
// the tested method
toggleAndScroll(param.getRowIndex());
verifyLocation(param);
} catch (Throwable t) {
throw new Throwable("" + param, t);
}
}
}
@Test
public void scrollAndToggle() throws Throwable {
for (Param param : parameters()) {
try {
openTestURL();
useGenerator(param.useGenerator());
scrollToBottom(param.scrollFirstToBottom());
// the tested method
scrollAndToggle(param.getRowIndex());
verifyLocation(param);
} catch (Throwable t) {
throw new Throwable("" + param, t);
}
}
}
@Test
public void testDetailsHeightWithNoGenerator() {
openTestURL();
toggleAndScroll(5);
verifyDetailsRowHeight(5, detailsDefaultHeight, 0);
verifyDetailsDecoratorLocation(5, 0, 0);
toggleAndScroll(0);
verifyDetailsRowHeight(0, detailsDefaultHeight, 0);
verifyDetailsDecoratorLocation(0, 0, 1);
verifyDetailsRowHeight(5, detailsDefaultHeight, 1);
verifyDetailsDecoratorLocation(5, 1, 0);
}
@Test
public void testDetailsHeightWithGenerator() {
openTestURL();
useGenerator(true);
toggleAndScroll(5);
verifyDetailsRowHeight(5, getDefinedHeight(), 0);
verifyDetailsDecoratorLocation(5, 0, 0);
toggleAndScroll(0);
verifyDetailsRowHeight(0, getDefinedHeight(), 0);
// decorator elements are in DOM in the order they have been added
verifyDetailsDecoratorLocation(0, 0, 1);
verifyDetailsRowHeight(5, getDefinedHeight(), 1);
verifyDetailsDecoratorLocation(5, 1, 0);
}
private int getDefinedHeight() {
boolean ie8 = isIE8();
return ie8 ? detailsDefinedHeightIE8 : detailsDefinedHeight;
}
private void verifyDetailsRowHeight(int rowIndex, int expectedHeight,
int visibleIndexOfSpacer) {
waitForDetailsVisible();
WebElement details = getDetailsElement(visibleIndexOfSpacer);
Assert.assertEquals("Wrong details row height", expectedHeight, details
.getSize().getHeight());
}
private void verifyDetailsDecoratorLocation(int row,
int visibleIndexOfSpacer, int visibleIndexOfDeco) {
WebElement detailsElement = getDetailsElement(visibleIndexOfSpacer);
WebElement detailsDecoElement = getDetailsDecoElement(visibleIndexOfDeco);
GridRowElement rowElement = getGrid().getRow(row);
int diff = 0;
if (isIE8() || BrowserUtil.isIE(getDesiredCapabilities(), 9)) {
diff = 1;
}
Assert.assertEquals(
"Details deco top position does not match row top pos",
rowElement.getLocation().getY(), detailsDecoElement
.getLocation().getY());
Assert.assertEquals(
"Details deco bottom position does not match details bottom pos",
detailsElement.getLocation().getY()
+ detailsElement.getSize().getHeight(),
detailsDecoElement.getLocation().getY()
+ detailsDecoElement.getSize().getHeight() + diff);
}
private void verifyLocation(Param param) {
Assert.assertFalse("Notification was present",
isElementPresent(By.className("v-Notification")));
TestBenchElement headerRow = getGrid().getHeaderRow(0);
final int topBoundary = headerRow.getLocation().getX()
+ headerRow.getSize().height;
final int bottomBoundary = getGrid().getLocation().getX()
+ getGrid().getSize().getHeight()
- getHorizontalScrollbar().getSize().height;
GridRowElement row = getGrid().getRow(param.getRowIndex());
final int rowTop = row.getLocation().getX();
waitForDetailsVisible();
WebElement details = getDetailsElement();
final int detailsBottom = details.getLocation().getX()
+ details.getSize().getHeight();
assertGreaterOrEqual("Row top should be inside grid, gridTop:"
+ topBoundary + " rowTop" + rowTop, topBoundary, rowTop);
assertLessThanOrEqual(
"Decorator bottom should be inside grid, gridBottom:"
+ bottomBoundary + " decoratorBotton:" + detailsBottom,
detailsBottom, bottomBoundary);
verifyDetailsRowHeight(param.getRowIndex(),
param.useGenerator() ? getDefinedHeight()
: detailsDefaultHeight, 0);
verifyDetailsDecoratorLocation(param.getRowIndex(), 0, 0);
Assert.assertFalse("Notification was present",
isElementPresent(By.className("v-Notification")));
}
private final By locator = By.className("v-grid-spacer");
private WebElement getDetailsElement() {
return getDetailsElement(0);
}
private WebElement getDetailsElement(int index) {
return findElements(locator).get(index);
}
private WebElement getDetailsDecoElement(int index) {
return findElements(By.className("v-grid-spacer-deco")).get(index);
}
private void waitForDetailsVisible() {
waitUntil(new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
try {
WebElement detailsElement = getDetailsElement();
return detailsElement.isDisplayed()
&& detailsElement.getSize().getHeight() > 3 ? detailsElement
: null;
} catch (StaleElementReferenceException e) {
return null;
}
}
@Override
public String toString() {
return "visibility of element located by " + locator;
}
}, 5);
waitForElementVisible(By.className("v-grid-spacer"));
}
private void scrollToBottom(boolean scrollFirstToBottom) {
if (scrollFirstToBottom) {
executeScript("arguments[0].scrollTop = 9999999",
getVerticalScrollbar());
}
}
private void useGenerator(boolean use) {
CheckBoxElement checkBox = $(CheckBoxElement.class).first();
boolean isChecked = isCheckedValo(checkBox);
if (use != isChecked) {
clickValo(checkBox);
}
}
private boolean isIE8() {
return BrowserUtil.isIE8(getDesiredCapabilities());
}
@SuppressWarnings("boxing")
private boolean isCheckedValo(CheckBoxElement checkBoxElement) {
WebElement checkbox = checkBoxElement.findElement(By.tagName("input"));
Object value = executeScript("return arguments[0].checked;", checkbox);
return (Boolean) value;
}
private void clickValo(CheckBoxElement checkBoxElement) {
checkBoxElement.click(5, 5);
}
private void scrollAndToggle(int row) {
setRow(row);
getScrollAndToggle().click();
}
private void toggleAndScroll(int row) {
setRow(row);
getToggleAndScroll().click();
}
private ButtonElement getScrollAndToggle() {
return $(ButtonElement.class).caption("Scroll and toggle").first();
}
private ButtonElement getToggleAndScroll() {
return $(ButtonElement.class).caption("Toggle and scroll").first();
}
private void setRow(int row) {
$(TextFieldElement.class).first().clear();
$(TextFieldElement.class).first().sendKeys(String.valueOf(row),
Keys.ENTER, Keys.TAB);
}
private CustomGridElement getGrid() {
return $(CustomGridElement.class).first();
}
private WebElement getVerticalScrollbar() {
WebElement scrollBar = getGrid().findElement(
By.className("v-grid-scroller-vertical"));
return scrollBar;
}
private WebElement getHorizontalScrollbar() {
WebElement scrollBar = getGrid().findElement(
By.className("v-grid-scroller-horizontal"));
return scrollBar;
}
}
|