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