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