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