SynthScrollbarThumbPainterTest.java revision 15235:fe58d505fffd
1/*
2 * Copyright (c) 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 7172750
28 * @summary Test to check Synth ScrollBar:ScrollBarThumb[].backgroundPainter is invoked
29 * @run main SynthScrollbarThumbPainterTest
30 */
31
32import java.awt.Color;
33import java.awt.Component;
34import java.awt.Dimension;
35import java.awt.Graphics2D;
36import java.awt.Point;
37import java.awt.Robot;
38import java.awt.event.InputEvent;
39import java.awt.image.BufferedImage;
40import javax.swing.JComponent;
41import javax.swing.JEditorPane;
42import javax.swing.JFrame;
43import javax.swing.JScrollPane;
44import javax.swing.Painter;
45import javax.swing.SwingUtilities;
46import javax.swing.UIManager;
47
48public class SynthScrollbarThumbPainterTest {
49
50    private static Robot testRobot;
51    private static Point pos = new Point();
52    private static MyFrame testFrame;
53
54    public static void main(String[] args) throws Exception {
55        // Create Robot
56        testRobot = new Robot();
57
58        // Create test UI
59        String lookAndFeelString = "javax.swing.plaf.nimbus.NimbusLookAndFeel";
60        SwingUtilities.invokeAndWait(new Runnable() {
61            @Override
62            public void run() {
63                try {
64                    constructTestUI(lookAndFeelString);
65                } catch (Exception ex) {
66                    throw new RuntimeException("Exception creating test UI");
67                }
68            }
69        });
70
71        testRobot.waitForIdle();
72        testRobot.delay(200);
73
74        // Run test method
75        testScrollBarThumbPainter();
76
77        // Dispose test UI
78        disposeTestUI();
79    }
80
81    private static void disposeTestUI() throws Exception {
82        SwingUtilities.invokeAndWait(() -> {
83            testFrame.dispose();
84        });
85    }
86
87
88    private static void constructTestUI(String lookAndFeelString) throws Exception {
89        // Set look and feel
90        UIManager.setLookAndFeel(lookAndFeelString);
91
92        // Set ScrollBarThumb background painters
93        UIManager.getLookAndFeelDefaults().put("ScrollBar:ScrollBarThumb[Enabled].backgroundPainter", new FillPainter(Color.RED));
94        UIManager.getLookAndFeelDefaults().put("ScrollBar:ScrollBarThumb[MouseOver].backgroundPainter", new FillPainter(Color.GREEN));
95        UIManager.getLookAndFeelDefaults().put("ScrollBar:ScrollBarThumb[Pressed].backgroundPainter", new FillPainter(Color.BLUE));
96
97        // Create UI
98        testFrame = new MyFrame();
99    }
100
101   private static void testScrollBarThumbPainter() throws Exception {
102        Point p = testFrame.getLocation();
103        pos.setLocation(p.x + 185, p.y + 80); // offset where scrollbar exists
104
105        testRobot.delay(200);
106
107        // Get the scrollbar color
108        Color ScrollbarColor = testFrame.getPixelColor(pos.x - p.x, pos.y - p.y);
109
110        // Assert ScrollbarThumb 'Enable' state color
111        if (!ScrollbarColor.equals(Color.RED)) {
112            disposeTestUI();
113            throw new RuntimeException("ScrollbarThumb 'Enable' state color does not match expected color");
114        }
115
116        // Move the mouse over scrollbar
117        testRobot.mouseMove(pos.x, pos.y);
118        testRobot.delay(200);
119
120        ScrollbarColor = testFrame.getPixelColor(pos.x - p.x, pos.y - p.y);
121
122        //Assert ScrollbarThumb 'MouseOver' state color
123        if (!ScrollbarColor.equals(Color.GREEN)) {
124            disposeTestUI();
125            throw new RuntimeException("ScrollbarThumb 'MouseOver' state color does not match expected color");
126        }
127
128        // Mouse Press on scrollbar
129        testRobot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
130        testRobot.delay(200);
131
132        ScrollbarColor = testFrame.getPixelColor(pos.x - p.x, pos.y - p.y);
133
134        testRobot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
135        testRobot.delay(200);
136
137        //Assert ScrollbarThumb 'Pressed' state color
138        if (!ScrollbarColor.equals(Color.BLUE)) {
139            disposeTestUI();
140            throw new RuntimeException("ScrollbarThumb 'Pressed' state color does not match expected color");
141        }
142    }
143
144}
145
146
147class FillPainter implements Painter<JComponent> {
148
149    private final Color color;
150
151    FillPainter(Color c) {
152        color = c;
153    }
154
155    @Override
156    public void paint(Graphics2D g, JComponent object, int width, int height) {
157        g.setColor(color);
158        g.fillRect(0, 0, width - 1, height - 1);
159    }
160}
161
162class MyFrame extends JFrame {
163
164    private BufferedImage bi;
165    private final String content = "A\nB\nC\nD\nE\nF\nG\nH\nI\nJ\nK\nL\nM\nN\nO";
166
167    public MyFrame() {
168        JEditorPane editpane = new JEditorPane();
169        editpane.setEditable(false);
170        editpane.setText(content);
171        editpane.setCaretPosition(0);
172
173        JScrollPane scrollpane = new JScrollPane(editpane);
174
175        add(scrollpane);
176
177        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
178
179        setSize(new Dimension(200, 200));
180        bi = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
181        setResizable(false);
182        setVisible(true);
183    }
184
185
186    public Color getPixelColor(int x, int y) {
187
188        paintFrameToBufferedImage(this);
189
190        int pixel = bi.getRGB(x, y);
191
192        int alpha = (pixel >> 24) & 0xff;
193        int red = (pixel >> 16) & 0xff;
194        int green = (pixel >> 8) & 0xff;
195        int blue = (pixel) & 0xff;
196
197        Color pixelColor = new Color(red, green, blue, alpha);
198        return pixelColor;
199    }
200
201    private void paintFrameToBufferedImage(Component component) {
202       component.paint(bi.getGraphics());
203    }
204} //MyFrame
205
206