1 /*
   2  * Copyright (c) 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 package javafx.commons;
  26 
  27 import client.test.ScreenshotCheck;
  28 import javafx.scene.Node;
  29 import org.jemmy.fx.ByID;
  30 import org.jemmy.image.Image;
  31 import org.jemmy.interfaces.Selectable;
  32 import org.junit.Test;
  33 import static javafx.commons.ControlChooserApp.*;
  34 import org.junit.runner.RunWith;
  35 import test.javaclient.shared.FilteredTestRunner;
  36 import javafx.scene.control.test.utils.ptables.AbstractPropertyController.SettingType;
  37 import org.jemmy.env.Environment;
  38 import org.jemmy.env.Timeout;
  39 import org.jemmy.image.AWTImage;
  40 import org.jemmy.image.GlassImage;
  41 import org.jemmy.image.ImageComparator;
  42 import org.jemmy.image.pixel.MaxDistanceComparator;
  43 import org.jemmy.image.pixel.Raster;
  44 import org.jemmy.image.pixel.RasterComparator;
  45 import org.jemmy.timing.State;
  46 import org.jemmy.timing.Waiter;
  47 import org.junit.Assert;
  48 import test.javaclient.shared.screenshots.ImagesManager;
  49 
  50 /**
  51  *
  52  * @author Dmitry Zinkevich <dmitry.zinkevich@oracle.com>
  53  */
  54 @RunWith(FilteredTestRunner.class)
  55 public class ControlsTest extends ControlsBase {
  56 
  57     /**
  58      * Checks opacity of controls by comparing two screenshots:
  59      * first - control having 40% opacity,
  60      * second - initial control screenshot with applied 40% opacity
  61      */
  62     @ScreenshotCheck
  63     //Test//Switched off.
  64     public void opacityTest() throws InterruptedException, Throwable {
  65 
  66         final MaxDistanceComparator maxDistanceComparator = new MaxDistanceComparator(0.0);
  67         final double THRESHOLD = 0.03 * Math.sqrt(3);
  68         
  69         /*
  70          * Change comparator because it is called by
  71          */
  72         final RasterComparator rasterComparator = Environment.getEnvironment().getProperty(RasterComparator.class);
  73         Environment.getEnvironment().setProperty(RasterComparator.class, new MaxDistanceComparator(THRESHOLD));
  74         
  75         String nodeName;
  76 
  77         for (Object node : nodeChooser.as(Selectable.class).getStates()) {
  78             nodeChooser.as(Selectable.class).selector().select(node);
  79 
  80             nodeName = node.toString();
  81             System.out.println("node = " + nodeName);
  82 
  83             for (SettingType settingType : SettingType.values()) {
  84                 System.out.println("settingType = " + settingType);
  85 
  86                 checkSimpleListenerValue(TestedProperties.focused, "false");
  87 
  88                 testedControl = parent.lookup(Node.class, new ByID<Node>(TESTED_CONTROL_ID)).wrap();
  89 
  90                 final Image imgInitial = testedControl.getScreenImage();
  91                 final Image imgOpacity40 = testedControl.getScreenImage();
  92                 
  93                 Assert.assertTrue("Two initial state screenshots have differences",
  94                         maxDistanceComparator.compare((Raster) imgInitial, (Raster) imgOpacity40));
  95 
  96                 if (imgOpacity40 instanceof AWTImage) {
  97                     setOpacity(0.4, (AWTImage) imgOpacity40);
  98                 } else if (imgOpacity40 instanceof GlassImage) {
  99                     setOpacity(0.4, (GlassImage) imgOpacity40);
 100                 } else {
 101                     throw new Exception("Unknown image format");
 102                 }
 103 
 104                 /*
 105                  * Decrease control opacity by x percent
 106                  * and check that the image distance between opaque and translucent states is less or equal x
 107                  */
 108                 final double OPACITY = 0.4;
 109                 setPropertyBySlider(settingType, TestedProperties.opacity, OPACITY);
 110 
 111                 maxDistanceComparator.setThreshold(THRESHOLD);
 112                 try {
 113                     new Waiter(new Timeout("", 2000)).ensureState(new State<Boolean>() {
 114                         public Boolean reached() {
 115                             Image imgTranslucent = testedControl.getScreenImage();
 116                             Boolean isDifference = !maxDistanceComparator.compare((Raster) imgOpacity40, (Raster) imgTranslucent);
 117                             return isDifference ? null : Boolean.valueOf(true);
 118                         }
 119                     });
 120                 } catch (Exception ex) {
 121                     System.out.println(ex.getMessage());
 122                     Image imgTranslucent = testedControl.getScreenImage();
 123                     
 124                     String screenshotPath = ImagesManager.getInstance().getScreenshotPath(String.format("%s_img_opacity40", nodeName));
 125                     imgOpacity40.save(screenshotPath);
 126                     
 127                     screenshotPath = ImagesManager.getInstance().getScreenshotPath(String.format("%s_control_opacity40", nodeName));
 128                     imgTranslucent.save(screenshotPath);
 129 
 130                     Image diff = Environment.getEnvironment().getProperty(ImageComparator.class).compare(imgOpacity40, imgTranslucent);
 131                     
 132                     Assert.assertTrue(diff != null);
 133                     
 134                     screenshotPath = ImagesManager.getInstance().getScreenshotPath(String.format("%s_diff", nodeName));
 135                     diff.save(screenshotPath);
 136                 }
 137                 setPropertyBySlider(settingType, TestedProperties.opacity, 1);
 138             }
 139             clearCache();
 140         }
 141         
 142         /*
 143          * Restore comparator
 144          */
 145         if (rasterComparator != null) {
 146             Environment.getEnvironment().setProperty(RasterComparator.class, rasterComparator);
 147         }
 148     }
 149 }