Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright 2000-2026 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.flow.component.grid.it;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.data.bean.Person;
import com.vaadin.flow.data.provider.DataProvider;
import com.vaadin.flow.router.Route;

@Route("vaadin-grid/dump")
public class GridDumpPage extends Div {

public GridDumpPage() {
createSmallGrid();
createMediumGrid();
createLargeGrid();
createGridWithHiddenColumn();
createEmptyGrid();
}

private void createEmptyGrid() {
Grid<Person> grid = new Grid<>();
grid.setItems(List.of());

grid.addColumn(Person::getFirstName).setHeader("Name");
grid.addColumn(Person::getAge).setHeader("Age");

grid.setId("empty-grid");

add(grid);
}

private void createSmallGrid() {
Grid<Person> grid = new Grid<>();
grid.setItems(IntStream.range(0, 10)
.mapToObj(i -> new Person("Person " + i, i))
.collect(Collectors.toList()));

Check warning on line 55 in vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/main/java/com/vaadin/flow/component/grid/it/GridDumpPage.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this usage of 'Stream.collect(Collectors.toList())' with 'Stream.toList()' and ensure that the list is unmodified.

See more on https://sonarcloud.io/project/issues?id=vaadin_flow-components&issues=AZ875O-QFw61q_dzfHd0&open=AZ875O-QFw61q_dzfHd0&pullRequest=8513

grid.addColumn(Person::getFirstName).setHeader("Name");
grid.addColumn(Person::getAge).setHeader("Age");

grid.setId("small-grid");

add(grid);
}

private void createMediumGrid() {
Grid<Person> grid = new Grid<>();
grid.setItems(IntStream.range(0, 100)
.mapToObj(i -> new Person("Person " + i, i))
.collect(Collectors.toList()));

Check warning on line 69 in vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/main/java/com/vaadin/flow/component/grid/it/GridDumpPage.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this usage of 'Stream.collect(Collectors.toList())' with 'Stream.toList()' and ensure that the list is unmodified.

See more on https://sonarcloud.io/project/issues?id=vaadin_flow-components&issues=AZ875O-QFw61q_dzfHdz&open=AZ875O-QFw61q_dzfHdz&pullRequest=8513

grid.addColumn(Person::getFirstName).setHeader("Name");
grid.addColumn(Person::getAge).setHeader("Age");

grid.setId("medium-grid");

add(grid);
}

private void createLargeGrid() {
Grid<Person> grid = new Grid<>();
grid.setItems(
DataProvider
.fromCallbacks(
query -> IntStream
.range(query.getOffset(),
query.getOffset()
+ query.getLimit())
.mapToObj(index -> new Person(
"Person " + index, index)),
query -> 1000));

grid.addColumn(Person::getFirstName).setHeader("Name");
grid.addColumn(Person::getAge).setHeader("Age");

grid.setId("large-grid");

add(grid);
}

private void createGridWithHiddenColumn() {
Grid<Person> grid = new Grid<>();
grid.setItems(IntStream.range(0, 10)
.mapToObj(i -> new Person("Person " + i, i))
.collect(Collectors.toList()));

Check warning on line 104 in vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/main/java/com/vaadin/flow/component/grid/it/GridDumpPage.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this usage of 'Stream.collect(Collectors.toList())' with 'Stream.toList()' and ensure that the list is unmodified.

See more on https://sonarcloud.io/project/issues?id=vaadin_flow-components&issues=AZ875O-QFw61q_dzfHd1&open=AZ875O-QFw61q_dzfHd1&pullRequest=8513

grid.addColumn(Person::getFirstName).setHeader("Name");
grid.addColumn(Person::getAge).setHeader("Age").setVisible(false);
grid.addColumn(person -> "Email" + person.getAge()).setHeader("Email");

grid.setId("hidden-column-grid");

add(grid);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
/*
* Copyright 2000-2026 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.flow.component.grid.it;

import java.util.List;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.vaadin.flow.component.grid.testbench.GridElement;
import com.vaadin.flow.testutil.TestPath;
import com.vaadin.tests.AbstractComponentIT;

@TestPath("vaadin-grid/dump")
public class GridDumpIT extends AbstractComponentIT {

@Before
public void init() {
open();
}

@Test
public void getVisibleCellContents_smallGrid_returnsVisibleCells() {
GridElement grid = $(GridElement.class).id("small-grid");
scrollToElement(grid);
waitUntil(driver -> grid.getRowCount() > 0);

List<List<String>> cells = grid.getVisibleCellContents();

Assert.assertNotNull("Cells should not be null", cells);
Assert.assertTrue("Grid should have visible cells", cells.size() > 0);

// Check first row
List<String> firstRow = cells.get(0);
Assert.assertEquals("First row should have 2 columns", 2,
firstRow.size());
Assert.assertEquals("Person 0", firstRow.get(0));
Assert.assertEquals("0", firstRow.get(1));
}

@Test
public void getAllCellContents_smallGrid_returnsAllCells() {
GridElement grid = $(GridElement.class).id("small-grid");
scrollToElement(grid);
waitUntil(driver -> grid.getRowCount() > 0);

List<List<String>> cells = grid.getAllCellContents();

Assert.assertEquals("Should have 10 rows", 10, cells.size());

// Verify first and last rows
Assert.assertEquals("Person 0", cells.get(0).get(0));
Assert.assertEquals("0", cells.get(0).get(1));
Assert.assertEquals("Person 9", cells.get(9).get(0));
Assert.assertEquals("9", cells.get(9).get(1));
}

@Test
public void getAllCellContents_mediumGrid_returnsAllCells() {
GridElement grid = $(GridElement.class).id("medium-grid");
scrollToElement(grid);
waitUntil(driver -> grid.getRowCount() > 0);

List<List<String>> cells = grid.getAllCellContents();

Assert.assertEquals("Should have 100 rows", 100, cells.size());

// Verify first, middle and last rows
Assert.assertEquals("Person 0", cells.get(0).get(0));
Assert.assertEquals("Person 50", cells.get(50).get(0));
Assert.assertEquals("Person 99", cells.get(99).get(0));
}

@Test
public void getCellContents_mediumGrid_returnsSpecifiedRange() {
GridElement grid = $(GridElement.class).id("medium-grid");
scrollToElement(grid);
waitUntil(driver -> grid.getRowCount() > 0);

List<List<String>> cells = grid.getCellContents(20, 29);

Assert.assertEquals("Should have 10 rows", 10, cells.size());

// Verify row data
Assert.assertEquals("Person 20", cells.get(0).get(0));
Assert.assertEquals("20", cells.get(0).get(1));
Assert.assertEquals("Person 29", cells.get(9).get(0));
Assert.assertEquals("29", cells.get(9).get(1));
}

@Test
public void getAllCellContents_largeGrid_returnsAllCells() {
GridElement grid = $(GridElement.class).id("large-grid");
scrollToElement(grid);
waitUntil(driver -> grid.getRowCount() > 0);

List<List<String>> cells = grid.getAllCellContents();

Assert.assertEquals("Should have 1000 rows", 1000, cells.size());

// Verify sampling of rows
Assert.assertEquals("Person 0", cells.get(0).get(0));
Assert.assertEquals("Person 500", cells.get(500).get(0));
Assert.assertEquals("Person 999", cells.get(999).get(0));
}

@Test
public void getCellContents_largeGrid_returnsSpecifiedRange() {
GridElement grid = $(GridElement.class).id("large-grid");
scrollToElement(grid);
waitUntil(driver -> grid.getRowCount() > 0);

List<List<String>> cells = grid.getCellContents(800, 850);

Assert.assertEquals("Should have 51 rows", 51, cells.size());
Assert.assertEquals("Person 800", cells.get(0).get(0));
Assert.assertEquals("Person 850", cells.get(50).get(0));
}

@Test
public void getAllCellContents_hiddenColumn_onlyVisibleColumns() {
GridElement grid = $(GridElement.class).id("hidden-column-grid");
scrollToElement(grid);
waitUntil(driver -> grid.getRowCount() > 0);

List<List<String>> cells = grid.getAllCellContents();

Assert.assertEquals("Should have 10 rows", 10, cells.size());

// Should only have 2 visible columns (Name and Email, Age is hidden)
List<String> firstRow = cells.get(0);
Assert.assertEquals("Should have 2 visible columns", 2,
firstRow.size());
Assert.assertEquals("Person 0", firstRow.get(0));
Assert.assertEquals("Email0", firstRow.get(1));
}

@Test
public void getCellContents_invalidRange_throwsException() {
GridElement grid = $(GridElement.class).id("small-grid");
scrollToElement(grid);
waitUntil(driver -> grid.getRowCount() > 0);

try {
grid.getCellContents(-1, 5);
Assert.fail("Should throw IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
// Expected
}

try {
grid.getCellContents(0, 100);
Assert.fail("Should throw IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
// Expected
}

try {
grid.getCellContents(5, 3);
Assert.fail("Should throw IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
// Expected
}
}

@Test
public void getAllCellContents_emptyGrid_returnsEmptyList() {
GridElement grid = $(GridElement.class).id("empty-grid");
scrollToElement(grid);

List<List<String>> cells = grid.getAllCellContents();

Assert.assertTrue("Empty grid should return no rows", cells.isEmpty());
}

@Test
public void getCellContents_emptyGrid_throwsWithClearMessage() {
GridElement grid = $(GridElement.class).id("empty-grid");
scrollToElement(grid);

try {
grid.getCellContents(0, 0);
Assert.fail("Should throw IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
Assert.assertTrue("Message should mention the empty grid, but was: "
+ e.getMessage(), e.getMessage().contains("empty"));
}
}

@Test
public void getCellContents_matchesGetCellText() {
GridElement grid = $(GridElement.class).id("hidden-column-grid");
scrollToElement(grid);
waitUntil(driver -> grid.getRowCount() > 0);

List<List<String>> cells = grid.getAllCellContents();

int visibleColumns = grid.getVisibleColumns().size();
for (int row = 0; row < cells.size(); row++) {
for (int col = 0; col < visibleColumns; col++) {
Assert.assertEquals(
"Cell content should match getCell().getText() at row "
+ row + ", column " + col,
grid.getCell(row, col).getText(),
cells.get(row).get(col));
}
}
}

@Test
public void getVisibleCellContents_scrolled_returnsVisibleRowsInOrder() {
GridElement grid = $(GridElement.class).id("large-grid");
scrollToElement(grid);
waitUntil(driver -> grid.getRowCount() > 0);

grid.scrollToRow(500);

List<List<String>> cells = grid.getVisibleCellContents();

Assert.assertTrue("Grid should show more than one visible row",
cells.size() > 1);

// Only the rows in the viewport should be returned, in ascending index
// order, excluding the buffer rows the grid renders out of view. The
// grid also recycles row elements, so DOM order does not match index
// order after scrolling.
int firstVisible = grid.getFirstVisibleRowIndex();
int lastVisible = grid.getLastVisibleRowIndex();
Assert.assertEquals("Should return exactly the visible rows",
lastVisible - firstVisible + 1, cells.size());

int expectedIndex = firstVisible;
for (List<String> row : cells) {
Assert.assertEquals("Person " + expectedIndex, row.get(0));
expectedIndex++;
}
}
}
Loading
Loading