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 import java.awt.Color;
  25 import java.awt.Frame;
  26 import java.awt.Point;
  27 import java.awt.Robot;
  28 import java.util.List;
  29 
  30 /**
  31  * @test
  32  * @key headful
  33  * @bug 8215105
  34  * @summary tests that Robot can capture the common colors without artifacts
  35  */
  36 public final class CheckCommonColors {
  37 
  38     private static final Frame frame = new Frame();
  39     private static Robot robot;
  40 
  41     public static void main(final String[] args) throws Exception {
  42         robot = new Robot();
  43         try {
  44             test();
  45         } finally {
  46             frame.dispose();
  47         }
  48     }
  49 
  50     private static void test() {
  51         frame.setSize(400, 400);
  52         frame.setLocationRelativeTo(null);
  53         frame.setUndecorated(true);
  54         for (final Color color : List.of(Color.WHITE, Color.LIGHT_GRAY,
  55                                          Color.GRAY, Color.DARK_GRAY,
  56                                          Color.BLACK, Color.RED, Color.PINK,
  57                                          Color.ORANGE, Color.YELLOW,
  58                                          Color.GREEN, Color.MAGENTA, Color.CYAN,
  59                                          Color.BLUE)) {
  60             frame.dispose();
  61             frame.setBackground(color);
  62             frame.setVisible(true);
  63             checkPixels(color);
  64         }
  65     }
  66 
  67     private static void checkPixels(final Color color) {
  68         int attempt = 0;
  69         while (true) {
  70             Point p = frame.getLocationOnScreen();
  71             Color pixel = robot.getPixelColor(p.x + frame.getWidth() / 2,
  72                                               p.y + frame.getHeight() / 2);
  73             if (color.equals(pixel)) {
  74                 return;
  75             }
  76             if (attempt > 10) {
  77                 System.err.println("Expected: " + color);
  78                 System.err.println("Actual: " + pixel);
  79                 throw new RuntimeException("Too many attempts: " + attempt);
  80             }
  81             // skip Robot.waitForIdle to speedup the common case, but also take
  82             // care about slow systems
  83             robot.delay((int) Math.pow(2.2, attempt++));
  84         }
  85     }
  86 }