1 /*
   2  * Copyright (c) 2010, 2016, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import com.sun.swingset3.demos.tree.TreeDemo;
  25 import static com.sun.swingset3.demos.tree.TreeDemo.DEMO_TITLE;
  26 
  27 import java.awt.Component;
  28 import javax.swing.tree.TreePath;
  29 
  30 import org.jtregext.GuiTestListener;
  31 
  32 import org.netbeans.jemmy.ClassReference;
  33 import org.netbeans.jemmy.ComponentChooser;
  34 import org.netbeans.jemmy.operators.JFrameOperator;
  35 import org.netbeans.jemmy.operators.JTreeOperator;
  36 
  37 import org.testng.annotations.Listeners;
  38 import org.testng.annotations.Test;
  39 import static org.testng.AssertJUnit.*;
  40 
  41 /*
  42  * @test
  43  * @key headful
  44  * @summary Verifies SwingSet3 TreeDemo by expanding all collapsed nodes in the
  45  *          tree and then collapsing all the expanded nodes in the tree. It
  46  *          verifies the number of nodes expanded, number of nodes collapsed and
  47  *          number of rows in the tree in the begininng, after expanding and
  48  *          after collapsing the nodes. It also checks that the tree grows
  49  *          vertically (as ScrollPane allows it).
  50  *
  51  * @library /sanity/client/lib/jemmy/src
  52  * @library /sanity/client/lib/Extensions/src
  53  * @library /sanity/client/lib/SwingSet3/src
  54  * @modules java.desktop
  55  *          java.logging
  56  * @build org.jemmy2ext.JemmyExt
  57  * @build com.sun.swingset3.demos.tree.TreeDemo
  58  * @run testng TreeDemoTest
  59  */
  60 @Listeners(GuiTestListener.class)
  61 public class TreeDemoTest {
  62 
  63     private static final int NODES_TO_EXPAND = 75;
  64     private static final int NODES_TOTAL = 616;
  65 
  66     private void waitRowCount(JTreeOperator tree, int count) {
  67         tree.waitState(new ComponentChooser() {
  68             public boolean checkComponent(Component comp) {
  69                 return tree.getRowCount() == count;
  70             }
  71             public String getDescription() {
  72                 return "A tree to have " + count + " rows";
  73             }
  74         });
  75     }
  76 
  77     @Test
  78     public void test() throws Exception {
  79 
  80         new ClassReference(TreeDemo.class.getCanonicalName()).startApplication();
  81 
  82         JFrameOperator frame = new JFrameOperator(DEMO_TITLE);
  83 
  84         JTreeOperator tree = new JTreeOperator(frame);
  85 
  86         assertEquals("Initial number of rows in the tree", 4, tree.getRowCount());
  87 
  88         int initialTreeHeight = tree.getHeight();
  89 
  90         // expand all nodes
  91         int expandsCount = 0;
  92         for (int i = 0; i < tree.getRowCount(); i++) {
  93             TreePath tp = tree.getPathForRow(i);
  94             if (tree.getChildCount(tp) > 0 && !tree.isExpanded(tp)) {
  95                 tree.expandRow(i);
  96                 expandsCount++;
  97             }
  98         }
  99 
 100         assertEquals("Number of rows expanded", NODES_TO_EXPAND, expandsCount);
 101         waitRowCount(tree, NODES_TOTAL);
 102 
 103         int expandedTreeHeight = tree.getHeight();
 104         assertTrue("Expanded tree height has increased, current "
 105                 + expandedTreeHeight + " > initial " + initialTreeHeight,
 106                 expandedTreeHeight > initialTreeHeight);
 107 
 108         // collapse all nodes
 109         int collapsesCount = 0;
 110         for (int i = tree.getRowCount() - 1; i >= 0; i--) {
 111             TreePath tp = tree.getPathForRow(i);
 112             if (tree.getChildCount(tp) > 0 && tree.isExpanded(tp)) {
 113                 tree.collapseRow(i);
 114                 collapsesCount++;
 115             }
 116         }
 117 
 118         assertEquals("Number of rows collapsed", NODES_TO_EXPAND + 1, collapsesCount);
 119         waitRowCount(tree, 1);
 120 
 121         int collapsedTreeHeight = tree.getHeight();
 122         assertTrue("Collpased tree height is not longer than initial, "
 123                 + "current " + collapsedTreeHeight + " <= initial "
 124                 + initialTreeHeight,
 125                 collapsedTreeHeight <= initialTreeHeight);
 126 
 127     }
 128 
 129 }