1 /* 2 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 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 test.javafx.scene.chart; 27 28 import java.util.HashMap; 29 import javafx.collections.FXCollections; 30 import javafx.collections.ObservableList; 31 32 import javafx.scene.Node; 33 import javafx.scene.chart.XYChart; 34 import javafx.scene.chart.XYChartShim; 35 import static org.junit.Assert.assertEquals; 36 37 38 public abstract class XYChartTestBase extends ChartTestBase { 39 40 protected int countSymbols(XYChart chart, String style) { 41 int numSymbols = 0; 42 ObservableList<Node> childrenList = XYChartShim.getPlotChildren(chart); 43 for (Node n : childrenList) { 44 if (n.getStyleClass().contains(style)) numSymbols++; 45 } 46 return numSymbols; 47 } 48 49 ObservableList<XYChart.Series<?, ?>> createTestSeries() { 50 ObservableList<XYChart.Series<?, ?>> list = FXCollections.observableArrayList(); 51 for (int i = 0; i != 10; i++) { 52 XYChart.Series<Number, Number> series = new XYChart.Series<>(); 53 series.getData().add(new XYChart.Data<>(i*10, i*10)); 54 series.getData().add(new XYChart.Data<>(i*20, i*20)); 55 series.getData().add(new XYChart.Data<>(i*30, i*30)); 56 list.add(series); 57 } 58 return list; 59 } 60 61 abstract void checkSeriesStyleClasses(XYChart.Series<?, ?> series, 62 int seriesIndex, int colorIndex); 63 abstract void checkDataStyleClasses(XYChart.Data<?, ?> data, 64 int seriesIndex, int dataIndex, int colorIndex); 65 66 void checkSeriesRemoveAnimatedStyleClasses(XYChart chart, int nodesPerSeries, int fadeOutTime) { 67 ObservableList<XYChart.Series<?, ?>> series = createTestSeries(); 68 int seriesSize = series.size(); 69 HashMap<XYChart.Series<?, ?>, Integer> seriesColors = new HashMap<>(); 70 for (int i = 0; i != seriesSize; i++) { 71 XYChart.Series<?, ?> s = series.get(i); 72 seriesColors.put(s, i % 8); 73 } 74 chart.getData().addAll(series); 75 pulse(); 76 assertEquals(nodesPerSeries * seriesSize, XYChartShim.getPlotChildren(chart).size()); 77 for (int i = 0; i != seriesSize; i++) { 78 XYChart.Series<?, ?> s = series.get(i); 79 int colorIndex = seriesColors.get(s); 80 checkSeriesStyleClasses(s, i, colorIndex); 81 for (int j = 0; j != s.getData().size(); j++) { 82 checkDataStyleClasses(s.getData().get(j), i, j, colorIndex); 83 } 84 } 85 86 chart.setAnimated(true); 87 chart.getData().remove(1); 88 toolkit.setAnimationTime(fadeOutTime/2); 89 assertEquals(nodesPerSeries * seriesSize, XYChartShim.getPlotChildren(chart).size()); 90 // colors shouldn't change 91 // data index should change 92 for (int i = 0; i != seriesSize; i++) { 93 XYChart.Series<?, ?> s = series.get(i); 94 int seriesIndex = chart.getData().indexOf(s); 95 int seriesStyleIndex = seriesIndex == -1 ? i : seriesIndex; 96 int colorIndex = seriesColors.get(s); 97 checkSeriesStyleClasses(s, seriesStyleIndex, seriesColors.get(s)); 98 for (int j = 0; j != s.getData().size(); j++) { 99 checkDataStyleClasses(s.getData().get(j), seriesStyleIndex, j, colorIndex); 100 } 101 } 102 103 toolkit.setAnimationTime(fadeOutTime); 104 assertEquals(nodesPerSeries * (seriesSize - 1), XYChartShim.getPlotChildren(chart).size()); 105 for (int i = 0; i != seriesSize; i++) { 106 XYChart.Series<?, ?> s = series.get(i); 107 int seriesIndex = chart.getData().indexOf(s); 108 int seriesStyleIndex = seriesIndex == -1 ? i : seriesIndex; 109 int colorIndex = seriesColors.get(s); 110 checkSeriesStyleClasses(s, seriesStyleIndex, seriesColors.get(s)); 111 for (int j = 0; j != s.getData().size(); j++) { 112 checkDataStyleClasses(s.getData().get(j), seriesStyleIndex, j, colorIndex); 113 } 114 } 115 116 chart.getData().add(series.get(1)); 117 seriesColors.put(series.get(1), 1); 118 toolkit.setAnimationTime(fadeOutTime); 119 assertEquals(nodesPerSeries * seriesSize, XYChartShim.getPlotChildren(chart).size()); 120 for (int i = 0; i != seriesSize; i++) { 121 XYChart.Series s = (XYChart.Series) chart.getData().get(i); 122 int colorIndex = seriesColors.get(s); 123 checkSeriesStyleClasses(s, i, colorIndex); 124 for (int j = 0; j != s.getData().size(); j++) { 125 checkDataStyleClasses((XYChart.Data<?, ?>) s.getData().get(j), i, j, colorIndex); 126 } 127 } 128 } 129 }