modules/controls/src/test/java/javafx/scene/control/TableViewTest.java

Print this page
rev 9240 : 8076423: JEP 253: Prepare JavaFX UI Controls & CSS APIs for Modularization


   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.scene.control;
  27 
  28 import static com.sun.javafx.scene.control.infrastructure.ControlTestUtils.assertStyleClassContains;
  29 import static javafx.application.Platform.runLater;
  30 import static javafx.scene.control.TableColumn.SortType.ASCENDING;
  31 import static javafx.scene.control.TableColumn.SortType.DESCENDING;
  32 import static org.junit.Assert.*;
  33 
  34 import java.util.*;
  35 import java.util.concurrent.Callable;
  36 import java.util.concurrent.atomic.AtomicLong;
  37 import java.util.function.Supplier;
  38 
  39 import com.sun.javafx.scene.control.ReadOnlyUnbackedObservableList;
  40 import com.sun.javafx.scene.control.SelectedCellsMap;
  41 import com.sun.javafx.scene.control.behavior.ListCellBehavior;
  42 import com.sun.javafx.scene.control.behavior.TableCellBehavior;
  43 import com.sun.javafx.scene.control.infrastructure.KeyEventFirer;
  44 import com.sun.javafx.scene.control.infrastructure.KeyModifier;
  45 import com.sun.javafx.scene.control.infrastructure.MouseEventFirer;
  46 import com.sun.javafx.scene.control.infrastructure.StageLoader;
  47 import com.sun.javafx.scene.control.skin.*;
  48 import javafx.application.Platform;
  49 import javafx.beans.InvalidationListener;
  50 import javafx.beans.binding.Bindings;
  51 import javafx.beans.binding.ObjectBinding;
  52 import javafx.beans.property.*;
  53 import javafx.beans.value.ChangeListener;
  54 import javafx.beans.value.ObservableValue;
  55 import javafx.collections.FXCollections;
  56 import javafx.collections.ListChangeListener;
  57 import test.javafx.collections.MockSetObserver;
  58 import javafx.collections.ObservableList;
  59 import javafx.collections.transformation.SortedList;
  60 import javafx.event.Event;
  61 import javafx.event.EventHandler;
  62 import javafx.geometry.Orientation;
  63 import javafx.scene.Group;
  64 import javafx.scene.Scene;
  65 import javafx.scene.control.cell.*;







  66 import javafx.scene.image.ImageView;
  67 import javafx.scene.input.KeyCode;
  68 import javafx.scene.input.KeyEvent;
  69 import javafx.scene.layout.StackPane;
  70 import javafx.scene.layout.VBox;
  71 import javafx.scene.paint.Color;
  72 import javafx.scene.shape.Rectangle;
  73 import javafx.util.Callback;
  74 
  75 import com.sun.javafx.tk.Toolkit;
  76 import org.junit.Before;
  77 import org.junit.Ignore;
  78 import org.junit.Test;
  79 
  80 import com.sun.javafx.scene.control.TableColumnComparatorBase.TableColumnComparator;
  81 import com.sun.javafx.scene.control.infrastructure.VirtualFlowTestUtils;
  82 import com.sun.javafx.scene.control.test.Person;
  83 import com.sun.javafx.scene.control.test.RT_22463_Person;
  84 
  85 import static com.sun.javafx.scene.control.skin.TableColumnHeaderRetriever.*;
  86 import static org.junit.Assert.assertEquals;
  87 
  88 public class TableViewTest {
  89     private TableView<String> table;
  90     private TableView.TableViewSelectionModel sm;
  91     private TableView.TableViewFocusModel<String> fm;
  92 
  93     private ObservableList<Person> personTestData;
  94 
  95     @Before public void setup() {
  96         table = new TableView<>();
  97         sm = table.getSelectionModel();
  98         fm = table.getFocusModel();
  99 
 100         personTestData = FXCollections.observableArrayList(
 101                 new Person("Jacob", "Smith", "jacob.smith@example.com"),
 102                 new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
 103                 new Person("Ethan", "Williams", "ethan.williams@example.com"),
 104                 new Person("Emma", "Jones", "emma.jones@example.com"),
 105                 new Person("Michael", "Brown", "michael.brown@example.com"));


2733     @Test public void test_getColumnHeaderForColumn() {
2734         TableView<Person> table = new TableView<>();
2735         table.setItems(FXCollections.observableArrayList(
2736                 new Person("John", "Smith", "jacob.smith@example.com")
2737         ));
2738 
2739         TableColumn<Person,String> first = new TableColumn<Person,String>("first");
2740         first.setCellValueFactory(new PropertyValueFactory("firstName"));
2741         TableColumn<Person,String> last = new TableColumn<Person,String>("last");
2742         first.setCellValueFactory(new PropertyValueFactory("lastName"));
2743 
2744         TableColumn name = new TableColumn("Name");
2745         name.getColumns().addAll(first, last);
2746 
2747         table.getColumns().setAll(name);
2748 
2749         StageLoader sl = new StageLoader(table);
2750 
2751         TableHeaderRow headerRow = VirtualFlowTestUtils.getTableHeaderRow(table);
2752 
2753         TableColumnHeader nameHeader = headerRow.getColumnHeaderFor(name);
2754         TableColumnHeader firstHeader = headerRow.getColumnHeaderFor(first);
2755         TableColumnHeader lastHeader = headerRow.getColumnHeaderFor(last);
2756         assertNotNull(nameHeader);
2757         assertEquals(name, nameHeader.getTableColumn());
2758         assertNotNull(firstHeader);
2759         assertEquals(first, firstHeader.getTableColumn());
2760         assertNotNull(lastHeader);
2761         assertEquals(last, lastHeader.getTableColumn());
2762 
2763         sl.dispose();
2764     }
2765 
2766     @Test public void test_rt36220() {
2767         ObservableList<AtomicLong> tableItems = FXCollections.observableArrayList();
2768         tableItems.add(new AtomicLong(0L));
2769 
2770         TableView<AtomicLong> tableView = new TableView<>();
2771         tableView.getItems().setAll(tableItems);
2772 
2773         TableColumn<AtomicLong, String> col = new TableColumn<>();
2774         col.setCellValueFactory(obj -> new SimpleStringProperty(String.valueOf(obj.getValue().longValue())));
2775         col.setPrefWidth(180);


3009         });
3010 
3011         TableColumn<String, String> tableColumn = new TableColumn<>();
3012         tableColumn.setCellValueFactory(rowValue -> new SimpleStringProperty(rowValue.getValue()));
3013         tableView.getColumns().add(tableColumn);
3014 
3015         for (int i = 0; i < 1000; i++) {
3016             tableView.getItems().add("Row " + i);
3017         }
3018 
3019         StackPane root = new StackPane();
3020         root.getChildren().add(tableView);
3021 
3022         StageLoader sl = new StageLoader(root);
3023 
3024         final int cellCountAtStart = rt36556_instanceCount;
3025 
3026         // start scrolling - we call VirtualFlow.adjustPixels, which is what
3027         // is called when the mouse wheel is scrolled
3028         VirtualFlow flow = VirtualFlowTestUtils.getVirtualFlow(tableView);
3029         flow.adjustPixels(1000 * 24);
3030 
3031         assertEquals(cellCountAtStart, rt36556_instanceCount);
3032 
3033         sl.dispose();
3034     }
3035 
3036     @Test public void test_rt_36656_removeFromSortOrder() {
3037         test_rt_36656(true, false, false);
3038     }
3039 
3040     @Test public void test_rt_36656_removeFromColumns() {
3041         test_rt_36656(false, true, false);
3042     }
3043 
3044     @Test public void test_rt_36656_setInvisible() {
3045         test_rt_36656(false, false, true);
3046     }
3047 
3048     private void test_rt_36656(boolean removeFromSortOrder, boolean removeFromColumns, boolean setInvisible) {
3049         TableView<Person> table = new TableView<Person>();




   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.scene.control;
  27 
  28 import static com.sun.javafx.scene.control.infrastructure.ControlTestUtils.assertStyleClassContains;

  29 import static javafx.scene.control.TableColumn.SortType.ASCENDING;
  30 import static javafx.scene.control.TableColumn.SortType.DESCENDING;
  31 import static org.junit.Assert.*;
  32 
  33 import java.util.*;

  34 import java.util.concurrent.atomic.AtomicLong;
  35 import java.util.function.Supplier;
  36 
  37 import com.sun.javafx.scene.control.ReadOnlyUnbackedObservableList;
  38 import com.sun.javafx.scene.control.SelectedCellsMap;

  39 import com.sun.javafx.scene.control.behavior.TableCellBehavior;
  40 import com.sun.javafx.scene.control.infrastructure.KeyEventFirer;
  41 import com.sun.javafx.scene.control.infrastructure.KeyModifier;
  42 import com.sun.javafx.scene.control.infrastructure.MouseEventFirer;
  43 import com.sun.javafx.scene.control.infrastructure.StageLoader;

  44 import javafx.application.Platform;

  45 import javafx.beans.binding.Bindings;
  46 import javafx.beans.binding.ObjectBinding;
  47 import javafx.beans.property.*;


  48 import javafx.collections.FXCollections;
  49 import javafx.collections.ListChangeListener;

  50 import javafx.collections.ObservableList;
  51 import javafx.collections.transformation.SortedList;

  52 import javafx.event.EventHandler;
  53 import javafx.geometry.Orientation;
  54 import javafx.scene.Group;
  55 import javafx.scene.Scene;
  56 import javafx.scene.control.cell.*;
  57 import javafx.scene.control.skin.TableCellSkin;
  58 import javafx.scene.control.skin.TableColumnHeader;
  59 import javafx.scene.control.skin.TableColumnHeaderRetriever;
  60 import javafx.scene.control.skin.TableHeaderRow;
  61 import javafx.scene.control.skin.TableHeaderRowRetriever;
  62 import javafx.scene.control.skin.VirtualFlow;
  63 import com.sun.javafx.scene.control.VirtualScrollBar;
  64 import javafx.scene.image.ImageView;
  65 import javafx.scene.input.KeyCode;

  66 import javafx.scene.layout.StackPane;
  67 import javafx.scene.layout.VBox;
  68 import javafx.scene.paint.Color;
  69 import javafx.scene.shape.Rectangle;
  70 import javafx.util.Callback;
  71 
  72 import com.sun.javafx.tk.Toolkit;
  73 import org.junit.Before;
  74 import org.junit.Ignore;
  75 import org.junit.Test;
  76 
  77 import com.sun.javafx.scene.control.TableColumnComparatorBase.TableColumnComparator;
  78 import com.sun.javafx.scene.control.infrastructure.VirtualFlowTestUtils;
  79 import com.sun.javafx.scene.control.test.Person;
  80 import com.sun.javafx.scene.control.test.RT_22463_Person;
  81 
  82 import static javafx.scene.control.skin.TableColumnHeaderRetriever.*;
  83 import static org.junit.Assert.assertEquals;
  84 
  85 public class TableViewTest {
  86     private TableView<String> table;
  87     private TableView.TableViewSelectionModel sm;
  88     private TableView.TableViewFocusModel<String> fm;
  89 
  90     private ObservableList<Person> personTestData;
  91 
  92     @Before public void setup() {
  93         table = new TableView<>();
  94         sm = table.getSelectionModel();
  95         fm = table.getFocusModel();
  96 
  97         personTestData = FXCollections.observableArrayList(
  98                 new Person("Jacob", "Smith", "jacob.smith@example.com"),
  99                 new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
 100                 new Person("Ethan", "Williams", "ethan.williams@example.com"),
 101                 new Person("Emma", "Jones", "emma.jones@example.com"),
 102                 new Person("Michael", "Brown", "michael.brown@example.com"));


2730     @Test public void test_getColumnHeaderForColumn() {
2731         TableView<Person> table = new TableView<>();
2732         table.setItems(FXCollections.observableArrayList(
2733                 new Person("John", "Smith", "jacob.smith@example.com")
2734         ));
2735 
2736         TableColumn<Person,String> first = new TableColumn<Person,String>("first");
2737         first.setCellValueFactory(new PropertyValueFactory("firstName"));
2738         TableColumn<Person,String> last = new TableColumn<Person,String>("last");
2739         first.setCellValueFactory(new PropertyValueFactory("lastName"));
2740 
2741         TableColumn name = new TableColumn("Name");
2742         name.getColumns().addAll(first, last);
2743 
2744         table.getColumns().setAll(name);
2745 
2746         StageLoader sl = new StageLoader(table);
2747 
2748         TableHeaderRow headerRow = VirtualFlowTestUtils.getTableHeaderRow(table);
2749 
2750         TableColumnHeader nameHeader = TableHeaderRowRetriever.getColumnHeaderFor(headerRow, name);
2751         TableColumnHeader firstHeader = TableHeaderRowRetriever.getColumnHeaderFor(headerRow, first);
2752         TableColumnHeader lastHeader = TableHeaderRowRetriever.getColumnHeaderFor(headerRow, last);
2753         assertNotNull(nameHeader);
2754         assertEquals(name, nameHeader.getTableColumn());
2755         assertNotNull(firstHeader);
2756         assertEquals(first, firstHeader.getTableColumn());
2757         assertNotNull(lastHeader);
2758         assertEquals(last, lastHeader.getTableColumn());
2759 
2760         sl.dispose();
2761     }
2762 
2763     @Test public void test_rt36220() {
2764         ObservableList<AtomicLong> tableItems = FXCollections.observableArrayList();
2765         tableItems.add(new AtomicLong(0L));
2766 
2767         TableView<AtomicLong> tableView = new TableView<>();
2768         tableView.getItems().setAll(tableItems);
2769 
2770         TableColumn<AtomicLong, String> col = new TableColumn<>();
2771         col.setCellValueFactory(obj -> new SimpleStringProperty(String.valueOf(obj.getValue().longValue())));
2772         col.setPrefWidth(180);


3006         });
3007 
3008         TableColumn<String, String> tableColumn = new TableColumn<>();
3009         tableColumn.setCellValueFactory(rowValue -> new SimpleStringProperty(rowValue.getValue()));
3010         tableView.getColumns().add(tableColumn);
3011 
3012         for (int i = 0; i < 1000; i++) {
3013             tableView.getItems().add("Row " + i);
3014         }
3015 
3016         StackPane root = new StackPane();
3017         root.getChildren().add(tableView);
3018 
3019         StageLoader sl = new StageLoader(root);
3020 
3021         final int cellCountAtStart = rt36556_instanceCount;
3022 
3023         // start scrolling - we call VirtualFlow.adjustPixels, which is what
3024         // is called when the mouse wheel is scrolled
3025         VirtualFlow flow = VirtualFlowTestUtils.getVirtualFlow(tableView);
3026         flow.scrollPixels(1000 * 24);
3027 
3028         assertEquals(cellCountAtStart, rt36556_instanceCount);
3029 
3030         sl.dispose();
3031     }
3032 
3033     @Test public void test_rt_36656_removeFromSortOrder() {
3034         test_rt_36656(true, false, false);
3035     }
3036 
3037     @Test public void test_rt_36656_removeFromColumns() {
3038         test_rt_36656(false, true, false);
3039     }
3040 
3041     @Test public void test_rt_36656_setInvisible() {
3042         test_rt_36656(false, false, true);
3043     }
3044 
3045     private void test_rt_36656(boolean removeFromSortOrder, boolean removeFromColumns, boolean setInvisible) {
3046         TableView<Person> table = new TableView<Person>();