1 /*
   2  * Copyright (c) 2014, 2016 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.style.size;
  26 
  27 import javafx.scene.Node;
  28 import javafx.scene.Scene;
  29 import javafx.scene.control.Control;
  30 import javafx.scene.layout.Region;
  31 import org.jemmy.control.Wrap;
  32 import org.jemmy.fx.ByID;
  33 import org.jemmy.fx.Root;
  34 import org.jemmy.image.Image;
  35 import org.jemmy.image.pixel.Raster;
  36 import org.jemmy.image.pixel.RasterComparator;
  37 import org.jemmy.interfaces.Parent;
  38 import org.jemmy.timing.State;
  39 import org.junit.Assert;
  40 import org.junit.Before;
  41 import org.junit.Test;
  42 import test.css.controls.api.SizeStyleApp;
  43 import test.javaclient.shared.TestBase;
  44 import test.javaclient.shared.screenshots.GoldenImageManager;
  45 
  46 /**
  47  *
  48  * @author sergey.lugovoy@oracle.com
  49  */
  50 abstract public class SizeStyleBaseTest extends TestBase {
  51 
  52     protected Wrap<? extends Region> golden_control;
  53     protected Wrap<? extends Region> control;
  54     protected Wrap<? extends Scene> sceneWrap;
  55 
  56     abstract String getPageName();
  57 
  58     @Before
  59     public void clean() {
  60         sceneWrap = null;
  61         control = null;
  62         golden_control = null;
  63     }
  64 
  65     /**
  66      * Compare two Controls, first with set API,second with set stylesheet style
  67      * "-fx-min-width"
  68      */
  69     @Test
  70     public void minWidthTest() {
  71         testCommon(getPageName(), SizeStyleApp.SizePages.MIN_WIDTH.toString(), false, false);
  72         lookupControls();
  73         checkMinWidth();
  74         checkScreenShots(SizeStyleApp.SizePages.MIN_WIDTH.toString());
  75     }
  76 
  77     /**
  78      * Compare two Controls, first with set API,second with set stylesheet style
  79      * "-fx-min-height"
  80      */
  81     @Test
  82     public void minHeightTest() {
  83         testCommon(getPageName(), SizeStyleApp.SizePages.MIN_HEIGHT.toString(), false, false);
  84         lookupControls();
  85         checkMinHeight();
  86         checkScreenShots(SizeStyleApp.SizePages.MIN_HEIGHT.toString());
  87     }
  88 
  89     /**
  90      * Compare two Controls, first with set API,second with set stylesheet style
  91      * "-fx-max-height"
  92      */
  93     @Test
  94     public void maxHeightTest() {
  95         testCommon(getPageName(), SizeStyleApp.SizePages.MAX_HEIGHT.toString(), false, false);
  96         lookupControls();
  97         checkMaxHeight();
  98         checkScreenShots(SizeStyleApp.SizePages.MAX_HEIGHT.toString());
  99     }
 100 
 101     /**
 102      * Compare two Controls, first with set API,second with set stylesheet style
 103      * "-fx-max-width"
 104      */
 105     @Test
 106     public void maxWidthTest() {
 107         testCommon(getPageName(), SizeStyleApp.SizePages.MAX_WIDTH.toString(), false, false);
 108         lookupControls();
 109         checkMaxWidth();
 110         checkScreenShots(SizeStyleApp.SizePages.MAX_WIDTH.toString());
 111     }
 112 
 113     /**
 114      * Compare two Controls, first with set API,second with set stylesheet style
 115      * "-fx-pref-width"
 116      */
 117     @Test
 118     public void prefWidthTest() {
 119         testCommon(getPageName(), SizeStyleApp.SizePages.PREF_WIDTH.toString(), false, false);
 120         lookupControls();
 121         checkPrefWidth();
 122         checkScreenShots(SizeStyleApp.SizePages.PREF_WIDTH.toString());
 123     }
 124 
 125     /**
 126      * Compare two Controls, first with set API,second with set stylesheet style
 127      * "-fx-pref-height"
 128      */
 129     @Test
 130     public void prefHeightTest() {
 131         testCommon(getPageName(), SizeStyleApp.SizePages.PREF_HEIGHT.toString(), false, false);
 132         lookupControls();
 133         checkPrefHeight();
 134         checkScreenShots(SizeStyleApp.SizePages.PREF_HEIGHT.toString());
 135     }
 136 
 137     public void lookupControls() {
 138         sceneWrap = Root.ROOT.lookup(Scene.class).wrap();
 139         control = sceneWrap.as(Parent.class, Node.class).lookup(Region.class, new ByID(SizeStyleApp.CONTROL_ID)).wrap();
 140         golden_control = sceneWrap.as(Parent.class, Node.class).lookup(Region.class, new ByID(SizeStyleApp.GOLDEN_CONTROL_ID)).wrap();
 141     }
 142 
 143     public void checkMinWidth() {
 144         Assert.assertNotNull(control);
 145         Assert.assertNotNull(golden_control);
 146         Assert.assertEquals(control.getControl().getMinWidth(), golden_control.getControl().getMinWidth(), 0.0);
 147     }
 148 
 149     public void checkMinHeight() {
 150         Assert.assertNotNull(control);
 151         Assert.assertNotNull(golden_control);
 152         Assert.assertEquals(control.getControl().getMinHeight(), golden_control.getControl().getMinHeight(), 0.0);
 153     }
 154 
 155     public void checkPrefWidth() {
 156         Assert.assertNotNull(control);
 157         Assert.assertNotNull(golden_control);
 158         Assert.assertEquals(control.getControl().getPrefWidth(), golden_control.getControl().getPrefWidth(), 0.0);
 159     }
 160 
 161     public void checkPrefHeight() {
 162         Assert.assertNotNull(control);
 163         Assert.assertNotNull(golden_control);
 164         Assert.assertEquals(control.getControl().getPrefHeight(), golden_control.getControl().getPrefHeight(), 0.0);
 165     }
 166 
 167     public void checkMaxWidth() {
 168         Assert.assertNotNull(control);
 169         Assert.assertNotNull(golden_control);
 170         Assert.assertEquals(control.getControl().getMaxWidth(), golden_control.getControl().getMaxWidth(), 0.0);
 171     }
 172     
 173     public void checkMaxHeight() {
 174         Assert.assertNotNull(control);
 175         Assert.assertNotNull(golden_control);
 176         Assert.assertEquals(control.getControl().getMaxHeight(), golden_control.getControl().getMaxHeight(), 0.0);
 177     }
 178 
 179     public void checkScreenShots(final String testName) {
 180         control.waitState(new State<Boolean>() {
 181             public Boolean reached() {
 182                 Image testedImage = control.getScreenImage();
 183                 Image goldenImage = golden_control.getScreenImage();
 184                 RasterComparator comparator = sceneWrap.getEnvironment().getProperty(RasterComparator.class);
 185                 if (comparator.compare((Raster) testedImage, (Raster) goldenImage)) {
 186                     return true;
 187                 }
 188                 else {
 189                     String cssStyleImage = GoldenImageManager.getScreenshotPath(getPageName() + testName + "_css");
 190                     String apiImage = GoldenImageManager.getScreenshotPath(getPageName() + testName + "_api");
 191                     String diffPath   = GoldenImageManager.getScreenshotPath(getPageName() + testName + "_diff");
 192                     testedImage.save(cssStyleImage);
 193                     goldenImage.save(apiImage);
 194                     testedImage.compareTo(goldenImage).save(diffPath);
 195                 }
 196                 return null;
 197             }
 198         });
 199     }
 200 }