1 /*
   2  * Copyright (c) 2018, 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 package org.netbeans.jemmy.operators;
  26 
  27 import javax.swing.JFrame;
  28 import javax.swing.JLabel;
  29 import javax.swing.JToolTip;
  30 
  31 import org.netbeans.jemmy.ComponentChooser;
  32 import org.netbeans.jemmy.TimeoutExpiredException;
  33 import org.testng.Assert;
  34 import org.testng.annotations.AfterClass;
  35 import org.testng.annotations.BeforeClass;
  36 import org.testng.annotations.Test;
  37 
  38 public class JToolTipOperatorTest {
  39 
  40     
  41     private JFrame frame = null;
  42     
  43     @BeforeClass
  44     protected void setUp() throws Exception {
  45         frame = new JFrame();
  46         frame.setSize(400,400);
  47         frame.setLocationRelativeTo(null);
  48     }
  49 
  50     @AfterClass
  51     protected void tearDown() throws Exception {
  52         frame.setVisible(false);
  53         frame.dispose();
  54     }
  55 
  56     @Test
  57     public void testToolTip() {
  58         final String TOLLTIP_TEXT = "A simple Tooltip";
  59         final String LABEL_TEXT = "Roll over here to see a tooltip";
  60         JLabel label = new JLabel(LABEL_TEXT);
  61         label.setToolTipText(TOLLTIP_TEXT);
  62         label.setBounds(0, 0, 200, 200);
  63         frame.add(label);
  64         frame.setVisible(true);
  65         JLabelOperator labelOperator = new JLabelOperator(label);
  66         JToolTipOperator toolTipOperator = new JToolTipOperator(
  67                 labelOperator.showToolTip());
  68         toolTipOperator.waitTipText(TOLLTIP_TEXT);
  69         
  70         // Testing different constructors
  71         new JToolTipOperator();
  72         new JToolTipOperator(labelOperator);
  73         new JToolTipOperator(TOLLTIP_TEXT);
  74         ComponentChooser chooser = comp -> ((JLabel)((JToolTip)comp).
  75                 getComponent()).getText().equals(LABEL_TEXT);
  76         new JToolTipOperator(chooser);
  77         new JToolTipOperator(labelOperator, TOLLTIP_TEXT);
  78         new JToolTipOperator(labelOperator, chooser);
  79 
  80         labelOperator.clickMouse();
  81         try {
  82             JToolTipOperator.waitJToolTip();
  83             Assert.fail();
  84         } catch (TimeoutExpiredException e) {
  85         }
  86     }
  87     
  88     @Test
  89     public void testToolTipConstructorsNegativeScenarios() {
  90         final String LABEL_TEXT = "Random Text";
  91         JLabelOperator dummyLabel = new JLabelOperator(new JLabel());
  92         ComponentChooser chooser = comp -> ((JLabel)((JToolTip)comp).
  93                 getComponent()).getText().equals(LABEL_TEXT);
  94         try {
  95             new JToolTipOperator(dummyLabel);
  96             Assert.fail();
  97         } catch (TimeoutExpiredException e) {
  98         }
  99         
 100         try {
 101             new JToolTipOperator(LABEL_TEXT);
 102             Assert.fail();
 103         } catch (TimeoutExpiredException e) {
 104         }
 105         
 106         try {
 107             
 108             new JToolTipOperator(chooser);
 109             Assert.fail();
 110         } catch (TimeoutExpiredException e) {
 111         }
 112         
 113         try {
 114             new JToolTipOperator(dummyLabel, LABEL_TEXT);
 115             Assert.fail();
 116         } catch (TimeoutExpiredException e) {
 117         }
 118         
 119         try {
 120             new JToolTipOperator(dummyLabel, chooser);
 121             Assert.fail();
 122         } catch (TimeoutExpiredException e) {
 123         }
 124     }
 125 }