/*
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * Portions Copyright (c) 2012 IBM Corporation
 */

/*
 * @test
 * @bug 7194184
 * @summary Tests JColorChooser Swatch keyboard accessibility.
 * @author Sean Chou
 */

import java.awt.AWTException;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import sun.awt.SunToolkit;

public class Test7194184 implements Runnable {
    private static final SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
    private static Robot robot;
    private static volatile JFrame frame;
    private static volatile JColorChooser colorChooser;

    private static boolean rightToLeft = false;

    public static void main(String[] args) throws AWTException {
        robot = new Robot();

        testKeyBoardAccess();
        testRightToLeftOrientation();
    }



    private static void testRightToLeftOrientation() {
        frame = null;
        colorChooser = null;
        rightToLeft = true;

        SwingUtilities.invokeLater(new Test7194184());
        toolkit.realSync();
        Color defaultColor = colorChooser.getColor();

        // Tab to move the focus to MainSwatch
        click(KeyEvent.VK_TAB);

        // Select the color on right,
        click(KeyEvent.VK_RIGHT);
        click(KeyEvent.VK_SPACE);
        toolkit.realSync();
        Rectangle rec = new Rectangle(100, 100, 70, 70);
        BufferedImage image0 = robot.createScreenCapture(rec);

        // Put 3 more color into RecentSwatch
        click(KeyEvent.VK_RIGHT);
        click(KeyEvent.VK_SPACE);
        toolkit.realSync();
        BufferedImage image1 = robot.createScreenCapture(rec);

        click(KeyEvent.VK_RIGHT);
        click(KeyEvent.VK_SPACE);
        toolkit.realSync();
        BufferedImage image2 = robot.createScreenCapture(rec);




        if (!isMoveFromLeftToRight(image0, image1, image2)) {
            frame.dispose();
            throw new RuntimeException("ColorSwatch Orientation error");
        }

        frame.dispose();
    }



    /**
     * Check if the recent color grow from left to right. This method
     * find the difference between image0 and image1 (d1), image0 and image2 (d2);
     * then check if the difference between d1 and d2. If the x-coordinator
     * grows from d1 to d2, return true.
     *
     * @param image0
     * @param image1
     * @param image2
     * @return
     */
    private static boolean isMoveFromLeftToRight(BufferedImage image0,
            BufferedImage image1, BufferedImage image2) {
        // get the max and min x-cordinator in d1
        int d1minX = 100;
        int d1maxX = 0;
        for (int i = image0.getMinX(); i < image0.getWidth(); i++) {
            for (int j = image0.getMinY(); j < image0.getHeight(); j++) {
            System.out.printf("(%d, %d) ",image0.getRGB(i, j), image1.getRGB(i,j) );

                if (image0.getRGB(i, j) != image1.getRGB(i, j)) {
                    if (i < d1minX) {
                        d1minX = i;
                    } else if (i > d1maxX) {
                        d1maxX = i;
                    }
                }
            }
            System.out.println("");
        }

        System.out.println("");
        System.out.println("");

        int d2minX = 100;
        int d2maxX = 0;
        for (int i = image0.getMinX(); i < image0.getWidth(); i++) {
            for (int j = image0.getMinY(); j < image0.getHeight(); j++) {
                if (image0.getRGB(i, j) != image2.getRGB(i, j)) {
                    System.out.printf("(%d, %d) ",image0.getRGB(i, j), image2.getRGB(i,j) );
                    if (i < d2minX) {
                        d2minX = i;
                    } else if (i > d2maxX) {
                        d2maxX = i;
                    }
                }
            }
            System.out.println("");
        }

        System.out.printf("d1minX, d1maxX, d2minX, d2maxX: %d, %d, %d, %d", d1minX, d1maxX, d2minX, d2maxX );
        if (d1minX == d2minX && d1maxX < d2maxX) {
            return true;
        }

        return false;
    }



    private static void testKeyBoardAccess() {
        SwingUtilities.invokeLater(new Test7194184());
        toolkit.realSync();

        // The default color
        Color selectedColor = colorChooser.getColor();

        // Tab to move the focus to MainSwatch
        click(KeyEvent.VK_TAB);

        // Select the color on right,
        click(KeyEvent.VK_RIGHT);
        click(KeyEvent.VK_RIGHT);
        click(KeyEvent.VK_SPACE);
        toolkit.realSync();

        // Check
        if (selectedColor == colorChooser.getColor()) {
            frame.dispose();
            throw new RuntimeException("JColorChooser misses keyboard accessibility");
        }

        frame.dispose();
    }

    private static void click(int...keys) {
        toolkit.realSync();
        for (int key : keys) {
            robot.keyPress(key);
        }
        for (int key : keys) {
            robot.keyRelease(key);
        }
    }

    public void run() {
        String title = getClass().getName();
        frame = new JFrame(title);

        colorChooser = new JColorChooser();
        if (rightToLeft) {
            colorChooser.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        }

        frame.add(colorChooser);
        frame.pack();
        frame.setVisible(true);
    }

}
