aboutsummaryrefslogtreecommitdiffstatsci
path: root/UnitTest
diff options
context:
space:
mode:
authorjpbruyere <jp.bruyere@hotmail.com>2016-01-30 09:42:37 +0100
committerjpbruyere <jp.bruyere@hotmail.com>2016-01-30 09:42:37 +0100
commit9fa17cdc9487e3ba92ef467c0c6d5d9941530de1 (patch)
tree0ac2e35c537e132bfa16adc486e7481e166a150d /UnitTest
parent0bf825fce6f1946034dbea6b1f7eb8d0dacdab8a (diff)
NUnit try, renaming, debugging and uniformizating
Diffstat (limited to 'UnitTest')
-rw-r--r--UnitTest/ExpectedOutputs/0.pngbin0 -> 937 bytes
-rw-r--r--UnitTest/ExpectedOutputs/1.pngbin0 -> 1366 bytes
-rwxr-xr-xUnitTest/Interfaces/0.crow2
-rwxr-xr-xUnitTest/Interfaces/1.crow7
-rwxr-xr-xUnitTest/Interfaces/2.crow7
-rw-r--r--UnitTest/NUnitCrowWindow.cs456
-rw-r--r--UnitTest/Test.cs64
-rw-r--r--UnitTest/UnitTest.csproj78
-rw-r--r--UnitTest/packages.config4
9 files changed, 618 insertions, 0 deletions
diff --git a/UnitTest/ExpectedOutputs/0.png b/UnitTest/ExpectedOutputs/0.png
new file mode 100644
index 00000000..acf96fa6
--- /dev/null
+++ b/UnitTest/ExpectedOutputs/0.png
Binary files differ
diff --git a/UnitTest/ExpectedOutputs/1.png b/UnitTest/ExpectedOutputs/1.png
new file mode 100644
index 00000000..f792986c
--- /dev/null
+++ b/UnitTest/ExpectedOutputs/1.png
Binary files differ
diff --git a/UnitTest/Interfaces/0.crow b/UnitTest/Interfaces/0.crow
new file mode 100755
index 00000000..64d6db6d
--- /dev/null
+++ b/UnitTest/Interfaces/0.crow
@@ -0,0 +1,2 @@
+<?xml version="1.0"?>
+<GraphicObject Width="100" Height="100" Background="Green">
diff --git a/UnitTest/Interfaces/1.crow b/UnitTest/Interfaces/1.crow
new file mode 100755
index 00000000..142d0721
--- /dev/null
+++ b/UnitTest/Interfaces/1.crow
@@ -0,0 +1,7 @@
+<?xml version="1.0"?>
+<Container Width="300" Height="300" Margin="20" Background="Yellow">
+ <Container Width="0" Height="0" Margin="20" Background="Green">
+ <GraphicObject VerticalAlignment="Bottom" Width="50" Height="50"
+ Background="Red"/>
+ </Container>
+</Container> \ No newline at end of file
diff --git a/UnitTest/Interfaces/2.crow b/UnitTest/Interfaces/2.crow
new file mode 100755
index 00000000..dcccba04
--- /dev/null
+++ b/UnitTest/Interfaces/2.crow
@@ -0,0 +1,7 @@
+<?xml version="1.0"?>
+<VerticalStack Fit="true">
+ <HorizontalStack Fit="true">
+ <Label Text="label1"/>
+ <TextRun Text="textrun1"/>
+ </HorizontalStack>
+</VerticalStack> \ No newline at end of file
diff --git a/UnitTest/NUnitCrowWindow.cs b/UnitTest/NUnitCrowWindow.cs
new file mode 100644
index 00000000..0c73b8e7
--- /dev/null
+++ b/UnitTest/NUnitCrowWindow.cs
@@ -0,0 +1,456 @@
+#define MONO_CAIRO_DEBUG_DISPOSE
+
+
+using System;
+using System.Runtime.InteropServices;
+using OpenTK;
+using OpenTK.Graphics.OpenGL;
+using OpenTK.Input;
+
+using System.Diagnostics;
+
+//using GGL;
+using System.Threading;
+using System.Collections.Generic;
+using System.Linq;
+using Cairo;
+
+
+namespace Crow
+{
+ class NUnitCrowWindow : IValueChange, ILayoutable, IGOLibHost
+ {
+ #region IValueChange implementation
+ public event EventHandler<ValueChangeEventArgs> ValueChanged;
+ public virtual void NotifyValueChanged(string MemberName, object _value)
+ {
+ ValueChanged.Raise(this, new ValueChangeEventArgs(MemberName, _value));
+ }
+ #endregion
+
+ public Rectangle ClientRectangle = new Rectangle(0,0,800,600);
+ public List<GraphicObject> GraphicObjects = new List<GraphicObject>();
+ public Color Background = Color.Transparent;
+
+ Rectangles _redrawClip = new Rectangles();//should find another way to access it from child
+ List<GraphicObject> _gobjsToRedraw = new List<GraphicObject>();
+
+ #region IGOLibHost implementation
+ public Rectangles redrawClip {
+ get {
+ return _redrawClip;
+ }
+ set {
+ _redrawClip = value;
+ }
+ }
+
+ public List<GraphicObject> gobjsToRedraw {
+ get {
+ return _gobjsToRedraw;
+ }
+ set {
+ _gobjsToRedraw = value;
+ }
+ }
+ public void AddWidget(GraphicObject g)
+ {
+ g.Parent = this;
+ GraphicObjects.Insert (0, g);
+
+ g.RegisterForLayouting ((int)LayoutingType.Sizing);
+ }
+ public void DeleteWidget(GraphicObject g)
+ {
+ g.Visible = false;//trick to ensure clip is added to refresh zone
+ g.ClearBinding();
+ GraphicObjects.Remove (g);
+ }
+ public void PutOnTop(GraphicObject g)
+ {
+ if (GraphicObjects.IndexOf(g) > 0)
+ {
+ GraphicObjects.Remove(g);
+ GraphicObjects.Insert(0, g);
+ g.registerClipRect ();
+ }
+ }
+ public void Quit ()
+ {
+ }
+
+ #region focus
+ GraphicObject _activeWidget; //button is pressed on widget
+ GraphicObject _hoverWidget; //mouse is over
+ GraphicObject _focusedWidget; //has keyboard (or other perif) focus
+
+ public GraphicObject activeWidget
+ {
+ get { return _activeWidget; }
+ set
+ {
+ if (_activeWidget == value)
+ return;
+ _activeWidget = value;
+ }
+ }
+ public GraphicObject hoverWidget
+ {
+ get { return _hoverWidget; }
+ set { _hoverWidget = value; }
+ }
+ public GraphicObject FocusedWidget {
+ get { return _focusedWidget; }
+ set {
+ if (_focusedWidget == value)
+ return;
+ if (_focusedWidget != null)
+ _focusedWidget.onUnfocused (this, null);
+ _focusedWidget = value;
+ if (_focusedWidget != null)
+ _focusedWidget.onFocused (this, null);
+ }
+ }
+ #endregion
+
+ #endregion
+
+ #region Events
+ //those events are raised only if mouse isn't in a graphic object
+ public event EventHandler<MouseWheelEventArgs> MouseWheelChanged;
+ public event EventHandler<MouseButtonEventArgs> MouseButtonUp;
+ public event EventHandler<MouseButtonEventArgs> MouseButtonDown;
+ public event EventHandler<MouseButtonEventArgs> MouseClick;
+ public event EventHandler<MouseMoveEventArgs> MouseMove;
+ #endregion
+
+ #region graphic contexte
+ Context ctx;
+ public Surface surf;
+ string testId;
+ #endregion
+
+ #region update
+ public Stopwatch updateTime = new Stopwatch ();
+ public Stopwatch layoutTime = new Stopwatch ();
+ public Stopwatch guTime = new Stopwatch ();
+ public Stopwatch drawingTime = new Stopwatch ();
+
+ public void Update ()
+ {
+ guTime.Reset ();
+ updateTime.Restart ();
+ layoutTime.Restart ();
+
+ ctx = new Context(surf);
+
+ GraphicObject[] invGOList = new GraphicObject[GraphicObjects.Count];
+ GraphicObjects.CopyTo (invGOList,0);
+ invGOList = invGOList.Reverse ().ToArray ();
+
+ //Debug.WriteLine ("======= Layouting queue start =======");
+ lock (Interface.LayoutingQueue) {
+ while (Interface.LayoutingQueue.Count > 0) {
+ // Stopwatch lqiProcTime = new Stopwatch ();
+ // lqiProcTime.Start ();
+ LayoutingQueueItem lqi = Interface.LayoutingQueue.Dequeue ();
+ lqi.ProcessLayouting ();
+ // lqiProcTime.Stop ();
+ // if (lqiProcTime.ElapsedMilliseconds > 10) {
+ // Debug.WriteLine("lqi {2}: {0} ticks \t, {1} ms",
+ // updateTime.ElapsedTicks,
+ // updateTime.ElapsedMilliseconds, lqi.ToString());
+ // }
+ }
+ }
+
+ //final redraw clips should be added only when layout is completed among parents,
+ //that's why it take place in a second pass
+ GraphicObject[] gotr = new GraphicObject[gobjsToRedraw.Count];
+ gobjsToRedraw.CopyTo (gotr);
+ gobjsToRedraw.Clear ();
+ foreach (GraphicObject p in gotr) {
+ p.registerClipRect ();
+ }
+
+ layoutTime.Stop ();
+ guTime.Start ();
+
+ lock (redrawClip) {
+ if (redrawClip.count > 0) {
+ // #if DEBUG_CLIP_RECTANGLE
+ // redrawClip.stroke (ctx, new Color(1.0,0,0,0.3));
+ // #endif
+ redrawClip.clearAndClip (ctx);//rajouté après, tester si utile
+
+ //Link.draw (ctx);
+ foreach (GraphicObject p in invGOList) {
+ if (p.Visible) {
+ drawingTime.Start ();
+
+ ctx.Save ();
+ if (redrawClip.count > 0) {
+ Rectangles clip = redrawClip.intersectingRects (p.Slot);
+
+ if (clip.count > 0)
+ p.Paint (ref ctx, clip);
+ }
+ ctx.Restore ();
+
+ drawingTime.Stop ();
+ }
+ }
+ ctx.ResetClip ();
+ // #if DEBUG_CLIP_RECTANGLE
+ // redrawClip.stroke (ctx, Color.Red.AdjustAlpha(0.1));
+ // #endif
+ redrawClip.Reset ();
+ }
+ }
+ guTime.Stop ();
+ updateTime.Stop ();
+ ctx.Dispose ();
+ Console.WriteLine("{3} => layout:{0,8} t\tdraw:{1,8} t\tupdate:{2,8} t",
+ layoutTime.ElapsedTicks,
+ guTime.ElapsedTicks,
+ updateTime.ElapsedTicks,
+ testId);
+// Console.WriteLine("{3} => layout:{0}ms\tdraw{1}ms\tupdate:{2}ms",
+// layoutTime.ElapsedMilliseconds,
+// guTime.ElapsedMilliseconds,
+// updateTime.ElapsedMilliseconds,
+// testId);
+ surf.WriteToPng (@"ExpectedOutputs/" + testId + ".png");
+ surf.WriteToPng (@"tmp.png");
+ }
+ #endregion
+
+ #region loading
+ public GraphicObject LoadTest (string id)
+ {
+ testId = id;
+ GraphicObject tmp = Interface.Load ("Interfaces/" + testId + ".crow", this);
+ AddWidget (tmp);
+ return tmp;
+ }
+ /// <summary> Remove all Graphic objects from top container </summary>
+ public void ClearInterface()
+ {
+ int i = 0;
+ while (GraphicObjects.Count>0) {
+ GraphicObject g = GraphicObjects [i];
+ g.Visible = false;
+ g.ClearBinding ();
+ GraphicObjects.RemoveAt (0);
+ }
+ }
+ #endregion
+
+ public NUnitCrowWindow (int width, int height)
+ {
+ ClientRectangle.Width = width;
+ ClientRectangle.Height = height;
+
+ surf = new ImageSurface(Format.Argb32, ClientRectangle.Width, ClientRectangle.Height);
+ }
+
+ int frameCpt = 0;
+ int idx = 0;
+
+
+ #region FPS
+ int _fps = 0;
+
+ public int fps {
+ get { return _fps; }
+ set {
+ if (_fps == value)
+ return;
+
+ _fps = value;
+
+ if (_fps > fpsMax) {
+ fpsMax = _fps;
+ ValueChanged.Raise(this, new ValueChangeEventArgs ("fpsMax", fpsMax));
+ } else if (_fps < fpsMin) {
+ fpsMin = _fps;
+ ValueChanged.Raise(this, new ValueChangeEventArgs ("fpsMin", fpsMin));
+ }
+
+ ValueChanged.Raise(this, new ValueChangeEventArgs ("fps", _fps));
+ ValueChanged.Raise (this, new ValueChangeEventArgs ("update",
+ this.updateTime.ElapsedMilliseconds.ToString () + " ms"));
+ }
+ }
+
+ public int fpsMin = int.MaxValue;
+ public int fpsMax = 0;
+
+ void resetFps ()
+ {
+ fpsMin = int.MaxValue;
+ fpsMax = 0;
+ _fps = 0;
+ }
+ //public string update = "";
+ #endregion
+
+
+ #region Mouse Handling
+ void Mouse_Move(object sender, MouseMoveEventArgs e)
+ {
+ if (_activeWidget != null) {
+ //first, ensure object is still in the graphic tree
+ if (_activeWidget.TopContainer == null) {
+ activeWidget = null;
+ } else {
+
+ //send move evt even if mouse move outside bounds
+ _activeWidget.onMouseMove (_activeWidget, e);
+ return;
+ }
+ }
+
+ if (_hoverWidget != null) {
+ //first, ensure object is still in the graphic tree
+ if (_hoverWidget.TopContainer == null) {
+ hoverWidget = null;
+ } else {
+ //check topmost graphicobject first
+ GraphicObject tmp = _hoverWidget;
+ GraphicObject topc = null;
+ while (tmp is GraphicObject) {
+ topc = tmp;
+ tmp = tmp.Parent as GraphicObject;
+ }
+ int idxhw = GraphicObjects.IndexOf (topc);
+ if (idxhw != 0) {
+ int i = 0;
+ while (i < idxhw) {
+ if (GraphicObjects [i].MouseIsIn (e.Position)) {
+ _hoverWidget.onMouseLeave (this, e);
+ GraphicObjects [i].checkHoverWidget (e);
+ return;
+ }
+ i++;
+ }
+ }
+
+
+ if (_hoverWidget.MouseIsIn (e.Position)) {
+ _hoverWidget.checkHoverWidget (e);
+ return;
+ } else {
+ _hoverWidget.onMouseLeave (this, e);
+ //seek upward from last focused graph obj's
+ while (_hoverWidget.Parent as GraphicObject != null) {
+ _hoverWidget = _hoverWidget.Parent as GraphicObject;
+ if (_hoverWidget.MouseIsIn (e.Position)) {
+ _hoverWidget.checkHoverWidget (e);
+ return;
+ } else
+ _hoverWidget.onMouseLeave (this, e);
+ }
+ }
+ }
+ }
+
+ //top level graphic obj's parsing
+ for (int i = 0; i < GraphicObjects.Count; i++) {
+ GraphicObject g = GraphicObjects[i];
+ if (g.MouseIsIn (e.Position)) {
+ g.checkHoverWidget (e);
+ PutOnTop (g);
+ return;
+ }
+ }
+ _hoverWidget = null;
+ MouseMove.Raise (this, e);
+ }
+ void Mouse_ButtonUp(object sender, MouseButtonEventArgs e)
+ {
+ if (_activeWidget == null) {
+ MouseButtonUp.Raise (this, e);
+ return;
+ }
+
+ _activeWidget.onMouseButtonUp (this, e);
+ _activeWidget = null;
+ }
+ void Mouse_ButtonDown(object sender, MouseButtonEventArgs e)
+ {
+ if (_hoverWidget == null) {
+ MouseButtonDown.Raise (this, e);
+ return;
+ }
+
+ GraphicObject g = _hoverWidget;
+ while (!g.Focusable) {
+ g = g.Parent as GraphicObject;
+ if (g == null) {
+ return;
+ }
+ }
+
+ _activeWidget = g;
+ _activeWidget.onMouseButtonDown (this, e);
+ }
+
+ void Mouse_WheelChanged(object sender, MouseWheelEventArgs e)
+ {
+ if (_hoverWidget == null) {
+ MouseWheelChanged.Raise (this, e);
+ return;
+ }
+ _hoverWidget.onMouseWheel (this, e);
+ }
+ #endregion
+
+ #region keyboard Handling
+ void Keyboard_KeyDown(object sender, KeyboardKeyEventArgs e)
+ {
+ if (_focusedWidget == null)
+ return;
+ _focusedWidget.onKeyDown (sender, e);
+ }
+ #endregion
+
+ #region ILayoutable implementation
+
+ public void RegisterForLayouting (int layoutType) { throw new NotImplementedException (); }
+ public void UpdateLayout (LayoutingType layoutType) { throw new NotImplementedException (); }
+ public Rectangle ContextCoordinates (Rectangle r)
+ {
+ return r;
+ }
+ public Rectangle ScreenCoordinates (Rectangle r)
+ {
+ return r;
+ }
+
+ public ILayoutable Parent {
+ get {
+ return null;
+ }
+ set {
+ throw new NotImplementedException ();
+ }
+ }
+ Rectangle ILayoutable.ClientRectangle {
+ get { return new Size(this.ClientRectangle.Size.Width,this.ClientRectangle.Size.Height); }
+ }
+ public IGOLibHost TopContainer {
+ get { return this; }
+ }
+
+ public Rectangle getSlot ()
+ {
+ return ClientRectangle;
+ }
+ public Rectangle getBounds ()//redundant but fill ILayoutable implementation
+ {
+ return ClientRectangle;
+ }
+ #endregion
+ }
+} \ No newline at end of file
diff --git a/UnitTest/Test.cs b/UnitTest/Test.cs
new file mode 100644
index 00000000..11555696
--- /dev/null
+++ b/UnitTest/Test.cs
@@ -0,0 +1,64 @@
+//
+// Test.cs
+//
+// Author:
+// Jean-Philippe Bruyère <jp_bruyere@hotmail.com>
+//
+// Copyright (c) 2016 jp
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program 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 for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+using NUnit.Framework;
+using System;
+using Crow;
+using System.IO;
+
+namespace UnitTest
+{
+ [TestFixture ()]
+ public class Test
+ {
+ NUnitCrowWindow win;
+
+ [SetUp]
+ public void Init()
+ {
+ win = new NUnitCrowWindow (400,400);
+ }
+
+ [Test ()]
+ [Category("Alignment")]
+ public void GraphicObject ()
+ {
+ string[] tests = new string[] { "0","1" };
+
+ foreach (string s in tests) {
+ win.LoadTest (s);
+ win.Update ();
+ win.Update ();
+ byte[] model = File.ReadAllBytes("ExpectedOutputs/" + s + ".png");
+ byte[] result = File.ReadAllBytes(@"tmp.png");
+
+ CollectionAssert.AreEqual (model, result);
+
+ win.ClearInterface ();
+ }
+ }
+
+ void testAlignment(GraphicObject g){
+ g.HorizontalAlignment = HorizontalAlignment.Left;
+
+ }
+ }
+}
+
diff --git a/UnitTest/UnitTest.csproj b/UnitTest/UnitTest.csproj
new file mode 100644
index 00000000..ddd53b61
--- /dev/null
+++ b/UnitTest/UnitTest.csproj
@@ -0,0 +1,78 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>8.0.30703</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{7BAE4448-E8F4-48B3-BB11-FA78E7F4506B}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <RootNamespace>UnitTest</RootNamespace>
+ <AssemblyName>UnitTest</AssemblyName>
+ <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug</OutputPath>
+ <DefineConstants>DEBUG;</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <ConsolePause>false</ConsolePause>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>full</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release</OutputPath>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <ConsolePause>false</ConsolePause>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System" />
+ <Reference Include="Mono.Cairo" />
+ <Reference Include="OpenTK">
+ <HintPath>..\..\opentk\Binaries\OpenTK\Release\OpenTK.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Data" />
+ <Reference Include="System.Drawing" />
+ <Reference Include="System.Xml" />
+ <Reference Include="nunit.framework, Version=2.6.4.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77">
+ <Package>nunit</Package>
+ </Reference>
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="Test.cs" />
+ <Compile Include="NUnitCrowWindow.cs" />
+ </ItemGroup>
+ <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+ <ItemGroup>
+ <None Include="packages.config" />
+ <None Include="Interfaces\0.crow">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </None>
+ <None Include="ExpectedOutputs\0.png">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </None>
+ <None Include="Interfaces\1.crow">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </None>
+ <None Include="ExpectedOutputs\1.png">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </None>
+ <None Include="Interfaces\2.crow">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </None>
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\Crow.csproj">
+ <Project>{C2980F9B-4798-4C05-99E2-E174810F7C7B}</Project>
+ <Name>Crow</Name>
+ </ProjectReference>
+ </ItemGroup>
+ <ItemGroup>
+ <Folder Include="Interfaces\" />
+ <Folder Include="ExpectedOutputs\" />
+ </ItemGroup>
+</Project> \ No newline at end of file
diff --git a/UnitTest/packages.config b/UnitTest/packages.config
new file mode 100644
index 00000000..b183023e
--- /dev/null
+++ b/UnitTest/packages.config
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+ <package id="NUnit" version="3.0.1" targetFramework="net45" />
+</packages> \ No newline at end of file