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