1 /*
   2  * Copyright (c) 2011, 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 static org.junit.Assert.assertEquals;
  29 import static org.junit.Assert.assertSame;
  30 import static org.junit.Assert.assertTrue;
  31 
  32 import java.util.Set;
  33 
  34 import org.junit.Before;
  35 import org.junit.Test;
  36 
  37 public class Node_lookup_Test {
  38     //                  Group & #root
  39     //                /      \
  40     //              #a      .b.c
  41     //             /   \        \
  42     //           .d    #e        .d
  43     private Group root, a, bc, d, e, d2;
  44     
  45     @Before public void setup() {
  46         root = new Group();
  47         root.setId("root");
  48         a = new Group();
  49         a.setId("a");
  50         d = new Group();
  51         d.getStyleClass().add("d");
  52         e = new Group();
  53         e.setId("e");
  54         bc = new Group();
  55         bc.getStyleClass().addAll("b", "c");
  56         d2 = new Group();
  57         d2.getStyleClass().add("d");
  58         root.getChildren().addAll(a, bc);
  59         a.getChildren().addAll(d, e);
  60         bc.getChildren().addAll(d2);
  61     }
  62     
  63     @Test public void quickTest() {
  64         Node found = root.lookup("Group");
  65         assertSame(root, found);
  66         
  67         found = root.lookup("#a");
  68         assertSame(a, found);
  69         
  70         found = root.lookup("#a > .d");
  71         assertSame(d, found);
  72         
  73         found = root.lookup("#e");
  74         assertSame(e, found);
  75         
  76         found = root.lookup(".b .d");
  77         assertSame(d2, found);
  78         
  79         found = root.lookup(".c .d");
  80         assertSame(d2, found);
  81         
  82         found = root.lookup(".b");
  83         assertSame(bc, found);
  84     }
  85     
  86     @Test public void lookupAllTest() {
  87         Set<Node> nodes = root.lookupAll("#a");
  88         assertEquals(1, nodes.size());
  89         assertTrue(nodes.contains(a));
  90         
  91         nodes = root.lookupAll(".d");
  92         assertEquals(2, nodes.size());
  93         assertTrue(nodes.contains(d));
  94         assertTrue(nodes.contains(d2));
  95     }
  96 }