Browse Source

Can now generate new guids from the Setup Files property grid.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@1858 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Matt Ward 19 years ago
parent
commit
e6682a2264
  1. 56
      src/AddIns/BackendBindings/WixBinding/Project/Src/PropertyGrid/GuidEditor.cs
  2. 73
      src/AddIns/BackendBindings/WixBinding/Project/Src/PropertyGrid/GuidEditorListBox.cs
  3. 23
      src/AddIns/BackendBindings/WixBinding/Project/Src/PropertyGrid/WixXmlAttributePropertyDescriptor.cs
  4. 2
      src/AddIns/BackendBindings/WixBinding/Project/Src/WixPackageFilesEditor.cs
  5. 76
      src/AddIns/BackendBindings/WixBinding/Project/Src/WixSchemaCompletionData.cs
  6. 37
      src/AddIns/BackendBindings/WixBinding/Project/Src/WixXmlAttribute.cs
  7. 3
      src/AddIns/BackendBindings/WixBinding/Project/Src/WixXmlAttributeType.cs
  8. 2
      src/AddIns/BackendBindings/WixBinding/Project/WixBinding.csproj
  9. 29
      src/AddIns/BackendBindings/WixBinding/Test/PackageFiles/MissingFileAttributesTestFixture.cs
  10. 56
      src/AddIns/BackendBindings/WixBinding/Test/PackageFiles/WixAttributeTypeTests.cs
  11. 13
      src/AddIns/BackendBindings/WixBinding/Test/PackageFiles/WixSchemaTests.cs
  12. 83
      src/AddIns/BackendBindings/WixBinding/Test/PropertyGrid/GuidPropertyDescriptorTestFixture.cs
  13. 115
      src/AddIns/BackendBindings/WixBinding/Test/PropertyGrid/GuidTypeEditorTestFixture.cs
  14. 51
      src/AddIns/BackendBindings/WixBinding/Test/Utils/MockServiceProvider.cs
  15. 57
      src/AddIns/BackendBindings/WixBinding/Test/Utils/MockWindowsFormsEditorService.cs
  16. 4
      src/AddIns/BackendBindings/WixBinding/Test/WixBinding.Tests.csproj

56
src/AddIns/BackendBindings/WixBinding/Project/Src/PropertyGrid/GuidEditor.cs

@ -0,0 +1,56 @@ @@ -0,0 +1,56 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace ICSharpCode.WixBinding
{
/// <summary>
/// Property grid editor for Guids
/// </summary>
public class GuidEditor : UITypeEditor
{
public GuidEditor()
{
}
/// <summary>
/// Returns the drop down style.
/// </summary>
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.DropDown;
}
/// <summary>
/// Shows the Guid editor control in the drop down so the user
/// can change the Guid.
/// </summary>
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
IWindowsFormsEditorService editorService = null;
if (provider != null) {
editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
}
if (editorService != null) {
using (GuidEditorListBox listBox = new GuidEditorListBox(editorService)) {
listBox.Guid = (string)value;
editorService.DropDownControl(listBox);
value = listBox.Guid;
}
}
return value;
}
}
}

73
src/AddIns/BackendBindings/WixBinding/Project/Src/PropertyGrid/GuidEditorListBox.cs

@ -0,0 +1,73 @@ @@ -0,0 +1,73 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using ICSharpCode.Core;
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace ICSharpCode.WixBinding
{
/// <summary>
/// A list box which has one entry called "New Guid".
/// </summary>
public class GuidEditorListBox : ListBox
{
string guid = String.Empty;
IWindowsFormsEditorService editorService;
public GuidEditorListBox(IWindowsFormsEditorService editorService)
{
this.editorService = editorService;
Items.Add("New Guid");
Size = new Size(Width, ItemHeight);
BorderStyle = BorderStyle.None;
}
public string Guid {
get {
return guid;
}
set {
guid = value;
}
}
protected override bool ShowFocusCues {
get {
return false;
}
}
protected override void OnMouseClick(MouseEventArgs e)
{
base.OnMouseClick(e);
int index = IndexFromPoint(e.Location);
if (index != -1) {
CreateNewGuid();
editorService.CloseDropDown();
}
}
protected override void OnPreviewKeyDown(PreviewKeyDownEventArgs e)
{
base.OnPreviewKeyDown(e);
if (e.KeyData == Keys.Return) {
if (SelectedIndex != -1) {
CreateNewGuid();
}
editorService.CloseDropDown();
}
}
void CreateNewGuid()
{
guid = System.Guid.NewGuid().ToString().ToUpperInvariant();
}
}
}

23
src/AddIns/BackendBindings/WixBinding/Project/Src/PropertyGrid/WixXmlAttributePropertyDescriptor.cs

@ -9,6 +9,8 @@ using System; @@ -9,6 +9,8 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms.Design;
namespace ICSharpCode.WixBinding
{
@ -22,7 +24,7 @@ namespace ICSharpCode.WixBinding @@ -22,7 +24,7 @@ namespace ICSharpCode.WixBinding
/// <summary>
/// Creates a new instance of the WixXmlAttributePropertyDescriptor class.
/// </summary>
public WixXmlAttributePropertyDescriptor(WixXmlAttribute wixXmlAttribute) : base(wixXmlAttribute.Name, new Attribute[0])
public WixXmlAttributePropertyDescriptor(WixXmlAttribute wixXmlAttribute) : base(wixXmlAttribute.Name, GetAttributes(wixXmlAttribute))
{
this.wixXmlAttribute = wixXmlAttribute;
}
@ -90,5 +92,24 @@ namespace ICSharpCode.WixBinding @@ -90,5 +92,24 @@ namespace ICSharpCode.WixBinding
{
return true;
}
/// <summary>
/// Gets the attributes for the property descriptor based on the
/// WixXmlAttribute.
/// </summary>
/// <remarks>Adds an EditorAttribute for types (e.g. AutogenUuid)
/// that have a custom UITypeEditor.</remarks>
static Attribute[] GetAttributes(WixXmlAttribute wixXmlAttribute)
{
List<Attribute> attributes = new List<Attribute>();
switch (wixXmlAttribute.AttributeType) {
case WixXmlAttributeType.ComponentGuid:
case WixXmlAttributeType.AutogenUuid:
case WixXmlAttributeType.Uuid:
attributes.Add(new EditorAttribute(typeof(GuidEditor), typeof(UITypeEditor)));
break;
}
return attributes.ToArray();
}
}
}

2
src/AddIns/BackendBindings/WixBinding/Project/Src/WixPackageFilesEditor.cs

@ -135,7 +135,7 @@ namespace ICSharpCode.WixBinding @@ -135,7 +135,7 @@ namespace ICSharpCode.WixBinding
view.AllowedChildElements.Clear();
view.Attributes.Clear();
if (element != null) {
view.Attributes.AddRange(WixXmlAttribute.GetAttributes(element, WixSchemaCompletionData.GetAttributes(element.Name)));
view.Attributes.AddRange(WixSchemaCompletionData.GetAttributes(element));
view.AllowedChildElements.AddRange(WixSchemaCompletionData.GetChildElements(element.Name));
} else {
view.AllowedChildElements.Add("Directory");

76
src/AddIns/BackendBindings/WixBinding/Project/Src/WixSchemaCompletionData.cs

@ -44,7 +44,7 @@ namespace ICSharpCode.WixBinding @@ -44,7 +44,7 @@ namespace ICSharpCode.WixBinding
/// The method assumes the element is defined in the root of the schema
/// (i.e. is accessible via the XmlSchema.Elements property).
/// </remarks>
public string[] GetAttributes(string elementName)
public string[] GetAttributeNames(string elementName)
{
List<string> attributes = GetItems(elementName, GetAttributeCompletionData);
@ -56,6 +56,24 @@ namespace ICSharpCode.WixBinding @@ -56,6 +56,24 @@ namespace ICSharpCode.WixBinding
return attributes.ToArray();
}
/// <summary>
/// Gets the attributes for the specified element. Also adds any standard
/// attributes for the element which are not set.
/// </summary>
public WixXmlAttributeCollection GetAttributes(XmlElement element)
{
WixXmlAttributeCollection attributes = new WixXmlAttributeCollection();
string elementName = element.Name;
foreach (XmlAttribute attribute in element.Attributes) {
string attributeName = attribute.Name;
WixXmlAttributeType type = GetWixAttributeType(elementName, attributeName);
WixXmlAttribute wixAttribute = new WixXmlAttribute(attributeName, attribute.Value, type);
attributes.Add(wixAttribute);
}
attributes.AddRange(GetMissingAttributes(elementName, attributes, GetAttributeNames(elementName)));
return attributes;
}
/// <summary>
/// Returns the deprecated attributes for the specified element name.
/// </summary>
@ -64,6 +82,30 @@ namespace ICSharpCode.WixBinding @@ -64,6 +82,30 @@ namespace ICSharpCode.WixBinding
return FindDeprecatedAttributes(elementName).ToArray();
}
/// <summary>
/// Returns the qualified name of the attribute's type.
/// </summary>
public QualifiedName GetAttributeType(string elementName, string attributeName)
{
XmlElementPath path = GetPath(elementName);
XmlSchemaElement element = FindElement(path);
if (element != null) {
XmlSchemaComplexType complexType = GetElementAsComplexType(element);
if (complexType != null) {
foreach (XmlSchemaAttribute attribute in complexType.Attributes) {
if (attribute.Name == attributeName) {
XmlQualifiedName name = attribute.SchemaTypeName;
if (name != null) {
return new QualifiedName(name.Name, name.Namespace);
}
return null;
}
}
}
}
return null;
}
/// <summary>
/// Delegate that allows us to get a different type of completion data, but use
/// the same GetItems method to create a list of strings.
@ -164,5 +206,37 @@ namespace ICSharpCode.WixBinding @@ -164,5 +206,37 @@ namespace ICSharpCode.WixBinding
}
return attributes;
}
WixXmlAttributeType GetWixAttributeType(string elementName, string attributeName)
{
QualifiedName attributeTypeName = GetAttributeType(elementName, attributeName);
if (attributeTypeName != null) {
switch (attributeTypeName.Name) {
case "autogenuuid":
return WixXmlAttributeType.AutogenUuid;
case "uuid":
return WixXmlAttributeType.Uuid;
case "ComponentGuid":
return WixXmlAttributeType.ComponentGuid;
}
}
return WixXmlAttributeType.Text;
}
/// <summary>
/// Gets the attributes that have not been added to the
/// <paramref name="attributes"/>.
/// </summary>
WixXmlAttributeCollection GetMissingAttributes(string elementName, WixXmlAttributeCollection existingAttributes, string[] attributes)
{
WixXmlAttributeCollection missingAttributes = new WixXmlAttributeCollection();
foreach (string name in attributes) {
if (existingAttributes[name] == null) {
WixXmlAttributeType type = GetWixAttributeType(elementName, name);
missingAttributes.Add(new WixXmlAttribute(name, type));
}
}
return missingAttributes;
}
}
}

37
src/AddIns/BackendBindings/WixBinding/Project/Src/WixXmlAttribute.cs

@ -30,10 +30,11 @@ namespace ICSharpCode.WixBinding @@ -30,10 +30,11 @@ namespace ICSharpCode.WixBinding
this.type = type;
}
public WixXmlAttribute(string name) : this(name, String.Empty, WixXmlAttributeType.Text)
public WixXmlAttribute(string name, WixXmlAttributeType type)
: this(name, String.Empty, type)
{
}
/// <summary>
/// Gets the name of the attribute.
/// </summary>
@ -67,37 +68,5 @@ namespace ICSharpCode.WixBinding @@ -67,37 +68,5 @@ namespace ICSharpCode.WixBinding
return type;
}
}
/// <summary>
/// Gets the attributes for the specified element. Also adds any standard
/// attributes for the element which are not set.
/// </summary>
/// <param name="attributes"/>All attributes that should exist in the returned
/// attribute collection</param>
public static WixXmlAttributeCollection GetAttributes(XmlElement element, string[] attributeNames)
{
WixXmlAttributeCollection attributes = new WixXmlAttributeCollection();
foreach (XmlAttribute attribute in element.Attributes) {
WixXmlAttribute wixAttribute = new WixXmlAttribute(attribute.Name, attribute.Value, WixXmlAttributeType.Text);
attributes.Add(wixAttribute);
}
attributes.AddRange(GetMissingAttributes(attributes, attributeNames));
return attributes;
}
/// <summary>
/// Gets the attributes that have not been added to the
/// <paramref name="existingAttributes"/>.
/// </summary>
static WixXmlAttributeCollection GetMissingAttributes(WixXmlAttributeCollection existingAttributes, string[] attributes)
{
WixXmlAttributeCollection missingAttributes = new WixXmlAttributeCollection();
foreach (string name in attributes) {
if (existingAttributes[name] == null) {
missingAttributes.Add(new WixXmlAttribute(name));
}
}
return missingAttributes;
}
}
}

3
src/AddIns/BackendBindings/WixBinding/Project/Src/WixXmlAttributeType.cs

@ -11,6 +11,9 @@ namespace ICSharpCode.WixBinding @@ -11,6 +11,9 @@ namespace ICSharpCode.WixBinding
{
public enum WixXmlAttributeType
{
AutogenUuid,
ComponentGuid,
Uuid,
Text
}
}

2
src/AddIns/BackendBindings/WixBinding/Project/WixBinding.csproj

@ -139,6 +139,8 @@ @@ -139,6 +139,8 @@
<Compile Include="Src\Gui\LibraryParametersPanel.cs" />
<Compile Include="Src\IDirectoryReader.cs" />
<Compile Include="Src\Commands\AddDirectoryCommand.cs" />
<Compile Include="Src\PropertyGrid\GuidEditor.cs" />
<Compile Include="Src\PropertyGrid\GuidEditorListBox.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="WixBinding.addin">

29
src/AddIns/BackendBindings/WixBinding/Test/PackageFiles/MissingFileAttributesTestFixture.cs

@ -30,11 +30,8 @@ namespace WixBinding.Tests.PackageFiles @@ -30,11 +30,8 @@ namespace WixBinding.Tests.PackageFiles
public void ElementHasNoAttributes()
{
XmlElement element = doc.CreateElement("File", WixNamespaceManager.Namespace);
string[] attributeNames = new string[] {"Foo", "Bar"};
WixXmlAttributeCollection attributes = WixXmlAttribute.GetAttributes(element, attributeNames);
Assert.IsNotNull(attributes[0]);
Assert.IsNotNull(attributes[1]);
Assert.AreEqual(2, attributes.Count);
WixXmlAttributeCollection attributes = schema.GetAttributes(element);
Assert.IsTrue(attributes.Count > 0);
}
[Test]
@ -42,21 +39,25 @@ namespace WixBinding.Tests.PackageFiles @@ -42,21 +39,25 @@ namespace WixBinding.Tests.PackageFiles
{
XmlElement element = doc.CreateElement("File", WixNamespaceManager.Namespace);
element.SetAttribute("Id", "Test");
string[] attributeNames = new string[] {"Id"};
WixXmlAttributeCollection attributes = WixXmlAttribute.GetAttributes(element, attributeNames);
Assert.AreEqual(1, attributes.Count);
Assert.IsNotNull(attributes[0]);
WixXmlAttributeCollection attributes = schema.GetAttributes(element);
int idAttributeCount = 0;
foreach (WixXmlAttribute attribute in attributes) {
if (attribute.Name == "Id") {
idAttributeCount++;
}
}
Assert.AreEqual(1, idAttributeCount);
}
[Test]
public void UnknownElementAttribute()
{
XmlElement element = doc.CreateElement("File", WixNamespaceManager.Namespace);
element.SetAttribute("Id", "Test");
string[] attributeNames = new string[0];
WixXmlAttributeCollection attributes = WixXmlAttribute.GetAttributes(element, attributeNames);
Assert.AreEqual(1, attributes.Count);
Assert.IsNotNull(attributes[0]);
element.SetAttribute("Test", "TestValue");
WixXmlAttributeCollection attributes = schema.GetAttributes(element);
Assert.IsTrue(attributes.Count > 1);
Assert.IsNotNull(attributes["Test"]);
}
}
}

56
src/AddIns/BackendBindings/WixBinding/Test/PackageFiles/WixAttributeTypeTests.cs

@ -8,10 +8,66 @@ @@ -8,10 +8,66 @@
using ICSharpCode.WixBinding;
using NUnit.Framework;
using System;
using System.Xml;
namespace WixBinding.Tests.PackageFiles
{
[TestFixture]
public class WixAttributeTypeTests
{
WixXmlAttribute productIdAttribute;
WixXmlAttribute productUpgradeCodeAttribute;
WixXmlAttribute componentGuidAttribute;
WixSchemaCompletionData wixSchema = new WixSchemaCompletionData();
[SetUp]
public void Init()
{
WixDocument doc = new WixDocument();
doc.LoadXml(GetWixXml());
XmlElement productElement = doc.Product;
WixXmlAttributeCollection attributes = wixSchema.GetAttributes(productElement);
productIdAttribute = attributes["Id"];
productUpgradeCodeAttribute = attributes["UpgradeCode"];
XmlElement componentElement = (XmlElement)doc.SelectSingleNode("//w:Component", new WixNamespaceManager(doc.NameTable));
attributes = wixSchema.GetAttributes(componentElement);
componentGuidAttribute = attributes["Guid"];
}
[Test]
public void ProductIdAttributeIsAutogenUuid()
{
Assert.AreEqual(WixXmlAttributeType.AutogenUuid, productIdAttribute.AttributeType);
}
[Test]
public void UpgradeCodeAttributeIsUuid()
{
Assert.AreEqual(WixXmlAttributeType.Uuid, productUpgradeCodeAttribute.AttributeType);
}
[Test]
public void ComponentGuidAttributeIsUuid()
{
Assert.AreEqual(WixXmlAttributeType.ComponentGuid, componentGuidAttribute.AttributeType);
}
string GetWixXml()
{
return "<Wix xmlns='http://schemas.microsoft.com/wix/2003/01/wi'>\r\n" +
"\t<Product Name='DialogTest' \r\n" +
"\t Version='1.0' \r\n" +
"\t Language='1013' \r\n" +
"\t Manufacturer='#develop' \r\n" +
"\t Id='????????-????-????-????-????????????'>\r\n" +
"\t\t<Fragment>\r\n" +
"\t\t\t<Directory Id=\"TARGETDIR\" SourceName=\"SourceDir\">\r\n" +
"\t\t\t\t<Component Guid=''/>\r\n" +
"\t\t\t</Directory>\r\n" +
"\t\t</Fragment>\r\n" +
"\t</Product>\r\n" +
"</Wix>";
}
}
}

13
src/AddIns/BackendBindings/WixBinding/Test/PackageFiles/WixSchemaTests.cs

@ -6,8 +6,10 @@ @@ -6,8 +6,10 @@
// </file>
using ICSharpCode.WixBinding;
using ICSharpCode.XmlEditor;
using NUnit.Framework;
using System;
using System.Xml.Schema;
namespace WixBinding.Tests.PackageFiles
{
@ -35,7 +37,7 @@ namespace WixBinding.Tests.PackageFiles @@ -35,7 +37,7 @@ namespace WixBinding.Tests.PackageFiles
[Test]
public void DirectoryElementAttributes()
{
string[] attributes = schema.GetAttributes("Directory");
string[] attributes = schema.GetAttributeNames("Directory");
Assert.IsTrue(attributes.Length > 0);
Assert.Contains("Id", attributes);
Assert.Contains("FileSource", attributes);
@ -44,7 +46,7 @@ namespace WixBinding.Tests.PackageFiles @@ -44,7 +46,7 @@ namespace WixBinding.Tests.PackageFiles
[Test]
public void SrcAttributeExcluded()
{
string[] attributes = schema.GetAttributes("Directory");
string[] attributes = schema.GetAttributeNames("Directory");
Assert.IsTrue(attributes.Length > 0);
foreach (string attribute in attributes) {
Assert.IsFalse(attribute == "src");
@ -58,5 +60,12 @@ namespace WixBinding.Tests.PackageFiles @@ -58,5 +60,12 @@ namespace WixBinding.Tests.PackageFiles
Assert.Contains("src", attributes);
Assert.Contains("srcPatch", attributes);
}
[Test]
public void ProductAutogenuuidAttributeType()
{
QualifiedName attributeName = schema.GetAttributeType("Product", "Id");
Assert.AreEqual("autogenuuid", attributeName.Name);
}
}
}

83
src/AddIns/BackendBindings/WixBinding/Test/PropertyGrid/GuidPropertyDescriptorTestFixture.cs

@ -0,0 +1,83 @@ @@ -0,0 +1,83 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using ICSharpCode.WixBinding;
using NUnit.Framework;
using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms.Design;
namespace WixBinding.Tests.PropertyGrid
{
[TestFixture]
public class GuidPropertyDescriptorTestFixture
{
WixXmlAttributePropertyDescriptor propertyDescriptor;
EditorAttribute editorAttribute;
[TestFixtureSetUp]
public void SetUpFixture()
{
WixXmlAttribute attribute = new WixXmlAttribute("Id", WixXmlAttributeType.ComponentGuid);
propertyDescriptor = new WixXmlAttributePropertyDescriptor(attribute);
editorAttribute = GetEditorAttribute(propertyDescriptor.Attributes);
}
[Test]
public void EditorAttributeAdded()
{
Assert.IsNotNull(editorAttribute);
}
[Test]
public void EditorAttributeBaseTypeName()
{
Assert.AreEqual(typeof(UITypeEditor).AssemblyQualifiedName, editorAttribute.EditorBaseTypeName);
}
[Test]
public void EditorAttributeTypeName()
{
Assert.AreEqual(typeof(GuidEditor).AssemblyQualifiedName, editorAttribute.EditorTypeName);
}
[Test]
public void AutogenUuid()
{
WixXmlAttribute attribute = new WixXmlAttribute("Id", WixXmlAttributeType.AutogenUuid);
propertyDescriptor = new WixXmlAttributePropertyDescriptor(attribute);
EditorAttribute editorAttribute = GetEditorAttribute(propertyDescriptor.Attributes);
Assert.IsNotNull(editorAttribute);
Assert.AreEqual(typeof(GuidEditor).AssemblyQualifiedName, editorAttribute.EditorTypeName);
}
[Test]
public void Uuid()
{
WixXmlAttribute attribute = new WixXmlAttribute("Id", WixXmlAttributeType.Uuid);
propertyDescriptor = new WixXmlAttributePropertyDescriptor(attribute);
EditorAttribute editorAttribute = GetEditorAttribute(propertyDescriptor.Attributes);
Assert.IsNotNull(editorAttribute);
Assert.AreEqual(typeof(GuidEditor).AssemblyQualifiedName, editorAttribute.EditorTypeName);
}
static EditorAttribute GetEditorAttribute(AttributeCollection attributes)
{
foreach (Attribute attribute in attributes) {
EditorAttribute editorAttribute = attribute as EditorAttribute;
if (editorAttribute != null) {
return editorAttribute;
}
}
return null;
}
}
}

115
src/AddIns/BackendBindings/WixBinding/Test/PropertyGrid/GuidTypeEditorTestFixture.cs

@ -0,0 +1,115 @@ @@ -0,0 +1,115 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using ICSharpCode.WixBinding;
using NUnit.Framework;
using System;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using WixBinding.Tests.Utils;
namespace WixBinding.Tests.PropertyGrid
{
[TestFixture]
public class GuidTypeEditorTestFixture
{
GuidEditor editor;
object newValue;
string expectedNewGuid;
MockServiceProvider mockServiceProvider;
MockWindowsFormsEditorService mockWindowsFormsEditorService;
Type expectedGuidControlType;
[TestFixtureSetUp]
public void SetUpFixture()
{
editor = new GuidEditor();
// Edit the guid value.
mockServiceProvider = new MockServiceProvider();
mockWindowsFormsEditorService = new MockWindowsFormsEditorService();
mockServiceProvider.SetServiceToReturn(mockWindowsFormsEditorService);
expectedNewGuid = Guid.NewGuid().ToString().ToUpperInvariant();
mockWindowsFormsEditorService.SetNewValue(expectedNewGuid);
Guid guid = Guid.NewGuid();
newValue = editor.EditValue(mockServiceProvider, guid.ToString().ToUpperInvariant());
expectedGuidControlType = mockWindowsFormsEditorService.GetDropDownControlTypeUsed();
}
[Test]
public void DropDownEditStyle()
{
Assert.AreEqual(UITypeEditorEditStyle.DropDown, editor.GetEditStyle());
}
[Test]
public void IsDropDownResizable()
{
Assert.IsFalse(editor.IsDropDownResizable);
}
[Test]
public void NewGuidReturned()
{
Assert.IsInstanceOfType(typeof(String), newValue);
}
[Test]
public void NewGuidValue()
{
Assert.AreEqual(newValue, expectedNewGuid);
}
[Test]
public void WindowsFormsEditorServiceRequested()
{
Assert.AreEqual(typeof(IWindowsFormsEditorService),
mockServiceProvider.GetServiceRequested(0));
}
[Test]
public void SameValueReturnedIfNoServiceProviderSet()
{
string oldValue = "Test";
string newValue = (string)editor.EditValue(null, null, oldValue);
Assert.IsTrue(Object.ReferenceEquals(oldValue, newValue));
}
[Test]
public void SameValueReturnedIfWindowsFormsServiceReturned()
{
mockServiceProvider.SetServiceToReturn(null);
string oldValue = "Test";
string newValue = (string)editor.EditValue(null, mockServiceProvider, oldValue);
Assert.IsTrue(Object.ReferenceEquals(oldValue, newValue));
Assert.AreEqual(typeof(IWindowsFormsEditorService),
mockServiceProvider.GetServiceRequested(1));
}
[Test]
public void SameGuidValueReturnedIfNotEdited()
{
mockWindowsFormsEditorService = new MockWindowsFormsEditorService();
mockServiceProvider.SetServiceToReturn(mockWindowsFormsEditorService);
string oldValue = Guid.NewGuid().ToString().ToUpperInvariant();
string newValue = (string)editor.EditValue(null, mockServiceProvider, oldValue);
Assert.AreEqual(oldValue, newValue);
}
[Test]
public void GuidEditorControlUsedAsDropDownControl()
{
Assert.AreEqual("GuidEditorListBox", expectedGuidControlType.Name);
}
}
}

51
src/AddIns/BackendBindings/WixBinding/Test/Utils/MockServiceProvider.cs

@ -0,0 +1,51 @@ @@ -0,0 +1,51 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using System.Collections.Generic;
namespace WixBinding.Tests.Utils
{
public class MockServiceProvider : IServiceProvider
{
List<object> services = new List<object>();
List<Type> servicesRequested = new List<Type>();
public MockServiceProvider()
{
}
public object GetService(Type serviceType)
{
if (services.Count > 0) {
object service = services[0];
services.RemoveAt(0);
servicesRequested.Add(serviceType);
return service;
}
return null;
}
public void SetServiceToReturn(object service)
{
services.Add(service);
}
/// <summary>
/// Returns the service requested via the GetService method.
/// </summary>
/// <param name="index">The first service requested will
/// be at index 0.</param>
public Type GetServiceRequested(int index)
{
if (index < servicesRequested.Count) {
return servicesRequested[index];
}
return null;
}
}
}

57
src/AddIns/BackendBindings/WixBinding/Test/Utils/MockWindowsFormsEditorService.cs

@ -0,0 +1,57 @@ @@ -0,0 +1,57 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using ICSharpCode.WixBinding;
using System;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace WixBinding.Tests.Utils
{
public class MockWindowsFormsEditorService : IWindowsFormsEditorService
{
Type controlType;
string newValue;
bool newValueSet;
public MockWindowsFormsEditorService()
{
}
public void CloseDropDown()
{
}
public void DropDownControl(Control control)
{
controlType = control.GetType();
if (newValueSet) {
GuidEditorListBox listBox = control as GuidEditorListBox;
if (listBox != null) {
listBox.Guid = newValue;
}
}
}
public DialogResult ShowDialog(Form dialog)
{
return DialogResult.None;
}
public Type GetDropDownControlTypeUsed()
{
return controlType;
}
public void SetNewValue(string value)
{
newValue = value;
newValueSet = true;
}
}
}

4
src/AddIns/BackendBindings/WixBinding/Test/WixBinding.Tests.csproj

@ -203,6 +203,10 @@ @@ -203,6 +203,10 @@
<Compile Include="DirectoryImport\DuplicateShortDirectoryNameTestFixture.cs" />
<Compile Include="DirectoryImport\ExcludedItemsTestFixture.cs" />
<EmbeddedResource Include="Strings.resx" />
<Compile Include="PropertyGrid\GuidTypeEditorTestFixture.cs" />
<Compile Include="Utils\MockServiceProvider.cs" />
<Compile Include="Utils\MockWindowsFormsEditorService.cs" />
<Compile Include="PropertyGrid\GuidPropertyDescriptorTestFixture.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Project\WixBinding.csproj">

Loading…
Cancel
Save