Skip to content

Commit 6e03930

Browse files
committed
8237602: TabPane doesn't respect order of TabPane.getTabs() list
Reviewed-by: kcr, fastegal
1 parent bb24322 commit 6e03930

File tree

2 files changed

+69
-6
lines changed

2 files changed

+69
-6
lines changed

modules/javafx.controls/src/main/java/javafx/scene/control/skin/TabPaneSkin.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,6 @@ private void initializeTabListener() {
579579
getSkinnable().getTabs().addListener((ListChangeListener<Tab>) c -> {
580580
List<Tab> tabsToRemove = new ArrayList<>();
581581
List<Tab> tabsToAdd = new ArrayList<>();
582-
int insertPos = -1;
583582

584583
while (c.next()) {
585584
if (c.wasPermutated()) {
@@ -619,7 +618,6 @@ private void initializeTabListener() {
619618
}
620619
if (c.wasAdded()) {
621620
tabsToAdd.addAll(c.getAddedSubList());
622-
insertPos = c.getFrom();
623621
}
624622
}
625623

@@ -647,7 +645,9 @@ private void initializeTabListener() {
647645
}
648646
}
649647

650-
addTabs(tabsToAdd, insertPos == -1 ? tabContentRegions.size() : insertPos);
648+
if (!tabsToAdd.isEmpty()) {
649+
addTabs(tabsToAdd, getSkinnable().getTabs().indexOf(tabsToAdd.get(0)));
650+
}
651651
for (Pair<Integer, TabHeaderSkin> move : headersToMove) {
652652
tabHeaderArea.moveTab(move.getKey(), move.getValue());
653653
}
@@ -991,8 +991,10 @@ private void removeTab(Tab tab) {
991991
}
992992

993993
private void moveTab(int moveToIndex, TabHeaderSkin tabHeaderSkin) {
994-
headersRegion.getChildren().remove(tabHeaderSkin);
995-
headersRegion.getChildren().add(moveToIndex, tabHeaderSkin);
994+
if (moveToIndex != headersRegion.getChildren().indexOf(tabHeaderSkin)) {
995+
headersRegion.getChildren().remove(tabHeaderSkin);
996+
headersRegion.getChildren().add(moveToIndex, tabHeaderSkin);
997+
}
996998
}
997999

9981000
private TabHeaderSkin getTabHeaderSkin(Tab tab) {

tests/system/src/test/java/test/robot/javafx/scene/TabPanePermuteGetTabsTest.java

+62-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -70,6 +70,14 @@
7070
* 3.2 tab[1] is the first tab in tab header.
7171
* 3.3 Pressing RIGHT key should select tabs in order: tab 4,3,2,0
7272
*
73+
* b1. testPermuteGetTabsWithMoreTabs1()
74+
* 1. Add tabs 0,1
75+
* 2. Permute tabs to tab 0,1,2,3 using TabPane.getTabs().setAll().
76+
* 3. Verify that,
77+
* 3.1 tab[1] should remain selected tab.
78+
* 3.2 tab[0] is the first tab in tab header.
79+
* 3.3 Pressing RIGHT key should select tabs in order: tab 1,2,3
80+
*
7381
* c. testPermuteGetTabsWithLessTabs()
7482
* 1. Add tab 3,1 and some(6) more tabs, and select tab 1.
7583
* 2. Permute tabs to, tab 1,4,3,2 using TabPane.getTabs().setAll().
@@ -181,6 +189,59 @@ public void testPermuteGetTabsWithMoreTabs() {
181189
}
182190
}
183191

192+
// Test for JDK-8237602
193+
@Test
194+
public void testAddingNewTabsWithExistingTabsAtSameIndex() {
195+
// Step #1
196+
Util.runAndWait(() -> {
197+
tabPane.getTabs().setAll(tab[0], tab[1]);
198+
tabPane.getSelectionModel().select(tab[1]);
199+
});
200+
delay();
201+
202+
Assert.assertSame("Sanity: tab[1] should be the selected tab.",
203+
tab[1], tabPane.getSelectionModel().getSelectedItem());
204+
205+
// Step #2
206+
Util.runAndWait(() -> {
207+
tabPane.getTabs().setAll(tab[0], tab[1], tab[2], tab[3]);
208+
});
209+
delay();
210+
211+
// Step #3.1
212+
Assert.assertSame("Sanity: tab[1] should remain selected tab after permuting.",
213+
tab[1], tabPane.getSelectionModel().getSelectedItem());
214+
215+
// Step #3.2
216+
// Click on first tab header
217+
selectionLatch[0] = new CountDownLatch(1);
218+
Util.runAndWait(() -> {
219+
robot.mouseMove((int) (scene.getWindow().getX() + scene.getX() + firstTabdXY),
220+
(int) (scene.getWindow().getY() + scene.getY() + firstTabdXY));
221+
robot.mousePress(MouseButton.PRIMARY);
222+
robot.mouseRelease(MouseButton.PRIMARY);
223+
});
224+
delay();
225+
226+
waitForLatch(selectionLatch[0], 5,
227+
"Timeout: Waiting for tab[" + 0 + "] to get selected.");
228+
Assert.assertSame("tab[0] should be first tab after permuting.",
229+
tab[0], tabPane.getSelectionModel().getSelectedItem());
230+
231+
// step #3.3
232+
selectionLatch[1] = new CountDownLatch(1);
233+
for (int i = 1; i <= 3; i++) {
234+
Util.runAndWait(() -> {
235+
robot.keyPress(KeyCode.RIGHT);
236+
robot.keyRelease(KeyCode.RIGHT);
237+
});
238+
waitForLatch(selectionLatch[i], 5,
239+
"Timeout: Waiting for tab[" + i + "] to get selected.");
240+
Assert.assertSame("tab[" + i + "] should get selected on RIGHT key press.",
241+
tab[i], tabPane.getSelectionModel().getSelectedItem());
242+
}
243+
}
244+
184245
@Test
185246
public void testPermutGetTabsWithLessTabs() {
186247
// Step #1

0 commit comments

Comments
 (0)