/*
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
 */

package com.sun.glass.bugs;

import com.sun.glass.ui.*;

import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.AfterClass;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class RT10274_PixelsUpload
{
    static int WIDTH = 128;
    static int HEIGHT = 128;

    @BeforeClass
    static public void prepare()
    {
        try
        {
            Application app = Application.GetApplication();
            Screen screen = Screen.getMainScreen();

            int windowMask = Window.UNTITLED;
            Window window = app.createWindow(screen, windowMask);
            window.setView(app.createView(new NullGraphics()));
            window.setPosition((screen.getWidth()-WIDTH)/2, (screen.getHeight()-HEIGHT)/2);
            window.setSize(WIDTH, HEIGHT);
            window.setBackground(1.0f, 1.0f, 1.0f);
            window.setVisible(true);
            
            Thread.sleep(100);
        }
        catch (Throwable t)
        {
            t.printStackTrace();
            System.exit(0);
        }
    }

    @Test
    public void testWindowsList()
    {
        List<Window> list = Window.getWindows();
        assertTrue(list != null);
        assertTrue(list.size() == 1);
    }
    
    private void uploadWindowPixel(Robot robot, Window window, ByteBuffer data, int pixel)
    {
        data.rewind();
        {
            for (int i=0; i<data.capacity(); i+=4)
            {
                data.putInt(pixel);
            }
        }
        data.rewind();
//        System.err.println("data.order(): "+data.order());
//        System.err.println("data.getInt(0): "+Integer.toHexString(data.getInt(0)));
//        System.err.println("data.get(0): "+Integer.toHexString(data.get(0)&0x000000ff));
//        System.err.println("data.get(1): "+Integer.toHexString(data.get(1)&0x000000ff));
//        System.err.println("data.get(2): "+Integer.toHexString(data.get(2)&0x000000ff));
//        System.err.println("data.get(3): "+Integer.toHexString(data.get(3)&0x000000ff));

        Pixels pixels = Application.GetApplication().createPixels(window.getWidth(), window.getHeight(), data);
        assertTrue(pixels != null);
        
        Screen screen = window.getScreen();
        assertTrue(screen != null);
        
        window.getView().uploadPixels(pixels);
        try
        {
            Thread.currentThread().sleep(100);
        }
        catch (InterruptedException ex)
        {
            assertTrue(false);
        }
        
        int pixelOut = robot.getPixelColor(screen.getWidth()/2, screen.getHeight()/2);
//        System.err.println("pixel: "+Integer.toHexString(pixel));
//        System.err.println("pixelOut: "+Integer.toHexString(pixelOut));
        assertTrue(pixelOut == pixel);
    }
    
    @Test
    public void testWindowPixels()
    {
        Window window = Window.getWindows().get(0);
        assertTrue(window != null);
        
        ByteBuffer data = ByteBuffer.allocateDirect(window.getWidth()*window.getHeight()*4);
        data.order(ByteOrder.nativeOrder());
        assertTrue(data != null);
        assertTrue(window.getWidth()*window.getHeight()*4 == data.capacity());
        
        Robot robot = Application.GetApplication().createRobot();
        uploadWindowPixel(robot, window, data, 0xff000000);
        uploadWindowPixel(robot, window, data, 0xff7f0000);
        uploadWindowPixel(robot, window, data, 0xff007f00);
        uploadWindowPixel(robot, window, data, 0xff00007f);
        uploadWindowPixel(robot, window, data, 0xfff0f0f0);
        uploadWindowPixel(robot, window, data, 0xff0f0f0f);
        uploadWindowPixel(robot, window, data, 0xff123456);
    }
    
    @AfterClass
    static public void finish()
    {
        Window window = Window.getWindows().get(0);
        assertTrue(window != null);
        window.close();
    }

    static class NullGraphics extends Pen
    {
        @Override public Map getCapabilities()
        {
            Map map = new HashMap();
            map.put(View.Capability.k3dKey, new Boolean(true));
            return map;
        }
        @Override public void paint(long timeStamp, int width, int height)
        {
            // NOP
        }
    }
}
