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 import sun.awt.SunToolkit; 25 26 import java.awt.Button; 27 import java.awt.Canvas; 28 import java.awt.Checkbox; 29 import java.awt.Choice; 30 import java.awt.Component; 31 import java.awt.Container; 32 import java.awt.Dimension; 33 import java.awt.Frame; 34 import java.awt.Graphics; 35 import java.awt.GridLayout; 36 import java.awt.Label; 37 import java.awt.List; 38 import java.awt.Panel; 39 import java.awt.ScrollPane; 40 import java.awt.Scrollbar; 41 import java.awt.TextArea; 42 import java.awt.TextField; 43 import java.awt.Toolkit; 44 import java.awt.image.BufferedImage; 45 46 /* 47 @test 48 @bug 6596915 49 @summary Test Component.paintAll() method 50 @author sergey.bylokhov@oracle.com: area=awt.component 51 @run main PaintAll 52 */ 53 public class PaintAll { 54 55 private static volatile boolean lwPainted; 56 private static volatile boolean buttonPainted; 57 private static volatile boolean canvasPainted; 58 private static volatile boolean checkboxPainted; 59 private static volatile boolean choicePainted; 60 private static volatile boolean containerPainted; 61 private static volatile boolean framePainted; 62 private static volatile boolean labelPainted; 63 private static volatile boolean listPainted; 64 private static volatile boolean panelPainted; 65 private static volatile boolean scrollbarPainted; 66 private static volatile boolean scrollPanePainted; 67 private static volatile boolean textAreaPainted; 68 private static volatile boolean textFieldPainted; 69 70 private static final Button buttonStub = new Button() { 71 @Override 72 public void paint(final Graphics g) { 73 buttonPainted = true; 74 } 75 }; 76 77 private static final Canvas canvasStub = new Canvas() { 78 @Override 79 public void paint(final Graphics g) { 80 canvasPainted = true; 81 } 82 }; 83 84 private static final Checkbox checkboxStub = new Checkbox() { 85 @Override 86 public void paint(final Graphics g) { 87 checkboxPainted = true; 88 } 89 }; 90 91 private static final Choice choiceStub = new Choice() { 92 @Override 93 public void paint(final Graphics g) { 94 choicePainted = true; 95 } 96 }; 97 98 private static final Component lwComponentStub = new Component() { 99 @Override 100 public void paint(final Graphics g) { 101 lwPainted = true; 102 } 103 }; 104 105 private static final Container containerStub = new Container() { 106 @Override 107 public void paint(final Graphics g) { 108 containerPainted = true; 109 } 110 }; 111 112 private static final Frame frame = new Frame() { 113 @Override 114 public void paint(final Graphics g) { 115 super.paint(g); 116 framePainted = true; 117 } 118 }; 119 120 private static final Label labelStub = new Label() { 121 @Override 122 public void paint(final Graphics g) { 123 labelPainted = true; 124 } 125 }; 126 127 private static final List listStub = new List() { 128 @Override 129 public void paint(final Graphics g) { 130 listPainted = true; 131 } 132 }; 133 134 private static final Panel panelStub = new Panel() { 135 @Override 136 public void paint(final Graphics g) { 137 panelPainted = true; 138 } 139 }; 140 141 private static final Scrollbar scrollbarStub = new Scrollbar() { 142 @Override 143 public void paint(final Graphics g) { 144 scrollbarPainted = true; 145 } 146 }; 147 148 private static final ScrollPane scrollPaneStub = new ScrollPane() { 149 @Override 150 public void paint(final Graphics g) { 151 scrollPanePainted = true; 152 } 153 }; 154 155 private static final TextArea textAreaStub = new TextArea() { 156 @Override 157 public void paint(final Graphics g) { 158 textAreaPainted = true; 159 } 160 }; 161 162 private static final TextField textFieldStub = new TextField() { 163 @Override 164 public void paint(final Graphics g) { 165 textFieldPainted = true; 166 } 167 }; 168 169 public static void main(final String[] args) throws Exception { 170 //Frame initialisation 171 final BufferedImage graphicsProducer = 172 new BufferedImage(BufferedImage.TYPE_INT_ARGB, 1, 1); 173 174 final Graphics g = graphicsProducer.getGraphics(); 175 176 frame.setLayout(new GridLayout()); 177 frame.add(buttonStub); 178 frame.add(canvasStub); 179 frame.add(checkboxStub); 180 frame.add(choiceStub); 181 frame.add(lwComponentStub); 182 frame.add(containerStub); 183 frame.add(labelStub); 184 frame.add(listStub); 185 frame.add(panelStub); 186 frame.add(scrollbarStub); 187 frame.add(scrollPaneStub); 188 frame.add(textAreaStub); 189 frame.add(textFieldStub); 190 frame.setSize(new Dimension(500, 500)); 191 frame.setLocationRelativeTo(null); 192 frame.setVisible(true); 193 sleep(); 194 195 //Check results. 196 validation(); 197 198 //Reset all flags to 'false'. 199 initPaintedFlags(); 200 201 //Tested method. 202 frame.paintAll(g); 203 sleep(); 204 205 //Check results. 206 validation(); 207 cleanup(); 208 } 209 210 private static void initPaintedFlags() { 211 lwPainted = false; 212 buttonPainted = false; 213 canvasPainted = false; 214 checkboxPainted = false; 215 choicePainted = false; 216 containerPainted = false; 217 framePainted = false; 218 labelPainted = false; 219 listPainted = false; 220 panelPainted = false; 221 scrollbarPainted = false; 222 scrollPanePainted = false; 223 textAreaPainted = false; 224 textFieldPainted = false; 225 } 226 227 private static void validation() { 228 if (!buttonPainted) { 229 fail("Paint is not called a Button " 230 + "when paintAll() invoked on a parent"); 231 } 232 if (!canvasPainted) { 233 fail("Paint is not called a Canvas " 234 + "when paintAll() invoked on a parent"); 235 } 236 if (!checkboxPainted) { 237 fail("Paint is not called a Checkbox " 238 + "when paintAll() invoked on a parent"); 239 } 240 if (!choicePainted) { 241 fail("Paint is not called a Choice " 242 + "when paintAll() invoked on a parent"); 243 } 244 if (!lwPainted) { 245 fail("Paint is not called on a lightweight" 246 + " subcomponent when paintAll() invoked on a parent"); 247 } 248 if (!containerPainted) { 249 fail("Paint is not called on a Container" 250 + " subcomponent when paintAll() invoked on a parent"); 251 } 252 if (!labelPainted) { 253 fail("Paint is not called on a Label" 254 + " subcomponent when paintAll() invoked on a parent"); 255 } 256 if (!listPainted) { 257 fail("Paint is not called on a List" 258 + " subcomponent when paintAll() invoked on a parent"); 259 } 260 if (!panelPainted) { 261 fail("Paint is not called on a Panel" 262 + " subcomponent when paintAll() invoked on a parent"); 263 } 264 if (!scrollbarPainted) { 265 fail("Paint is not called on a Scrollbar" 266 + " subcomponent when paintAll() invoked on a parent"); 267 } 268 if (!scrollPanePainted) { 269 fail("Paint is not called on a ScrollPane" 270 + " subcomponent when paintAll() invoked on a parent"); 271 } 272 if (!textAreaPainted) { 273 fail("Paint is not called on a TextArea" 274 + " subcomponent when paintAll() invoked on a parent"); 275 } 276 if (!textFieldPainted) { 277 fail("Paint is not called on a TextField" 278 + " subcomponent when paintAll() invoked on a parent"); 279 } 280 if (!framePainted) { 281 fail("Paint is not called on a Frame when paintAll()"); 282 } 283 } 284 285 private static void sleep() { 286 ((SunToolkit) Toolkit.getDefaultToolkit()).realSync(); 287 try { 288 Thread.sleep(500L); 289 } catch (InterruptedException ignored) { 290 } 291 } 292 293 private static void fail(final String message) { 294 cleanup(); 295 throw new RuntimeException(message); 296 } 297 298 private static void cleanup() { 299 frame.dispose(); 300 } 301 }