1 /*
   2  * Copyright (c) 2011, 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 test.javafx.scene;
  27 
  28 import com.sun.javafx.css.StyleManager;
  29 import javafx.css.Stylesheet;
  30 import javafx.css.CssParser;
  31 import com.sun.javafx.tk.Toolkit;
  32 import static javafx.geometry.NodeOrientation.*;
  33 import javafx.scene.Group;
  34 import javafx.scene.Scene;
  35 import javafx.scene.paint.Color;
  36 import javafx.scene.shape.Rectangle;
  37 import javafx.stage.Stage;
  38 import org.junit.After;
  39 import org.junit.Ignore;
  40 import org.junit.Test;
  41 import static org.junit.Assert.*;
  42 import org.junit.Before;
  43 
  44 /**
  45  * Test :dir functional pseudo-class 
  46  */
  47 @Ignore
  48 public class Node_effectiveOrientation_Css_Test {
  49     
  50     private Group root;
  51     private Scene scene;
  52     private Stage stage;
  53 
  54     @Before
  55     public void setUp() {
  56         root = new Group();
  57         scene = new Scene(root);
  58         stage = new Stage();
  59         stage.setScene(scene);
  60         stage.show();
  61         stage.requestFocus();
  62     }
  63 
  64     @After
  65     public void tearDown() {
  66         stage.hide();
  67     }
  68     
  69     public Node_effectiveOrientation_Css_Test() {}
  70     
  71     @Test
  72     public void test_SimpleSelector_dir_pseudoClass_with_scene_effective_orientation_ltr() {
  73         Stylesheet stylesheet = new CssParser().parse(
  74                 ".rect:dir(rtl) { -fx-fill: #ff0000; }" +
  75                 ".rect:dir(ltr) { -fx-fill: #00ff00; }" +
  76                 ".rect { -fx-fill: #0000ff; }" 
  77         );
  78         StyleManager.getInstance().setDefaultUserAgentStylesheet(stylesheet);
  79 
  80         Rectangle rect = new Rectangle();
  81         rect.getStyleClass().add("rect");
  82         root.getChildren().add(rect);
  83 
  84         // CSS is applied on next pulse after child is added
  85         root.applyCss();
  86         
  87         assertEquals(scene.getEffectiveNodeOrientation(), LEFT_TO_RIGHT);
  88         assertEquals(Color.web("#00ff00"), rect.getFill());
  89     }
  90     
  91     @Test
  92     public void test_SimpleSelector_dir_pseudoClass_with_scene_effective_orientation_rtl() {
  93         Stylesheet stylesheet = new CssParser().parse(
  94                 ".rect:dir(rtl) { -fx-fill: #ff0000; }" +
  95                 ".rect:dir(ltr) { -fx-fill: #00ff00; }" +
  96                 ".rect { -fx-fill: #0000ff; }" 
  97         );
  98         StyleManager.getInstance().setDefaultUserAgentStylesheet(stylesheet);
  99 
 100         Rectangle rect = new Rectangle();
 101         rect.getStyleClass().add("rect");
 102         root.getChildren().add(rect);
 103 
 104         scene.setNodeOrientation(RIGHT_TO_LEFT);
 105         // CSS is applied on next pulse after child is added
 106         root.applyCss();
 107         
 108         assertEquals(scene.getEffectiveNodeOrientation(), RIGHT_TO_LEFT);
 109         assertEquals(Color.web("#ff0000"), rect.getFill());
 110     }
 111 
 112     @Test
 113     public void test_CompounSelector_dir_pseudoClass_on_parent_with_scene_effective_orientation_ltr() {
 114         Stylesheet stylesheet = new CssParser().parse(
 115                 ".root:dir(rtl) .rect { -fx-fill: #ff0000; }" +
 116                 ".root:dir(ltr) .rect { -fx-fill: #00ff00; }" +
 117                 ".root .rect { -fx-fill: #0000ff; }" 
 118         );
 119         StyleManager.getInstance().setDefaultUserAgentStylesheet(stylesheet);
 120 
 121         Rectangle rect = new Rectangle();
 122         rect.getStyleClass().add("rect");
 123         root.getChildren().add(rect);
 124 
 125         // CSS is applied on next pulse after child is added
 126         root.applyCss();
 127         
 128         assertEquals(scene.getEffectiveNodeOrientation(), LEFT_TO_RIGHT);
 129         assertEquals(Color.web("#00ff00"), rect.getFill());
 130     }
 131     
 132     @Test
 133     public void test_CompoundSelector_dir_pseudoClass_on_parent_with_scene_effective_orientation_rtl() {
 134         Stylesheet stylesheet = new CssParser().parse(
 135                 ".root:dir(rtl) .rect { -fx-fill: #ff0000; }" +
 136                 ".root:dir(ltr) .rect { -fx-fill: #00ff00; }" +
 137                 ".root .rect { -fx-fill: #0000ff; }" 
 138         );
 139         StyleManager.getInstance().setDefaultUserAgentStylesheet(stylesheet);
 140 
 141         Rectangle rect = new Rectangle();
 142         rect.getStyleClass().add("rect");
 143         root.getChildren().add(rect);
 144 
 145         scene.setNodeOrientation(RIGHT_TO_LEFT);
 146         // CSS is applied on next pulse after child is added
 147         root.applyCss();
 148         
 149         assertEquals(scene.getEffectiveNodeOrientation(), RIGHT_TO_LEFT);
 150         assertEquals(Color.web("#ff0000"), rect.getFill());
 151     }
 152 
 153     @Test
 154     public void test_CompounSelector_dir_pseudoClass_on_child_with_scene_effective_orientation_ltr() {
 155         Stylesheet stylesheet = new CssParser().parse(
 156                 ".root .rect:dir(rtl) { -fx-fill: #ff0000; }" +
 157                 ".root .rect:dir(ltr) { -fx-fill: #00ff00; }" +
 158                 ".root .rect { -fx-fill: #0000ff; }" 
 159         );
 160         StyleManager.getInstance().setDefaultUserAgentStylesheet(stylesheet);
 161 
 162         Rectangle rect = new Rectangle();
 163         rect.getStyleClass().add("rect");
 164         root.getChildren().add(rect);
 165 
 166         // CSS is applied on next pulse after child is added
 167         root.applyCss();
 168         
 169         assertEquals(scene.getEffectiveNodeOrientation(), LEFT_TO_RIGHT);
 170         assertEquals(Color.web("#00ff00"), rect.getFill());
 171     }
 172     
 173     @Test
 174     public void test_CompoundSelector_dir_pseudoClass_on_child_with_scene_effective_orientation_rtl() {
 175         Stylesheet stylesheet = new CssParser().parse(
 176                 ".root .rect:dir(rtl) { -fx-fill: #ff0000; }" +
 177                 ".root .rect:dir(ltr) { -fx-fill: #00ff00; }" +
 178                 ".root .rect { -fx-fill: #0000ff; }" 
 179         );
 180         StyleManager.getInstance().setDefaultUserAgentStylesheet(stylesheet);
 181 
 182         Rectangle rect = new Rectangle();
 183         rect.getStyleClass().add("rect");
 184         root.getChildren().add(rect);
 185 
 186         scene.setNodeOrientation(RIGHT_TO_LEFT);
 187         // CSS is applied on next pulse after child is added
 188         root.applyCss();
 189         
 190         assertEquals(scene.getEffectiveNodeOrientation(), RIGHT_TO_LEFT);
 191         assertEquals(Color.web("#ff0000"), rect.getFill());
 192     }
 193     
 194     @Test
 195     public void test_dir_pseudoClass_functions_on_scene_effective_orientation_not_node() {
 196         Stylesheet stylesheet = new CssParser().parse(
 197                 ".rect:dir(rtl) { -fx-fill: #ff0000; }" +
 198                 ".rect:dir(ltr) { -fx-fill: #00ff00; }" +
 199                 ".rect { -fx-fill: #0000ff; }" 
 200         );
 201         StyleManager.getInstance().setDefaultUserAgentStylesheet(stylesheet);
 202 
 203         Rectangle rect = new Rectangle();
 204         rect.getStyleClass().add("rect");
 205         root.getChildren().add(rect);
 206         
 207         rect.setNodeOrientation(RIGHT_TO_LEFT);
 208 
 209         // CSS is applied on next pulse after child is added
 210         root.applyCss();
 211         
 212         assertEquals(scene.getEffectiveNodeOrientation(), LEFT_TO_RIGHT);
 213         assertEquals(rect.getEffectiveNodeOrientation(), RIGHT_TO_LEFT);
 214         // :dir() pseudo-class functions on scene effective orientation, not node
 215         assertEquals(Color.web("#00ff00"), rect.getFill());
 216         
 217         scene.setNodeOrientation(RIGHT_TO_LEFT);
 218         rect.setNodeOrientation(LEFT_TO_RIGHT);
 219         
 220         root.applyCss();
 221         
 222         assertEquals(scene.getEffectiveNodeOrientation(), RIGHT_TO_LEFT);
 223         assertEquals(rect.getEffectiveNodeOrientation(), LEFT_TO_RIGHT);
 224         // :dir() pseudo-class functions on scene effective orientation, not node
 225         assertEquals(Color.web("#ff0000"), rect.getFill());
 226         
 227     }
 228     
 229 }