2 * Copyright 2000-2016 Vaadin Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
16 package com.vaadin.tests.components.grid.basicfeatures.server;
18 import static org.junit.Assert.assertFalse;
19 import static org.junit.Assert.assertTrue;
21 import java.util.List;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.openqa.selenium.By;
26 import org.openqa.selenium.WebElement;
27 import org.openqa.selenium.interactions.Actions;
29 import com.vaadin.testbench.TestBenchElement;
30 import com.vaadin.testbench.elements.GridElement.GridCellElement;
31 import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeaturesTest;
34 * Tests that Grid columns can be reordered by user with drag and drop #16643.
38 public class GridColumnReorderTest extends GridBasicFeaturesTest {
40 private static final String[] COLUMN_REORDERING_PATH = { "Component",
41 "State", "Column Reordering Allowed" };
42 private static final String[] COLUMN_REORDER_LISTENER_PATH = { "Component",
43 "State", "ColumnReorderListener" };
51 public void testColumnReordering_firstColumnDroppedOnThird_dropOnLeftSide() {
54 assertColumnHeaderOrder(0, 1, 2);
55 toggleColumnReordering();
58 dragAndDropDefaultColumnHeader(0, 2, CellSide.LEFT);
61 assertColumnHeaderOrder(1, 0, 2);
65 public void testColumnReordering_firstColumnDroppedOnThird_dropOnRightSide() {
68 assertColumnHeaderOrder(0, 1, 2);
69 toggleColumnReordering();
72 dragAndDropDefaultColumnHeader(0, 2, CellSide.RIGHT);
75 assertColumnHeaderOrder(1, 2, 0);
79 public void testColumnReordering_reorderingTwiceBackForth_reordered() {
82 selectMenuPath("Component", "Size", "Width", "800px");
83 assertColumnHeaderOrder(0, 1, 2, 3, 4);
84 toggleColumnReordering();
87 dragAndDropDefaultColumnHeader(2, 0, CellSide.LEFT);
90 assertColumnHeaderOrder(2, 0, 1, 3, 4);
93 dragAndDropDefaultColumnHeader(1, 3, CellSide.RIGHT);
96 assertColumnHeaderOrder(2, 1, 3, 0);
100 public void testColumnReordering_notEnabled_noReordering() {
103 assertColumnHeaderOrder(0, 1, 2);
106 dragAndDropDefaultColumnHeader(0, 2, CellSide.RIGHT);
109 assertColumnHeaderOrder(0, 1, 2);
113 public void testColumnReordering_userChangesRevertedByServer_columnsAreUpdated() {
116 assertColumnHeaderOrder(0, 1, 2);
117 toggleColumnReordering();
120 dragAndDropDefaultColumnHeader(0, 2, CellSide.LEFT);
121 assertColumnHeaderOrder(1, 0, 2);
122 moveColumnManuallyLeftByOne(0);
125 assertColumnHeaderOrder(0, 1, 2);
129 public void testColumnReordering_concurrentUpdatesFromServer_columnOrderFromServerUsed() {
132 assertColumnHeaderOrder(0, 1, 2);
133 toggleColumnReordering();
136 selectMenuPath(new String[] { "Component", "Internals",
137 "Update column order without updating client" });
138 dragAndDropDefaultColumnHeader(2, 0, CellSide.LEFT);
141 assertColumnHeaderOrder(1, 0, 2);
145 public void testColumnReordering_triggersReorderEvent_isUserInitiated() {
148 toggleColumnReordering();
151 toggleColumnReorderListener();
152 dragAndDropDefaultColumnHeader(0, 2, CellSide.LEFT);
155 assertColumnReorderEvent(true);
159 public void testColumnReordering_addAndRemoveListener_registerUnRegisterWorks() {
162 toggleColumnReordering();
163 dragAndDropDefaultColumnHeader(0, 2, CellSide.LEFT);
164 assertNoColumnReorderEvent();
167 toggleColumnReorderListener();
168 dragAndDropDefaultColumnHeader(0, 2, CellSide.RIGHT);
171 assertColumnReorderEvent(true);
174 toggleColumnReorderListener();
175 dragAndDropDefaultColumnHeader(0, 3, CellSide.LEFT);
178 assertNoColumnReorderEvent();
182 public void testColumnReorderingEvent_serverSideReorder_triggersReorderEvent() {
186 toggleColumnReorderListener();
187 moveColumnManuallyLeftByOne(3);
190 assertColumnReorderEvent(false);
194 public void testColumnReorder_draggingFrozenColumns_impossible() {
197 toggleColumnReordering();
199 assertColumnHeaderOrder(0, 1, 2, 3);
202 dragAndDropDefaultColumnHeader(0, 2, CellSide.LEFT);
205 assertColumnHeaderOrder(0, 1, 2, 3);
206 assertTrue(getGridElement().getHeaderCell(0, 0).isFrozen());
207 assertTrue(getGridElement().getHeaderCell(0, 1).isFrozen());
208 assertFalse(getGridElement().getHeaderCell(0, 2).isFrozen());
212 public void testColumnReorder_draggingColumnOnTopOfFrozenColumn_columnDroppedRightOfFrozenColumns() {
215 toggleColumnReordering();
217 assertColumnHeaderOrder(0, 1, 2, 3);
220 dragAndDropDefaultColumnHeader(2, 0, CellSide.LEFT);
223 assertColumnHeaderOrder(0, 2, 1, 3);
227 public void testColumnReorder_draggingColumnLeftOfMultiSelectionColumn_columnDroppedRight() {
230 toggleColumnReordering();
231 selectMenuPath("Component", "State", "Selection mode", "multi");
232 List<TestBenchElement> gridHeaderRowCells = getGridHeaderRowCells();
233 assertTrue(gridHeaderRowCells.get(0).getText().equals(""));
234 assertColumnHeader("Column 0", gridHeaderRowCells.get(1));
235 assertColumnHeader("Column 1", gridHeaderRowCells.get(2));
236 assertColumnHeader("Column 2", gridHeaderRowCells.get(3));
239 dragAndDropDefaultColumnHeader(2, 0, CellSide.LEFT);
242 gridHeaderRowCells = getGridHeaderRowCells();
243 assertTrue(gridHeaderRowCells.get(0).getText().equals(""));
244 assertColumnHeader("Column 1", gridHeaderRowCells.get(1));
245 assertColumnHeader("Column 0", gridHeaderRowCells.get(2));
246 assertColumnHeader("Column 2", gridHeaderRowCells.get(3));
250 public void testColumnReorder_multiSelectionAndFrozenColumns_columnDroppedRight() {
253 toggleColumnReordering();
254 selectMenuPath("Component", "State", "Selection mode", "multi");
256 List<TestBenchElement> gridHeaderRowCells = getGridHeaderRowCells();
257 assertTrue(gridHeaderRowCells.get(0).getText().equals(""));
258 assertColumnHeader("Column 0", gridHeaderRowCells.get(1));
259 assertColumnHeader("Column 1", gridHeaderRowCells.get(2));
260 assertColumnHeader("Column 2", gridHeaderRowCells.get(3));
263 dragAndDropDefaultColumnHeader(3, 0, CellSide.LEFT);
266 gridHeaderRowCells = getGridHeaderRowCells();
267 assertTrue(gridHeaderRowCells.get(0).getText().equals(""));
268 assertColumnHeader("Column 0", gridHeaderRowCells.get(1));
269 assertColumnHeader("Column 2", gridHeaderRowCells.get(2));
270 assertColumnHeader("Column 1", gridHeaderRowCells.get(3));
274 public void testColumnReordering_multiSelectionColumnNotFrozen_stillCantDropLeftSide() {
277 toggleColumnReordering();
278 selectMenuPath("Component", "State", "Selection mode", "multi");
279 setFrozenColumns(-1);
280 List<TestBenchElement> gridHeaderRowCells = getGridHeaderRowCells();
281 assertTrue(gridHeaderRowCells.get(0).getText().equals(""));
282 assertColumnHeader("Column 0", gridHeaderRowCells.get(1));
283 assertColumnHeader("Column 1", gridHeaderRowCells.get(2));
284 assertColumnHeader("Column 2", gridHeaderRowCells.get(3));
287 dragAndDropDefaultColumnHeader(2, 0, CellSide.LEFT);
290 gridHeaderRowCells = getGridHeaderRowCells();
291 assertTrue(gridHeaderRowCells.get(0).getText().equals(""));
292 assertColumnHeader("Column 1", gridHeaderRowCells.get(1));
293 assertColumnHeader("Column 0", gridHeaderRowCells.get(2));
294 assertColumnHeader("Column 2", gridHeaderRowCells.get(3));
298 public void testColumnReordering_twoHeaderRows_dndReorderingPossibleFromFirstRow() {
301 toggleColumnReordering();
302 selectMenuPath("Component", "Header", "Append row");
303 assertColumnHeaderOrder(0, 1, 2, 3);
306 dragAndDropColumnHeader(0, 0, 2, CellSide.RIGHT);
309 assertColumnHeaderOrder(1, 2, 0, 3);
313 public void testColumnReordering_twoHeaderRows_dndReorderingPossibleFromSecondRow() {
316 toggleColumnReordering();
317 selectMenuPath("Component", "Header", "Append row");
318 assertColumnHeaderOrder(0, 1, 2, 3);
321 dragAndDropColumnHeader(1, 0, 2, CellSide.RIGHT);
324 assertColumnHeaderOrder(1, 2, 0, 3);
328 public void testColumnReordering_bigWidth_dragElementPositionCorrect() {
330 toggleColumnReordering();
331 selectMenuPath("Component", "Size", "Width", "900px");
332 assertColumnHeaderOrder(0, 1, 2, 3);
334 GridCellElement draggedHeaderCell = getGridElement().getHeaderCell(0,
336 final int xOffset = 500;
337 new Actions(getDriver()).moveToElement(draggedHeaderCell, 20, 10)
338 .clickAndHold().moveByOffset(xOffset, 0).build().perform();
340 WebElement floatingDragElement = findElement(
341 By.className("dragged-column-header"));
343 int expectedLeft = draggedHeaderCell.getLocation().getX() + xOffset + 20
344 - (floatingDragElement.getSize().getWidth() / 2);
345 int realLeft = floatingDragElement.getLocation().getX();
348 "Dragged element location wrong, expected " + expectedLeft
349 + " was " + realLeft,
350 Math.abs(expectedLeft - realLeft) < 10);
354 public void testDropMarker_sidebarOpenButtonVisible_dropMarkerOnCorrectPosition() {
355 // using runo since there the sidebar opening button is wider than the
356 // scroll deco, and because using Valo has some TB issues
357 openTestURL("theme=runo");
359 selectMenuPath("Component", "Size", "Width", "100%");
360 selectMenuPath("Component", "Columns", "All columns hidable");
361 toggleColumnReordering();
362 scrollGridHorizontallyTo(100000);
364 new Actions(getDriver()).clickAndHold(getDefaultColumnHeader(10))
365 .moveByOffset(800, 0).build().perform();
367 WebElement dragDropMarker = findElement(
368 By.className("v-grid-drop-marker"));
369 WebElement sidebar = findElement(By.className("v-grid-sidebar"));
371 int dragDropMarkerX = dragDropMarker.getLocation().getX();
372 int sidebarX = sidebar.getLocation().getX();
374 "Drop marker misplaced " + dragDropMarkerX
375 + " compared to sidebar open button " + sidebarX,
376 dragDropMarkerX <= sidebarX);
379 private void toggleColumnReordering() {
380 selectMenuPath(COLUMN_REORDERING_PATH);
383 private void toggleColumnReorderListener() {
384 selectMenuPath(COLUMN_REORDER_LISTENER_PATH);
387 private void moveColumnManuallyLeftByOne(int index) {
388 selectMenuPath(new String[] { "Component", "Columns", "Column " + index,
392 private void assertColumnReorderEvent(boolean userOriginated) {
393 final String logRow = getLogRow(0);
394 assertTrue(logRow.contains(
395 "Columns reordered, userOriginated: " + userOriginated));
398 private void assertNoColumnReorderEvent() {
399 final String logRow = getLogRow(0);
400 assertFalse(logRow.contains("Columns reordered"));