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.scene.control.test.labeled;
  26 
  27 import client.test.ScreenshotCheck;
  28 import client.test.Smoke;
  29 import com.sun.javafx.scene.control.skin.LabeledText;
  30 import javafx.collections.ObservableList;
  31 import javafx.geometry.Bounds;
  32 import javafx.scene.Node;
  33 import javafx.scene.Parent;
  34 import javafx.scene.control.Labeled;
  35 import javafx.scene.control.test.labeleds.LabeledsAbstactApp;
  36 import javafx.scene.control.test.labeleds.LabeledsAbstactApp.LabeledsPages;
  37 import junit.framework.Assert;
  38 import org.jemmy.control.Wrap;
  39 import org.jemmy.fx.ByID;
  40 import org.jemmy.fx.ByText;
  41 import org.jemmy.timing.State;
  42 import org.jemmy.timing.Waiter;
  43 import org.junit.Test;
  44 import org.junit.runner.RunWith;
  45 import test.javaclient.shared.FilteredTestRunner;
  46 import test.javaclient.shared.TestBase;
  47 import test.javaclient.shared.screenshots.ScreenshotUtils;
  48 
  49 /**
  50  * Labeled control API test
  51  *
  52  */
  53 @RunWith(FilteredTestRunner.class)
  54 public abstract class LabeledsBase extends TestBase {
  55 
  56     {
  57         ScreenshotUtils.setComparatorDistance(0.005f);
  58     }
  59 
  60     /**
  61      * Constructor test
  62      */
  63     @ScreenshotCheck
  64     @Test(timeout = 300000)
  65     public void constructorsTest() throws InterruptedException {
  66         testCommon(LabeledsPages.Constructors.name(), null, true, true);
  67     }
  68 
  69     /**
  70      * Test for setText API
  71      */
  72     @ScreenshotCheck
  73     @Smoke
  74     @Test(timeout = 300000)
  75     public void setTextTest() throws InterruptedException {
  76         testCommon(LabeledsPages.setText.name(), null, true, true);
  77     }
  78 
  79     /**
  80      * Test for setContentDisplay API
  81      */
  82     @ScreenshotCheck
  83     @Test(timeout = 300000)
  84     public void setContentDisplayTest() throws InterruptedException {
  85         testCommon(LabeledsPages.setContentDisplay.name(), null, true, true);
  86     }
  87 
  88     /**
  89      * Test for setWrapText API
  90      */
  91     @ScreenshotCheck
  92     @Test(timeout = 300000)
  93     public void setWrapTextTest() throws InterruptedException {
  94         testCommon(LabeledsPages.setWrapText.name(), null, true, true);
  95     }
  96 
  97     /**
  98      * Test for setFont API
  99      */
 100     @ScreenshotCheck
 101     @Test(timeout = 300000)
 102     public void setFontTest() throws InterruptedException {
 103         testCommon(LabeledsPages.setFont.name(), null, true, true);
 104     }
 105 
 106     /**
 107      * Test for setGraphicTextGap API
 108      */
 109     @ScreenshotCheck
 110     @Test(timeout = 300000)
 111     public void setGraphicTextGapTest() throws InterruptedException {
 112         testCommon(LabeledsPages.setGraphicTextGap.name(), null, true, true);
 113     }
 114 
 115     /**
 116      * Test for setAlignment API
 117      */
 118     @ScreenshotCheck
 119     @Test(timeout = 300000)
 120     public void setAlignmentTest() throws InterruptedException {
 121         testCommon(LabeledsPages.setAlignment.name(), null, true, true);
 122     }
 123 
 124     /**
 125      * Test for setTextAlignment API
 126      */
 127     @ScreenshotCheck
 128     @Test(timeout = 300000)
 129     public void setTextAlignmentTest() throws InterruptedException {
 130         testCommon(LabeledsPages.setTextAlignment.name(), null, true, true);
 131     }
 132 
 133     /**
 134      * Test for setTextOverrun API
 135      */
 136     @ScreenshotCheck
 137     @Test(timeout = 300000)
 138     public void setTextOverrunSingleLineTest() throws InterruptedException {
 139         testCommon(LabeledsPages.setTextOverrunSingleLine.name(), null, true, true);
 140     }
 141 
 142     /**
 143      * Test for setTextOverrun API
 144      */
 145     @ScreenshotCheck
 146     @Test(timeout = 300000)
 147     public void setTextOverrunMultiLineTest() throws InterruptedException {
 148         testCommon(LabeledsPages.setTextOverrunMultiLine.name(), null, true, true);
 149     }
 150 
 151     /**
 152      * Test for setEllipsisString API by screenshot
 153      */
 154     @ScreenshotCheck
 155     @Test(timeout = 300000)
 156     public void setEllipsisStringScreenshotTest() throws InterruptedException {
 157         testCommon(LabeledsPages.setEllipsisString.name(), null, true, true);
 158     }
 159 
 160     /**
 161      * Test for setEllipsisString API. Testing by text.
 162      */
 163     @Test(timeout = 300000)
 164     public void setEllipsisStringTextTest() throws InterruptedException {
 165         openPage(LabeledsPages.setEllipsisString.name());
 166         testEllipsisText();
 167     }
 168 
 169     /**
 170      * Test for setEllipsisString API. Testing by size.
 171      */
 172     @Test(timeout = 300000)
 173     public void setEllipsisStringSizeTest() throws InterruptedException {
 174         openPage(LabeledsPages.setEllipsisString.name());
 175         testEllipsisSize();
 176     }
 177 
 178     /**
 179      * Test for setTextOverrun API
 180      */
 181     @ScreenshotCheck
 182     @Test(timeout = 300000)
 183     public void setTextOverrunSingleLineWrappedTest() throws InterruptedException {
 184         testCommon(LabeledsPages.setTextOverrunSingleLineWrapped.name(), null, true, true);
 185     }
 186 
 187     /**
 188      * Test for setTextOverrun API
 189      */
 190     @ScreenshotCheck
 191     @Test(timeout = 300000)
 192     public void setTextOverrunMultiLineWrappedTest() throws InterruptedException {
 193         testCommon(LabeledsPages.setTextOverrunMultiLineWrapped.name(), null, true, true);
 194     }
 195 
 196     private void testEllipsisText() throws InterruptedException {
 197         org.jemmy.interfaces.Parent p = getScene().as(org.jemmy.interfaces.Parent.class, Node.class);
 198         final Wrap<? extends Labeled> node = p.lookup(new ByText<Labeled>(LabeledsAbstactApp.ELLIPSIS_TEXT)).wrap();
 199         new Waiter(Wrap.WAIT_STATE_TIMEOUT).ensureState(new State() {
 200             @Override
 201             public Object reached() {
 202                 return (node.getControl().getChildrenUnmodifiable().size() > 0) ? true : null;
 203             }
 204         });
 205         Assert.assertEquals(getEllipsingString(), searchText(node.getControl()));
 206     }
 207 
 208     private static String searchText(Node node) {
 209         if (node instanceof LabeledText) {
 210             return (((LabeledText) node).getText());
 211         }
 212         if (node instanceof Parent) {
 213             Parent parent = (Parent) node;
 214             ObservableList<Node> list = parent.getChildrenUnmodifiable();
 215             String temp = null;
 216             for (Node n : list) {
 217                 temp = searchText(n);
 218                 if (temp != null) {
 219                     return temp;
 220                 }
 221             }
 222         }
 223         return null;
 224     }
 225 
 226     private void testEllipsisSize() {
 227         org.jemmy.interfaces.Parent p = getScene().as(org.jemmy.interfaces.Parent.class, Node.class);
 228 
 229         final Wrap<? extends Labeled> node = p.lookup(new ByID<Labeled>(LabeledsPages.setEllipsisString.name() + LabeledsAbstactApp.CONTROL)).wrap();
 230         final Wrap<? extends Labeled> goldennode = p.lookup(new ByID<Labeled>(LabeledsPages.setEllipsisString.name() + LabeledsAbstactApp.GOLDEN)).wrap();
 231 
 232         new Waiter(Wrap.WAIT_STATE_TIMEOUT).waitState(new State() {
 233             @Override
 234             public Object reached() {
 235                 return (node.getControl().getChildrenUnmodifiable().size() > 0) ? true : null;
 236             }
 237         });
 238 
 239         LabeledText targetText = searchLabeledText(node.getControl());
 240         Bounds targetTextBounds = targetText.getBoundsInParent();
 241         Bounds controlBounds = node.getControl().getBoundsInLocal();
 242         Bounds goldenControlBounds = goldennode.getControl().getBoundsInLocal();
 243         Assert.assertTrue(controlBounds.contains(targetTextBounds));
 244         Assert.assertEquals(goldenControlBounds.getWidth(), controlBounds.getWidth());
 245         Assert.assertEquals(goldenControlBounds.getHeight(), controlBounds.getHeight());
 246     }
 247 
 248     private static LabeledText searchLabeledText(Node node) {
 249 
 250         if (node instanceof LabeledText) {
 251             return (LabeledText) node;
 252         }
 253         if (node instanceof Parent) {
 254             Parent parent = (Parent) node;
 255             ObservableList<Node> list = parent.getChildrenUnmodifiable();
 256             LabeledText temp = null;
 257             for (Node n : list) {
 258                 temp = searchLabeledText(n);
 259                 if (temp != null) {
 260                     return temp;
 261                 }
 262             }
 263         }
 264         return null;
 265     }
 266 
 267     /**
 268      * function for Ellipsis Tests. Function get "golden string" from the
 269      * Application, specific for every control test.
 270      *
 271      * @return Expected string
 272      */
 273     protected abstract String getEllipsingString();
 274 }