1 /*
   2  * Copyright (c) 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 package test.css.controls.api.styles;
  26 
  27 import com.sun.javafx.scene.layout.region.BorderImageSlices;
  28 import com.sun.javafx.scene.layout.region.RepeatStruct;
  29 import java.util.ArrayList;
  30 import javafx.css.CssMetaData;
  31 import java.util.HashMap;
  32 import java.util.List;
  33 import java.util.Map;
  34 import java.util.Set;
  35 import java.util.TreeSet;
  36 import javafx.css.Styleable;
  37 import javafx.scene.Node;
  38 import javafx.scene.layout.BackgroundPosition;
  39 import javafx.scene.layout.BorderWidths;
  40 import org.junit.BeforeClass;
  41 import test.css.controls.EmptyApp;
  42 import test.javaclient.shared.Utils;
  43 
  44 /**
  45  *
  46  * @author sergey.lugovoy@oracle.com
  47  */
  48 public abstract class BaseStyleNodeTest {
  49 
  50     protected static final BorderWidths[] DEFAULT_WIDTHS = new BorderWidths[] {BorderWidths.DEFAULT};
  51 
  52     protected Node control;
  53 
  54     abstract Node getControl();
  55 
  56     @BeforeClass
  57     public static void createGui() {
  58         Utils.launch(EmptyApp.class, new String[]{test.javaclient.shared.AppLauncher.WAIT_TOOLKIT_START_ONLY});
  59     }
  60 
  61     protected Map<String, CssMetaData> getStyles() {
  62         Map<String, CssMetaData> styles = new HashMap<>();
  63         for (CssMetaData data : getListWithSubProperty(getControl().getCssMetaData())) {
  64             styles.put(data.getProperty(), data);
  65         }
  66         return styles;
  67     }
  68 
  69     protected Set<String> getStyleNames() {
  70         List<CssMetaData<? extends Styleable, ?>> stylesList = getListWithSubProperty(getControl().getCssMetaData());
  71         Set<String> styles = new TreeSet<>();
  72         for (CssMetaData data : stylesList) {
  73             styles.add(data.getProperty());
  74         }
  75         return styles;
  76     }
  77 
  78     private List<CssMetaData<? extends Styleable, ?>> getListWithSubProperty(List<CssMetaData<? extends Styleable, ?>> list) {
  79         ArrayList<CssMetaData<? extends Styleable, ?>> newList = new ArrayList<>(list);
  80         for (CssMetaData data : list) {
  81             List<CssMetaData<? extends Styleable, ?>> sub = data.getSubProperties();
  82             if (sub != null && sub.size() > 0) {
  83                 newList.addAll(getListWithSubProperty(sub));
  84             }
  85         }
  86         return newList;
  87     }
  88 
  89     public boolean checkBackgroundPosition(BackgroundPosition[] first, BackgroundPosition[] second) {
  90         if (first == second) {
  91             return true;
  92         }
  93         if (first.length != second.length) {
  94             return false;
  95         }
  96         for (int i = 0; i < first.length; i++) {
  97             BackgroundPosition firstPosition = first[i];
  98             BackgroundPosition secondPosition = second[i];
  99             if (firstPosition == secondPosition) {
 100                 continue;
 101             }
 102             if (firstPosition.isHorizontalAsPercentage() != secondPosition.isHorizontalAsPercentage()) {
 103                 return false;
 104             }
 105             if (firstPosition.isVerticalAsPercentage() != secondPosition.isVerticalAsPercentage()) {
 106                 return false;
 107             }
 108             if (Double.valueOf(firstPosition.getHorizontalPosition()).longValue() != Double.valueOf(secondPosition.getHorizontalPosition()).longValue()) {
 109                 return false;
 110             }
 111             if (Double.valueOf(firstPosition.getVerticalPosition()).longValue() != Double.valueOf(secondPosition.getVerticalPosition()).longValue()) {
 112                 return false;
 113             }
 114             if (firstPosition.getHorizontalSide() != secondPosition.getHorizontalSide()) {
 115                 return false;
 116             }
 117             if (firstPosition.getVerticalPosition() != secondPosition.getVerticalPosition()) {
 118                 return false;
 119             }
 120         }
 121         return true;
 122     }
 123 
 124     public boolean checkRepeatStruct(RepeatStruct[] first, RepeatStruct[] second) {
 125         if (first == second) {
 126             return true;
 127         }
 128         if (first.length != second.length) {
 129             return false;
 130         }
 131         for (int i = 0; i < first.length; i++) {
 132             RepeatStruct firstStruct = first[i];
 133             RepeatStruct secondStruct = second[i];
 134             if (firstStruct == secondStruct) {
 135                 continue;
 136             }
 137             if (firstStruct.repeatX != secondStruct.repeatX) {
 138                 return false;
 139             }
 140             if (firstStruct.repeatY != secondStruct.repeatY) {
 141                 return false;
 142             }
 143         }
 144         return true;
 145     }
 146 
 147     public boolean checkBorderImageSlices(BorderImageSlices[] first, BorderImageSlices[] second) {
 148         if (first == second) {
 149             return true;
 150         }
 151         if (first.length != second.length) {
 152             return false;
 153         }
 154         for (int i = 0; i < first.length; i++) {
 155             BorderImageSlices firstStruct = first[i];
 156             BorderImageSlices secondStruct = second[i];
 157             if (firstStruct == secondStruct) {
 158                 continue;
 159             }
 160             if (!firstStruct.widths.equals(secondStruct.widths)) {
 161                 return false;
 162             }
 163             if (firstStruct.filled != secondStruct.filled) {
 164                 return false;
 165             }
 166         }
 167         return true;
 168     }
 169 }