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