|
1 | 1 | /* |
2 | | - * Copyright (c) 2010, 2022, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2010, 2023, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
28 | 28 | import java.util.AbstractList; |
29 | 29 | import static org.junit.Assert.assertEquals; |
30 | 30 | import static org.junit.Assert.assertFalse; |
| 31 | +import static org.junit.Assert.assertNotEquals; |
31 | 32 | import static org.junit.Assert.assertNotNull; |
32 | 33 | import static org.junit.Assert.assertNull; |
33 | 34 | import static org.junit.Assert.assertSame; |
34 | 35 | import static org.junit.Assert.assertTrue; |
| 36 | +import static org.junit.Assert.fail; |
35 | 37 |
|
36 | 38 | import java.util.Iterator; |
37 | 39 | import java.util.LinkedList; |
@@ -1523,6 +1525,194 @@ protected double computeMaxHeight(double width) { |
1523 | 1525 | assertEquals(3, firstCell.getIndex()); |
1524 | 1526 | assertEquals(-10, firstCell.getLayoutY(),1); |
1525 | 1527 | } |
| 1528 | + |
| 1529 | + /** |
| 1530 | + * The VirtualFlow should never call the compute height methods when a fixed cell size is set. |
| 1531 | + * If it is called the height will be wrong for some cells. |
| 1532 | + */ |
| 1533 | + @Test |
| 1534 | + public void testComputeHeightShouldNotBeUsedWhenFixedCellSizeIsSet() { |
| 1535 | + int cellSize = 24; |
| 1536 | + |
| 1537 | + flow = new VirtualFlowShim<>(); |
| 1538 | + flow.setFixedCellSize(cellSize); |
| 1539 | + flow.setCellFactory(p -> new CellStub(flow) { |
| 1540 | + |
| 1541 | + @Override |
| 1542 | + protected double computeMinHeight(double width) { |
| 1543 | + return 1337; |
| 1544 | + } |
| 1545 | + |
| 1546 | + @Override |
| 1547 | + protected double computeMaxHeight(double width) { |
| 1548 | + return 1337; |
| 1549 | + } |
| 1550 | + |
| 1551 | + @Override |
| 1552 | + protected double computePrefHeight(double width) { |
| 1553 | + return 1337; |
| 1554 | + } |
| 1555 | + }); |
| 1556 | + flow.setCellCount(100); |
| 1557 | + flow.resize(cellSize * 10, cellSize * 10); |
| 1558 | + |
| 1559 | + pulse(); |
| 1560 | + pulse(); |
| 1561 | + |
| 1562 | + for (int i = 0; i < 10; i++) { |
| 1563 | + IndexedCell<?> cell = flow.getCell(i); |
| 1564 | + double cellPosition = flow.getCellPosition(cell); |
| 1565 | + int expectedPosition = i * cellSize; |
| 1566 | + assertEquals(expectedPosition, cellPosition, 0d); |
| 1567 | + assertEquals(cellSize, cell.getHeight(), 0d); |
| 1568 | + |
| 1569 | + assertNotEquals(cellSize, cell.getWidth(), 0d); |
| 1570 | + } |
| 1571 | + |
| 1572 | + flow.scrollPixels(cellSize * 10); |
| 1573 | + |
| 1574 | + for (int i = 10; i < 20; i++) { |
| 1575 | + IndexedCell<?> cell = flow.getCell(i); |
| 1576 | + double cellPosition = flow.getCellPosition(cell); |
| 1577 | + int expectedPosition = (i - 10) * cellSize; |
| 1578 | + assertEquals(expectedPosition, cellPosition, 0d); |
| 1579 | + assertEquals(cellSize, cell.getHeight(), 0d); |
| 1580 | + |
| 1581 | + assertNotEquals(cellSize, cell.getWidth(), 0d); |
| 1582 | + } |
| 1583 | + } |
| 1584 | + |
| 1585 | + /** |
| 1586 | + * The VirtualFlow should never call the compute width methods when a fixed cell size is set. |
| 1587 | + * If it is called the width will be wrong for some cells. |
| 1588 | + */ |
| 1589 | + @Test |
| 1590 | + public void testComputeWidthShouldNotBeUsedWhenFixedCellSizeIsSet() { |
| 1591 | + int cellSize = 24; |
| 1592 | + |
| 1593 | + flow = new VirtualFlowShim<>(); |
| 1594 | + flow.setVertical(false); |
| 1595 | + flow.setFixedCellSize(cellSize); |
| 1596 | + flow.setCellFactory(p -> new CellStub(flow) { |
| 1597 | + |
| 1598 | + @Override |
| 1599 | + protected double computeMinWidth(double height) { |
| 1600 | + return 1337; |
| 1601 | + } |
| 1602 | + |
| 1603 | + @Override |
| 1604 | + protected double computeMaxWidth(double height) { |
| 1605 | + return 1337; |
| 1606 | + } |
| 1607 | + |
| 1608 | + @Override |
| 1609 | + protected double computePrefWidth(double height) { |
| 1610 | + return 1337; |
| 1611 | + } |
| 1612 | + }); |
| 1613 | + flow.setCellCount(100); |
| 1614 | + flow.resize(cellSize * 10, cellSize * 10); |
| 1615 | + |
| 1616 | + pulse(); |
| 1617 | + pulse(); |
| 1618 | + |
| 1619 | + for (int i = 0; i < 10; i++) { |
| 1620 | + IndexedCell<?> cell = flow.getCell(i); |
| 1621 | + double cellPosition = flow.getCellPosition(cell); |
| 1622 | + int expectedPosition = i * cellSize; |
| 1623 | + assertEquals(expectedPosition, cellPosition, 0d); |
| 1624 | + assertEquals(cellSize, cell.getWidth(), 0d); |
| 1625 | + |
| 1626 | + assertNotEquals(cellSize, cell.getHeight(), 0d); |
| 1627 | + } |
| 1628 | + |
| 1629 | + flow.scrollPixels(cellSize * 10); |
| 1630 | + |
| 1631 | + for (int i = 10; i < 20; i++) { |
| 1632 | + IndexedCell<?> cell = flow.getCell(i); |
| 1633 | + double cellPosition = flow.getCellPosition(cell); |
| 1634 | + int expectedPosition = (i - 10) * cellSize; |
| 1635 | + assertEquals(expectedPosition, cellPosition, 0d); |
| 1636 | + assertEquals(cellSize, cell.getWidth(), 0d); |
| 1637 | + |
| 1638 | + assertNotEquals(cellSize, cell.getHeight(), 0d); |
| 1639 | + } |
| 1640 | + } |
| 1641 | + |
| 1642 | + /** |
| 1643 | + * The VirtualFlow should never call the compute height methods when a fixed cell size is set. |
| 1644 | + */ |
| 1645 | + @Test |
| 1646 | + public void testComputeHeightShouldNotBeCalledWhenFixedCellSizeIsSet() { |
| 1647 | + int cellSize = 24; |
| 1648 | + |
| 1649 | + flow = new VirtualFlowShim<>(); |
| 1650 | + flow.setFixedCellSize(cellSize); |
| 1651 | + flow.setCellFactory(p -> new CellStub(flow) { |
| 1652 | + |
| 1653 | + @Override |
| 1654 | + protected double computeMinHeight(double width) { |
| 1655 | + fail(); |
| 1656 | + return 1337; |
| 1657 | + } |
| 1658 | + |
| 1659 | + @Override |
| 1660 | + protected double computeMaxHeight(double width) { |
| 1661 | + fail(); |
| 1662 | + return 1337; |
| 1663 | + } |
| 1664 | + |
| 1665 | + @Override |
| 1666 | + protected double computePrefHeight(double width) { |
| 1667 | + fail(); |
| 1668 | + return 1337; |
| 1669 | + } |
| 1670 | + }); |
| 1671 | + flow.setCellCount(100); |
| 1672 | + flow.resize(cellSize * 10, cellSize * 10); |
| 1673 | + |
| 1674 | + // Trigger layout and see if the computeXXX method are called above. |
| 1675 | + pulse(); |
| 1676 | + pulse(); |
| 1677 | + } |
| 1678 | + |
| 1679 | + /** |
| 1680 | + * The VirtualFlow should never call the compute width methods when a fixed cell size is set. |
| 1681 | + */ |
| 1682 | + @Test |
| 1683 | + public void testComputeWidthShouldNotBeCalledWhenFixedCellSizeIsSet() { |
| 1684 | + int cellSize = 24; |
| 1685 | + |
| 1686 | + flow = new VirtualFlowShim<>(); |
| 1687 | + flow.setVertical(false); |
| 1688 | + flow.setFixedCellSize(cellSize); |
| 1689 | + flow.setCellFactory(p -> new CellStub(flow) { |
| 1690 | + |
| 1691 | + @Override |
| 1692 | + protected double computeMinWidth(double height) { |
| 1693 | + fail(); |
| 1694 | + return 1337; |
| 1695 | + } |
| 1696 | + |
| 1697 | + @Override |
| 1698 | + protected double computeMaxWidth(double height) { |
| 1699 | + fail(); |
| 1700 | + return 1337; |
| 1701 | + } |
| 1702 | + |
| 1703 | + @Override |
| 1704 | + protected double computePrefWidth(double height) { |
| 1705 | + fail(); |
| 1706 | + return 1337; |
| 1707 | + } |
| 1708 | + }); |
| 1709 | + flow.setCellCount(100); |
| 1710 | + flow.resize(cellSize * 10, cellSize * 10); |
| 1711 | + |
| 1712 | + // Trigger layout and see if the computeXXX method are called above. |
| 1713 | + pulse(); |
| 1714 | + pulse(); |
| 1715 | + } |
1526 | 1716 | } |
1527 | 1717 |
|
1528 | 1718 | class GraphicalCellStub extends IndexedCellShim<Node> { |
|
0 commit comments