21 changed files with 477 additions and 212 deletions
@ -0,0 +1,93 @@ |
|||
// -----------------------------------------------------------------------
|
|||
// <copyright file="GridLengthTests.cs" company="Steven Kirk">
|
|||
// Copyright 2015 MIT Licence. See licence.md for more information.
|
|||
// </copyright>
|
|||
// -----------------------------------------------------------------------
|
|||
|
|||
namespace Perspex.Controls.UnitTests |
|||
{ |
|||
using System; |
|||
using System.Linq; |
|||
using Xunit; |
|||
|
|||
public class GridLengthTests |
|||
{ |
|||
[Fact] |
|||
public void Parse_Should_Parse_Auto() |
|||
{ |
|||
var result = GridLength.Parse("Auto"); |
|||
|
|||
Assert.Equal(GridLength.Auto, result); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Parse_Should_Parse_Auto_Lowercase() |
|||
{ |
|||
var result = GridLength.Parse("auto"); |
|||
|
|||
Assert.Equal(GridLength.Auto, result); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Parse_Should_Parse_Star() |
|||
{ |
|||
var result = GridLength.Parse("*"); |
|||
|
|||
Assert.Equal(new GridLength(1, GridUnitType.Star), result); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Parse_Should_Parse_Star_Value() |
|||
{ |
|||
var result = GridLength.Parse("2*"); |
|||
|
|||
Assert.Equal(new GridLength(2, GridUnitType.Star), result); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Parse_Should_Parse_Pixel_Value() |
|||
{ |
|||
var result = GridLength.Parse("2"); |
|||
|
|||
Assert.Equal(new GridLength(2, GridUnitType.Pixel), result); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Parse_Should_Throw_FormatException_For_Invalid_String() |
|||
{ |
|||
Assert.Throws<FormatException>(() => GridLength.Parse("2x")); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ParseLengths_Accepts_Comma_Separators() |
|||
{ |
|||
var result = GridLength.ParseLengths("*,Auto,2*,4").ToList(); |
|||
|
|||
Assert.Equal( |
|||
new[] |
|||
{ |
|||
new GridLength(1, GridUnitType.Star), |
|||
GridLength.Auto, |
|||
new GridLength(2, GridUnitType.Star), |
|||
new GridLength(4, GridUnitType.Pixel), |
|||
}, |
|||
result); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ParseLengths_Accepts_Space_Separators() |
|||
{ |
|||
var result = GridLength.ParseLengths("* Auto 2* 4").ToList(); |
|||
|
|||
Assert.Equal( |
|||
new[] |
|||
{ |
|||
new GridLength(1, GridUnitType.Star), |
|||
GridLength.Auto, |
|||
new GridLength(2, GridUnitType.Star), |
|||
new GridLength(4, GridUnitType.Pixel), |
|||
}, |
|||
result); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,77 @@ |
|||
// -----------------------------------------------------------------------
|
|||
// <copyright file="BrushTests.cs" company="Steven Kirk">
|
|||
// Copyright 2015 MIT Licence. See licence.md for more information.
|
|||
// </copyright>
|
|||
// -----------------------------------------------------------------------
|
|||
|
|||
namespace Perspex.SceneGraph.UnitTests.Media |
|||
{ |
|||
using System; |
|||
using Perspex.Media; |
|||
using Xunit; |
|||
|
|||
public class BrushTests |
|||
{ |
|||
[Fact] |
|||
public void Parse_Parses_RGB_Hash_Brush() |
|||
{ |
|||
var result = (SolidColorBrush)Brush.Parse("#ff8844"); |
|||
|
|||
Assert.Equal(0xff, result.Color.R); |
|||
Assert.Equal(0x88, result.Color.G); |
|||
Assert.Equal(0x44, result.Color.B); |
|||
Assert.Equal(0xff, result.Color.A); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Parse_Parses_ARGB_Hash_Brush() |
|||
{ |
|||
var result = (SolidColorBrush)Brush.Parse("#40ff8844"); |
|||
|
|||
Assert.Equal(0xff, result.Color.R); |
|||
Assert.Equal(0x88, result.Color.G); |
|||
Assert.Equal(0x44, result.Color.B); |
|||
Assert.Equal(0x40, result.Color.A); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Parse_Parses_Named_Brush_Lowercase() |
|||
{ |
|||
var result = (SolidColorBrush)Brush.Parse("red"); |
|||
|
|||
Assert.Equal(0xff, result.Color.R); |
|||
Assert.Equal(0x00, result.Color.G); |
|||
Assert.Equal(0x00, result.Color.B); |
|||
Assert.Equal(0xff, result.Color.A); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Parse_Parses_Named_Brush_Uppercase() |
|||
{ |
|||
var result = (SolidColorBrush)Brush.Parse("RED"); |
|||
|
|||
Assert.Equal(0xff, result.Color.R); |
|||
Assert.Equal(0x00, result.Color.G); |
|||
Assert.Equal(0x00, result.Color.B); |
|||
Assert.Equal(0xff, result.Color.A); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Parse_Hex_Value_Doesnt_Accept_Too_Few_Chars() |
|||
{ |
|||
Assert.Throws<FormatException>(() => Brush.Parse("#ff")); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Parse_Hex_Value_Doesnt_Accept_Too_Many_Chars() |
|||
{ |
|||
Assert.Throws<FormatException>(() => Brush.Parse("#ff5555555")); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Parse_Hex_Value_Doesnt_Accept_Invalid_Number() |
|||
{ |
|||
Assert.Throws<FormatException>(() => Brush.Parse("#ff808g80")); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,77 @@ |
|||
// -----------------------------------------------------------------------
|
|||
// <copyright file="ColorTests.cs" company="Steven Kirk">
|
|||
// Copyright 2015 MIT Licence. See licence.md for more information.
|
|||
// </copyright>
|
|||
// -----------------------------------------------------------------------
|
|||
|
|||
namespace Perspex.SceneGraph.UnitTests.Media |
|||
{ |
|||
using System; |
|||
using Perspex.Media; |
|||
using Xunit; |
|||
|
|||
public class ColorTests |
|||
{ |
|||
[Fact] |
|||
public void Parse_Parses_RGB_Hash_Color() |
|||
{ |
|||
var result = Color.Parse("#ff8844"); |
|||
|
|||
Assert.Equal(0xff, result.R); |
|||
Assert.Equal(0x88, result.G); |
|||
Assert.Equal(0x44, result.B); |
|||
Assert.Equal(0xff, result.A); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Parse_Parses_ARGB_Hash_Color() |
|||
{ |
|||
var result = Color.Parse("#40ff8844"); |
|||
|
|||
Assert.Equal(0xff, result.R); |
|||
Assert.Equal(0x88, result.G); |
|||
Assert.Equal(0x44, result.B); |
|||
Assert.Equal(0x40, result.A); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Parse_Parses_Named_Color_Lowercase() |
|||
{ |
|||
var result = Color.Parse("red"); |
|||
|
|||
Assert.Equal(0xff, result.R); |
|||
Assert.Equal(0x00, result.G); |
|||
Assert.Equal(0x00, result.B); |
|||
Assert.Equal(0xff, result.A); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Parse_Parses_Named_Color_Uppercase() |
|||
{ |
|||
var result = Color.Parse("RED"); |
|||
|
|||
Assert.Equal(0xff, result.R); |
|||
Assert.Equal(0x00, result.G); |
|||
Assert.Equal(0x00, result.B); |
|||
Assert.Equal(0xff, result.A); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Parse_Hex_Value_Doesnt_Accept_Too_Few_Chars() |
|||
{ |
|||
Assert.Throws<FormatException>(() => Color.Parse("#ff")); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Parse_Hex_Value_Doesnt_Accept_Too_Many_Chars() |
|||
{ |
|||
Assert.Throws<FormatException>(() => Color.Parse("#ff5555555")); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Parse_Hex_Value_Doesnt_Accept_Invalid_Number() |
|||
{ |
|||
Assert.Throws<FormatException>(() => Color.Parse("#ff808g80")); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
// -----------------------------------------------------------------------
|
|||
// <copyright file="ColumnDefinitionsTypeConverter.cs" company="Steven Kirk">
|
|||
// Copyright 2015 MIT Licence. See licence.md for more information.
|
|||
// </copyright>
|
|||
// -----------------------------------------------------------------------
|
|||
|
|||
namespace Perspex.Markup.Xaml.Converters |
|||
{ |
|||
using System; |
|||
using System.Globalization; |
|||
using Controls; |
|||
using OmniXaml.TypeConversion; |
|||
|
|||
public class ColumnDefinitionsTypeConverter : ITypeConverter |
|||
{ |
|||
public bool CanConvertFrom(IXamlTypeConverterContext context, Type sourceType) |
|||
{ |
|||
return sourceType == typeof(string); |
|||
} |
|||
|
|||
public bool CanConvertTo(IXamlTypeConverterContext context, Type destinationType) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
public object ConvertFrom(IXamlTypeConverterContext context, CultureInfo culture, object value) |
|||
{ |
|||
return new ColumnDefinitions((string)value); |
|||
} |
|||
|
|||
public object ConvertTo(IXamlTypeConverterContext context, CultureInfo culture, object value, Type destinationType) |
|||
{ |
|||
throw new NotImplementedException(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
// -----------------------------------------------------------------------
|
|||
// <copyright file="RowDefinitionsTypeConverter.cs" company="Steven Kirk">
|
|||
// Copyright 2015 MIT Licence. See licence.md for more information.
|
|||
// </copyright>
|
|||
// -----------------------------------------------------------------------
|
|||
|
|||
namespace Perspex.Markup.Xaml.Converters |
|||
{ |
|||
using System; |
|||
using System.Globalization; |
|||
using Controls; |
|||
using OmniXaml.TypeConversion; |
|||
|
|||
public class RowDefinitionsTypeConverter : ITypeConverter |
|||
{ |
|||
public bool CanConvertFrom(IXamlTypeConverterContext context, Type sourceType) |
|||
{ |
|||
return sourceType == typeof(string); |
|||
} |
|||
|
|||
public bool CanConvertTo(IXamlTypeConverterContext context, Type destinationType) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
public object ConvertFrom(IXamlTypeConverterContext context, CultureInfo culture, object value) |
|||
{ |
|||
return new RowDefinitions((string)value); |
|||
} |
|||
|
|||
public object ConvertTo(IXamlTypeConverterContext context, CultureInfo culture, object value, Type destinationType) |
|||
{ |
|||
throw new NotImplementedException(); |
|||
} |
|||
} |
|||
} |
|||
@ -1,52 +0,0 @@ |
|||
// -----------------------------------------------------------------------
|
|||
// <copyright file="GridLengthsParser.cs" company="Steven Kirk">
|
|||
// Copyright 2014 MIT Licence. See licence.md for more information.
|
|||
// </copyright>
|
|||
// -----------------------------------------------------------------------
|
|||
|
|||
namespace Perspex.Controls.Parsers |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
|
|||
/// <summary>
|
|||
/// Parses a string of <see cref="GridLength"/>s for <see cref="ColumnDefinitions"/> and
|
|||
/// <see cref="RowDefinitions"/>.
|
|||
/// </summary>
|
|||
public static class GridLengthsParser |
|||
{ |
|||
/// <summary>
|
|||
/// Parses a string of <see cref="GridLength"/>s.
|
|||
/// </summary>
|
|||
/// <param name="s">The string.</param>
|
|||
/// <returns>A collection of <see cref="GridLength"/>s.</returns>
|
|||
public static IEnumerable<GridLength> Parse(string s) |
|||
{ |
|||
var parts = s.Split(',').Select(x => x.ToUpperInvariant().Trim()); |
|||
|
|||
foreach (var part in parts) |
|||
{ |
|||
if (part == "AUTO") |
|||
{ |
|||
yield return GridLength.Auto; |
|||
} |
|||
else if (part.EndsWith("*")) |
|||
{ |
|||
var valueString = part.Substring(0, part.Length - 1).Trim(); |
|||
var value = valueString.Length > 0 ? double.Parse(valueString) : 1; |
|||
yield return new GridLength(value, GridUnitType.Star); |
|||
} |
|||
else if (part.EndsWith("PX")) |
|||
{ |
|||
var value = double.Parse(part.Substring(0, part.Length - 2)); |
|||
yield return new GridLength(value, GridUnitType.Pixel); |
|||
} |
|||
else |
|||
{ |
|||
throw new FormatException("Invalid grid length: " + part); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1,23 +1,61 @@ |
|||
// -----------------------------------------------------------------------
|
|||
// <copyright file="Brush.cs" company="Steven Kirk">
|
|||
// Copyright 2014 MIT Licence. See licence.md for more information.
|
|||
// Copyright 2015 MIT Licence. See licence.md for more information.
|
|||
// </copyright>
|
|||
// -----------------------------------------------------------------------
|
|||
|
|||
namespace Perspex.Media |
|||
{ |
|||
using System; |
|||
using System.Linq; |
|||
using System.Reflection; |
|||
|
|||
/// <summary>
|
|||
/// Describes how an area is painted.
|
|||
/// </summary>
|
|||
public abstract class Brush : PerspexObject |
|||
{ |
|||
/// <summary>
|
|||
/// Defines the <see cref="Opacity"/> property.
|
|||
/// </summary>
|
|||
public static readonly PerspexProperty<double> OpacityProperty = |
|||
PerspexProperty.Register<Brush, double>(nameof(Opacity), 1.0); |
|||
PerspexProperty.Register<Brush, double>(nameof(Opacity), 1.0); |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the opacity of the brush.
|
|||
/// </summary>
|
|||
public double Opacity |
|||
{ |
|||
get { return this.GetValue(OpacityProperty); } |
|||
set { this.SetValue(OpacityProperty, value); } |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Parses a brush string.
|
|||
/// </summary>
|
|||
/// <param name="s">The brush string.</param>
|
|||
/// <returns>The <see cref="Color"/>.</returns>
|
|||
public static Brush Parse(string s) |
|||
{ |
|||
if (s[0] == '#') |
|||
{ |
|||
return new SolidColorBrush(Color.Parse(s)); |
|||
} |
|||
else |
|||
{ |
|||
var upper = s.ToUpperInvariant(); |
|||
var member = typeof(Brushes).GetTypeInfo().DeclaredProperties |
|||
.FirstOrDefault(x => x.Name.ToUpperInvariant() == upper); |
|||
|
|||
if (member != null) |
|||
{ |
|||
return (Brush)member.GetValue(null); |
|||
} |
|||
else |
|||
{ |
|||
throw new FormatException($"Invalid brush string: '{s}'."); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
@ -1,54 +0,0 @@ |
|||
// -----------------------------------------------------------------------
|
|||
// <copyright file="GridLengthsParserTests.cs" company="Steven Kirk">
|
|||
// Copyright 2015 MIT Licence. See licence.md for more information.
|
|||
// </copyright>
|
|||
// -----------------------------------------------------------------------
|
|||
|
|||
namespace Perspex.Controls.UnitTests.Parsers |
|||
{ |
|||
using System; |
|||
using System.Linq; |
|||
using Perspex.Controls.Parsers; |
|||
using Xunit; |
|||
|
|||
public class GridLengthsParserTests |
|||
{ |
|||
[Fact] |
|||
public void Parser_Should_Correctly_Parse_Grid_Lengths() |
|||
{ |
|||
var s = "*,Auto,2*,4px"; |
|||
var result = GridLengthsParser.Parse(s); |
|||
|
|||
Assert.Equal( |
|||
new[] |
|||
{ |
|||
new GridLength(1, GridUnitType.Star), |
|||
GridLength.Auto, |
|||
new GridLength(2, GridUnitType.Star), |
|||
new GridLength(4, GridUnitType.Pixel), |
|||
}, |
|||
result); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Parser_Should_Throw_For_Invalid_Star_Value() |
|||
{ |
|||
var s = "*,Auto,x*,4px"; |
|||
Assert.Throws<FormatException>(() => GridLengthsParser.Parse(s).ToList()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Parser_Should_Throw_For_Invalid_Unit_Value() |
|||
{ |
|||
var s = "*,Auto,4ab,4px"; |
|||
Assert.Throws<FormatException>(() => GridLengthsParser.Parse(s).ToList()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Parser_Should_Throw_For_Empty_Entry() |
|||
{ |
|||
var s = "*,Auto,,4px"; |
|||
Assert.Throws<FormatException>(() => GridLengthsParser.Parse(s).ToList()); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue