Browse Source

Added some Visual tests.

pull/4/head
Steven Kirk 12 years ago
parent
commit
e67c73ff2f
  1. 2
      Perspex.Controls/TreeViewItem.cs
  2. 102
      Perspex.SceneGraph.UnitTests/Perspex.SceneGraph.UnitTests.csproj
  3. 36
      Perspex.SceneGraph.UnitTests/Properties/AssemblyInfo.cs
  4. 20
      Perspex.SceneGraph.UnitTests/TestRoot.cs
  5. 77
      Perspex.SceneGraph.UnitTests/TestVisual.cs
  6. 119
      Perspex.SceneGraph.UnitTests/VisualTests.cs
  7. 4
      Perspex.SceneGraph.UnitTests/packages.config
  8. 66
      Perspex.SceneGraph/Visual.cs
  9. 6
      Perspex.sln
  10. 1
      packages/repositories.config

2
Perspex.Controls/TreeViewItem.cs

@ -48,7 +48,7 @@ namespace Perspex.Controls
}
}
protected override void OnVisualParentChanged(IVisual oldParent)
protected override void OnVisualParentChanged(Visual oldParent)
{
this.parent = this.GetVisualAncestors().OfType<TreeView>().FirstOrDefault();
}

102
Perspex.SceneGraph.UnitTests/Perspex.SceneGraph.UnitTests.csproj

@ -0,0 +1,102 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{76716382-3159-460E-BDA6-C5715CF606D7}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Perspex.SceneGraph.UnitTests</RootNamespace>
<AssemblyName>Perspex.SceneGraph.UnitTests</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Splat, Version=1.3.3.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Splat.1.3.3\lib\Net45\Splat.dll</HintPath>
</Reference>
<Reference Include="System" />
</ItemGroup>
<Choose>
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
</ItemGroup>
</When>
<Otherwise>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework" />
</ItemGroup>
</Otherwise>
</Choose>
<ItemGroup>
<Compile Include="TestRoot.cs" />
<Compile Include="TestVisual.cs" />
<Compile Include="VisualTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Perspex.Base\Perspex.Base.csproj">
<Project>{b09b78d8-9b26-48b0-9149-d64a2f120f3f}</Project>
<Name>Perspex.Base</Name>
</ProjectReference>
<ProjectReference Include="..\Perspex.SceneGraph\Perspex.SceneGraph.csproj">
<Project>{eb582467-6abb-43a1-b052-e981ba910e3a}</Project>
<Name>Perspex.SceneGraph</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.CodedUITestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Extension, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITesting, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
</ItemGroup>
</When>
</Choose>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

36
Perspex.SceneGraph.UnitTests/Properties/AssemblyInfo.cs

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Perspex.SceneGraph.UnitTests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Perspex.SceneGraph.UnitTests")]
[assembly: AssemblyCopyright("Copyright © 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("9b18d549-61cc-46b3-a3f2-80a0b68b747f")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

20
Perspex.SceneGraph.UnitTests/TestRoot.cs

@ -0,0 +1,20 @@
// -----------------------------------------------------------------------
// <copyright file="TestVisual.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.SceneGraph.UnitTests
{
using System;
using System.Collections.Generic;
using System.Linq;
using Perspex.Rendering;
public class TestRoot : TestVisual, IRenderRoot
{
public IRenderManager RenderManager
{
get { throw new NotImplementedException(); }
}
}
}

77
Perspex.SceneGraph.UnitTests/TestVisual.cs

@ -0,0 +1,77 @@
// -----------------------------------------------------------------------
// <copyright file="TestVisual.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.SceneGraph.UnitTests
{
using System;
using System.Collections.Generic;
using System.Linq;
using Perspex.Rendering;
public class ParamEventArgs<T> : EventArgs
{
public ParamEventArgs(T param)
{
this.Param = param;
}
public T Param { get; set; }
}
public class TestVisual : Visual
{
public Visual[] InitialChildren { get; set; }
public event EventHandler<ParamEventArgs<Visual>> VisualParentChangedCalled;
public event EventHandler<ParamEventArgs<IRenderRoot>> AttachedToVisualTreeCalled;
public event EventHandler<ParamEventArgs<IRenderRoot>> DetachedFromVisualTreeCalled;
public void AddChild(Visual v)
{
this.AddVisualChild(v);
}
public void RemoveChild(Visual v)
{
this.RemoveVisualChild(v);
}
public void ClearChildren()
{
this.ClearVisualChildren();
}
protected override IEnumerable<Visual> CreateVisualChildren()
{
return this.InitialChildren ?? Enumerable.Empty<Visual>();
}
protected override void OnVisualParentChanged(Visual oldParent)
{
if (this.VisualParentChangedCalled != null)
{
this.VisualParentChangedCalled(this, new ParamEventArgs<Visual>(oldParent));
}
}
protected override void OnAttachedToVisualTree(IRenderRoot root)
{
if (this.AttachedToVisualTreeCalled != null)
{
this.AttachedToVisualTreeCalled(this, new ParamEventArgs<IRenderRoot>(root));
}
}
protected override void OnDetachedFromVisualTree(IRenderRoot oldRoot)
{
if (this.DetachedFromVisualTreeCalled != null)
{
this.DetachedFromVisualTreeCalled(this, new ParamEventArgs<IRenderRoot>(oldRoot));
}
}
}
}

119
Perspex.SceneGraph.UnitTests/VisualTests.cs

@ -0,0 +1,119 @@
// -----------------------------------------------------------------------
// <copyright file="VisualTests.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Perspex.SceneGraph.UnitTests
{
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class VisualTests
{
[TestMethod]
public void Initial_Children_Should_Be_Created()
{
var target = new TestVisual
{
InitialChildren = new[] { new Visual(), new Visual() }
};
var result = target.GetVisualChildren().ToList();
CollectionAssert.AreEqual(target.InitialChildren.ToList(), result);
}
[TestMethod]
public void Initial_Children_Should_Have_VisualParent_Set()
{
var target = new TestVisual
{
InitialChildren = new[] { new Visual(), new Visual() }
};
var result = target.GetVisualChildren().Select(x => x.GetVisualParent()).ToList();
CollectionAssert.AreEqual(new[] { target, target }, result);
}
[TestMethod]
public void Added_Child_Should_Have_VisualParent_Set()
{
var target = new TestVisual();
var child = new Visual();
target.AddChild(child);
Assert.AreEqual(target, child.GetVisualParent());
}
[TestMethod]
public void Removed_Child_Should_Have_VisualParent_Cleared()
{
var target = new TestVisual();
var child = new Visual();
target.AddChild(child);
target.RemoveChild(child);
Assert.IsNull(child.GetVisualParent());
}
[TestMethod]
public void Clearing_Children_Should_Clear_VisualParent()
{
var target = new TestVisual
{
InitialChildren = new[] { new Visual(), new Visual() }
};
target.ClearChildren();
var result = target.InitialChildren.Select(x => x.GetVisualParent()).ToList();
CollectionAssert.AreEqual(new Visual[] { null, null }, result);
}
[TestMethod]
public void ParentChanged_Attached_Methods_Should_Be_Called_In_Right_Order()
{
var target = new TestRoot();
var child = new TestVisual();
int changed = 0;
int attched = 0;
int i = 1;
target.InitialChildren = new[] { child };
child.VisualParentChangedCalled += (s, e) => changed = i++;
child.AttachedToVisualTreeCalled += (s, e) => attched = i++;
target.GetVisualChildren().First();
Assert.AreEqual(1, changed);
Assert.AreEqual(2, attched);
}
[TestMethod]
public void ParentChanged_Detached_Methods_Should_Be_Called_In_Right_Order()
{
var target = new TestRoot();
var child = new TestVisual();
int changed = 0;
int detached = 0;
int i = 1;
target.InitialChildren = new[] { child };
target.GetVisualChildren().First();
child.VisualParentChangedCalled += (s, e) => changed = i++;
child.DetachedFromVisualTreeCalled += (s, e) => detached = i++;
target.ClearChildren();
Assert.AreEqual(1, changed);
Assert.AreEqual(2, detached);
}
}
}

4
Perspex.SceneGraph.UnitTests/packages.config

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Splat" version="1.3.3" targetFramework="net45" />
</packages>

66
Perspex.SceneGraph/Visual.cs

@ -14,7 +14,7 @@ namespace Perspex
using Perspex.Rendering;
using Splat;
public abstract class Visual : PerspexObject, IVisual
public class Visual : PerspexObject, IVisual
{
public static readonly PerspexProperty<bool> IsVisibleProperty =
PerspexProperty.Register<Visual, bool>("IsVisible", true);
@ -32,7 +32,7 @@ namespace Perspex
private PerspexList<Visual> visualChildren;
private IVisual visualParent;
private Visual visualParent;
static Visual()
{
@ -79,7 +79,10 @@ namespace Perspex
IVisual IVisual.VisualParent
{
get { return this.visualParent; }
get
{
return this.visualParent;
}
}
public void InvalidateVisual()
@ -119,7 +122,13 @@ namespace Perspex
protected void ClearVisualChildren()
{
this.EnsureVisualChildrenCreated();
this.visualChildren.Clear();
// TODO: Just call visualChildren.Clear() when we have a PerspexList that notifies of
// the removed items.
while (this.visualChildren.Count > 0)
{
this.visualChildren.RemoveAt(this.visualChildren.Count - 1);
}
}
protected void RemoveVisualChild(Visual visual)
@ -146,7 +155,7 @@ namespace Perspex
{
}
protected virtual void OnVisualParentChanged(IVisual oldParent)
protected virtual void OnVisualParentChanged(Visual oldParent)
{
}
@ -164,13 +173,58 @@ namespace Perspex
{
if (this.visualChildren == null)
{
this.visualChildren = new PerspexList<Visual>(this.CreateVisualChildren());
this.visualChildren = new PerspexList<Visual>();
this.visualChildren.CollectionChanged += VisualChildrenChanged;
this.visualChildren.AddRange(this.CreateVisualChildren());
}
}
private void SetVisualParent(Visual value)
{
if (this.visualParent != value)
{
var old = this.visualParent;
var oldRoot = this.GetVisualAncestors().OfType<IRenderRoot>().FirstOrDefault();
var newRoot = default(IRenderRoot);
if (value != null)
{
newRoot = value.GetSelfAndVisualAncestors().OfType<IRenderRoot>().FirstOrDefault();
}
this.visualParent = value;
this.OnVisualParentChanged(old);
if (oldRoot != null)
{
this.NotifyDetachedFromVisualTree(oldRoot);
}
if (newRoot != null)
{
this.NotifyAttachedToVisualTree(newRoot);
}
}
}
private void VisualChildrenChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
foreach (Visual v in e.NewItems)
{
v.SetVisualParent(this);
}
break;
case NotifyCollectionChangedAction.Remove:
foreach (Visual v in e.OldItems)
{
v.SetVisualParent(null);
}
break;
}
}
private void NotifyAttachedToVisualTree(IRenderRoot root)

6
Perspex.sln

@ -37,6 +37,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Perspex.Controls.UnitTests"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Perspex.Themes.Default", "Perspex.Themes.Default\Perspex.Themes.Default.csproj", "{3E10A5FA-E8DA-48B1-AD44-6A5B6CB7750F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Perspex.SceneGraph.UnitTests", "Perspex.SceneGraph.UnitTests\Perspex.SceneGraph.UnitTests.csproj", "{76716382-3159-460E-BDA6-C5715CF606D7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -107,6 +109,10 @@ Global
{3E10A5FA-E8DA-48B1-AD44-6A5B6CB7750F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3E10A5FA-E8DA-48B1-AD44-6A5B6CB7750F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3E10A5FA-E8DA-48B1-AD44-6A5B6CB7750F}.Release|Any CPU.Build.0 = Release|Any CPU
{76716382-3159-460E-BDA6-C5715CF606D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{76716382-3159-460E-BDA6-C5715CF606D7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{76716382-3159-460E-BDA6-C5715CF606D7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{76716382-3159-460E-BDA6-C5715CF606D7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

1
packages/repositories.config

@ -8,6 +8,7 @@
<repository path="..\Perspex.Input\packages.config" />
<repository path="..\Perspex.Interactivity\packages.config" />
<repository path="..\Perspex.Layout\packages.config" />
<repository path="..\Perspex.SceneGraph.UnitTests\packages.config" />
<repository path="..\Perspex.SceneGraph\packages.config" />
<repository path="..\Perspex.Styling.UnitTests\packages.config" />
<repository path="..\Perspex.Styling\packages.config" />

Loading…
Cancel
Save