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

Commit 5ccc93f

Browse files
author
gumme
committed
Added tests for undo/redo operations on implicit and explicit dictionaries.
Added tests for clearing explicit lists and dictionaries. Fixed so an IDictionary item is removed using the item key.
1 parent 300417b commit 5ccc93f

3 files changed

Lines changed: 196 additions & 5 deletions

File tree

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

Lines changed: 179 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,15 @@ void UndoRedoListInternal(bool useExplicitList)
167167
UndoService s = button.Context.Services.GetService<UndoService>();
168168
IComponentService component = button.Context.Services.Component;
169169
string expectedXamlWithList;
170+
DesignItemProperty otherListProp;
170171

171172
Assert.IsFalse(s.CanUndo);
172173
Assert.IsFalse(s.CanRedo);
173174

174175
using (ChangeGroup g = button.OpenGroup("UndoRedoListInternal test")) {
175176
DesignItem containerItem = component.RegisterComponentForDesigner(new ICSharpCode.WpfDesign.Tests.XamlDom.ExampleClassContainer());
176177

177-
var otherListProp = containerItem.Properties["OtherList"];
178+
otherListProp = containerItem.Properties["OtherList"];
178179

179180
if(useExplicitList) {
180181
otherListProp.SetValue(new ICSharpCode.WpfDesign.Tests.XamlDom.ExampleClassList());
@@ -190,8 +191,7 @@ void UndoRedoListInternal(bool useExplicitList)
190191
</Controls0:ExampleClassContainer>
191192
</Button.Tag>
192193
</Button>";
193-
}
194-
else {
194+
} else {
195195
expectedXamlWithList = @"<Button>
196196
<Button.Tag>
197197
<Controls0:ExampleClassContainer>
@@ -215,6 +215,9 @@ void UndoRedoListInternal(bool useExplicitList)
215215
Assert.IsFalse(s.CanRedo);
216216
AssertCanvasDesignerOutput(expectedXamlWithList, button.Context, "xmlns:Controls0=\"" + ICSharpCode.WpfDesign.Tests.XamlDom.XamlTypeFinderTests.XamlDomTestsNamespace + "\"");
217217

218+
otherListProp = button.Properties["Tag"].Value.Properties["OtherList"];
219+
Assert.IsTrue(((ICSharpCode.WpfDesign.Tests.XamlDom.ExampleClassList)otherListProp.ValueOnInstance).Count == otherListProp.CollectionElements.Count);
220+
218221
s.Undo();
219222
Assert.IsFalse(s.CanUndo);
220223
Assert.IsTrue(s.CanRedo);
@@ -225,8 +228,97 @@ void UndoRedoListInternal(bool useExplicitList)
225228
Assert.IsFalse(s.CanRedo);
226229
AssertCanvasDesignerOutput(expectedXamlWithList, button.Context, "xmlns:Controls0=\"" + ICSharpCode.WpfDesign.Tests.XamlDom.XamlTypeFinderTests.XamlDomTestsNamespace + "\"");
227230

231+
otherListProp = button.Properties["Tag"].Value.Properties["OtherList"];
232+
Assert.IsTrue(((ICSharpCode.WpfDesign.Tests.XamlDom.ExampleClassList)otherListProp.ValueOnInstance).Count == otherListProp.CollectionElements.Count);
233+
234+
AssertLog("");
235+
}
236+
237+
[Test]
238+
public void UndoRedoImplicitDictionary()
239+
{
240+
UndoRedoDictionaryInternal(false);
241+
}
242+
243+
[Test]
244+
public void UndoRedoExplicitDictionary()
245+
{
246+
UndoRedoDictionaryInternal(true);
247+
}
248+
249+
void UndoRedoDictionaryInternal(bool useExplicitDictionary)
250+
{
251+
DesignItem button = CreateCanvasContext("<Button/>");
252+
UndoService s = button.Context.Services.GetService<UndoService>();
253+
IComponentService component = button.Context.Services.Component;
254+
string expectedXamlWithDictionary;
255+
DesignItemProperty dictionaryProp;
256+
257+
Assert.IsFalse(s.CanUndo);
258+
Assert.IsFalse(s.CanRedo);
259+
260+
using (ChangeGroup g = button.OpenGroup("UndoRedoDictionaryInternal test")) {
261+
DesignItem containerItem = component.RegisterComponentForDesigner(new ICSharpCode.WpfDesign.Tests.XamlDom.ExampleClassContainer());
262+
263+
dictionaryProp = containerItem.Properties["Dictionary"];
264+
265+
if(useExplicitDictionary) {
266+
dictionaryProp.SetValue(new ICSharpCode.WpfDesign.Tests.XamlDom.ExampleClassDictionary());
267+
268+
expectedXamlWithDictionary = @"<Button>
269+
<Button.Tag>
270+
<Controls0:ExampleClassContainer>
271+
<Controls0:ExampleClassContainer.Dictionary>
272+
<Controls0:ExampleClassDictionary>
273+
<Controls0:ExampleClass x:Key=""testKey"" StringProp=""String value"" />
274+
</Controls0:ExampleClassDictionary>
275+
</Controls0:ExampleClassContainer.Dictionary>
276+
</Controls0:ExampleClassContainer>
277+
</Button.Tag>
278+
</Button>";
279+
} else {
280+
expectedXamlWithDictionary = @"<Button>
281+
<Button.Tag>
282+
<Controls0:ExampleClassContainer>
283+
<Controls0:ExampleClassContainer.Dictionary>
284+
<Controls0:ExampleClass x:Key=""testKey"" StringProp=""String value"" />
285+
</Controls0:ExampleClassContainer.Dictionary>
286+
</Controls0:ExampleClassContainer>
287+
</Button.Tag>
288+
</Button>";
289+
}
290+
291+
DesignItem exampleClassItem = component.RegisterComponentForDesigner(new ICSharpCode.WpfDesign.Tests.XamlDom.ExampleClass());
292+
exampleClassItem.Key = "testKey";
293+
exampleClassItem.Properties["StringProp"].SetValue("String value");
294+
dictionaryProp.CollectionElements.Add(exampleClassItem);
295+
296+
button.Properties["Tag"].SetValue(containerItem);
297+
g.Commit();
298+
}
299+
300+
Assert.IsTrue(s.CanUndo);
301+
Assert.IsFalse(s.CanRedo);
302+
AssertCanvasDesignerOutput(expectedXamlWithDictionary, button.Context, "xmlns:Controls0=\"" + ICSharpCode.WpfDesign.Tests.XamlDom.XamlTypeFinderTests.XamlDomTestsNamespace + "\"");
303+
304+
dictionaryProp = button.Properties["Tag"].Value.Properties["Dictionary"];
305+
Assert.IsTrue(((ICSharpCode.WpfDesign.Tests.XamlDom.ExampleClassDictionary)dictionaryProp.ValueOnInstance).Count == dictionaryProp.CollectionElements.Count);
306+
307+
s.Undo();
308+
Assert.IsFalse(s.CanUndo);
309+
Assert.IsTrue(s.CanRedo);
310+
AssertCanvasDesignerOutput("<Button>\n</Button>", button.Context, "xmlns:Controls0=\"" + ICSharpCode.WpfDesign.Tests.XamlDom.XamlTypeFinderTests.XamlDomTestsNamespace + "\"");
311+
312+
s.Redo();
313+
Assert.IsTrue(s.CanUndo);
314+
Assert.IsFalse(s.CanRedo);
315+
AssertCanvasDesignerOutput(expectedXamlWithDictionary, button.Context, "xmlns:Controls0=\"" + ICSharpCode.WpfDesign.Tests.XamlDom.XamlTypeFinderTests.XamlDomTestsNamespace + "\"");
316+
317+
dictionaryProp = button.Properties["Tag"].Value.Properties["Dictionary"];
318+
Assert.IsTrue(((ICSharpCode.WpfDesign.Tests.XamlDom.ExampleClassDictionary)dictionaryProp.ValueOnInstance).Count == dictionaryProp.CollectionElements.Count);
319+
228320
AssertLog("");
229-
}
321+
}
230322

231323
[Test]
232324
public void AddTextBoxToCanvas()
@@ -332,6 +424,89 @@ public void ClearImplicitChildCollection()
332424
AssertLog("");
333425
}
334426

427+
[Test]
428+
public void ClearExplicitList()
429+
{
430+
DesignItem button = CreateCanvasContext("<Button/>");
431+
432+
button.Properties["Tag"].SetValue(new ICSharpCode.WpfDesign.Tests.XamlDom.ExampleClassContainer());
433+
var containerItem = button.Properties["Tag"].Value;
434+
var otherListProp = containerItem.Properties["OtherList"];
435+
otherListProp.SetValue(new ICSharpCode.WpfDesign.Tests.XamlDom.ExampleClassList());
436+
437+
DesignItem exampleClassItem = button.Context.Services.Component.RegisterComponentForDesigner(new ICSharpCode.WpfDesign.Tests.XamlDom.ExampleClass());
438+
exampleClassItem.Properties["StringProp"].SetValue("String value");
439+
otherListProp.CollectionElements.Add(exampleClassItem);
440+
441+
var listInstance = (ICSharpCode.WpfDesign.Tests.XamlDom.ExampleClassList)otherListProp.ValueOnInstance;
442+
Assert.AreEqual(1, listInstance.Count);
443+
Assert.AreEqual(1, otherListProp.CollectionElements.Count);
444+
445+
button.Properties["Tag"].Value.Properties["OtherList"].CollectionElements.Clear();
446+
447+
Assert.AreEqual(0, listInstance.Count);
448+
Assert.AreEqual(0, otherListProp.CollectionElements.Count);
449+
450+
AssertCanvasDesignerOutput(@"<Button>
451+
<Button.Tag>
452+
<Controls0:ExampleClassContainer>
453+
<Controls0:ExampleClassContainer.OtherList>
454+
<Controls0:ExampleClassList>
455+
</Controls0:ExampleClassList>
456+
</Controls0:ExampleClassContainer.OtherList>
457+
</Controls0:ExampleClassContainer>
458+
</Button.Tag>
459+
</Button>", button.Context, "xmlns:Controls0=\"" + ICSharpCode.WpfDesign.Tests.XamlDom.XamlTypeFinderTests.XamlDomTestsNamespace + "\"");
460+
AssertLog("");
461+
}
462+
463+
[Test]
464+
public void ClearExplicitDictionary()
465+
{
466+
DesignItem button = CreateCanvasContext("<Button/>");
467+
468+
button.Properties["Tag"].SetValue(new ICSharpCode.WpfDesign.Tests.XamlDom.ExampleClassContainer());
469+
var containerItem = button.Properties["Tag"].Value;
470+
var dictionaryProp = containerItem.Properties["Dictionary"];
471+
dictionaryProp.SetValue(new ICSharpCode.WpfDesign.Tests.XamlDom.ExampleClassDictionary());
472+
473+
DesignItem exampleClassItem = button.Context.Services.Component.RegisterComponentForDesigner(new ICSharpCode.WpfDesign.Tests.XamlDom.ExampleClass());
474+
exampleClassItem.Key = "testKey";
475+
exampleClassItem.Properties["StringProp"].SetValue("String value");
476+
dictionaryProp.CollectionElements.Add(exampleClassItem);
477+
478+
var dictionaryInstance = (ICSharpCode.WpfDesign.Tests.XamlDom.ExampleClassDictionary)dictionaryProp.ValueOnInstance;
479+
Assert.AreEqual(1, dictionaryInstance.Count);
480+
Assert.AreEqual(1, dictionaryProp.CollectionElements.Count);
481+
482+
button.Properties["Tag"].Value.Properties["Dictionary"].CollectionElements.Clear();
483+
484+
Assert.AreEqual(0, dictionaryInstance.Count);
485+
Assert.AreEqual(0, dictionaryProp.CollectionElements.Count);
486+
487+
dictionaryProp.CollectionElements.Add(exampleClassItem);
488+
489+
Assert.AreEqual(1, dictionaryInstance.Count);
490+
Assert.AreEqual(1, dictionaryProp.CollectionElements.Count);
491+
492+
button.Properties["Tag"].Value.Properties["Dictionary"].CollectionElements.Clear();
493+
494+
Assert.AreEqual(0, dictionaryInstance.Count);
495+
Assert.AreEqual(0, dictionaryProp.CollectionElements.Count);
496+
497+
AssertCanvasDesignerOutput(@"<Button>
498+
<Button.Tag>
499+
<Controls0:ExampleClassContainer>
500+
<Controls0:ExampleClassContainer.Dictionary>
501+
<Controls0:ExampleClassDictionary>
502+
</Controls0:ExampleClassDictionary>
503+
</Controls0:ExampleClassContainer.Dictionary>
504+
</Controls0:ExampleClassContainer>
505+
</Button.Tag>
506+
</Button>", button.Context, "xmlns:Controls0=\"" + ICSharpCode.WpfDesign.Tests.XamlDom.XamlTypeFinderTests.XamlDomTestsNamespace + "\"");
507+
AssertLog("");
508+
}
509+
335510
[Test]
336511
public void AddMultiBindingToTextBox()
337512
{

src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/CollectionElementsCollection.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ protected override void RemoveItem(int index)
5454
XamlPropertyInfo info = property.propertyInfo;
5555
object collection = info.GetValue(property.ParentObject.Instance);
5656
if (!CollectionSupport.RemoveItemAt(info.ReturnType, collection, index)) {
57-
CollectionSupport.RemoveItem(info.ReturnType, collection, this[index].GetValueFor(info));
57+
var propertyValue = this[index];
58+
CollectionSupport.RemoveItem(info.ReturnType, collection, propertyValue.GetValueFor(info), propertyValue);
5859
}
5960

6061
this[index].RemoveNodeFromParent();

src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/CollectionSupport.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,5 +154,20 @@ public static void RemoveItem(Type collectionType, object collectionInstance, ob
154154
new object[] { item },
155155
CultureInfo.InvariantCulture);
156156
}
157+
158+
/// <summary>
159+
/// Removes an item instance from the specified collection.
160+
/// </summary>
161+
internal static void RemoveItem(Type collectionType, object collectionInstance, object item, XamlPropertyValue element)
162+
{
163+
var dictionary = collectionInstance as IDictionary;
164+
var xamlObject = element as XamlObject;
165+
166+
if (dictionary != null && xamlObject != null) {
167+
dictionary.Remove(xamlObject.GetXamlAttribute("Key"));
168+
} else {
169+
RemoveItem(collectionType, collectionInstance, item);
170+
}
171+
}
157172
}
158173
}

0 commit comments

Comments
 (0)