Browse Source

Added some Control tests.

pull/4/head
Steven Kirk 13 years ago
parent
commit
099def38ea
  1. 105
      Perspex.UnitTests/Controls/ControlTests.cs
  2. 1
      Perspex.UnitTests/Perspex.UnitTests.csproj
  3. 25
      Perspex/Controls/Control.cs
  4. 3
      Perspex/Perspex.csproj
  5. 15
      Perspex/Styling/IStyled.cs
  6. 15
      Perspex/Styling/IStyler.cs
  7. 24
      Perspex/Styling/Styler.cs
  8. 7
      Perspex/VisualExtensions.cs
  9. 14
      TestApplication/Program.cs

105
Perspex.UnitTests/Controls/ControlTests.cs

@ -0,0 +1,105 @@
// -----------------------------------------------------------------------
// <copyright file="ControlTests.cs" company="Steven Kirk">
// Copyright 2013 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.UnitTests.Controls
{
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Perspex.Controls;
using Perspex.Layout;
using Perspex.Styling;
using Splat;
[TestClass]
public class ControlTests
{
[TestMethod]
public void Classes_Should_Initially_Be_Empty()
{
var target = new Control();
Assert.AreEqual(0, target.Classes.Count);
}
[TestMethod]
public void Setting_IsMouseOver_Should_Add_Mouseover_Class()
{
var target = new Control();
target.IsMouseOver = true;
CollectionAssert.AreEqual(new[] { ":mouseover" }, target.Classes.ToArray());
}
[TestMethod]
public void Clearing_IsMouseOver_Should_Remove_Mouseover_Class()
{
var target = new Control();
target.IsMouseOver = true;
target.IsMouseOver = false;
Assert.AreEqual(0, target.Classes.Count);
}
[TestMethod]
public void Adding_Control_To_ILayoutRoot_Should_Style_Control()
{
using (Locator.CurrentMutable.WithResolver())
{
var root = new TestRoot();
var target = new Control();
var styler = new Mock<IStyler>();
Locator.CurrentMutable.Register(() => styler.Object, typeof(IStyler));
root.Content = target;
styler.Verify(x => x.ApplyStyles(target), Times.Once());
}
}
[TestMethod]
public void Adding_Tree_To_ILayoutRoot_Should_Style_Controls()
{
using (Locator.CurrentMutable.WithResolver())
{
var root = new TestRoot();
var parent = new Border();
var child = new Border();
var grandchild = new Control();
var styler = new Mock<IStyler>();
Locator.CurrentMutable.Register(() => styler.Object, typeof(IStyler));
parent.Content = child;
child.Content = grandchild;
styler.Verify(x => x.ApplyStyles(It.IsAny<IStyleable>()), Times.Never());
root.Content = parent;
styler.Verify(x => x.ApplyStyles(parent), Times.Once());
styler.Verify(x => x.ApplyStyles(child), Times.Once());
styler.Verify(x => x.ApplyStyles(grandchild), Times.Once());
}
}
private class TestRoot : Decorator, ILayoutRoot
{
public Size ClientSize
{
get { throw new System.NotImplementedException(); }
}
public ILayoutManager LayoutManager
{
get { throw new System.NotImplementedException(); }
}
}
}
}

1
Perspex.UnitTests/Perspex.UnitTests.csproj

@ -68,6 +68,7 @@
</Otherwise>
</Choose>
<ItemGroup>
<Compile Include="Controls\ControlTests.cs" />
<Compile Include="PerspexPropertyTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="PerspexObjectTests.cs" />

25
Perspex/Controls/Control.cs

@ -15,6 +15,7 @@ namespace Perspex.Controls
using Perspex.Layout;
using Perspex.Media;
using Perspex.Styling;
using Splat;
public enum HorizontalAlignment
{
@ -32,7 +33,7 @@ namespace Perspex.Controls
Bottom,
}
public abstract class Control : Interactive, ILayoutable, IStyleable
public class Control : Interactive, ILayoutable, IStyleable, IStyled
{
public static readonly PerspexProperty<Brush> BackgroundProperty =
PerspexProperty.Register<Control, Brush>("Background", inherits: true);
@ -291,26 +292,16 @@ namespace Perspex.Controls
return finalSize;
}
protected abstract Size MeasureContent(Size availableSize);
protected override void AttachedToVisualTree()
protected virtual Size MeasureContent(Size availableSize)
{
this.AttachStyles(this);
base.AttachedToVisualTree();
return new Size();
}
private void AttachStyles(Control styleContainer)
protected override void AttachedToVisualTree()
{
if (styleContainer != null)
{
Control parent = styleContainer.Parent;
this.AttachStyles(parent);
styleContainer.Styles.Attach(this);
}
else
{
Application.Current.Styles.Attach(this);
}
IStyler styler = Locator.Current.GetService<IStyler>();
styler.ApplyStyles(this);
base.AttachedToVisualTree();
}
}
}

3
Perspex/Perspex.csproj

@ -86,6 +86,9 @@
<Compile Include="Interactive.cs" />
<Compile Include="IObservableDescription.cs" />
<Compile Include="IVisual.cs" />
<Compile Include="Styling\IStyler.cs" />
<Compile Include="Styling\Styler.cs" />
<Compile Include="Styling\IStyled.cs" />
<Compile Include="Styling\StyleActivator.cs" />
<Compile Include="Styling\IStyleable.cs" />
<Compile Include="Styling\IStyle.cs" />

15
Perspex/Styling/IStyled.cs

@ -0,0 +1,15 @@
// -----------------------------------------------------------------------
// <copyright file="IStyled.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Styling
{
using Perspex.Controls;
public interface IStyled
{
Styles Styles { get; }
}
}

15
Perspex/Styling/IStyler.cs

@ -0,0 +1,15 @@
// -----------------------------------------------------------------------
// <copyright file="Styler.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Styling
{
using System;
public interface IStyler
{
void ApplyStyles(IStyleable control);
}
}

24
Perspex/Styling/Styler.cs

@ -0,0 +1,24 @@
// -----------------------------------------------------------------------
// <copyright file="Styler.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Styling
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class Styler : IStyler
{
public void ApplyStyles(IStyleable control)
{
IVisual visual = control as IVisual;
IStyled styleContainer = visual.GetVisualAncestorOrSelf<IStyled>();
styleContainer.Styles.Attach(control);
}
}
}

7
Perspex/VisualExtensions.cs

@ -32,6 +32,13 @@ namespace Perspex
return null;
}
public static T GetVisualAncestorOrSelf<T>(this IVisual visual) where T : class
{
Contract.Requires<NullReferenceException>(visual != null);
return (visual as T) ?? visual.GetVisualAncestor<T>();
}
public static IVisual GetVisualAt(this IVisual visual, Point p)
{
Contract.Requires<NullReferenceException>(visual != null);

14
TestApplication/Program.cs

@ -61,13 +61,13 @@ namespace TestApplication
new Setter(TestBorder.BackgroundProperty, new SolidColorBrush(0xff0000ff)),
}
},
//new Style(new Selector().OfType<TestBorder>().Class(":mouseover"))
//{
// Setters = new[]
// {
// new Setter(TestBorder.BackgroundProperty, new SolidColorBrush(0xffff0000)),
// }
//},
new Style(new Selector().OfType<TestBorder>().Class(":mouseover"))
{
Setters = new[]
{
new Setter(TestBorder.BackgroundProperty, new SolidColorBrush(0xffff0000)),
}
},
}
},

Loading…
Cancel
Save