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