1 /*
   2  * Copyright (c) 2010, 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 com.sun.javafx.scene.traversal;
  27 
  28 import static org.junit.Assert.assertEquals;
  29 import static org.junit.Assert.assertSame;
  30 import static org.junit.Assert.assertTrue;
  31 
  32 import java.util.Arrays;
  33 import java.util.Collection;
  34 
  35 import javafx.geometry.Bounds;
  36 import javafx.scene.Group;
  37 import javafx.scene.Node;
  38 import javafx.scene.Scene;
  39 import javafx.scene.shape.Rectangle;
  40 import javafx.stage.Stage;
  41 
  42 import org.junit.After;
  43 import org.junit.Before;
  44 import org.junit.Test;
  45 import org.junit.runner.RunWith;
  46 import org.junit.runners.Parameterized;
  47 import org.junit.runners.Parameterized.Parameters;
  48 
  49 /**
  50  * Tests TraversalEngine with invisible nodes, using the default ContainerTabOrder algorithm,
  51  */
  52 @RunWith(Parameterized.class)
  53 public final class TraverseInvisibleTest {
  54     private final int fromNumber;
  55     private final Direction direction;
  56     private final int invisibleNumber;
  57     private final int toNumber;
  58 
  59     private Stage stage;
  60     private Scene scene;
  61     private Node[] keypadNodes;
  62     private TraversalEngine traversalEngine;
  63 
  64     /*
  65     **
  66     ** Parameters: [fromNumber], [direction], [invisibleNumber], [toNumber]
  67     ** The Grid looks like :
  68     **    0 1 2
  69     **    3 4 5
  70     **    6 7 8
  71     */
  72     @Parameters
  73     public static Collection data() {
  74         return Arrays.asList(new Object[][] {
  75             { 3, Direction.RIGHT, 4, 5},
  76             { 5, Direction.LEFT, 4, 3},
  77             { 4, Direction.NEXT, 5, 6},
  78             { 6, Direction.PREVIOUS, 5, 4},
  79             { 8, Direction.UP, 5, 2 },
  80             { 2, Direction.DOWN, 5, 8 }
  81         });
  82     }
  83 
  84     public TraverseInvisibleTest(final int fromNumber,
  85                          final Direction direction,
  86                          final int invisibleNumber,
  87                          final int toNumber) {
  88         this.fromNumber = fromNumber;
  89         this.direction = direction;
  90         this.invisibleNumber = invisibleNumber;
  91         this.toNumber = toNumber;
  92     }
  93 
  94     @Before
  95     public void setUp() {
  96         stage = new Stage();
  97         scene = new Scene(new Group(), 500, 500);
  98         stage.setScene(scene);
  99 
 100         traversalEngine = new TraversalEngine(scene.getRoot(), true);
 101 
 102         keypadNodes = createKeypadNodesInScene(scene, traversalEngine);
 103         stage.show();
 104         stage.requestFocus();
 105     }
 106 
 107     @After
 108     public void tearDown() {
 109         stage = null;
 110         scene = null;
 111         keypadNodes = null;
 112         traversalEngine = null;
 113     }
 114 
 115     @Test
 116     public void traverseOverInvisible() {
 117         keypadNodes[fromNumber].requestFocus();
 118         keypadNodes[invisibleNumber].setVisible(false);
 119         traversalEngine.trav(keypadNodes[fromNumber], direction);
 120 
 121         assertTrue(keypadNodes[toNumber].isFocused());
 122 
 123         keypadNodes[invisibleNumber - 1].setVisible(true);
 124     }
 125 
 126 
 127 
 128     private static Node[] createKeypadNodesInScene(
 129             final Scene scene,
 130             final TraversalEngine traversalEngine) {
 131         final Node[] keypad = new Node[9];
 132 
 133         int index = 0;
 134         for (int row = 0; row < 3; ++row) {
 135             for (int column = 0; column < 3; ++column) {
 136                 final Node keyNode = new Rectangle(10 + column * 50,
 137                                                    10 + row * 50,
 138                                                    40, 40);
 139                 keyNode.setFocusTraversable(true);
 140 
 141                 keypad[index++] = keyNode;
 142                 ((Group)scene.getRoot()).getChildren().add(keyNode);
 143                 traversalEngine.reg(keyNode);
 144             }
 145         }
 146 
 147         return keypad;
 148     }
 149 
 150     private static final class TraverseListenerImpl
 151             implements TraverseListener {
 152         private int callCounter;
 153         private Node lastNode;
 154 
 155         public int getCallCounter() {
 156             return callCounter;
 157         }
 158         
 159         public Node getLastNode() {
 160             return lastNode;
 161         }
 162 
 163         @Override
 164         public void onTraverse(final Node node, final Bounds bounds) {
 165             ++callCounter;
 166             lastNode = node;
 167         }
 168     }
 169 }