Browse Source

Merge pull request #68 from gumme/WpfDesignerUnitTests

Wpf designer unit tests
pull/69/merge
Siegfried Pammer 12 years ago
parent
commit
973feaad7f
  1. 7
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/AssemblyInfo.cs
  2. 19
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/Designer/ModelTestHelper.cs
  3. 40
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/Designer/ModelTests.cs
  4. 184
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/Designer/NamespaceTests.cs
  5. 60
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/Designer/SetPropertyTests.cs
  6. 1
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/WpfDesign.Tests.csproj

7
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/AssemblyInfo.cs

@ -4,6 +4,7 @@ @@ -4,6 +4,7 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows.Markup;
using NUnit.Framework;
@ -20,4 +21,8 @@ using NUnit.Framework; @@ -20,4 +21,8 @@ using NUnit.Framework;
[assembly: AssemblyCulture("")]
// Run unit tests on STA thread.
[assembly: RequiresSTA]
[assembly: RequiresSTA]
[assembly: XmlnsPrefix("http://sharpdevelop.net/WpfDesign/Tests/Controls", "sdtcontrols")]
[assembly: XmlnsDefinition("http://sharpdevelop.net/WpfDesign/Tests/Controls", "ICSharpCode.WpfDesign.Tests.Controls")]

19
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/Designer/ModelTestHelper.cs

@ -59,14 +59,23 @@ namespace ICSharpCode.WpfDesign.Tests.Designer @@ -59,14 +59,23 @@ namespace ICSharpCode.WpfDesign.Tests.Designer
return canvasChild;
}
protected void AssertCanvasDesignerOutput(string expectedXaml, DesignContext context)
protected void AssertCanvasDesignerOutput(string expectedXaml, DesignContext context, params String[] additionalXmlns)
{
string canvasStartTag =
"<Canvas xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" " +
"xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" " +
"xmlns:t=\"" + DesignerTestsNamespace + "\"";
foreach(string ns in additionalXmlns) {
canvasStartTag += " " + ns;
}
expectedXaml = canvasStartTag + ">\n" + expectedXaml.Trim();
expectedXaml =
"<?xml version=\"1.0\" encoding=\"utf-16\"?>\n" +
("<Canvas xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" " +
"xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" " +
"xmlns:t=\"" + DesignerTestsNamespace + "\">\n" + expectedXaml.Trim())
.Replace("\r", "").Replace("\n", "\n ")
expectedXaml.Replace("\r", "").Replace("\n", "\n ")
+ "\n</Canvas>";
StringWriter stringWriter = new StringWriter();

40
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/Designer/ModelTests.cs

@ -2,17 +2,19 @@ @@ -2,17 +2,19 @@
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Text;
using System.IO;
using System.Xml;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using NUnit.Framework;
using System.Windows.Media;
using System.Xml;
using ICSharpCode.WpfDesign.Designer;
using ICSharpCode.WpfDesign.Designer.Xaml;
using ICSharpCode.WpfDesign.Designer.Services;
using ICSharpCode.WpfDesign.Designer.Xaml;
using NUnit.Framework;
namespace ICSharpCode.WpfDesign.Tests.Designer
{
@ -368,6 +370,34 @@ namespace ICSharpCode.WpfDesign.Tests.Designer @@ -368,6 +370,34 @@ namespace ICSharpCode.WpfDesign.Tests.Designer
{
AddBindingWithStaticResourceWhereResourceOnSameElement(true);
}
[Test]
public void AddBrushAsResource()
{
DesignItem checkBox = CreateCanvasContext("<CheckBox/>");
DesignItem canvas = checkBox.Parent;
DesignItemProperty canvasResources = canvas.Properties.GetProperty("Resources");
DesignItem brush = canvas.Services.Component.RegisterComponentForDesigner(new SolidColorBrush());
brush.Key = "testBrush";
brush.Properties[SolidColorBrush.ColorProperty].SetValue(Colors.Fuchsia);
Assert.IsTrue(canvasResources.IsCollection);
canvasResources.CollectionElements.Add(brush);
checkBox.Properties[CheckBox.ForegroundProperty].SetValue(new StaticResourceExtension());
DesignItemProperty prop = checkBox.Properties[CheckBox.ForegroundProperty];
prop.Value.Properties["ResourceKey"].SetValue("testBrush");
string expectedXaml = "<Canvas.Resources>\n" +
" <SolidColorBrush x:Key=\"testBrush\" Color=\"#FFFF00FF\" />\n" +
"</Canvas.Resources>\n" +
"<CheckBox Foreground=\"{StaticResource ResourceKey=testBrush}\" />";
AssertCanvasDesignerOutput(expectedXaml, checkBox.Context);
AssertLog("");
}
}
public class MyMultiConverter : IMultiValueConverter

184
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/Designer/NamespaceTests.cs

@ -0,0 +1,184 @@ @@ -0,0 +1,184 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Windows;
using System.Windows.Controls;
using NUnit.Framework;
namespace ICSharpCode.WpfDesign.Tests.Designer
{
[TestFixture]
public class NamespaceTests : ModelTestHelper
{
[Test]
public void AddControlFromTestNamespace()
{
DesignItem button = CreateCanvasContext("<Button />");
DesignItem canvas = button.Parent;
DesignItem customButton = canvas.Services.Component.RegisterComponentForDesigner(new CustomButton());
canvas.Properties["Children"].CollectionElements.Add(customButton);
AssertCanvasDesignerOutput("<Button />\n" +
"<t:CustomButton />", canvas.Context);
}
[Test]
public void AddControlWithUndeclaredNamespace()
{
DesignItem button = CreateCanvasContext("<Button />");
DesignItem canvas = button.Parent;
DesignItem customButton = canvas.Services.Component.RegisterComponentForDesigner(new ICSharpCode.WpfDesign.Tests.OtherControls.CustomButton());
canvas.Properties["Children"].CollectionElements.Add(customButton);
AssertCanvasDesignerOutput("<Button />\n" +
"<Controls0:CustomButton />",
canvas.Context,
"xmlns:Controls0=\"clr-namespace:ICSharpCode.WpfDesign.Tests.OtherControls;assembly=ICSharpCode.WpfDesign.Tests\"");
}
[Test]
public void AddControlWithUndeclaredNamespaceThatUsesXmlnsPrefixAttribute()
{
DesignItem button = CreateCanvasContext("<Button />");
DesignItem canvas = button.Parent;
DesignItem customButton = canvas.Services.Component.RegisterComponentForDesigner(new ICSharpCode.WpfDesign.Tests.Controls.CustomButton());
canvas.Properties["Children"].CollectionElements.Add(customButton);
AssertCanvasDesignerOutput("<Button />\n" +
"<sdtcontrols:CustomButton />",
canvas.Context,
"xmlns:sdtcontrols=\"http://sharpdevelop.net/WpfDesign/Tests/Controls\"");
}
[Test]
public void AddMultipleControls()
{
DesignItem button = CreateCanvasContext("<Button />");
DesignItem canvas = button.Parent;
DesignItem customControl = canvas.Services.Component.RegisterComponentForDesigner(new CustomButton());
canvas.Properties["Children"].CollectionElements.Add(customControl);
customControl = canvas.Services.Component.RegisterComponentForDesigner(new ICSharpCode.WpfDesign.Tests.Controls.CustomButton());
canvas.Properties["Children"].CollectionElements.Add(customControl);
customControl = canvas.Services.Component.RegisterComponentForDesigner(new ICSharpCode.WpfDesign.Tests.OtherControls.CustomButton());
canvas.Properties["Children"].CollectionElements.Add(customControl);
customControl = canvas.Services.Component.RegisterComponentForDesigner(new ICSharpCode.WpfDesign.Tests.SpecialControls.CustomButton());
canvas.Properties["Children"].CollectionElements.Add(customControl);
customControl = canvas.Services.Component.RegisterComponentForDesigner(new CustomCheckBox());
canvas.Properties["Children"].CollectionElements.Add(customControl);
customControl = canvas.Services.Component.RegisterComponentForDesigner(new ICSharpCode.WpfDesign.Tests.Controls.CustomCheckBox());
canvas.Properties["Children"].CollectionElements.Add(customControl);
customControl = canvas.Services.Component.RegisterComponentForDesigner(new ICSharpCode.WpfDesign.Tests.OtherControls.CustomCheckBox());
canvas.Properties["Children"].CollectionElements.Add(customControl);
customControl = canvas.Services.Component.RegisterComponentForDesigner(new ICSharpCode.WpfDesign.Tests.SpecialControls.CustomCheckBox());
canvas.Properties["Children"].CollectionElements.Add(customControl);
AssertCanvasDesignerOutput("<Button />\n" +
"<t:CustomButton />\n" +
"<sdtcontrols:CustomButton />\n" +
"<Controls0:CustomButton />\n" +
"<Controls1:CustomButton />\n" +
"<t:CustomCheckBox />\n" +
"<sdtcontrols:CustomCheckBox />\n" +
"<Controls0:CustomCheckBox />\n" +
"<Controls1:CustomCheckBox />",
canvas.Context,
"xmlns:sdtcontrols=\"http://sharpdevelop.net/WpfDesign/Tests/Controls\"",
"xmlns:Controls0=\"clr-namespace:ICSharpCode.WpfDesign.Tests.OtherControls;assembly=ICSharpCode.WpfDesign.Tests\"",
"xmlns:Controls1=\"clr-namespace:ICSharpCode.WpfDesign.Tests.SpecialControls;assembly=ICSharpCode.WpfDesign.Tests\"");
}
}
public class CustomButton : Button
{
public static readonly DependencyProperty TestAttachedProperty = DependencyProperty.RegisterAttached("TestAttached", typeof(double), typeof(CustomButton),
new FrameworkPropertyMetadata(Double.NaN));
public static double GetTestAttached(UIElement element)
{
return (double)element.GetValue(TestAttachedProperty);
}
public static void SetTestAttached(UIElement element, double value)
{
element.SetValue(TestAttachedProperty, value);
}
}
public class CustomCheckBox : CheckBox
{
}
}
namespace ICSharpCode.WpfDesign.Tests.Controls
{
public class CustomButton : Button
{
public static readonly DependencyProperty TestAttachedProperty = DependencyProperty.RegisterAttached("TestAttached", typeof(double), typeof(CustomButton),
new FrameworkPropertyMetadata(Double.NaN));
public static double GetTestAttached(UIElement element)
{
return (double)element.GetValue(TestAttachedProperty);
}
public static void SetTestAttached(UIElement element, double value)
{
element.SetValue(TestAttachedProperty, value);
}
}
public class CustomCheckBox : CheckBox
{
}
}
namespace ICSharpCode.WpfDesign.Tests.OtherControls
{
public class CustomButton : Button
{
public static readonly DependencyProperty TestAttachedProperty = DependencyProperty.RegisterAttached("TestAttached", typeof(double), typeof(CustomButton),
new FrameworkPropertyMetadata(Double.NaN));
public static double GetTestAttached(UIElement element)
{
return (double)element.GetValue(TestAttachedProperty);
}
public static void SetTestAttached(UIElement element, double value)
{
element.SetValue(TestAttachedProperty, value);
}
}
public class CustomCheckBox : CheckBox
{
}
}
namespace ICSharpCode.WpfDesign.Tests.SpecialControls
{
public class CustomButton : Button
{
}
public class CustomCheckBox : CheckBox
{
}
}

60
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/Designer/SetPropertyTests.cs

@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Markup;
@ -21,20 +22,22 @@ namespace ICSharpCode.WpfDesign.Tests.Designer @@ -21,20 +22,22 @@ namespace ICSharpCode.WpfDesign.Tests.Designer
AssertCanvasDesignerOutput("<Button Width=\"100\" Height=\"200\" Content=\"{Binding}\" />", button.Context);
}
[Test, Ignore("Properties are not present in XAML DOM")]
[Test]
public void SetContentToStaticResource()
{
DesignItem button = CreateCanvasContext(@"<Button Width='100' Height='200'/>");
button.Properties.GetProperty("Content").SetValue(new StaticResourceExtension("MyBrush"));
button.Properties.GetProperty("Content").SetValue(new StaticResourceExtension());
button.Properties.GetProperty("Content").Value.Properties["ResourceKey"].SetValue("MyBrush");
// TODO : maybe we should support positional arguments from ctors as well => {StaticResource MyBrush}?
AssertCanvasDesignerOutput("<Button Width=\"100\" Height=\"200\" Content=\"{StaticResource ResourceKey=MyBrush}\" />", button.Context);
}
[Test, Ignore("Properties are not present in XAML DOM")]
[Test]
public void SetContentToXStatic()
{
DesignItem button = CreateCanvasContext("<Button Width='100' Height='200'/>");
button.Properties.GetProperty("Content").SetValue(new StaticExtension("Button.ClickModeProperty"));
button.Properties.GetProperty("Content").SetValue(new StaticExtension());
button.Properties.GetProperty("Content").Value.Properties["Member"].SetValue("Button.ClickModeProperty");
AssertCanvasDesignerOutput("<Button Width=\"100\" Height=\"200\" Content=\"{x:Static Member=Button.ClickModeProperty}\" />", button.Context);
}
@ -45,5 +48,54 @@ namespace ICSharpCode.WpfDesign.Tests.Designer @@ -45,5 +48,54 @@ namespace ICSharpCode.WpfDesign.Tests.Designer
button.Properties.GetProperty("Content").SetValue("Hello World!");
AssertCanvasDesignerOutput("<Button Width=\"100\" Height=\"200\" Content=\"Hello World!\" />", button.Context);
}
[Test]
public void SetAttachedProperties()
{
DesignItem button = CreateCanvasContext("<Button />");
button.Properties.GetAttachedProperty(Grid.ColumnProperty).SetValue(0);
button.Properties.GetAttachedProperty(CustomButton.TestAttachedProperty).SetValue(0);
button.Properties.GetAttachedProperty(ICSharpCode.WpfDesign.Tests.Controls.CustomButton.TestAttachedProperty).SetValue(0);
button.Properties.GetAttachedProperty(ICSharpCode.WpfDesign.Tests.OtherControls.CustomButton.TestAttachedProperty).SetValue(0);
AssertCanvasDesignerOutput("<Button Grid.Column=\"0\" t:CustomButton.TestAttached=\"0\" sdtcontrols:CustomButton.TestAttached=\"0\" Controls0:CustomButton.TestAttached=\"0\" />",
button.Context,
"xmlns:sdtcontrols=\"http://sharpdevelop.net/WpfDesign/Tests/Controls\"",
"xmlns:Controls0=\"clr-namespace:ICSharpCode.WpfDesign.Tests.OtherControls;assembly=ICSharpCode.WpfDesign.Tests\"");
}
[Test]
public void SetInstanceProperty()
{
DesignItem button = CreateCanvasContext("<Button />");
button.Properties.GetProperty("Width").SetValue(10);
AssertCanvasDesignerOutput("<Button Width=\"10\" />", button.Context);
}
[Test]
public void SetInstancePropertyElement()
{
DesignItem button = CreateCanvasContext("<Button />");
DesignItem canvas = button.Parent;
canvas.Properties.GetProperty(Canvas.TagProperty).SetValue(new ExampleClass());
DesignItem customControl = canvas.Services.Component.RegisterComponentForDesigner(new ICSharpCode.WpfDesign.Tests.Controls.CustomButton());
canvas.Properties["Children"].CollectionElements.Add(customControl);
customControl.Properties.GetProperty(ICSharpCode.WpfDesign.Tests.Controls.CustomButton.TagProperty).SetValue(new ExampleClass());
AssertCanvasDesignerOutput("<Canvas.Tag>\n" +
" <t:ExampleClass />\n" +
"</Canvas.Tag>\n" +
"<Button />\n" +
"<sdtcontrols:CustomButton>\n" +
" <sdtcontrols:CustomButton.Tag>\n" +
" <t:ExampleClass />\n" +
" </sdtcontrols:CustomButton.Tag>\n" +
"</sdtcontrols:CustomButton>",
canvas.Context,
"xmlns:sdtcontrols=\"http://sharpdevelop.net/WpfDesign/Tests/Controls\"");
}
}
}

1
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/WpfDesign.Tests.csproj

@ -64,6 +64,7 @@ @@ -64,6 +64,7 @@
<Compile Include="Designer\MockFocusNavigator.cs" />
<Compile Include="Designer\ModelTestHelper.cs" />
<Compile Include="Designer\ModelTests.cs" />
<Compile Include="Designer\NamespaceTests.cs" />
<Compile Include="Designer\OutlineView\HierarchyTests.cs" />
<Compile Include="Designer\OutlineView\InsertTests.cs" />
<Compile Include="Designer\OutlineView\SelectionTests.cs" />

Loading…
Cancel
Save