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