1 /*
   2  * Copyright (c) 2012, 2014, 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 javafx.scene.chart;
  27 
  28 import com.sun.javafx.scene.control.infrastructure.ControlTestUtils;
  29 import javafx.collections.ObservableList;
  30 import javafx.scene.Node;
  31 import javafx.scene.shape.Path;
  32 import static org.junit.Assert.assertEquals;
  33 import org.junit.Ignore;
  34 import org.junit.Test;
  35 
  36 public class LineChartTest extends XYChartTestBase {
  37 
  38     LineChart<Number,Number> lineChart;
  39     final XYChart.Series<Number, Number> series1 = new XYChart.Series<Number, Number>();
  40     
  41     @Override protected Chart createChart() {
  42         final NumberAxis xAxis = new NumberAxis();
  43         final NumberAxis yAxis = new NumberAxis();
  44         lineChart = new LineChart<Number,Number>(xAxis,yAxis);
  45         xAxis.setLabel("X Axis");
  46         yAxis.setLabel("Y Axis");
  47         lineChart.setTitle("HelloLineChart");
  48         // add starting data
  49         series1.getData().add(new XYChart.Data(10d, 10d));
  50         series1.getData().add(new XYChart.Data(25d, 20d));
  51         series1.getData().add(new XYChart.Data(30d, 15d));
  52         series1.getData().add(new XYChart.Data(50d, 15d));
  53         series1.getData().add(new XYChart.Data(80d, 10d));
  54         return lineChart;
  55     }
  56     
  57     private StringBuffer getSeriesLineFromPlot() {
  58         ObservableList<Node> childrenList = lineChart.getPlotChildren();
  59         StringBuffer sb = new StringBuffer();
  60         for (Node n : childrenList) {
  61             if (n instanceof Path && "chart-series-line".equals(n.getStyleClass().get(0))) {
  62                 Path line = (Path)n;
  63                 sb = computeSVGPath(line);
  64                 return sb;
  65             }
  66         }
  67         return sb;
  68     }
  69 
  70     @Test
  71     public void testSeriesRemove() {
  72         startApp();
  73         lineChart.getData().addAll(series1);
  74         pulse();
  75         // 5 symbols and 1 line node
  76         assertEquals(6, lineChart.getPlotChildren().size());
  77         lineChart.getData().remove(0);
  78         pulse();
  79         assertEquals(0, lineChart.getPlotChildren().size());
  80     }
  81 
  82     @Test
  83     public void testSeriesRemoveWithoutSymbols() {
  84         startApp();
  85         lineChart.setCreateSymbols(false);
  86         lineChart.getData().addAll(series1);
  87         pulse();
  88         // 1 line node
  89         assertEquals(1, lineChart.getPlotChildren().size());
  90         lineChart.getData().remove(0);
  91         pulse();
  92         assertEquals(0, lineChart.getPlotChildren().size());
  93     }
  94 
  95     @Test
  96     public void testSeriesRemoveWithoutSymbolsAnimated_rt_22124() {
  97         startApp();
  98         lineChart.setCreateSymbols(false);
  99         lineChart.getData().addAll(series1);
 100         pulse();
 101         // 1 line node
 102         assertEquals(1, lineChart.getPlotChildren().size());
 103 
 104         lineChart.setAnimated(true);
 105         Thread.UncaughtExceptionHandler exceptionHandler = ControlTestUtils.setHandler();
 106         try {
 107             lineChart.getData().remove(0);
 108         } finally {
 109             ControlTestUtils.resetHandler(exceptionHandler);
 110         }
 111         toolkit.setAnimationTime(450);
 112         assertEquals(1, lineChart.getPlotChildren().size());
 113         assertEquals(0.5, lineChart.getPlotChildren().get(0).getOpacity(), 0.0);
 114         toolkit.setAnimationTime(900);
 115         assertEquals(0, lineChart.getPlotChildren().size());
 116     }
 117 
 118     @Test
 119     public void testCreateSymbols() {
 120         startApp();
 121         lineChart.setCreateSymbols(false);
 122         pulse();
 123         lineChart.getData().addAll(series1);
 124         pulse();
 125         assertEquals(0, countSymbols(lineChart, "chart-line-symbol"));
 126 
 127         lineChart.getData().clear();
 128         pulse();
 129         lineChart.setCreateSymbols(true);
 130         pulse();
 131         lineChart.getData().addAll(series1);
 132         assertEquals(5, countSymbols(lineChart, "chart-line-symbol"));
 133     }
 134 
 135     @Test
 136     public void testDataItemAdd() {
 137         startApp();
 138         lineChart.getData().addAll(series1);
 139         pulse();
 140         series1.getData().add(new XYChart.Data(60d, 30d));
 141         pulse();
 142         // 5 stackpane nodes and 1 path node + new stackpane for data added
 143         assertEquals(7, lineChart.getPlotChildren().size());
 144     }
 145 
 146      @Test @Ignore
 147      // Ignored because the animation's Timeline doesn't run. It used to be that the item was added before the
 148      // animation was run. Now the item is added as the onFinished handler of the first KeyFrame. Since the
 149      // Timeline doesn't run in the context of the unit test, this test fails. In fact, this test never really
 150      // achieved its purpose.
 151     public void testDataItemAddWithAnimation() {
 152         startApp();
 153         lineChart.setAnimated(true);
 154         lineChart.getData().addAll(series1);
 155         pulse();
 156         series1.getData().add(new XYChart.Data(60d, 30d));
 157         pulse();
 158         // 5 stackpane nodes and 1 path node + new stackpane for data added
 159         assertEquals(7, lineChart.getPlotChildren().size());
 160     }
 161       
 162     @Test
 163     public void testDataItemRemove() {
 164         startApp();
 165         lineChart.getData().addAll(series1);
 166         pulse();
 167         if (!lineChart.getData().isEmpty()) {
 168             series1.getData().remove(0);
 169             pulse();
 170             // 4 stackpane nodes and one path node
 171             assertEquals(5, lineChart.getPlotChildren().size());
 172         }
 173     }
 174      
 175      @Test
 176     public void testSeriesAddWithAnimation() {
 177         startApp();
 178         lineChart.setAnimated(true);
 179         final XYChart.Series<Number, Number> series2 = new XYChart.Series<Number, Number>();
 180         series1.getData().add(new XYChart.Data(15d, 40d));
 181         series1.getData().add(new XYChart.Data(25d, 10d));
 182         series1.getData().add(new XYChart.Data(40d, 35d));
 183         lineChart.getData().addAll(series1);
 184         pulse();
 185         assertEquals(true, lineChart.getAnimated());
 186     }
 187 }