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.
   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 8195738
  28  * @summary scroll poistion in ScrollPane is reset after calling validate()
  29  * @run main/othervm bug8195738Test
  30  */
  31 
  32 import java.awt.ScrollPane;
  33 import java.awt.BorderLayout;
  34 import java.awt.GridLayout;
  35 import java.awt.Button;
  36 import java.awt.Dimension;
  37 import java.awt.Frame;
  38 import java.awt.Panel;
  39 import java.awt.Point;
  40 import java.awt.Robot;
  41 import java.awt.AWTException;
  42 
  43 public class bug8195738Test extends Frame {
  44   ScrollPane pane;
  45 
  46   public bug8195738Test() {
  47     setBounds(300, 300, 300, 300);
  48     pane = new ScrollPane(ScrollPane.SCROLLBARS_NEVER);
  49     add(pane, BorderLayout.NORTH);
  50     pane.add(new InnerPanel());
  51   }
  52 
  53   public static void main(String[] args) throws AWTException {
  54     Robot robot = new Robot();
  55     final bug8195738Test obj = new bug8195738Test();
  56     obj.setVisible(true);
  57 
  58     // set to some scroll position
  59     obj.pane.setScrollPosition(600, 200);
  60 
  61     // get the newly set position
  62     Point scrollPosition = obj.pane.getScrollPosition();
  63 
  64     // call validate multiple times
  65     obj.pane.validate();
  66     robot.delay(1000);
  67     obj.pane.validate();
  68     robot.delay(1000);
  69 
  70     // compare position after calling the validate function
  71     if(!scrollPosition.equals(obj.pane.getScrollPosition())) {
  72       obj.dispose();
  73       throw new RuntimeException("Scrolling position is changed in ScrollPane");
  74     }
  75 
  76     obj.dispose();
  77     return;
  78   }
  79 
  80   class InnerPanel extends Panel {
  81     public InnerPanel() {
  82       this.setLayout(new GridLayout(2, 4));
  83       for (int i = 1; i <= 8; i++) {
  84         this.add(new Button("Button" + i));
  85       }
  86     }
  87 
  88     public Dimension getPreferredSize() {
  89       return new Dimension(980, 200);
  90     }
  91   }
  92 }