1 /*
   2  * Copyright (c) 2013, 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 javafx.scene;
  27 
  28 import com.sun.javafx.FXUnit;
  29 import com.sun.javafx.test.NodeOrientationTestBase;
  30 import java.util.Arrays;
  31 import java.util.Collection;
  32 import javafx.geometry.NodeOrientation;
  33 import org.junit.Assert;
  34 import org.junit.Rule;
  35 import org.junit.Test;
  36 import org.junit.runner.RunWith;
  37 import org.junit.runners.Parameterized;
  38 import org.junit.runners.Parameterized.Parameters;
  39 
  40 @RunWith(Parameterized.class)
  41 public final class Node_effectiveOrientation_Test
  42         extends NodeOrientationTestBase {
  43 
  44     @Rule
  45     public FXUnit fx = new FXUnit();
  46 
  47     private final Scene testScene;
  48     private final String orientationUpdate;
  49     private final String expectedOrientation;
  50 
  51     private static Scene lriiliScene() {
  52         return ltrScene(
  53                    rtlAutGroup(
  54                        inhAutGroup(
  55                            inhAutGroup(
  56                                ltrAutGroup(
  57                                    inhAutGroup())))));
  58     }
  59 
  60     private static Scene lriiliWithSubSceneScene() {
  61         return ltrScene(
  62                    rtlAutGroup(
  63                        inhSubScene(
  64                            inhAutGroup(
  65                                ltrAutGroup(
  66                                    inhAutGroup())))));
  67     }
  68 
  69     private static Scene liirliPrecachedScene() {
  70         final Scene scene =
  71                 ltrScene(
  72                     inhAutGroup(
  73                         inhAutGroup(
  74                             rtlAutGroup(
  75                                 ltrAutGroup(
  76                                     inhAutGroup())))));
  77         // force caching
  78         collectOrientation(scene);
  79         return scene;
  80     }
  81 
  82     private static Scene riirliPlugedPrecachedScenegraphScene() {
  83         final Group root =
  84                 inhAutGroup(
  85                     inhAutGroup(
  86                         rtlAutGroup(
  87                             ltrAutGroup(
  88                                 inhAutGroup()))));
  89         // force caching
  90         collectOrientation(root);
  91 
  92         final Scene scene = new Scene(new Group());
  93         scene.setNodeOrientation(NodeOrientation.RIGHT_TO_LEFT);
  94         scene.setRoot(root);
  95 
  96         return scene;
  97     }
  98 
  99     private static Scene lrIiilScene() {
 100         return ltrScene(
 101                    rtlAutGroup(
 102                        inhManGroup(
 103                            inhAutGroup(
 104                                inhAutGroup(
 105                                    ltrAutGroup())))));
 106     }
 107 
 108     /*
 109      * Parameters: [testScene], [orientationUpdate], [expectedOrientation]
 110      */
 111     @Parameters
 112     public static Collection data() {
 113         return Arrays.asList(
 114                 new Object[][] {
 115                         { lriiliScene(), "......", "LRRRLL" },
 116                         { lriiliScene(), ".I....", "LLLLLL" },
 117                         { lriiliScene(), "...L..", "LRRLLL" },
 118                         { lriiliScene(), "....I.", "LRRRRR" },
 119                         { lriiliScene(), "RIIIII", "RRRRRR" },
 120 
 121                         {
 122                             lriiliWithSubSceneScene(),
 123                             ".......", "LRRRLL"
 124                         },
 125                         {
 126                             lriiliWithSubSceneScene(),
 127                             ".L.....", "LLLLLL"
 128                         },
 129 
 130                         { liirliPrecachedScene(), "......", "LLLRLL" },
 131                         { liirliPrecachedScene(), "R.....", "RRRRLL" },
 132                         { liirliPrecachedScene(), "...I..", "LLLLLL" },
 133                         { liirliPrecachedScene(), "R..IR.", "RRRRRR" },
 134 
 135                         {
 136                             riirliPlugedPrecachedScenegraphScene(),
 137                             "......", "RRRRLL"
 138                         },
 139 
 140                         { lrIiilScene(), "......", "LRRRRL" },
 141                         { lrIiilScene(), ".L....", "LLLLLL" }
 142                     });
 143     }
 144 
 145     public Node_effectiveOrientation_Test(
 146             final Scene testScene,
 147             final String orientationUpdate,
 148             final String expectedOrientation) {
 149         this.testScene = testScene;
 150         this.orientationUpdate = orientationUpdate;
 151         this.expectedOrientation = expectedOrientation;
 152     }
 153 
 154     @Test
 155     public void effectiveOrientationTest() {
 156         updateOrientation(testScene, orientationUpdate);
 157         assertOrientation(testScene, expectedOrientation);
 158     }
 159 
 160     private static void assertOrientation(
 161             final Scene scene,
 162             final String expectedOrientation) {
 163         final String actualOrientation = collectOrientation(scene);
 164         Assert.assertEquals("Orientation mismatch",
 165                             expectedOrientation, actualOrientation);
 166     }
 167 
 168     private static final StateEncoder EFFECTIVE_ORIENTATION_ENCODER =
 169             new StateEncoder() {
 170                 @Override
 171                 public char map(final Scene scene) {
 172                     return map(scene.getEffectiveNodeOrientation());
 173                 }
 174 
 175                 @Override
 176                 public char map(final Node node) {
 177                     return map(node.getEffectiveNodeOrientation());
 178                 }
 179 
 180                 private char map(final NodeOrientation effectiveOrientation) {
 181                     switch (effectiveOrientation) {
 182                         case LEFT_TO_RIGHT:
 183                             return 'L';
 184                         case RIGHT_TO_LEFT:
 185                             return 'R';
 186                         default:
 187                             throw new IllegalArgumentException(
 188                                           "Invalid orientation");
 189                     }
 190                 }
 191             };
 192 
 193     private static String collectOrientation(final Scene scene) {
 194         return collectState(scene, EFFECTIVE_ORIENTATION_ENCODER);
 195     }
 196 
 197     private static String collectOrientation(final Node node) {
 198         return collectState(node, EFFECTIVE_ORIENTATION_ENCODER);
 199     }
 200 
 201 }