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