1 /*
   2  * Copyright (c) 2011, 2013, 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  @summary <rdar://problem/3155258> [JavaJDK15] AWT scrollbars have redraw problems (not opaque)
  27  @summary after resizing the scrollbars would leave garbage.
  28  @summary Note that the whole test is filled with numbers tuned to the current size; be careful about changing these.
  29  @summary com.apple.junit.java.awt;
  30  @library ../../../../java/awt/regtesthelpers
  31  @build VisibilityValidator
  32  @run main ScrollbarTest
  33  */
  34 
  35 import junit.framework.*;
  36 
  37 import java.awt.*;
  38 import java.awt.event.InputEvent;
  39 
  40 import test.java.awt.regtesthelpers.VisibilityValidator;
  41 
  42 public class ScrollbarTest extends TestCase {
  43 
  44     private Frame frame;
  45     private Robot robot;
  46 
  47     public void setUp() throws Exception {
  48         robot = new Robot();
  49 
  50         frame = new Frame();
  51         frame.setBackground(Color.orange);
  52 
  53         frame.setLayout(new GridLayout(1, 2));
  54         frame.add(new Scrollbar(Scrollbar.HORIZONTAL));
  55         frame.add(new Scrollbar(Scrollbar.VERTICAL));
  56 
  57         frame.setSize(300, 200);
  58     }
  59 
  60     public void testResize() throws Exception {
  61         VisibilityValidator.setVisibleAndConfirm(frame);
  62         assertTrue("Timed out without seeing our orange background", VisibilityValidator.waitForColor(frame, 50, 150, Color.orange));
  63 
  64         LiveResizeThread lrt = new LiveResizeThread();
  65         lrt.start();
  66         lrt.join();
  67         // wait a second before testing for the color result. If all goes well, there shouldn't be any garbage (screen dirt on the screen)
  68         // and test whether the color is white
  69 
  70         Thread.sleep(1000);
  71 
  72         VisibilityValidator.assertColorEquals(" Color should be orange at (50, 190) ", Color.orange, robot.getPixelColor(50, 210));
  73         VisibilityValidator.assertColorEquals(" Color should be orange at (70, 190) ", Color.orange, robot.getPixelColor(70, 210));
  74         VisibilityValidator.assertColorEquals(" Color should be orange at (50, 205) ", Color.orange, robot.getPixelColor(50, 220));
  75     }
  76 
  77     protected void tearDown() {
  78         frame.dispose();
  79     }
  80 
  81     public static Test suite() {
  82         return new TestSuite(ScrollbarTest.class);
  83     }
  84 
  85     public static void main (String[] args) throws RuntimeException {
  86         String name = System.getProperty("os.name");
  87         if (name.equals("Mac OS X")) {
  88             // This test makes a Mac OS X assumption about the growbox location
  89             TestResult tr = junit.textui.TestRunner.run(suite());
  90             if((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
  91                 throw new RuntimeException("### Unexpected JUnit errors or failures.");
  92             }
  93         }
  94     }
  95 
  96     class LiveResizeThread extends Thread {
  97         public void run() {
  98             int locX = (int)(frame.getLocationOnScreen().getX());
  99             int locY = (int)(frame.getLocationOnScreen().getY());
 100 
 101             boolean mouseDown = false;
 102             try {
 103 
 104                 robot.setAutoWaitForIdle(true);
 105 
 106                 int width = (int)(frame.getBounds().getWidth());
 107                 int height = (int)(frame.getBounds().getHeight());
 108 
 109                 int x = locX + width-6;
 110                 int y = locY + height-6;
 111 
 112                 robot.mouseMove(x, y);
 113                 mouseDown = true;
 114                 robot.mousePress(InputEvent.BUTTON1_MASK);
 115 
 116                 Thread.sleep(2000);
 117 
 118                 robot.mouseMove(x+100, y+100);
 119 
 120             } catch (Exception e)
 121             {
 122                 e.printStackTrace();
 123             } finally {
 124                 if (mouseDown)
 125                     robot.mouseRelease(InputEvent.BUTTON1_MASK);
 126             }
 127         }
 128     }
 129 }