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 import com.sun.javafx.scene.control.infrastructure.ControlTestUtils;
  30 import java.io.IOException;
  31 import java.io.OutputStream;
  32 import java.io.PrintStream;
  33 import org.junit.Test;
  34 import static org.junit.Assert.assertEquals;
  35 import javafx.collections.*;
  36 
  37 
  38 import javafx.scene.Node;
  39 import javafx.scene.Group;
  40 import javafx.scene.shape.*;
  41 import static org.junit.Assert.assertTrue;
  42 
  43 import org.junit.Ignore;
  44 
  45 
  46 public class AreaChartTest extends XYChartTestBase {
  47     AreaChart<Number,Number> ac;
  48     final XYChart.Series<Number, Number> series1 = new XYChart.Series<Number, Number>();
  49     boolean useCategoryAxis = false;
  50     final String[] countries = {"USA", "Italy", "France", "China", "India"};
  51     protected Chart createChart() {
  52         final NumberAxis yAxis = new NumberAxis();
  53         ObservableList<XYChart.Data> data = FXCollections.observableArrayList();
  54         Axis xAxis;
  55         if (useCategoryAxis) {
  56             xAxis = new CategoryAxis();
  57             ((CategoryAxis)xAxis).setCategories(FXCollections.observableArrayList(countries));
  58             // add starting data
  59         series1.getData().add(new XYChart.Data(countries[0], 10d));
  60         series1.getData().add(new XYChart.Data(countries[1], 20d));
  61         series1.getData().add(new XYChart.Data(countries[2], 15d));
  62         series1.getData().add(new XYChart.Data(countries[3], 15d));
  63         series1.getData().add(new XYChart.Data(countries[4], 10d));
  64         } else {
  65             xAxis = new NumberAxis();
  66             ac = new AreaChart<Number,Number>(xAxis,yAxis);
  67             // add starting data
  68         series1.getData().add(new XYChart.Data(10d, 10d));
  69         series1.getData().add(new XYChart.Data(25d, 20d));
  70         series1.getData().add(new XYChart.Data(30d, 15d));
  71         series1.getData().add(new XYChart.Data(50d, 15d));
  72         series1.getData().add(new XYChart.Data(80d, 10d));
  73         }
  74         
  75         xAxis.setLabel("X Axis");
  76         yAxis.setLabel("Y Axis");
  77         ac.setTitle("HelloAreaChart");
  78         
  79         return ac;
  80     }
  81     
  82     private StringBuffer getSeriesLineFromPlot() {
  83         ObservableList<Node> childrenList = ac.getPlotChildren();
  84         StringBuffer sb = new StringBuffer();
  85         for (Node n : childrenList) {
  86             if (n instanceof Group) {
  87                 for (Node gn : ((Group)n).getChildren()) {
  88                     if (gn instanceof Path && "chart-series-area-line".equals(gn.getStyleClass().get(0))) {
  89                         Path line = (Path)gn;
  90                         sb = computeSVGPath(line);
  91                         return sb;
  92                     }
  93                 }
  94             }
  95         }
  96         return sb;
  97     }
  98     
  99     @Test
 100     public void testSeriesRemove() {
 101         startApp();
 102         ac.getData().addAll(series1);
 103         pulse();
 104         if (!ac.getData().isEmpty()) {
 105             ac.getData().remove(0);
 106             pulse();
 107             StringBuffer sb = getSeriesLineFromPlot();
 108             assertEquals(sb.toString(), "");
 109         }
 110     }
 111     
 112     @Test @Ignore
 113     public void testDataItemRemove() {
 114         startApp();
 115         ac.getData().addAll(series1);
 116         pulse();
 117         if (!ac.getData().isEmpty()) {
 118             series1.getData().remove(0);
 119             pulse();
 120             StringBuffer sb = getSeriesLineFromPlot();
 121             assertEquals(sb.toString(), "L247.0 171.0 L412.0 171.0 L658.0 284.0 ");
 122         }
 123     }
 124     
 125     @Test  
 126     public void testAreaChartWithCategoryAxis() {
 127         useCategoryAxis = true;
 128         startApp();
 129         useCategoryAxis = false;
 130     }
 131     
 132     @Test public void testCreateSymbols() {
 133          startApp();
 134          ac.getData().clear();
 135          ac.setCreateSymbols(false);
 136          pulse();
 137          ac.getData().addAll(series1);
 138          pulse();
 139          assertEquals(0, countSymbols(ac, "chart-area-symbol"));
 140          
 141          ac.getData().clear();
 142          ac.setCreateSymbols(true);
 143          pulse();
 144          ac.getData().addAll(series1);
 145          pulse();
 146          assertEquals(5, countSymbols(ac, "chart-area-symbol"));
 147      }
 148 
 149     @Test
 150     public void testDataWithoutSymbolsAddWithAnimation_rt_39353() {
 151         startApp();
 152         ac.getData().addAll(series1);
 153         ac.setAnimated(true);
 154         ac.setCreateSymbols(false);
 155         series1.getData().add(new XYChart.Data(40d,10d));
 156         Thread.UncaughtExceptionHandler exceptionHandler = ControlTestUtils.setHandler();
 157         try {
 158             toolkit.setAnimationTime(0);
 159             // check remove just in case
 160             series1.getData().remove(0);
 161             toolkit.setAnimationTime(800);
 162         } finally {
 163             ControlTestUtils.resetHandler(exceptionHandler);
 164         }
 165     }
 166 }