Path: blob/master/test/jdk/sanity/client/SwingSet/src/TreeDemoTest.java
41153 views
/*1* Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import com.sun.swingset3.demos.tree.TreeDemo;24import static com.sun.swingset3.demos.tree.TreeDemo.DEMO_TITLE;2526import java.awt.Component;27import javax.swing.UIManager;28import javax.swing.tree.TreePath;2930import org.jtregext.GuiTestListener;3132import org.netbeans.jemmy.ClassReference;33import org.netbeans.jemmy.ComponentChooser;34import org.netbeans.jemmy.operators.JFrameOperator;35import org.netbeans.jemmy.operators.JTreeOperator;3637import org.testng.annotations.Listeners;38import org.testng.annotations.Test;39import static org.testng.AssertJUnit.*;4041/*42* @test43* @key headful44* @summary Verifies SwingSet3 TreeDemo by expanding all collapsed nodes in the45* tree and then collapsing all the expanded nodes in the tree. It46* verifies the number of nodes expanded, number of nodes collapsed and47* number of rows in the tree in the begininng, after expanding and48* after collapsing the nodes. It also checks that the tree grows49* vertically (as ScrollPane allows it).50*51* @library /sanity/client/lib/jemmy/src52* @library /sanity/client/lib/Extensions/src53* @library /sanity/client/lib/SwingSet3/src54* @modules java.desktop55* java.logging56* @build org.jemmy2ext.JemmyExt57* @build com.sun.swingset3.demos.tree.TreeDemo58* @run testng/timeout=600 TreeDemoTest59*/60@Listeners(GuiTestListener.class)61public class TreeDemoTest {6263private static final int NODES_TO_EXPAND = 75;64private static final int NODES_TOTAL = 616;6566private void waitRowCount(JTreeOperator tree, int count) {67tree.waitState(new ComponentChooser() {68public boolean checkComponent(Component comp) {69return tree.getRowCount() == count;70}7172public String getDescription() {73return "A tree to have " + count + " rows";74}75});76}7778@Test(dataProvider = "availableLookAndFeels", dataProviderClass = TestHelpers.class)79public void test(String lookAndFeel) throws Exception {80UIManager.setLookAndFeel(lookAndFeel);8182new ClassReference(TreeDemo.class.getCanonicalName()).startApplication();8384JFrameOperator frame = new JFrameOperator(DEMO_TITLE);8586JTreeOperator tree = new JTreeOperator(frame);8788assertEquals("Initial number of rows in the tree", 4, tree.getRowCount());8990int initialTreeHeight = tree.getHeight();9192// expand all nodes93int expandsCount = 0;94for (int i = 0; i < tree.getRowCount(); i++) {95TreePath tp = tree.getPathForRow(i);96if (tree.getChildCount(tp) > 0 && !tree.isExpanded(tp)) {97tree.expandRow(i);98expandsCount++;99}100}101102assertEquals("Number of rows expanded", NODES_TO_EXPAND, expandsCount);103waitRowCount(tree, NODES_TOTAL);104105int expandedTreeHeight = tree.getHeight();106assertTrue("Expanded tree height has increased, current "107+ expandedTreeHeight + " > initial " + initialTreeHeight,108expandedTreeHeight > initialTreeHeight);109110// collapse all nodes111int collapsesCount = 0;112for (int i = tree.getRowCount() - 1; i >= 0; i--) {113TreePath tp = tree.getPathForRow(i);114if (tree.getChildCount(tp) > 0 && tree.isExpanded(tp)) {115tree.collapseRow(i);116collapsesCount++;117}118}119120assertEquals("Number of rows collapsed", NODES_TO_EXPAND + 1, collapsesCount);121waitRowCount(tree, 1);122123int collapsedTreeHeight = tree.getHeight();124assertTrue("Collpased tree height is not longer than initial, "125+ "current " + collapsedTreeHeight + " <= initial "126+ initialTreeHeight,127collapsedTreeHeight <= initialTreeHeight);128129}130131}132133134