1 /*
   2  * Copyright (c) 2006, 2014, 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 /*
  25   @test
  26   @bug 6429174
  27   @summary Tests that mouse click at the are of intersection of two
  28    scrollbars for text area doesn't trigger any scrolling
  29   @author artem.ananiev@sun.com: area=awt.text
  30   @library ../../../../lib/testlibrary
  31   @build jdk.testlibrary.OSInfo
  32   @run main ScrollbarIntersectionTest
  33 */
  34 
  35 import java.awt.*;
  36 import java.awt.event.*;
  37 import jdk.testlibrary.OSInfo;
  38 
  39 
  40 public class ScrollbarIntersectionTest
  41 {
  42     private static void init()
  43     {
  44 
  45         Frame f = new Frame("F");
  46         f.setBounds(100, 100, 480, 360);
  47         f.setLayout(new BorderLayout());
  48 
  49         TextArea ta = new TextArea(null, 8, 24, TextArea.SCROLLBARS_BOTH);
  50         // append several lines to show vertical scrollbar
  51         for (int i = 0; i < 128; i++)
  52         {
  53             ta.append("" + i + "\n");
  54         }
  55         // and some characters into the last line for horizontal scrollbar
  56         for (int i = 0; i < 128; i++)
  57         {
  58             ta.append("" + i);
  59         }
  60         ta.append("\n");
  61         f.add(ta);
  62 
  63         f.setVisible(true);
  64 
  65         Robot r = null;
  66         try
  67         {
  68             r = new Robot();
  69             r.setAutoDelay(20);
  70         }
  71         catch (Exception z)
  72         {
  73             z.printStackTrace(System.err);
  74             fail(z.getMessage());
  75             return;
  76         }
  77         r.waitForIdle();
  78 
  79         ta.setCaretPosition(0);
  80         r.waitForIdle();
  81 
  82         Point p = ta.getLocationOnScreen();
  83         Dimension d = ta.getSize();
  84 
  85         int fh = 8;
  86         Graphics g = ta.getGraphics();
  87         try
  88         {
  89             FontMetrics fm = g.getFontMetrics();
  90             fh = fm.getHeight();
  91         }
  92         finally
  93         {
  94             if (g != null)
  95             {
  96                 g.dispose();
  97             }
  98         };
  99 
 100         r.mouseMove(p.x + d.width - 2, p.y + d.height - 2);
 101         r.mousePress(InputEvent.BUTTON1_MASK);
 102         r.mouseRelease(InputEvent.BUTTON1_MASK);
 103         r.waitForIdle();
 104 
 105         // select 1st line in the text area
 106         r.mouseMove(p.x + 2, p.y + 2 + fh / 2);
 107         r.mousePress(InputEvent.BUTTON1_MASK);
 108         for (int i = 0; i < d.width - 4; i += 4)
 109         {
 110             r.mouseMove(p.x + 2 + i, p.y + 2 + fh / 2);
 111         }
 112         r.mouseRelease(InputEvent.BUTTON1_MASK);
 113         r.waitForIdle();
 114 
 115         String sel = ta.getSelectedText();
 116         System.err.println("Selected text: " + sel);
 117         if ((sel == null) || !sel.startsWith("0"))
 118         {
 119             fail("Test FAILED: TextArea is scrolled");
 120             return;
 121         }
 122 
 123         pass();
 124     }
 125 
 126     private static boolean theTestPassed = false;
 127     private static boolean testGeneratedInterrupt = false;
 128     private static String failureMessage = "";
 129 
 130     private static Thread mainThread = null;
 131 
 132     private static int sleepTime = 300000;
 133 
 134     public static void main( String args[] ) throws InterruptedException
 135     {
 136         if (OSInfo.getOSType() == OSInfo.OSType.MACOSX) {
 137             // On OS X, this area is commandeered by the system, 
 138             // and frame would be wildly resized 
 139             System.out.println("Not for OS X");
 140             return;
 141         }
 142         mainThread = Thread.currentThread();
 143         try
 144         {
 145             init();
 146         }
 147         catch( TestPassedException e )
 148         {
 149             return;
 150         }
 151 
 152         try
 153         {
 154             Thread.sleep( sleepTime );
 155             throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
 156         }
 157         catch (InterruptedException e)
 158         {
 159             if( ! testGeneratedInterrupt ) throw e;
 160 
 161             testGeneratedInterrupt = false;
 162 
 163             if ( theTestPassed == false )
 164             {
 165                 throw new RuntimeException( failureMessage );
 166             }
 167         }
 168     }
 169 
 170     public static synchronized void setTimeoutTo( int seconds )
 171     {
 172         sleepTime = seconds * 1000;
 173     }
 174 
 175     public static synchronized void pass()
 176     {
 177         if ( mainThread == Thread.currentThread() )
 178         {
 179             theTestPassed = true;
 180             throw new TestPassedException();
 181         }
 182         theTestPassed = true;
 183         testGeneratedInterrupt = true;
 184         mainThread.interrupt();
 185     }
 186 
 187     public static synchronized void fail()
 188     {
 189         fail( "it just plain failed! :-)" );
 190     }
 191 
 192     public static synchronized void fail( String whyFailed )
 193     {
 194         if ( mainThread == Thread.currentThread() )
 195         {
 196             throw new RuntimeException( whyFailed );
 197         }
 198         theTestPassed = false;
 199         testGeneratedInterrupt = true;
 200         failureMessage = whyFailed;
 201         mainThread.interrupt();
 202     }
 203 }
 204 
 205 class TestPassedException extends RuntimeException
 206 {
 207 }