|
| 1 | +// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of this |
| 4 | +// software and associated documentation files (the "Software"), to deal in the Software |
| 5 | +// without restriction, including without limitation the rights to use, copy, modify, merge, |
| 6 | +// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
| 7 | +// to whom the Software is furnished to do so, subject to the following conditions: |
| 8 | +// |
| 9 | +// The above copyright notice and this permission notice shall be included in all copies or |
| 10 | +// substantial portions of the Software. |
| 11 | +// |
| 12 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 13 | +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
| 14 | +// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
| 15 | +// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 16 | +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 17 | +// DEALINGS IN THE SOFTWARE. |
| 18 | + |
| 19 | +using System; |
| 20 | +using System.Linq; |
| 21 | +using System.Text; |
| 22 | +using System.Windows.Controls; |
| 23 | +using System.Windows.Markup; |
| 24 | +using NUnit.Framework; |
| 25 | + |
| 26 | +namespace ICSharpCode.WpfDesign.Tests.Designer |
| 27 | +{ |
| 28 | + [TestFixture] |
| 29 | + public class MarkupExtensionModelTests : ModelTestHelper |
| 30 | + { |
| 31 | + private const string PathWithCommasAndSpaces = "C:\\Folder A\\Sub,Folder,A\\SubFolderB\\file,with,commas and spaces.txt"; |
| 32 | + private const string Simple = "AbcDef"; |
| 33 | + |
| 34 | + [Test] |
| 35 | + public void ElementMarkupExtensionWithSimpleString() |
| 36 | + { |
| 37 | + TestMarkupExtensionPrinter(Simple, true); |
| 38 | + } |
| 39 | + |
| 40 | + [Test] |
| 41 | + public void ShorthandMarkupExtensionWithSimpleString() |
| 42 | + { |
| 43 | + TestMarkupExtensionPrinter(Simple, false); |
| 44 | + } |
| 45 | + |
| 46 | + [Test] |
| 47 | + public void ElementMarkupExtensionWithFilePathString() |
| 48 | + { |
| 49 | + TestMarkupExtensionPrinter(PathWithCommasAndSpaces, true); |
| 50 | + } |
| 51 | + |
| 52 | + [Test] |
| 53 | + public void ShorthandMarkupExtensionWithFilePathString() |
| 54 | + { |
| 55 | + TestMarkupExtensionPrinter(PathWithCommasAndSpaces, false); |
| 56 | + } |
| 57 | + |
| 58 | + private void TestMarkupExtensionPrinter(string s, bool useElementStyle) |
| 59 | + { |
| 60 | + var checkBoxItem = CreateCanvasContext("<CheckBox/>"); |
| 61 | + var tagProp = checkBoxItem.Properties["Tag"]; |
| 62 | + |
| 63 | + tagProp.SetValue(new DataExtension()); |
| 64 | + tagProp.Value.Properties["Data"].SetValue(s); |
| 65 | + |
| 66 | + string expectedXaml; |
| 67 | + |
| 68 | + if (useElementStyle) { |
| 69 | + // Setting this should force element style |
| 70 | + tagProp.Value.Properties["Object"].SetValue(new ExampleClass()); |
| 71 | + |
| 72 | + expectedXaml = @"<CheckBox> |
| 73 | + <CheckBox.Tag> |
| 74 | + <t:DataExtension Data=""" + s + @"""> |
| 75 | + <t:DataExtension.Object> |
| 76 | + <t:ExampleClass /> |
| 77 | + </t:DataExtension.Object> |
| 78 | + </t:DataExtension> |
| 79 | + </CheckBox.Tag> |
| 80 | +</CheckBox>"; |
| 81 | + } else { |
| 82 | + StringBuilder sb = new StringBuilder("<CheckBox Tag=\"{t:Data Data="); |
| 83 | + |
| 84 | + bool containsSpace = s.Contains(' '); |
| 85 | + |
| 86 | + if(containsSpace) { |
| 87 | + sb.Append('\''); |
| 88 | + } |
| 89 | + |
| 90 | + sb.Append(s.Replace("\\", "\\\\")); |
| 91 | + |
| 92 | + if(containsSpace) { |
| 93 | + sb.Append('\''); |
| 94 | + } |
| 95 | + |
| 96 | + sb.Append("}\" />"); |
| 97 | + |
| 98 | + expectedXaml = sb.ToString(); |
| 99 | + } |
| 100 | + |
| 101 | + AssertCanvasDesignerOutput(expectedXaml, checkBoxItem.Context); |
| 102 | + AssertLog(""); |
| 103 | + |
| 104 | + // The following tests that the official XamlReader is parsing the resulting xaml into the |
| 105 | + // same string that we are testing, regardless if element or shorthand style is being used. |
| 106 | + |
| 107 | + string xaml = expectedXaml.Insert("<CheckBox".Length, " xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" " + |
| 108 | + "xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" " + |
| 109 | + "xmlns:t=\"" + DesignerTestsNamespace + "\""); |
| 110 | + |
| 111 | + var checkBox = (CheckBox)System.Windows.Markup.XamlReader.Parse(xaml); |
| 112 | + |
| 113 | + Assert.AreEqual(s, (string)checkBox.Tag); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + public class DataExtension : MarkupExtension |
| 118 | + { |
| 119 | + public DataExtension() |
| 120 | + { |
| 121 | + } |
| 122 | + |
| 123 | + public override object ProvideValue(IServiceProvider serviceProvider) |
| 124 | + { |
| 125 | + return Data; |
| 126 | + } |
| 127 | + |
| 128 | + public string Data { get; set; } |
| 129 | + |
| 130 | + public object Object { get; set; } |
| 131 | + } |
| 132 | +} |
0 commit comments