/*
 * 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;

public class RT11193_WindowWithViewAddedAfterVisible
{
    static int WINDOW_X = 101;
    static int WINDOW_Y = 102;
    static int VIEW_W = 512;
    static int VIEW_H = 256;

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

            int windowMask = Window.TITLED|Window.CLOSABLE|Window.MINIMIZABLE;
            Window window = app.createWindow(screen, windowMask);
            window.setPosition(WINDOW_X, WINDOW_Y);
            window.setTitle(app.getName());
            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);
    }

    @Test
    public void testWindowX()
    {
        Window window = Window.getWindows().get(0);
        assertTrue(window != null);
        assertTrue(window.getX() == WINDOW_X);
    }

    @Test
    public void testWindowY()
    {
        Window window = Window.getWindows().get(0);
        assertTrue(window != null);
        assertTrue(window.getY() == WINDOW_Y);
    }

    @Test
    public void testWindowAfterViewAdded()
    {
        Window window = Window.getWindows().get(0);
        assertTrue(window != null);

        int x = window.getX();
        int y = window.getY();
        
        View view = Application.GetApplication().createView(new NullGraphics());
        window.setView(view);
        assertTrue(window.getView() == view);

        assertTrue(window.getX() == x);
        assertTrue(window.getY() == y);
        
        window.setContentSize(VIEW_W, VIEW_H);
        assertTrue(view.getWidth() == VIEW_W);
        assertTrue(view.getHeight() == VIEW_H);
        
        try
        {
            Thread.currentThread().sleep(100);
        }
        catch (InterruptedException ex)
        {
            assertTrue(false);
        }
        
        assertTrue(window.getX() == x);
        assertTrue(window.getY() == y);

        assertTrue(window.getWidth() >= VIEW_W);
        assertTrue(window.getHeight() >= VIEW_H);
    }
    
    @AfterClass
    static public void finish()
    {
        Window window = Window.getWindows().get(0);
        assertTrue(window != null);
        window.close();
    }

    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
        }
    }
}
