1 /*
   2  * Copyright (c) 2011, 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 4865918
  27  * @summary REGRESSION:JCK1.4a-runtime api/javax_swing/interactive/JScrollBarTests.html#JScrollBar
  28  * @author Andrey Pikalev
  29  * @run main bug4865918
  30  */
  31 
  32 import javax.swing.*;
  33 import java.awt.*;
  34 import java.awt.event.*;
  35 import java.util.*;
  36 import sun.awt.SunToolkit;
  37 
  38 public class bug4865918 {
  39 
  40     private static TestScrollBar sbar;
  41 
  42     public static void main(String[] argv) throws Exception {
  43         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  44 
  45         SwingUtilities.invokeAndWait(new Runnable() {
  46 
  47             public void run() {
  48                 createAndShowGUI();
  49             }
  50         });
  51 
  52         toolkit.realSync();
  53 
  54         SwingUtilities.invokeAndWait(new Runnable() {
  55 
  56             @Override
  57             public void run() {
  58                 sbar.pressMouse();
  59             }
  60         });
  61 
  62         toolkit.realSync();
  63 
  64         int value = getValue();
  65 
  66         if (value != 9) {
  67             throw new Error("The scrollbar block increment is incorect");
  68         }
  69     }
  70 
  71     private static int getValue() throws Exception {
  72         final int[] result = new int[1];
  73 
  74         SwingUtilities.invokeAndWait(new Runnable() {
  75             @Override
  76             public void run() {
  77                 result[0] = sbar.getValue();
  78             }
  79         });
  80 
  81         return result[0];
  82     }
  83 
  84     private static void createAndShowGUI() {
  85         JFrame frame = new JFrame("bug4865918");
  86         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  87 
  88         sbar = new TestScrollBar(JScrollBar.HORIZONTAL, -1, 10, -100, 100);
  89         sbar.setPreferredSize(new Dimension(200, 20));
  90         sbar.setBlockIncrement(10);
  91 
  92         frame.getContentPane().add(sbar);
  93         frame.pack();
  94         frame.setVisible(true);
  95 
  96     }
  97 
  98     static class TestScrollBar extends JScrollBar {
  99 
 100         public TestScrollBar(int orientation, int value, int extent,
 101                 int min, int max) {
 102             super(orientation, value, extent, min, max);
 103 
 104         }
 105 
 106         public void pressMouse() {
 107             MouseEvent me = new MouseEvent(sbar,
 108                     MouseEvent.MOUSE_PRESSED,
 109                     (new Date()).getTime(),
 110                     MouseEvent.BUTTON1_MASK,
 111                     3 * getWidth() / 4, getHeight() / 2,
 112                     1, true);
 113             processMouseEvent(me);
 114         }
 115     }
 116 }