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 com.sun.javafx.css;
  27 
  28 import static org.junit.Assert.*;
  29 
  30 import javafx.beans.value.ChangeListener;
  31 import javafx.beans.value.ObservableValue;
  32 import javafx.scene.Cursor;
  33 import javafx.scene.Group;
  34 import javafx.scene.Scene;
  35 import javafx.scene.shape.Rectangle;
  36 import javafx.scene.text.Font;
  37 import javafx.scene.text.FontSmoothingType;
  38 import javafx.scene.text.Text;
  39 import javafx.stage.Stage;
  40 import javafx.stage.Window;
  41 
  42 import com.sun.javafx.util.Logging;
  43 import org.junit.Before;
  44 import org.junit.Test;
  45 import sun.util.logging.PlatformLogger;
  46 
  47 /**
  48  * AKA: RT-7401. Tests that the pattern used works by testing opacity
  49  * specifically. Tests for font and text-fill should be done in the tests
  50  * for Label and Labeled.
  51  */
  52 public class HonorDeveloperSettingsTest {
  53 
  54     private Scene scene;
  55     private Rectangle rect;
  56     private Text text;
  57 
  58     // Scene must have a Window for CSS to load the stylesheet.
  59     // And Window must have a Scene for StyleManager to find the right scene
  60     static class TestWindow extends Window {
  61         @Override public void setScene(Scene value) {
  62             super.setScene(value);
  63         }
  64     }
  65 
  66     @Before
  67     public void setUp() {
  68         rect = new Rectangle();
  69         rect.setId("rectangle");
  70 
  71         text = new Text();
  72         text.setId("text");
  73 
  74         Group group = new Group();
  75         group.getChildren().addAll(rect, text);
  76 
  77         scene = new Scene(group);/* {
  78             TestWindow window;
  79             {
  80                 window = new TestWindow();
  81                 window.setScene(HonorDeveloperSettingsTest.this.scene);
  82                 impl_setWindow(window);
  83             }
  84         };*/
  85         
  86         System.setProperty("binary.css", "false");
  87         String url = getClass().getResource("HonorDeveloperSettingsTest_UA.css").toExternalForm();
  88         StyleManager.getInstance().getInstance().setDefaultUserAgentStylesheet(url);
  89         
  90         Stage stage = new Stage();
  91         stage.setScene(scene);
  92         stage.show();
  93     }
  94 
  95     @Test
  96     public void testOpacityIsSetByCSSByDefault() {
  97         rect.applyCss();
  98         assertEquals(.76, rect.getOpacity(), 0.01);
  99     }
 100 
 101     @Test
 102     public void testOpacityWithInitializedValueSameAsDefaultValueIsIgnoredByCSS() {
 103         rect.setOpacity(1.0);
 104         rect.applyCss();
 105         assertEquals(1.0, rect.getOpacity(), 0.01);
 106     }
 107 
 108     @Test
 109     public void testOpacityWithInitializedValueIsIgnoredByCSS() {
 110         rect.setOpacity(0.535);
 111         rect.applyCss();
 112         assertEquals(0.535, rect.getOpacity(), 0.01);
 113     }
 114 
 115     @Test
 116     public void testOpacityWithManuallyChangedValueIsIgnoredByCSS() {
 117         rect.applyCss();
 118         assertEquals(.76, rect.getOpacity(), 0.01);
 119         rect.setOpacity(.873);
 120         rect.applyCss();
 121         assertEquals(.873, rect.getOpacity(), 0.01);
 122     }
 123 
 124     @Test
 125     public void testOpacityWithManuallyChangedValueAndInlineStyleIsSetToInlineStyle() {
 126         rect.applyCss();
 127         assertEquals(.76, rect.getOpacity(), 0.01);
 128         rect.setStyle("-fx-opacity: 42%;");
 129         rect.setOpacity(.873);
 130         rect.applyCss();
 131         assertEquals(.42, rect.getOpacity(), 0.01);
 132     }
 133 
 134     @Test
 135     public void testCursorIsSetByCSSByDefault() {
 136         rect.applyCss();
 137         assertEquals(Cursor.HAND, rect.getCursor());
 138     }
 139 
 140     @Test
 141     public void testCursorWithInitializedValueSameAsDefaultValueIsIgnoredByCSS() {
 142         rect.setCursor(null);
 143         rect.applyCss();
 144         assertEquals(null, rect.getCursor());
 145     }
 146 
 147     @Test
 148     public void testCursorWithInitializedValueIsIgnoredByCSS() {
 149         rect.setCursor(Cursor.WAIT);
 150         rect.applyCss();
 151         assertEquals(Cursor.WAIT, rect.getCursor());
 152     }
 153 
 154     @Test
 155     public void testCursorWithManuallyChangedValueIsIgnoredByCSS() {
 156         rect.applyCss();
 157         assertEquals(Cursor.HAND, rect.getCursor());
 158         rect.setCursor(Cursor.WAIT);
 159         rect.applyCss();
 160         assertEquals(Cursor.WAIT, rect.getCursor());
 161     }
 162 
 163 //    public void testEffectIsSetByCSSByDefault() {
 164 //        final Rectangle rect = Rectangle { id: "rectangle" }
 165 //        scene.stylesheets = "{__DIR__}HonorDeveloperSettingsTest.css";
 166 //        scene.content = rect;
 167 //        rect.applyCss();
 168 //        assertNotNull(rect.effect);
 169 //    }
 170 //
 171 //    public void testEffectWithInitializedValueSameAsDefaultValueIsIgnoredByCSS() {
 172 //        final Rectangle rect = Rectangle {
 173 //            id: "rectangle"
 174 //            effect: null
 175 //        }
 176 //        scene.stylesheets = "{__DIR__}HonorDeveloperSettingsTest.css";
 177 //        scene.content = rect;
 178 //        rect.applyCss();
 179 //        assertNull(rect.effect);
 180 //    }
 181 //
 182 //    public void testEffectWithInitializedValueIsIgnoredByCSS() {
 183 //        DropShadow shadow = DropShadow { }
 184 //        final Rectangle rect = Rectangle {
 185 //            id: "rectangle"
 186 //            effect: shadow
 187 //        }
 188 //        scene.stylesheets = "{__DIR__}HonorDeveloperSettingsTest.css";
 189 //        scene.content = rect;
 190 //        rect.applyCss();
 191 //        assertSame(shadow, rect.effect);
 192 //    }
 193 //
 194 //    public void testEffectWithManuallyChangedValueIsIgnoredByCSS() {
 195 //        DropShadow shadow = DropShadow { }
 196 //        final Rectangle rect = Rectangle { id: "rectangle" }
 197 //        scene.stylesheets = "{__DIR__}HonorDeveloperSettingsTest.css";
 198 //        scene.content = rect;
 199 //        rect.applyCss();
 200 //        assertNotSame(shadow, rect.effect);
 201 //        rect.effect = shadow;
 202 //        rect.applyCss();
 203 //        assertSame(shadow, rect.effect);
 204 //    }
 205 
 206     @Test
 207     public void testFontIsSetByCSSByDefault() {
 208         text.applyCss();
 209         assertNotSame(Font.getDefault(), text.getFont());
 210     }
 211 
 212     @Test
 213     public void testFontWithInitializedValueSameAsDefaultValueIsIgnoredByCSS() {
 214         text.setFont(Font.getDefault());
 215         text.applyCss();
 216         assertSame(Font.getDefault(), text.getFont());
 217     }
 218 
 219     @Test
 220     public void testFontWithInitializedValueIsIgnoredByCSS() {
 221         Font f = Font.font(Font.getDefault().getFamily(), 54.0);
 222         text.setFont(f);
 223         text.applyCss();
 224         assertSame(f, text.getFont());
 225     }
 226 
 227     @Test
 228     public void testFontWithManuallyChangedValueIsIgnoredByCSS() {
 229         Font f = Font.font(Font.getDefault().getFamily(), 54.0);
 230         text.applyCss();
 231         assertNotSame(f, text.getFont());
 232         text.setFont(f);
 233         text.applyCss();
 234         assertSame(f, text.getFont());
 235     }
 236     
 237     @Test
 238     public void testUseInheritedFontSizeFromStylesheetForEmSize() {
 239         
 240         String url = getClass().getResource("HonorDeveloperSettingsTest_AUTHOR.css").toExternalForm();
 241         scene.getStylesheets().add(url);
 242         scene.getRoot().applyCss();
 243         assertEquals(20, rect.getStrokeWidth(), 0.00001);
 244         
 245     }
 246     
 247     @Test
 248     public void testInhertWithNoStyleDoesNotOverrideUserSetValue() {
 249         Font font = Font.font("Amble", 14);
 250         text.setFont(font);
 251         
 252         String url = getClass().getResource("HonorDeveloperSettingsTest_AUTHOR.css").toExternalForm();
 253         scene.getStylesheets().add(url);
 254            
 255         scene.getRoot().applyCss();
 256         //
 257         // Stroke width is set to 1em in the author stylesheet. If 
 258         // RT-20145 is not working, then the code will pick up the 20px
 259         // font size.
 260         //
 261         assertEquals(14, text.getStrokeWidth(), 0.00001);
 262         
 263     }
 264     
 265     @Test
 266     public void testInlineStyleInheritedFromParentApplies() {
 267 
 268         // Must remove the id so we don't match on the ua style.
 269         text.setId(null);
 270         text.setStyle("-fx-stroke-width: 1em; -fx-stroke: red;");
 271         
 272         scene.getRoot().setStyle("-fx-font: 18 Amble;");
 273        
 274         scene.getRoot().applyCss();
 275         
 276         //
 277         // If RT-20513 is not working, then the code will _not_ 
 278         // pick up the inline style
 279         //        
 280         assertEquals(18, text.getStrokeWidth(), 0.00001);
 281         
 282     }
 283     
 284     @Test
 285     public void testInlineStyleNotInheritedFromParentWhenUserSetsFont() {
 286         
 287         text.setStyle("-fx-stroke-width: 1em;");
 288         
 289         Font font = Font.font("Amble", 14);
 290         text.setFont(font);
 291         
 292         scene.getRoot().setStyle("-fx-font: 18 Amble;");
 293         
 294         scene.getRoot().applyCss();
 295                 
 296         assertEquals(14, text.getStrokeWidth(), 0.00001);
 297         
 298     }
 299 
 300     
 301     @Test public void test_RT_20686_UAStyleDoesNotOverrideSetFontSmoothingType() {
 302         
 303         text.setId("rt-20686-ua");
 304         text.setFontSmoothingType(FontSmoothingType.LCD);
 305         
 306         scene.getRoot().applyCss();
 307          
 308         assertEquals(FontSmoothingType.LCD, text.getFontSmoothingType());
 309         
 310     }
 311     
 312     @Test public void test_RT_20686_AuthorStyleOverridesSetFontSmoothingType() {
 313         
 314         String url = getClass().getResource("HonorDeveloperSettingsTest_AUTHOR.css").toExternalForm();
 315         scene.getStylesheets().add(url);
 316 
 317         text.setId("rt-20686-author");
 318         text.setFontSmoothingType(FontSmoothingType.GRAY);
 319         
 320         scene.getRoot().applyCss();
 321                 
 322         assertEquals(FontSmoothingType.LCD, text.getFontSmoothingType());
 323         
 324     }
 325 
 326     // this test is the prerequisite for the inline font style tests
 327     @Test public void test_InlineFontStyleApplies() {
 328         
 329         // text  has id "text". still, inline style should win out. 
 330         text.setStyle("-fx-font-size: 24;");
 331 
 332         scene.getRoot().applyCss();
 333 
 334         double size = text.getFont().getSize();
 335         assertEquals(24, size, .0001);
 336         
 337     }
 338     
 339     // this test is the prerequisite for the inline font style tests
 340     @Test public void test_FontInheritsFromDotRootStyle() {
 341         
 342         String url = getClass().getResource("HonorDeveloperSettingsTest_AUTHOR.css").toExternalForm();
 343         scene.getStylesheets().add(url);
 344 
 345         // want text to get font style from .root
 346         text.setId(null);
 347         
 348         scene.getRoot().applyCss();
 349                 
 350         double size = text.getFont().getSize();
 351         assertEquals(20, size, .0001);
 352         
 353     }
 354     
 355     @Test public void test_InlineFontStyleOverridesStylesheetStyles() {
 356         
 357         String url = getClass().getResource("HonorDeveloperSettingsTest_AUTHOR.css").toExternalForm();
 358         scene.getStylesheets().add(url);
 359 
 360         // want text to get font style from .root
 361         // assuming here that test_FontInheritsFromDotRootStyle passed
 362         text.setId(null);
 363         text.setStyle("-fx-font-size: 24;");
 364 
 365         scene.getRoot().applyCss();
 366                
 367         double size = text.getFont().getSize();
 368         assertEquals(24, size, .0001);
 369         
 370     }
 371     
 372     @Test public void test_InlineFontStyleFromParentOverridesStylesheetStyles() {
 373         
 374         String url = getClass().getResource("HonorDeveloperSettingsTest_AUTHOR.css").toExternalForm();
 375         scene.getStylesheets().add(url);
 376         
 377         // want text to get font style from .root
 378         // assuming here that test_FontInheritsFromDotRootStyle passed
 379         text.setId(null);
 380 
 381         Group g = (Group)scene.getRoot();
 382         g.setStyle("-fx-font-size: 32;");
 383         
 384         g.applyCss();
 385                 
 386         double size = text.getFont().getSize();
 387         assertEquals(32, size, .0001);
 388         
 389     }
 390     
 391 }