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 javafx.collections.ObservableList;
  29 import javafx.scene.Node;
  30 import javafx.scene.chart.Chart;
  31 import javafx.scene.chart.LineChart;
  32 import javafx.scene.chart.NumberAxis;
  33 import javafx.scene.chart.XYChart;
  34 import javafx.scene.chart.XYChartShim;
  35 import javafx.scene.shape.Path;
  36 import static org.junit.Assert.assertEquals;
  37 import org.junit.Ignore;
  38 import org.junit.Test;
  39 
  40 public class LineChartTest extends XYChartTestBase {
  41 
  42     LineChart<Number,Number> lineChart;
  43     final XYChart.Series<Number, Number> series1 = new XYChart.Series<Number, Number>();
  44 
  45     @Override protected Chart createChart() {
  46         final NumberAxis xAxis = new NumberAxis();
  47         final NumberAxis yAxis = new NumberAxis();
  48         lineChart = new LineChart<Number,Number>(xAxis,yAxis);
  49         xAxis.setLabel("X Axis");
  50         yAxis.setLabel("Y Axis");
  51         lineChart.setTitle("HelloLineChart");
  52         // add starting data
  53         series1.getData().add(new XYChart.Data(10d, 10d));
  54         series1.getData().add(new XYChart.Data(25d, 20d));
  55         series1.getData().add(new XYChart.Data(30d, 15d));
  56         series1.getData().add(new XYChart.Data(50d, 15d));
  57         series1.getData().add(new XYChart.Data(80d, 10d));
  58         return lineChart;
  59     }
  60 
  61     private StringBuffer getSeriesLineFromPlot() {
  62         ObservableList<Node> childrenList = XYChartShim.getPlotChildren(lineChart);
  63         StringBuffer sb = new StringBuffer();
  64         for (Node n : childrenList) {
  65             if (n instanceof Path && "chart-series-line".equals(n.getStyleClass().get(0))) {
  66                 Path line = (Path)n;
  67                 sb = computeSVGPath(line);
  68                 return sb;
  69             }
  70         }
  71         return sb;
  72     }
  73 
  74     @Test
  75     public void testCreateSymbols() {
  76         startApp();
  77         lineChart.setCreateSymbols(false);
  78         pulse();
  79         lineChart.getData().addAll(series1);
  80         pulse();
  81         assertEquals(0, countSymbols(lineChart, "chart-line-symbol"));
  82 
  83         lineChart.getData().clear();
  84         pulse();
  85         lineChart.setCreateSymbols(true);
  86         pulse();
  87         lineChart.getData().addAll(series1);
  88         assertEquals(5, countSymbols(lineChart, "chart-line-symbol"));
  89     }
  90 
  91     @Test
  92     public void testDataItemAdd() {
  93         startApp();
  94         lineChart.getData().addAll(series1);
  95         pulse();
  96         series1.getData().add(new XYChart.Data(60d, 30d));
  97         pulse();
  98         // 5 stackpane nodes and 1 path node + new stackpane for data added
  99         assertEquals(7, XYChartShim.getPlotChildren(lineChart).size());
 100     }
 101 
 102      @Test @Ignore
 103      // Ignored because the animation's Timeline doesn't run. It used to be that the item was added before the
 104      // animation was run. Now the item is added as the onFinished handler of the first KeyFrame. Since the
 105      // Timeline doesn't run in the context of the unit test, this test fails. In fact, this test never really
 106      // achieved its purpose.
 107     public void testDataItemAddWithAnimation() {
 108         startApp();
 109         lineChart.setAnimated(true);
 110         lineChart.getData().addAll(series1);
 111         pulse();
 112         series1.getData().add(new XYChart.Data(60d, 30d));
 113         pulse();
 114         // 5 stackpane nodes and 1 path node + new stackpane for data added
 115         assertEquals(7, XYChartShim.getPlotChildren(lineChart).size());
 116     }
 117 
 118     @Test
 119     public void testDataItemRemove() {
 120         startApp();
 121         lineChart.getData().addAll(series1);
 122         pulse();
 123         if (!lineChart.getData().isEmpty()) {
 124             series1.getData().remove(0);
 125             pulse();
 126             // 4 stackpane nodes and one path node
 127             assertEquals(5, XYChartShim.getPlotChildren(lineChart).size());
 128         }
 129     }
 130 
 131      @Test
 132     public void testSeriesAddWithAnimation() {
 133         startApp();
 134         lineChart.setAnimated(true);
 135         final XYChart.Series<Number, Number> series2 = new XYChart.Series<Number, Number>();
 136         series1.getData().add(new XYChart.Data(15d, 40d));
 137         series1.getData().add(new XYChart.Data(25d, 10d));
 138         series1.getData().add(new XYChart.Data(40d, 35d));
 139         lineChart.getData().addAll(series1);
 140         pulse();
 141         assertEquals(true, lineChart.getAnimated());
 142     }
 143 }