Source

agg-sharp / Gui / UnitTests.cs

Full commit
/*
Copyright (c) 2012, Lars Brubaker
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met: 

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer. 
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies, 
either expressed or implied, of the FreeBSD Project.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NUnit.Framework;
using MatterHackers.Agg;
using MatterHackers.Agg.Image;

namespace MatterHackers.Agg.UI
{
    [TestFixture]
    public class RenderTests
    {
        [Test]
        public void DoubleBufferTests()
        {
            {
                bool textWidgetDoubleBufferDefault = TextWidget.DoubleBufferDefault;

                TextWidget.DoubleBufferDefault = true;
                ImageBuffer doubleBufferImage = new ImageBuffer(65, 50, 24, new BlenderBGR());
                Button doubleBufferButton = new Button(0, 0, "testing");
                doubleBufferButton.OnDraw(doubleBufferImage.NewGraphics2D());

                ImageBuffer doubleBufferImageCopy = new ImageBuffer(doubleBufferImage, new BlenderBGR());
                // make sure the frame comparison function works.
                Assert.IsTrue(doubleBufferImage == doubleBufferImageCopy);

                TextWidget.DoubleBufferDefault = false;
                ImageBuffer notDoubleBufferImage = new ImageBuffer(65, 50, 24, new BlenderBGR());
                Button notDoubleBufferButton = new Button(0, 0, "testing");
                notDoubleBufferButton.OnDraw(notDoubleBufferImage.NewGraphics2D());

                Assert.IsTrue(doubleBufferImage == notDoubleBufferImage);

                TextWidget.DoubleBufferDefault = textWidgetDoubleBufferDefault;
            }
        }
    }

    public class ButtonTests
    {
        bool gotClick = false;

        [Test]
        public void ButtonClick()
        {
            gotClick = false;
            RectangleWidget container = new RectangleWidget();
            container.LocalBounds = new rect_d(0, 0, 200, 200);
            Button button = new Button(100, 100, "Test");
            button.Click += new ButtonBase.ButtonEventHandler(button_Click);
            container.AddChild(button);

            container.OnLayout();

            Assert.IsTrue(gotClick == false);
            container.OnMouseDown(new MouseEventArgs(MouseButtons.Left, 1, 10, 10, 0));
            container.OnMouseUp(new MouseEventArgs(MouseButtons.Left, 1, 10, 10, 0));
            Assert.IsTrue(gotClick == false);
            container.OnMouseDown(new MouseEventArgs(MouseButtons.Left, 1, 110, 110, 0));
            container.OnMouseUp(new MouseEventArgs(MouseButtons.Left, 1, 10, 10, 0));
            Assert.IsTrue(gotClick == false);

            Assert.IsTrue(gotClick == false);
            container.OnMouseDown(new MouseEventArgs(MouseButtons.Left, 1, 110, 110, 0));
            container.OnMouseUp(new MouseEventArgs(MouseButtons.Left, 1, 110, 110, 0));
            Assert.IsTrue(gotClick == true);
        }

        [Test]
        public void WindowBlocksButtonClick()
        {
            gotClick = false;
            RectangleWidget container = new RectangleWidget();
            container.LocalBounds = new rect_d(0, 0, 200, 200);
            Button button = new Button(100, 100, "Test");
            button.Click += new ButtonBase.ButtonEventHandler(button_Click);
            container.AddChild(button);

            WindowWidget window = new WindowWidget(new rect_d(105, 105, 125, 125));
            container.AddChild(window);

            container.OnLayout();

            // the window is not in the way
            Assert.IsTrue(gotClick == false);
            container.OnMouseDown(new MouseEventArgs(MouseButtons.Left, 1, 101, 101, 0));
            container.OnMouseUp(new MouseEventArgs(MouseButtons.Left, 1, 101, 101, 0));
            Assert.IsTrue(gotClick == true);

            gotClick = false;

            // the window is in the way
            Assert.IsTrue(gotClick == false);
            container.OnMouseDown(new MouseEventArgs(MouseButtons.Left, 1, 110, 110, 0));
            container.OnMouseUp(new MouseEventArgs(MouseButtons.Left, 1, 110, 110, 0));
            Assert.IsTrue(gotClick == false);
        }

        void button_Click(object sender, MouseEventArgs mouseEvent)
        {
            gotClick = true;
        }
    }

    public class TextEditTests
    {
        public void SendKey(Keys keyDown, char keyPressed, GUIWidget reciever)
        {
            KeyEventArgs keyDownEvent = new KeyEventArgs(keyDown);
            reciever.OnKeyDown(keyDownEvent);
            if (!keyDownEvent.SuppressKeyPress)
            {
                KeyPressEventArgs keyPressEvent = new KeyPressEventArgs(keyPressed);
                reciever.OnKeyPress(keyPressEvent);
            }
        }

        [Test]
        public void TextEditGetsInput()
        {
            RectangleWidget container = new RectangleWidget();
            container.LocalBounds = new rect_d(0, 0, 200, 200);
            TextEditWidget editField1 = new TextEditWidget("", 0, 0, pixelWidth: 20);
            container.AddChild(editField1);

            TextEditWidget editField2 = new TextEditWidget("", 0, 20, pixelWidth: 20);
            container.AddChild(editField2);

            container.OnLayout();

            // select no edit field
            Assert.IsTrue(editField1.Text == "");
            SendKey(Keys.D, 'a', container);
            Assert.IsTrue(editField1.Text == "");
            Assert.IsTrue(editField2.Text == "");

            // select it edit field 1
            container.OnMouseDown(new MouseEventArgs(MouseButtons.Left, 1, 1, 1, 0));
            container.OnMouseUp(new MouseEventArgs(MouseButtons.Left, 1, 1, 1, 0));
            SendKey(Keys.D, 'b', container);
            Assert.IsTrue(editField1.Text == "b", "It should have b a in it.");

            // select it edit field 2
            container.OnMouseDown(new MouseEventArgs(MouseButtons.Left, 1, 1, 21, 0));
            container.OnMouseUp(new MouseEventArgs(MouseButtons.Left, 1, 1, 21, 0));
            SendKey(Keys.D, 'c', container);
            Assert.IsTrue(editField1.Text == "b", "It should stil only have a b in it.");
            Assert.IsTrue(editField2.Text == "c", "It should have c in it.");
        }
    }

    public static class UnitTests
    {
        static bool ranTests = false;

        public static bool RanTests { get { return ranTests; } }
        public static void Run()
        {
            if (!ranTests)
            {
                ranTests = true;
                RenderTests renderTests = new RenderTests();
                renderTests.DoubleBufferTests();

                ButtonTests buttonTests = new ButtonTests();
                buttonTests.ButtonClick();
                buttonTests.WindowBlocksButtonClick();

                TextEditTests textEditTests = new TextEditTests();
                textEditTests.TextEditGetsInput();
            }
        }
    }
}