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