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