1 /*
   2  * Copyright (c) 2012, 2013, 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.css;
  27 
  28 import javafx.scene.paint.Color;
  29 import com.sun.javafx.css.ParsedValueImpl;
  30 
  31 import java.util.ArrayList;
  32 import java.util.Arrays;
  33 import java.util.Collection;
  34 import java.util.Collections;
  35 import java.util.List;
  36 import org.junit.Test;
  37 import static org.junit.Assert.*;
  38 
  39 import org.junit.runner.RunWith;
  40 import org.junit.runners.Parameterized;
  41 import org.junit.runners.Parameterized.Parameters;
  42 
  43 @RunWith(Parameterized.class)
  44 public class DeclarationTest {
  45     
  46     private static class Data {
  47         private final Declaration d1, d2;
  48         private final boolean expected;
  49         Data(Declaration d1, Declaration d2, boolean expected){
  50             this.d1 = d1;
  51             this.d2 = d2;
  52             this.expected = expected;
  53         }
  54         
  55         @Override public String toString() {
  56             return "\"" + d1 + "\" " + (expected ? "==" : "!=") + " \"" + d2 + "\"";
  57         }
  58     }
  59     
  60     public DeclarationTest(Data data) {
  61         this.data = data;
  62     }
  63     private final Data data;
  64     
  65 
  66     @Parameters
  67     public static Collection data() {
  68 
  69         int n = 0;
  70         final int GI = n++; // green inline
  71         final int YI = n++; // yellow inline
  72         final int GA1 = n++; // green author 1 
  73         final int YA1 = n++; // yellow author 1 
  74         final int GA2 = n++; // green author 2 
  75         final int YA2 = n++; // yellow author 2 
  76         
  77         final Declaration[] DECLS = new Declaration[n];
  78         
  79         Stylesheet inlineSS = new Stylesheet() {
  80             {
  81                 setOrigin(StyleOrigin.INLINE);
  82                 
  83                 DECLS[GI] = new Declaration("-fx-base", new ParsedValueImpl<Color,Color>(Color.GREEN, null), false);
  84                 DECLS[YI] = new Declaration("-fx-color", new ParsedValueImpl<Color,Color>(Color.YELLOW, null), false);
  85     
  86                 Collections.addAll(getRules(),
  87                     new Rule(Arrays.asList(SimpleSelector.getUniversalSelector()), Arrays.asList(DECLS[GI])),
  88                     new Rule(Arrays.asList(SimpleSelector.getUniversalSelector()), Arrays.asList(DECLS[YI]))
  89                 );
  90             }
  91         };
  92         
  93         Stylesheet authorSS_1 = new Stylesheet() {
  94             {
  95                 setOrigin(StyleOrigin.AUTHOR);
  96                 
  97                 DECLS[GA1] = new Declaration("-fx-base", new ParsedValueImpl<Color,Color>(Color.GREEN, null), false);
  98                 DECLS[YA1] = new Declaration("-fx-color", new ParsedValueImpl<Color,Color>(Color.YELLOW, null), false);
  99     
 100                 Collections.addAll(getRules(),
 101                     new Rule(Arrays.asList(SimpleSelector.getUniversalSelector()), Arrays.asList(DECLS[GA1])),
 102                     new Rule(Arrays.asList(SimpleSelector.getUniversalSelector()), Arrays.asList(DECLS[YA1]))
 103                 );
 104             }
 105         };
 106         
 107         Stylesheet authorSS_2 = new Stylesheet() {
 108             {
 109                 setOrigin(StyleOrigin.AUTHOR);
 110                 
 111                 DECLS[GA2] = new Declaration("-fx-base", new ParsedValueImpl<Color,Color>(Color.GREEN, null), false);
 112                 DECLS[YA2] = new Declaration("-fx-color", new ParsedValueImpl<Color,Color>(Color.YELLOW, null), false);
 113     
 114                 Collections.addAll(getRules(),
 115                     new Rule(Arrays.asList(SimpleSelector.getUniversalSelector()), Arrays.asList(DECLS[GA2])),
 116                     new Rule(Arrays.asList(SimpleSelector.getUniversalSelector()), Arrays.asList(DECLS[YA2]))
 117                 );
 118             }
 119         };
 120         
 121         return Arrays.asList(new Object[] {
 122             new Object[] { new Data(DECLS[GA1], DECLS[GA2], true) },
 123             new Object[] { new Data(DECLS[GA1], DECLS[YA1], false) },
 124             new Object[] { new Data(DECLS[GA1], DECLS[GI],  false) }
 125         });
 126     }
 127     
 128     @Test
 129     public void testEquals() {
 130 
 131         Declaration instance = data.d1;
 132         Declaration obj = data.d2;
 133         boolean expected = data.expected;
 134         boolean actual = instance.equals(obj);
 135         assertTrue(data.toString(), expected == actual);
 136         
 137     }
 138 
 139 }