1 package com.vaadin.v7.tests.components.grid.basicfeatures.server;
3 import static org.junit.Assert.assertFalse;
4 import static org.junit.Assert.assertTrue;
8 import org.junit.Before;
10 import org.openqa.selenium.By;
11 import org.openqa.selenium.WebElement;
12 import org.openqa.selenium.interactions.Actions;
14 import com.vaadin.testbench.TestBenchElement;
15 import com.vaadin.testbench.elements.GridElement.GridCellElement;
16 import com.vaadin.v7.tests.components.grid.basicfeatures.GridBasicFeaturesTest;
19 * Tests that Grid columns can be reordered by user with drag and drop #16643.
23 public class GridColumnReorderTest extends GridBasicFeaturesTest {
25 private static final String[] COLUMN_REORDERING_PATH = { "Component",
26 "State", "Column Reordering Allowed" };
27 private static final String[] COLUMN_REORDER_LISTENER_PATH = { "Component",
28 "State", "ColumnReorderListener" };
36 public void testColumnReordering_firstColumnDroppedOnThird_dropOnLeftSide() {
39 assertColumnHeaderOrder(0, 1, 2);
40 toggleColumnReordering();
43 dragAndDropDefaultColumnHeader(0, 2, CellSide.LEFT);
46 assertColumnHeaderOrder(1, 0, 2);
50 public void testColumnReordering_firstColumnDroppedOnThird_dropOnRightSide() {
53 assertColumnHeaderOrder(0, 1, 2);
54 toggleColumnReordering();
57 dragAndDropDefaultColumnHeader(0, 2, CellSide.RIGHT);
60 assertColumnHeaderOrder(1, 2, 0);
64 public void testColumnReordering_reorderingTwiceBackForth_reordered() {
67 selectMenuPath("Component", "Size", "Width", "800px");
68 assertColumnHeaderOrder(0, 1, 2, 3, 4);
69 toggleColumnReordering();
72 dragAndDropDefaultColumnHeader(2, 0, CellSide.LEFT);
75 assertColumnHeaderOrder(2, 0, 1, 3, 4);
78 dragAndDropDefaultColumnHeader(1, 3, CellSide.RIGHT);
81 assertColumnHeaderOrder(2, 1, 3, 0);
85 public void testColumnReordering_notEnabled_noReordering() {
88 assertColumnHeaderOrder(0, 1, 2);
91 dragAndDropDefaultColumnHeader(0, 2, CellSide.RIGHT);
94 assertColumnHeaderOrder(0, 1, 2);
98 public void testColumnReordering_userChangesRevertedByServer_columnsAreUpdated() {
101 assertColumnHeaderOrder(0, 1, 2);
102 toggleColumnReordering();
105 dragAndDropDefaultColumnHeader(0, 2, CellSide.LEFT);
106 assertColumnHeaderOrder(1, 0, 2);
107 moveColumnManuallyLeftByOne(0);
110 assertColumnHeaderOrder(0, 1, 2);
114 public void testColumnReordering_concurrentUpdatesFromServer_columnOrderFromServerUsed() {
117 assertColumnHeaderOrder(0, 1, 2);
118 toggleColumnReordering();
121 selectMenuPath(new String[] { "Component", "Internals",
122 "Update column order without updating client" });
123 dragAndDropDefaultColumnHeader(2, 0, CellSide.LEFT);
126 assertColumnHeaderOrder(1, 0, 2);
130 public void testColumnReordering_triggersReorderEvent_isUserInitiated() {
133 toggleColumnReordering();
136 toggleColumnReorderListener();
137 dragAndDropDefaultColumnHeader(0, 2, CellSide.LEFT);
140 assertColumnReorderEvent(true);
144 public void testColumnReordering_addAndRemoveListener_registerUnRegisterWorks() {
147 toggleColumnReordering();
148 dragAndDropDefaultColumnHeader(0, 2, CellSide.LEFT);
149 assertNoColumnReorderEvent();
152 toggleColumnReorderListener();
153 dragAndDropDefaultColumnHeader(0, 2, CellSide.RIGHT);
156 assertColumnReorderEvent(true);
159 toggleColumnReorderListener();
160 dragAndDropDefaultColumnHeader(0, 3, CellSide.LEFT);
163 assertNoColumnReorderEvent();
167 public void testColumnReorderingEvent_serverSideReorder_triggersReorderEvent() {
171 toggleColumnReorderListener();
172 moveColumnManuallyLeftByOne(3);
175 assertColumnReorderEvent(false);
179 public void testColumnReorder_draggingFrozenColumns_impossible() {
182 toggleColumnReordering();
184 assertColumnHeaderOrder(0, 1, 2, 3);
187 dragAndDropDefaultColumnHeader(0, 2, CellSide.LEFT);
190 assertColumnHeaderOrder(0, 1, 2, 3);
191 assertTrue(getGridElement().getHeaderCell(0, 0).isFrozen());
192 assertTrue(getGridElement().getHeaderCell(0, 1).isFrozen());
193 assertFalse(getGridElement().getHeaderCell(0, 2).isFrozen());
197 public void testColumnReorder_draggingFrozenColumnsContainingHiddenColumns_impossible() {
200 selectMenuPath("Component", "Size", "Width", "900px");
201 toggleColumnReordering();
203 toggleColumnHidden(1);
204 toggleColumnHidden(2);
205 assertColumnHeaderOrder(0, 3, 4, 5);
208 // drag frozen column out between non-frozen columns
209 dragAndDropDefaultColumnHeader(1, 2, CellSide.RIGHT);
212 // everything should be as before
213 assertColumnHeaderOrder(0, 3, 4, 5);
214 assertTrue(getGridElement().getHeaderCell(0, 0).isFrozen());
215 assertTrue(getGridElement().getHeaderCell(0, 1).isFrozen());
216 assertFalse(getGridElement().getHeaderCell(0, 2).isFrozen());
220 public void testColumnReorder_draggingColumnOnTopOfFrozenColumn_columnDroppedRightOfFrozenColumns() {
223 toggleColumnReordering();
225 assertColumnHeaderOrder(0, 1, 2, 3);
228 dragAndDropDefaultColumnHeader(2, 0, CellSide.LEFT);
231 assertColumnHeaderOrder(0, 2, 1, 3);
235 public void testColumnReorder_draggingColumnLeftOfMultiSelectionColumn_columnDroppedRight() {
238 toggleColumnReordering();
239 selectMenuPath("Component", "State", "Selection mode", "multi");
240 List<TestBenchElement> gridHeaderRowCells = getGridHeaderRowCells();
241 assertTrue(gridHeaderRowCells.get(0).getText().isEmpty());
242 assertColumnHeader("Column 0", gridHeaderRowCells.get(1));
243 assertColumnHeader("Column 1", gridHeaderRowCells.get(2));
244 assertColumnHeader("Column 2", gridHeaderRowCells.get(3));
247 dragAndDropDefaultColumnHeader(2, 0, CellSide.LEFT);
250 gridHeaderRowCells = getGridHeaderRowCells();
251 assertTrue(gridHeaderRowCells.get(0).getText().isEmpty());
252 assertColumnHeader("Column 1", gridHeaderRowCells.get(1));
253 assertColumnHeader("Column 0", gridHeaderRowCells.get(2));
254 assertColumnHeader("Column 2", gridHeaderRowCells.get(3));
258 public void testColumnReorder_multiSelectionAndFrozenColumns_columnDroppedRight() {
261 toggleColumnReordering();
262 selectMenuPath("Component", "State", "Selection mode", "multi");
264 List<TestBenchElement> gridHeaderRowCells = getGridHeaderRowCells();
265 assertTrue(gridHeaderRowCells.get(0).getText().isEmpty());
266 assertColumnHeader("Column 0", gridHeaderRowCells.get(1));
267 assertColumnHeader("Column 1", gridHeaderRowCells.get(2));
268 assertColumnHeader("Column 2", gridHeaderRowCells.get(3));
271 dragAndDropDefaultColumnHeader(3, 0, CellSide.LEFT);
274 gridHeaderRowCells = getGridHeaderRowCells();
275 assertTrue(gridHeaderRowCells.get(0).getText().isEmpty());
276 assertColumnHeader("Column 0", gridHeaderRowCells.get(1));
277 assertColumnHeader("Column 2", gridHeaderRowCells.get(2));
278 assertColumnHeader("Column 1", gridHeaderRowCells.get(3));
282 public void testColumnReordering_multiSelectionColumnNotFrozen_stillCantDropLeftSide() {
285 toggleColumnReordering();
286 selectMenuPath("Component", "State", "Selection mode", "multi");
287 setFrozenColumns(-1);
288 List<TestBenchElement> gridHeaderRowCells = getGridHeaderRowCells();
289 assertTrue(gridHeaderRowCells.get(0).getText().isEmpty());
290 assertColumnHeader("Column 0", gridHeaderRowCells.get(1));
291 assertColumnHeader("Column 1", gridHeaderRowCells.get(2));
292 assertColumnHeader("Column 2", gridHeaderRowCells.get(3));
295 dragAndDropDefaultColumnHeader(2, 0, CellSide.LEFT);
298 gridHeaderRowCells = getGridHeaderRowCells();
299 assertTrue(gridHeaderRowCells.get(0).getText().isEmpty());
300 assertColumnHeader("Column 1", gridHeaderRowCells.get(1));
301 assertColumnHeader("Column 0", gridHeaderRowCells.get(2));
302 assertColumnHeader("Column 2", gridHeaderRowCells.get(3));
306 public void testColumnReordering_twoHeaderRows_dndReorderingPossibleFromFirstRow() {
309 toggleColumnReordering();
310 selectMenuPath("Component", "Header", "Append row");
311 assertColumnHeaderOrder(0, 1, 2, 3);
314 dragAndDropColumnHeader(0, 0, 2, CellSide.RIGHT);
317 assertColumnHeaderOrder(1, 2, 0, 3);
321 public void testColumnReordering_twoHeaderRows_dndReorderingPossibleFromSecondRow() {
324 toggleColumnReordering();
325 selectMenuPath("Component", "Header", "Append row");
326 assertColumnHeaderOrder(0, 1, 2, 3);
329 dragAndDropColumnHeader(1, 0, 2, CellSide.RIGHT);
332 assertColumnHeaderOrder(1, 2, 0, 3);
336 public void testColumnReordering_bigWidth_dragElementPositionCorrect() {
338 toggleColumnReordering();
339 selectMenuPath("Component", "Size", "Width", "900px");
340 assertColumnHeaderOrder(0, 1, 2, 3);
342 GridCellElement draggedHeaderCell = getGridElement().getHeaderCell(0,
344 final int xOffset = 500;
345 new Actions(getDriver()).moveToElement(draggedHeaderCell, 20, 10)
346 .clickAndHold().moveByOffset(xOffset, 0).build().perform();
348 WebElement floatingDragElement = findElement(
349 By.className("dragged-column-header"));
351 int expectedLeft = draggedHeaderCell.getLocation().getX() + xOffset + 20
352 - (floatingDragElement.getSize().getWidth() / 2);
353 int realLeft = floatingDragElement.getLocation().getX();
356 "Dragged element location wrong, expected " + expectedLeft
357 + " was " + realLeft,
358 Math.abs(expectedLeft - realLeft) < 10);
362 public void testDropMarker_sidebarOpenButtonVisible_dropMarkerOnCorrectPosition() {
363 // using runo since there the sidebar opening button is wider than the
364 // scroll deco, and because using Valo has some TB issues
365 openTestURL("theme=runo");
367 selectMenuPath("Component", "Size", "Width", "100%");
368 selectMenuPath("Component", "Columns", "All columns hidable");
369 toggleColumnReordering();
370 scrollGridHorizontallyTo(100000);
372 new Actions(getDriver()).clickAndHold(getDefaultColumnHeader(10))
373 .moveByOffset(800, 0).build().perform();
375 WebElement dragDropMarker = findElement(
376 By.className("v-grid-drop-marker"));
377 WebElement sidebar = findElement(By.className("v-grid-sidebar"));
379 int dragDropMarkerX = dragDropMarker.getLocation().getX();
380 int sidebarX = sidebar.getLocation().getX();
382 "Drop marker misplaced " + dragDropMarkerX
383 + " compared to sidebar open button " + sidebarX,
384 dragDropMarkerX <= sidebarX);
387 private void toggleColumnReordering() {
388 selectMenuPath(COLUMN_REORDERING_PATH);
391 private void toggleColumnReorderListener() {
392 selectMenuPath(COLUMN_REORDER_LISTENER_PATH);
395 private void moveColumnManuallyLeftByOne(int index) {
396 selectMenuPath(new String[] { "Component", "Columns", "Column " + index,
400 private void assertColumnReorderEvent(boolean userOriginated) {
401 final String logRow = getLogRow(0);
402 assertTrue(logRow.contains(
403 "Columns reordered, userOriginated: " + userOriginated));
406 private void assertNoColumnReorderEvent() {
407 final String logRow = getLogRow(0);
408 assertFalse(logRow.contains("Columns reordered"));