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

package com.sun.glass.tests;

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;

public class WindowMinSizeMaxSize
{
    final static int WINDOW_X = 101;
    final static int WINDOW_Y = 102;

    final static int WINDOW_W = 511;
    final static int WINDOW_H = 512;

    final static int WINDOW_SMALLER_W = WINDOW_W-2;
    final static int WINDOW_SMALLER_H = WINDOW_H-2;

    final static int WINDOW_LARGER_W = WINDOW_W+2;
    final static int WINDOW_LARGER_H = WINDOW_H+2;
    
    @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.setSize(WINDOW_W, WINDOW_H);
            window.setTitle(app.getName());
            window.setVisible(true);
        }
        catch (Throwable t)
        {
            t.printStackTrace();
            System.exit(0);
        }
    }

    @AfterClass
    static public void finish()
    {
        Window window = Window.getWindows().get(0);
        window.close();
    }

    private void reset(Window window)
    {
        window.setMinimumSize(Integer.MIN_VALUE, Integer.MIN_VALUE);
        window.setMaximumSize(Integer.MAX_VALUE, Integer.MAX_VALUE);
        window.setSize(1, 1);
    }
    
    @Test
    public void testVerify()
    {
        Window window = Window.getWindows().get(0);
        assertTrue(window != null);

        assertTrue(window.getX() == WINDOW_X);
        assertTrue(window.getY() == WINDOW_Y);
        assertTrue(window.getWidth() == WINDOW_W);
        assertTrue(window.getHeight() == WINDOW_H);
    }

    @Test
    public void testNoChangeMinSize()
    {
        Window window = Window.getWindows().get(0);
        assertTrue(window != null);
        reset(window);
        
        window.setSize(WINDOW_W, WINDOW_H);
        window.setMinimumSize(WINDOW_SMALLER_W, WINDOW_SMALLER_H);

        // no change expected
        assertTrue(window.getX() == WINDOW_X);
        assertTrue(window.getY() == WINDOW_Y);
        assertTrue(window.getWidth() == WINDOW_W);
        assertTrue(window.getHeight() == WINDOW_H);
    }

    @Test
    public void testNoChangeMaxSize()
    {
        Window window = Window.getWindows().get(0);
        assertTrue(window != null);
        reset(window);
        
        window.setSize(WINDOW_W, WINDOW_H);
        window.setMaximumSize(WINDOW_LARGER_W, WINDOW_LARGER_H);
        
        // no change expected
        assertTrue(window.getX() == WINDOW_X);
        assertTrue(window.getY() == WINDOW_Y);
        assertTrue(window.getWidth() == WINDOW_W);
        assertTrue(window.getHeight() == WINDOW_H);
    }

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

        window.setSize(WINDOW_W, WINDOW_H);
        window.setMaximumSize(WINDOW_SMALLER_W, WINDOW_SMALLER_H);
        
        // size, then minimum
        assertTrue(window.getX() == WINDOW_X);
        assertTrue(window.getY() == WINDOW_Y);
        assertTrue(window.getWidth() == WINDOW_SMALLER_W);
        assertTrue(window.getHeight() == WINDOW_SMALLER_H);
    }

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

        window.setMaximumSize(WINDOW_SMALLER_W, WINDOW_SMALLER_H);
        window.setSize(WINDOW_W, WINDOW_H);

        // maximum, then size
        assertTrue(window.getX() == WINDOW_X);
        assertTrue(window.getY() == WINDOW_Y);
        assertTrue(window.getWidth() == WINDOW_SMALLER_W);
        assertTrue(window.getHeight() == WINDOW_SMALLER_H);
    }

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

        window.setSize(WINDOW_W, WINDOW_H);
        window.setMinimumSize(WINDOW_LARGER_W, WINDOW_LARGER_H);
        
        // size, then maximum
        assertTrue(window.getX() == WINDOW_X);
        assertTrue(window.getY() == WINDOW_Y);
        assertTrue(window.getWidth() == WINDOW_LARGER_W);
        assertTrue(window.getHeight() == WINDOW_LARGER_H);
    }

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

        window.setMinimumSize(WINDOW_LARGER_W, WINDOW_LARGER_H);
        window.setSize(WINDOW_W, WINDOW_H);

        // maximum, then size
        assertTrue(window.getX() == WINDOW_X);
        assertTrue(window.getY() == WINDOW_Y);
        assertTrue(window.getWidth() == WINDOW_LARGER_W);
        assertTrue(window.getHeight() == WINDOW_LARGER_H);
    }

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

        window.setSize(WINDOW_W, WINDOW_H);
        window.maximize(true);

        window.setMinimumSize(WINDOW_LARGER_W, WINDOW_LARGER_H);
        window.maximize(false);

        assertTrue(window.getWidth() == WINDOW_LARGER_W);
        assertTrue(window.getHeight() == WINDOW_LARGER_H);
    }

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

        window.setSize(WINDOW_W, WINDOW_H);
        window.maximize(true);

        window.setMaximumSize(WINDOW_SMALLER_W, WINDOW_SMALLER_H);
        window.maximize(false);

        assertTrue(window.getWidth() == WINDOW_SMALLER_W);
        assertTrue(window.getHeight() == WINDOW_SMALLER_H);
    }

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

        window.setSize(WINDOW_W, WINDOW_H);
        window.minimize(true);

        window.setMinimumSize(WINDOW_LARGER_W, WINDOW_LARGER_H);
        window.minimize(false);

        assertTrue(window.getWidth() == WINDOW_LARGER_W);
        assertTrue(window.getHeight() == WINDOW_LARGER_H);
    }

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

        window.setSize(WINDOW_W, WINDOW_H);
        window.minimize(true);

        window.setMaximumSize(WINDOW_SMALLER_W, WINDOW_SMALLER_H);
        window.minimize(false);

        assertTrue(window.getWidth() == WINDOW_SMALLER_W);
        assertTrue(window.getHeight() == WINDOW_SMALLER_H);
    }
}
