1 /*
   2  * Copyright (c) 2012, 2016, 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 
  29 import javafx.beans.property.ListProperty;
  30 import javafx.beans.property.SimpleListProperty;
  31 import org.junit.Test;
  32 import javafx.collections.*;
  33 import javafx.scene.chart.Axis.TickMark;
  34 import javafx.css.ParsedValue;
  35 import javafx.css.CssMetaData;
  36 import javafx.css.StyleableProperty;
  37 import javafx.css.CssParserShim;
  38 import javafx.scene.Node;
  39 import javafx.scene.ParentShim;
  40 import javafx.scene.Scene;
  41 import javafx.scene.chart.AreaChart;
  42 import javafx.scene.chart.Axis;
  43 import javafx.scene.chart.AxisShim;
  44 import javafx.scene.chart.CategoryAxis;
  45 import javafx.scene.chart.Chart;
  46 import javafx.scene.chart.ChartShim;
  47 import javafx.scene.chart.NumberAxis;
  48 import javafx.scene.chart.XYChart;
  49 import javafx.scene.layout.StackPane;
  50 import javafx.scene.paint.Color;
  51 
  52 import javafx.scene.text.Font;
  53 import javafx.scene.text.Text;
  54 import javafx.stage.Stage;
  55 import org.junit.Assert;
  56 import static org.junit.Assert.assertEquals;
  57 import static org.junit.Assert.assertSame;
  58 import test.com.sun.javafx.scene.control.infrastructure.ControlTestUtils;
  59 
  60 public class XYChartTest extends ChartTestBase {
  61 
  62     NumberAxis yaxis;
  63     AreaChart<String, Number> areachart;
  64 
  65     @Override
  66     protected Chart createChart() {
  67         yaxis = new NumberAxis();
  68         areachart = new AreaChart<>(new CategoryAxis(), yaxis);
  69         return areachart;
  70     }
  71 
  72     @Test
  73     public void testTickMarksToString() {
  74         startApp();
  75         pulse();
  76         yaxis.getTickMarks().toString();
  77         System.out.println(" --- "+yaxis.getTickMarks().toString());
  78     }
  79 
  80     // RT-22166
  81     @Test public void testTickLabelFont() {
  82         startApp();
  83         Font f = yaxis.getTickLabelFont();
  84         // default caspian value for font size = 10
  85         assertEquals(10, new Double(f.getSize()).intValue());
  86         assertEquals(10, new Double(AxisShim.get_measure(yaxis).getFont().getSize()).intValue());
  87 
  88         // set tick label font via css and test if ticklabelfont, measure and tick textnode follow.
  89         ParsedValue pv = new CssParserShim().parseExpr("-fx-tick-label-font","0.916667em System");
  90         Object val = pv.convert(null);
  91         CssMetaData prop = ((StyleableProperty)yaxis.tickLabelFontProperty()).getCssMetaData();
  92         prop.set(yaxis, val, null);
  93         // confirm tickLabelFont, measure and tick's textnode all are in sync with -fx-tick-label-font
  94         assertEquals(11, new Double(yaxis.getTickLabelFont().getSize()).intValue());
  95         assertEquals(11, new Double(AxisShim.get_measure(yaxis).getFont().getSize()).intValue());
  96         final ObservableList<Axis.TickMark<Number>> yaTickMarks = yaxis.getTickMarks();
  97         TickMark tm = yaTickMarks.get(0);
  98         assertEquals(11, new Double(AxisShim.TickMark_get_textNode(tm).getFont().getSize()).intValue());
  99         // set tick label font programmatically and test.
 100         yaxis.setTickLabelFont(new Font(12.0f));
 101         assertEquals(12, new Double(yaxis.getTickLabelFont().getSize()).intValue());
 102         assertEquals(12, new Double(AxisShim.get_measure(yaxis).getFont().getSize()).intValue());
 103         assertEquals(12, new Double(AxisShim.TickMark_get_textNode(tm).getFont().getSize()).intValue());
 104     }
 105 
 106     @Test public void testSetTickLabelFill() {
 107         startApp();
 108         pulse();
 109         yaxis.setTickLabelFill(Color.web("#444444"));
 110         pulse();
 111         // Check if text node on axis has the right fill
 112         for (Node n : yaxis.getChildrenUnmodifiable()) {
 113             if (n instanceof Text) {
 114                 assertEquals(((Text)n).getFill(), Color.web("#444444"));
 115             }
 116         }
 117     }
 118 
 119     @Test public void testAddAxisWithoutSpecifyingSide() {
 120         final NumberAxis axis = new NumberAxis(0, 12, 1);
 121         axis.setMaxWidth(Double.MAX_VALUE);
 122         axis.setPrefWidth(400);
 123         pulse();
 124         StackPane layout = new StackPane();
 125         ParentShim.getChildren(layout).addAll(axis);
 126         pulse();
 127         setTestScene(new Scene(layout));
 128         setTestStage(new Stage());
 129         getTestStage().setScene(getTestScene());
 130         getTestStage().show();
 131         pulse();
 132     }
 133 
 134     @Test public void testLegendSizeWhenThereIsNoChartData() {
 135         startApp();
 136         assertEquals(0, ChartShim.getLegend(areachart).prefHeight(-1), 0);
 137         assertEquals(0, ChartShim.getLegend(areachart).prefWidth(-1), 0);
 138     }
 139 
 140 
 141     @Test public void canModifySeriesWithoutChart() {
 142         XYChart.Series series = new XYChart.Series();
 143 
 144         ObservableList<XYChart.Data> dataList1 = FXCollections.observableArrayList();
 145         dataList1.add(new XYChart.Data(0, 1));
 146         dataList1.add(new XYChart.Data(1, 2));
 147         dataList1.add(new XYChart.Data(2, 3));
 148 
 149         series.setData(dataList1);
 150 
 151         assertSame(dataList1, series.getData());
 152 
 153         ObservableList<XYChart.Data> dataList2 = FXCollections.observableArrayList();
 154         dataList2.add(new XYChart.Data(0, 3));
 155         dataList2.add(new XYChart.Data(1, 2));
 156         dataList2.add(new XYChart.Data(2, 1));
 157 
 158         series.setData(dataList2);
 159 
 160         assertSame(dataList2, series.getData());
 161     }
 162 
 163     @Test
 164     public void testBindDataToListProperty() {
 165         createChart();
 166         ListProperty<XYChart.Series<String, Number>> seriesProperty =
 167                 new SimpleListProperty<>(FXCollections.observableArrayList());
 168 
 169         areachart.dataProperty().bind(seriesProperty);
 170         ControlTestUtils.runWithExceptionHandler(() -> {
 171             seriesProperty.add(new XYChart.Series<>());
 172         });
 173     }
 174 }