Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit 489d9ab

Browse files
author
gumme
committed
Merge remote-tracking branch 'origin/WpfDesignerUnitTests'
2 parents efdc7df + f1da926 commit 489d9ab

6 files changed

Lines changed: 296 additions & 15 deletions

File tree

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Reflection;
55
using System.Runtime.CompilerServices;
66
using System.Runtime.InteropServices;
7+
using System.Windows.Markup;
78

89
using NUnit.Framework;
910

@@ -20,4 +21,8 @@
2021
[assembly: AssemblyCulture("")]
2122

2223
// Run unit tests on STA thread.
23-
[assembly: RequiresSTA]
24+
[assembly: RequiresSTA]
25+
26+
[assembly: XmlnsPrefix("http://sharpdevelop.net/WpfDesign/Tests/Controls", "sdtcontrols")]
27+
28+
[assembly: XmlnsDefinition("http://sharpdevelop.net/WpfDesign/Tests/Controls", "ICSharpCode.WpfDesign.Tests.Controls")]

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,23 @@ protected DesignItem CreateCanvasContext(string xaml)
5959
return canvasChild;
6060
}
6161

62-
protected void AssertCanvasDesignerOutput(string expectedXaml, DesignContext context)
62+
protected void AssertCanvasDesignerOutput(string expectedXaml, DesignContext context, params String[] additionalXmlns)
6363
{
64+
string canvasStartTag =
65+
"<Canvas xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" " +
66+
"xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" " +
67+
"xmlns:t=\"" + DesignerTestsNamespace + "\"";
68+
69+
70+
foreach(string ns in additionalXmlns) {
71+
canvasStartTag += " " + ns;
72+
}
73+
74+
expectedXaml = canvasStartTag + ">\n" + expectedXaml.Trim();
75+
6476
expectedXaml =
6577
"<?xml version=\"1.0\" encoding=\"utf-16\"?>\n" +
66-
("<Canvas xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" " +
67-
"xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" " +
68-
"xmlns:t=\"" + DesignerTestsNamespace + "\">\n" + expectedXaml.Trim())
69-
.Replace("\r", "").Replace("\n", "\n ")
78+
expectedXaml.Replace("\r", "").Replace("\n", "\n ")
7079
+ "\n</Canvas>";
7180

7281
StringWriter stringWriter = new StringWriter();

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

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
33

44
using System;
5-
using System.Text;
6-
using System.IO;
7-
using System.Xml;
85
using System.Diagnostics;
6+
using System.IO;
7+
using System.Text;
98
using System.Windows;
109
using System.Windows.Controls;
1110
using System.Windows.Data;
12-
using NUnit.Framework;
11+
using System.Windows.Media;
12+
using System.Xml;
13+
1314
using ICSharpCode.WpfDesign.Designer;
14-
using ICSharpCode.WpfDesign.Designer.Xaml;
1515
using ICSharpCode.WpfDesign.Designer.Services;
16+
using ICSharpCode.WpfDesign.Designer.Xaml;
17+
using NUnit.Framework;
1618

1719
namespace ICSharpCode.WpfDesign.Tests.Designer
1820
{
@@ -368,6 +370,34 @@ public void AddBindingWithStaticResourceWhereResourceOnSameElementAlt()
368370
{
369371
AddBindingWithStaticResourceWhereResourceOnSameElement(true);
370372
}
373+
374+
[Test]
375+
public void AddBrushAsResource()
376+
{
377+
DesignItem checkBox = CreateCanvasContext("<CheckBox/>");
378+
DesignItem canvas = checkBox.Parent;
379+
380+
DesignItemProperty canvasResources = canvas.Properties.GetProperty("Resources");
381+
382+
DesignItem brush = canvas.Services.Component.RegisterComponentForDesigner(new SolidColorBrush());
383+
brush.Key = "testBrush";
384+
brush.Properties[SolidColorBrush.ColorProperty].SetValue(Colors.Fuchsia);
385+
386+
Assert.IsTrue(canvasResources.IsCollection);
387+
canvasResources.CollectionElements.Add(brush);
388+
389+
checkBox.Properties[CheckBox.ForegroundProperty].SetValue(new StaticResourceExtension());
390+
DesignItemProperty prop = checkBox.Properties[CheckBox.ForegroundProperty];
391+
prop.Value.Properties["ResourceKey"].SetValue("testBrush");
392+
393+
string expectedXaml = "<Canvas.Resources>\n" +
394+
" <SolidColorBrush x:Key=\"testBrush\" Color=\"#FFFF00FF\" />\n" +
395+
"</Canvas.Resources>\n" +
396+
"<CheckBox Foreground=\"{StaticResource ResourceKey=testBrush}\" />";
397+
398+
AssertCanvasDesignerOutput(expectedXaml, checkBox.Context);
399+
AssertLog("");
400+
}
371401
}
372402

373403
public class MyMultiConverter : IMultiValueConverter
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2+
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
3+
using System;
4+
using System.Windows;
5+
using System.Windows.Controls;
6+
7+
using NUnit.Framework;
8+
9+
namespace ICSharpCode.WpfDesign.Tests.Designer
10+
{
11+
[TestFixture]
12+
public class NamespaceTests : ModelTestHelper
13+
{
14+
15+
[Test]
16+
public void AddControlFromTestNamespace()
17+
{
18+
DesignItem button = CreateCanvasContext("<Button />");
19+
20+
DesignItem canvas = button.Parent;
21+
22+
DesignItem customButton = canvas.Services.Component.RegisterComponentForDesigner(new CustomButton());
23+
canvas.Properties["Children"].CollectionElements.Add(customButton);
24+
25+
AssertCanvasDesignerOutput("<Button />\n" +
26+
"<t:CustomButton />", canvas.Context);
27+
}
28+
29+
[Test]
30+
public void AddControlWithUndeclaredNamespace()
31+
{
32+
DesignItem button = CreateCanvasContext("<Button />");
33+
34+
DesignItem canvas = button.Parent;
35+
36+
DesignItem customButton = canvas.Services.Component.RegisterComponentForDesigner(new ICSharpCode.WpfDesign.Tests.OtherControls.CustomButton());
37+
canvas.Properties["Children"].CollectionElements.Add(customButton);
38+
39+
AssertCanvasDesignerOutput("<Button />\n" +
40+
"<Controls0:CustomButton />",
41+
canvas.Context,
42+
"xmlns:Controls0=\"clr-namespace:ICSharpCode.WpfDesign.Tests.OtherControls;assembly=ICSharpCode.WpfDesign.Tests\"");
43+
}
44+
45+
[Test]
46+
public void AddControlWithUndeclaredNamespaceThatUsesXmlnsPrefixAttribute()
47+
{
48+
DesignItem button = CreateCanvasContext("<Button />");
49+
50+
DesignItem canvas = button.Parent;
51+
52+
DesignItem customButton = canvas.Services.Component.RegisterComponentForDesigner(new ICSharpCode.WpfDesign.Tests.Controls.CustomButton());
53+
canvas.Properties["Children"].CollectionElements.Add(customButton);
54+
55+
AssertCanvasDesignerOutput("<Button />\n" +
56+
"<sdtcontrols:CustomButton />",
57+
canvas.Context,
58+
"xmlns:sdtcontrols=\"http://sharpdevelop.net/WpfDesign/Tests/Controls\"");
59+
}
60+
61+
[Test]
62+
public void AddMultipleControls()
63+
{
64+
DesignItem button = CreateCanvasContext("<Button />");
65+
66+
DesignItem canvas = button.Parent;
67+
68+
DesignItem customControl = canvas.Services.Component.RegisterComponentForDesigner(new CustomButton());
69+
canvas.Properties["Children"].CollectionElements.Add(customControl);
70+
71+
customControl = canvas.Services.Component.RegisterComponentForDesigner(new ICSharpCode.WpfDesign.Tests.Controls.CustomButton());
72+
canvas.Properties["Children"].CollectionElements.Add(customControl);
73+
74+
customControl = canvas.Services.Component.RegisterComponentForDesigner(new ICSharpCode.WpfDesign.Tests.OtherControls.CustomButton());
75+
canvas.Properties["Children"].CollectionElements.Add(customControl);
76+
77+
customControl = canvas.Services.Component.RegisterComponentForDesigner(new ICSharpCode.WpfDesign.Tests.SpecialControls.CustomButton());
78+
canvas.Properties["Children"].CollectionElements.Add(customControl);
79+
80+
customControl = canvas.Services.Component.RegisterComponentForDesigner(new CustomCheckBox());
81+
canvas.Properties["Children"].CollectionElements.Add(customControl);
82+
83+
customControl = canvas.Services.Component.RegisterComponentForDesigner(new ICSharpCode.WpfDesign.Tests.Controls.CustomCheckBox());
84+
canvas.Properties["Children"].CollectionElements.Add(customControl);
85+
86+
customControl = canvas.Services.Component.RegisterComponentForDesigner(new ICSharpCode.WpfDesign.Tests.OtherControls.CustomCheckBox());
87+
canvas.Properties["Children"].CollectionElements.Add(customControl);
88+
89+
customControl = canvas.Services.Component.RegisterComponentForDesigner(new ICSharpCode.WpfDesign.Tests.SpecialControls.CustomCheckBox());
90+
canvas.Properties["Children"].CollectionElements.Add(customControl);
91+
92+
AssertCanvasDesignerOutput("<Button />\n" +
93+
"<t:CustomButton />\n" +
94+
"<sdtcontrols:CustomButton />\n" +
95+
"<Controls0:CustomButton />\n" +
96+
"<Controls1:CustomButton />\n" +
97+
"<t:CustomCheckBox />\n" +
98+
"<sdtcontrols:CustomCheckBox />\n" +
99+
"<Controls0:CustomCheckBox />\n" +
100+
"<Controls1:CustomCheckBox />",
101+
canvas.Context,
102+
"xmlns:sdtcontrols=\"http://sharpdevelop.net/WpfDesign/Tests/Controls\"",
103+
"xmlns:Controls0=\"clr-namespace:ICSharpCode.WpfDesign.Tests.OtherControls;assembly=ICSharpCode.WpfDesign.Tests\"",
104+
"xmlns:Controls1=\"clr-namespace:ICSharpCode.WpfDesign.Tests.SpecialControls;assembly=ICSharpCode.WpfDesign.Tests\"");
105+
}
106+
}
107+
108+
public class CustomButton : Button
109+
{
110+
public static readonly DependencyProperty TestAttachedProperty = DependencyProperty.RegisterAttached("TestAttached", typeof(double), typeof(CustomButton),
111+
new FrameworkPropertyMetadata(Double.NaN));
112+
113+
public static double GetTestAttached(UIElement element)
114+
{
115+
return (double)element.GetValue(TestAttachedProperty);
116+
}
117+
118+
public static void SetTestAttached(UIElement element, double value)
119+
{
120+
element.SetValue(TestAttachedProperty, value);
121+
}
122+
}
123+
124+
public class CustomCheckBox : CheckBox
125+
{
126+
}
127+
}
128+
129+
namespace ICSharpCode.WpfDesign.Tests.Controls
130+
{
131+
public class CustomButton : Button
132+
{
133+
public static readonly DependencyProperty TestAttachedProperty = DependencyProperty.RegisterAttached("TestAttached", typeof(double), typeof(CustomButton),
134+
new FrameworkPropertyMetadata(Double.NaN));
135+
136+
public static double GetTestAttached(UIElement element)
137+
{
138+
return (double)element.GetValue(TestAttachedProperty);
139+
}
140+
141+
public static void SetTestAttached(UIElement element, double value)
142+
{
143+
element.SetValue(TestAttachedProperty, value);
144+
}
145+
}
146+
147+
public class CustomCheckBox : CheckBox
148+
{
149+
}
150+
}
151+
152+
namespace ICSharpCode.WpfDesign.Tests.OtherControls
153+
{
154+
public class CustomButton : Button
155+
{
156+
public static readonly DependencyProperty TestAttachedProperty = DependencyProperty.RegisterAttached("TestAttached", typeof(double), typeof(CustomButton),
157+
new FrameworkPropertyMetadata(Double.NaN));
158+
159+
public static double GetTestAttached(UIElement element)
160+
{
161+
return (double)element.GetValue(TestAttachedProperty);
162+
}
163+
164+
public static void SetTestAttached(UIElement element, double value)
165+
{
166+
element.SetValue(TestAttachedProperty, value);
167+
}
168+
}
169+
170+
public class CustomCheckBox : CheckBox
171+
{
172+
}
173+
}
174+
175+
namespace ICSharpCode.WpfDesign.Tests.SpecialControls
176+
{
177+
public class CustomButton : Button
178+
{
179+
}
180+
181+
public class CustomCheckBox : CheckBox
182+
{
183+
}
184+
}

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

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Windows;
6+
using System.Windows.Controls;
67
using System.Windows.Data;
78
using System.Windows.Markup;
89

@@ -21,20 +22,22 @@ public void SetContentToBinding()
2122
AssertCanvasDesignerOutput("<Button Width=\"100\" Height=\"200\" Content=\"{Binding}\" />", button.Context);
2223
}
2324

24-
[Test, Ignore("Properties are not present in XAML DOM")]
25+
[Test]
2526
public void SetContentToStaticResource()
2627
{
2728
DesignItem button = CreateCanvasContext(@"<Button Width='100' Height='200'/>");
28-
button.Properties.GetProperty("Content").SetValue(new StaticResourceExtension("MyBrush"));
29+
button.Properties.GetProperty("Content").SetValue(new StaticResourceExtension());
30+
button.Properties.GetProperty("Content").Value.Properties["ResourceKey"].SetValue("MyBrush");
2931
// TODO : maybe we should support positional arguments from ctors as well => {StaticResource MyBrush}?
3032
AssertCanvasDesignerOutput("<Button Width=\"100\" Height=\"200\" Content=\"{StaticResource ResourceKey=MyBrush}\" />", button.Context);
3133
}
3234

33-
[Test, Ignore("Properties are not present in XAML DOM")]
35+
[Test]
3436
public void SetContentToXStatic()
3537
{
3638
DesignItem button = CreateCanvasContext("<Button Width='100' Height='200'/>");
37-
button.Properties.GetProperty("Content").SetValue(new StaticExtension("Button.ClickModeProperty"));
39+
button.Properties.GetProperty("Content").SetValue(new StaticExtension());
40+
button.Properties.GetProperty("Content").Value.Properties["Member"].SetValue("Button.ClickModeProperty");
3841
AssertCanvasDesignerOutput("<Button Width=\"100\" Height=\"200\" Content=\"{x:Static Member=Button.ClickModeProperty}\" />", button.Context);
3942
}
4043

@@ -45,5 +48,54 @@ public void SetContentToString()
4548
button.Properties.GetProperty("Content").SetValue("Hello World!");
4649
AssertCanvasDesignerOutput("<Button Width=\"100\" Height=\"200\" Content=\"Hello World!\" />", button.Context);
4750
}
51+
52+
[Test]
53+
public void SetAttachedProperties()
54+
{
55+
DesignItem button = CreateCanvasContext("<Button />");
56+
57+
button.Properties.GetAttachedProperty(Grid.ColumnProperty).SetValue(0);
58+
button.Properties.GetAttachedProperty(CustomButton.TestAttachedProperty).SetValue(0);
59+
button.Properties.GetAttachedProperty(ICSharpCode.WpfDesign.Tests.Controls.CustomButton.TestAttachedProperty).SetValue(0);
60+
button.Properties.GetAttachedProperty(ICSharpCode.WpfDesign.Tests.OtherControls.CustomButton.TestAttachedProperty).SetValue(0);
61+
62+
AssertCanvasDesignerOutput("<Button Grid.Column=\"0\" t:CustomButton.TestAttached=\"0\" sdtcontrols:CustomButton.TestAttached=\"0\" Controls0:CustomButton.TestAttached=\"0\" />",
63+
button.Context,
64+
"xmlns:sdtcontrols=\"http://sharpdevelop.net/WpfDesign/Tests/Controls\"",
65+
"xmlns:Controls0=\"clr-namespace:ICSharpCode.WpfDesign.Tests.OtherControls;assembly=ICSharpCode.WpfDesign.Tests\"");
66+
}
67+
68+
[Test]
69+
public void SetInstanceProperty()
70+
{
71+
DesignItem button = CreateCanvasContext("<Button />");
72+
button.Properties.GetProperty("Width").SetValue(10);
73+
AssertCanvasDesignerOutput("<Button Width=\"10\" />", button.Context);
74+
}
75+
76+
[Test]
77+
public void SetInstancePropertyElement()
78+
{
79+
DesignItem button = CreateCanvasContext("<Button />");
80+
DesignItem canvas = button.Parent;
81+
82+
canvas.Properties.GetProperty(Canvas.TagProperty).SetValue(new ExampleClass());
83+
84+
DesignItem customControl = canvas.Services.Component.RegisterComponentForDesigner(new ICSharpCode.WpfDesign.Tests.Controls.CustomButton());
85+
canvas.Properties["Children"].CollectionElements.Add(customControl);
86+
customControl.Properties.GetProperty(ICSharpCode.WpfDesign.Tests.Controls.CustomButton.TagProperty).SetValue(new ExampleClass());
87+
88+
AssertCanvasDesignerOutput("<Canvas.Tag>\n" +
89+
" <t:ExampleClass />\n" +
90+
"</Canvas.Tag>\n" +
91+
"<Button />\n" +
92+
"<sdtcontrols:CustomButton>\n" +
93+
" <sdtcontrols:CustomButton.Tag>\n" +
94+
" <t:ExampleClass />\n" +
95+
" </sdtcontrols:CustomButton.Tag>\n" +
96+
"</sdtcontrols:CustomButton>",
97+
canvas.Context,
98+
"xmlns:sdtcontrols=\"http://sharpdevelop.net/WpfDesign/Tests/Controls\"");
99+
}
48100
}
49101
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
<Compile Include="Designer\MockFocusNavigator.cs" />
6565
<Compile Include="Designer\ModelTestHelper.cs" />
6666
<Compile Include="Designer\ModelTests.cs" />
67+
<Compile Include="Designer\NamespaceTests.cs" />
6768
<Compile Include="Designer\OutlineView\HierarchyTests.cs" />
6869
<Compile Include="Designer\OutlineView\InsertTests.cs" />
6970
<Compile Include="Designer\OutlineView\SelectionTests.cs" />

0 commit comments

Comments
 (0)