Added INotifyPropertyChanging implementation.

This commit is contained in:
Laurent Bugnion 2012-04-15 22:45:08 +02:00
Родитель 7362a7e773
Коммит ba7a892558
12 изменённых файлов: 1216 добавлений и 62 удалений

Просмотреть файл

@ -28,13 +28,18 @@ namespace GalaSoft.MvvmLight
/// A base class for objects of which the properties must be observable.
/// </summary>
//// [ClassInfo(typeof(ViewModelBase))]
public class ObservableObject : INotifyPropertyChanged
public class ObservableObject : INotifyPropertyChanged, INotifyPropertyChanging
{
/// <summary>
/// Occurs after a property value changes.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Occurs before a property value changes.
/// </summary>
public event PropertyChangingEventHandler PropertyChanging;
/// <summary>
/// Provides access to the PropertyChanged event handler to derived classes.
/// </summary>
@ -46,6 +51,17 @@ namespace GalaSoft.MvvmLight
}
}
/// <summary>
/// Provides access to the PropertyChanging event handler to derived classes.
/// </summary>
protected PropertyChangingEventHandler PropertyChangingHandler
{
get
{
return PropertyChanging;
}
}
/// <summary>
/// Verifies that a property name exists in this ViewModel. This method
/// can be called before the property is used, for instance before
@ -60,7 +76,7 @@ namespace GalaSoft.MvvmLight
{
var myType = this.GetType();
#if WIN8
#if NETFX_CORE
if (!string.IsNullOrEmpty(propertyName)
&& myType.GetTypeInfo().GetDeclaredProperty(propertyName) == null)
{
@ -70,11 +86,58 @@ namespace GalaSoft.MvvmLight
if (!string.IsNullOrEmpty(propertyName)
&& myType.GetProperty(propertyName) == null)
{
#if !SILVERLIGHT
var descriptor = this as ICustomTypeDescriptor;
if (descriptor != null)
{
if (descriptor.GetProperties()
.Cast<PropertyDescriptor>()
.Any(property => property.Name == propertyName))
{
return;
}
}
#endif
throw new ArgumentException("Property not found", propertyName);
}
#endif
}
/// <summary>
/// Raises the PropertyChanging event if needed.
/// </summary>
/// <remarks>If the propertyName parameter
/// does not correspond to an existing property on the current class, an
/// exception is thrown in DEBUG configuration only.</remarks>
/// <param name="propertyName">The name of the property that
/// changed.</param>
[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate",
Justification = "This cannot be an event")]
protected virtual void RaisePropertyChanging(string propertyName)
{
#if NETFX_CORE
if (string.IsNullOrEmpty(propertyName))
{
throw new NotSupportedException(
"Raising the PropertyChanged event with an empty string or null is not supported in the Windows 8 developer preview");
}
else
{
#endif
VerifyPropertyName(propertyName);
var handler = PropertyChanging;
if (handler != null)
{
handler(this, new PropertyChangingEventArgs(propertyName));
}
#if NETFX_CORE
}
#endif
}
/// <summary>
/// Raises the PropertyChanged event if needed.
/// </summary>
@ -90,13 +153,35 @@ namespace GalaSoft.MvvmLight
VerifyPropertyName(propertyName);
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
/// <summary>
/// Raises the PropertyChanging event if needed.
/// </summary>
/// <typeparam name="T">The type of the property that
/// changes.</typeparam>
/// <param name="propertyExpression">An expression identifying the property
/// that changes.</param>
[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate",
Justification = "This cannot be an event")]
[SuppressMessage(
"Microsoft.Design",
"CA1006:GenericMethodsShouldProvideTypeParameter",
Justification = "This syntax is more convenient than other alternatives.")]
protected virtual void RaisePropertyChanging<T>(Expression<Func<T>> propertyExpression)
{
var handler = PropertyChanging;
if (handler != null)
{
var propertyName = GetPropertyName(propertyExpression);
handler(this, new PropertyChangingEventArgs(propertyName));
}
}
/// <summary>
/// Raises the PropertyChanged event if needed.
/// </summary>
@ -112,20 +197,46 @@ namespace GalaSoft.MvvmLight
Justification = "This syntax is more convenient than other alternatives.")]
protected virtual void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
{
if (propertyExpression == null)
{
return;
}
var handler = PropertyChanged;
if (handler != null)
{
var body = propertyExpression.Body as MemberExpression;
handler(this, new PropertyChangedEventArgs(body.Member.Name));
var propertyName = GetPropertyName(propertyExpression);
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
/// <summary>
/// Extracts the name of a property from an expression.
/// </summary>
/// <typeparam name="T">The type of the property.</typeparam>
/// <param name="propertyExpression">An expression returning the property's name.</param>
/// <returns>The name of the property returned by the expression.</returns>
/// <exception cref="ArgumentNullException">If the expression is null.</exception>
/// <exception cref="ArgumentException">If the expression does not represent a property.</exception>
protected string GetPropertyName<T>(Expression<Func<T>> propertyExpression)
{
if (propertyExpression == null)
{
throw new ArgumentNullException("propertyExpression");
}
var body = propertyExpression.Body as MemberExpression;
if (body == null)
{
throw new ArgumentException("Invalid argument", "propertyExpression");
}
var property = body.Member as PropertyInfo;
if (property == null)
{
throw new ArgumentException("Argument is not a property", "propertyExpression");
}
return property.Name;
}
/// <summary>
/// Assigns a new value to the property. Then, raises the
/// PropertyChanged event if needed.
@ -137,18 +248,23 @@ namespace GalaSoft.MvvmLight
/// <param name="field">The field storing the property's value.</param>
/// <param name="newValue">The property's value after the change
/// occurred.</param>
protected void Set<T>(
/// <returns>True if the PropertyChanged event has been raised,
/// false otherwise. The event is not raised if the old
/// value is equal to the new value.</returns>
protected bool Set<T>(
Expression<Func<T>> propertyExpression,
ref T field,
T newValue)
{
if (EqualityComparer<T>.Default.Equals(field, newValue))
{
return;
return false;
}
RaisePropertyChanging(propertyExpression);
field = newValue;
RaisePropertyChanged(propertyExpression);
return true;
}
/// <summary>
@ -162,18 +278,23 @@ namespace GalaSoft.MvvmLight
/// <param name="field">The field storing the property's value.</param>
/// <param name="newValue">The property's value after the change
/// occurred.</param>
protected void Set<T>(
/// <returns>True if the PropertyChanged event has been raised,
/// false otherwise. The event is not raised if the old
/// value is equal to the new value.</returns>
protected bool Set<T>(
string propertyName,
ref T field,
T newValue)
{
if (EqualityComparer<T>.Default.Equals(field, newValue))
{
return;
return false;
}
RaisePropertyChanging(propertyName);
field = newValue;
RaisePropertyChanged(propertyName);
return true;
}
}
}

Просмотреть файл

@ -1,5 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<TestLists xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
<TestList name="SimpleIoc" id="8964cb7d-aaec-4f61-a296-1628f8242ec0" parentListId="8c43106b-9dc1-4907-a29f-aa66a61bf5b6">
<TestLinks>
<TestLink id="520df210-a347-a0a5-ad00-d8c0d43e8ef1" name="TestReset" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="b0414ff4-fbc9-390a-7852-d5781d2d14e2" name="TestIsClassRegistered" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="55a3822b-cc01-9466-3a46-b6231a953d38" name="TestGetEmptyInstancesGeneric" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="ea7326a6-a8fa-1660-c03e-67ed7e4a1193" name="TestGetAllInstancesWithTypeGeneric" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="dc67f48a-c467-7881-3d02-4fda586d095d" name="TestAddingFactoryAndKeyForClassRegisteredWithFactoryAndSameKey" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="fa2829c9-31b5-1f65-f6ea-d5ccbdcab932" name="TestGetInstancesWithInterfaceGeneric" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="13224dff-9259-eed6-0c35-c7779659b987" name="TestRegisterInterfaceOnly" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="6c4b4076-2b10-723d-0b25-9224be4d3abe" name="TestDefaultClassCreation" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="12a0e0a7-8f55-000c-d249-a7c63db0bee7" name="TestGetEmptyInstances" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="07b18e55-469d-c708-06e0-08e3764e78c3" name="TestOverwritingDefaultClassWithFactory" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="a51ea75e-094c-7fef-f863-5cbd6bf9edb3" name="TestGetInstanceWithUnregisteredClass" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="9a5964af-f96f-be68-d997-4a1d3ffc4727" name="TestUnregisterInstanceWithKey" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="8666f713-c4a7-1e65-37c3-b72704b0eb23" name="TestGetInstancesWithInstance" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="7da8c526-c4f8-4486-2897-7cf0024a75ee" name="TestCreationTimeWithFactory" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="44e2e532-2e8e-5eca-dc49-8ce48e61c97b" name="TestGetInstancesWithParameters" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="5d162ba1-0656-a312-f433-6bd99dc4d69f" name="TestRegisterInstanceWithMultiConstructors" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="fe0628ed-d35b-7d07-a016-e129d626113c" name="TestGetAllInstancesOfClassWithCreation" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="7aa752e6-e12b-829f-8a5d-f6bd8e93ec6a" name="TestGetAllInstancesOfInterfaceWithCreation" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="c43fd5dd-9288-e1c6-123f-ba71b9f60dc0" name="TestGetAllInstancesWithInstance" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="f41081fd-0ad2-9fd5-35ee-c0a467b8798c" name="TestGetInstancesWithType" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="347a2511-c4b2-0ed6-e2e4-369a0200966c" name="TestIsInterfaceRegistered" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="0a3a8ffc-af50-be1e-bc35-61a40209e400" name="TestGetInstanceWithGenericInterface" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="1b9b6b85-e334-2e58-aa06-1b9b9058967d" name="TestGetInstanceWithNullKey" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="bde6490b-7962-b56f-af8e-bef896b1556d" name="TestOverwritingFactoryWithDefaultClass" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="893c334b-0c55-5f4b-6369-cedf03d158d3" name="TestAddingFactoryAndKeyForClassRegisteredWithFactory" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="acf0f8f6-19cc-a948-955e-9bde18bb9a13" name="TestContainsInstance" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="99d76832-477c-2b1c-ea6d-721e1cbf3b55" name="TestAddingFactoryAndKeyForClassRegisteredWithDefault" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="73e488ad-ba55-5783-c224-ca830ad31430" name="TestContainsInstanceForKey" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="acada0fc-7c4a-0bde-f9f6-6dff1682b1e0" name="TestAddingDefaultForClassRegisteredWithFactoryAndKey" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="0635ff4d-7498-3c72-841d-13e2a90e653c" name="TestBuildWithMultipleConstructors" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="39d8a354-f037-a5ee-b576-80c32bfe2909" name="TestGetInstancesWithInterface" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="3661cb73-1fb6-51e2-07fc-06bc1b872429" name="TestBuildInstanceWithMultipleConstructorsNotMarkedWithAttribute" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="f1e6481e-449f-f5fe-7a1d-6c9a28bcde32" name="TestGetAllInstancesWithType" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="8772e5f9-faf4-0cac-9467-141deb52a69a" name="TestOverwritingDefaultClassWithSameDefaultClass" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="4c8eeb8d-6c9d-e0e3-b17e-3111ed6e6207" name="TestIsClassRegisteredWithFactory" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="42d47a50-6673-ba43-e2ce-25632b0275a0" name="TestGetInstanceWithGenericType" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="fc939926-57de-4305-2d1d-18dde2fad855" name="TestCreationTimeForDefaultInstance" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="a8a94682-1183-adae-3efa-1b8a6c5fe65e" name="TestConstructWithProperty" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="57dc5534-a709-cd43-9920-14be94c38d2a" name="TestUnregisterClass" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="6c811438-aba3-fc7c-7296-4ebcf9c48bca" name="TestGetInstanceWithInterface" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="8d85a48f-ed8a-8c8f-e7e9-c6b385d3ab67" name="TestGetInstancesWithInstanceGeneric" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="78488c80-b15d-b967-52fd-7ab880db3f56" name="TestCreationTimeWithInterfaceForDefaultInstance" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="d64292c9-43bc-20fb-dba6-8ecc6eff74c1" name="TestCreationTimeForNamedInstance" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="c3c0ee59-eeed-659f-9693-f0aa9bfff5c0" name="TestCreationOfMultipleInstances" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="61ebffd7-b70b-7246-a363-002400faad86" name="TestAddingFactoryForClassRegisteredWithFactoryAndKey" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="887bad84-5d69-9a39-43e7-352f9af4f57b" name="TestOverwritingFactoryWithFactory" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="bc37a0f2-e8e7-c6d1-607c-9e598f7b641e" name="TestAddingFactoryAndKeyForClassRegisteredWithFactoryAndDifferentKey" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="dcea7062-a5e2-3bf9-9eeb-4d81030ef95c" name="TestGetAllInstancesWithInterface" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="1cd7464c-9f49-cb3a-aaee-cfa706595b69" name="TestGetAllInstancesWithInterfaceGeneric" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="a0266317-6806-2d37-4f81-22d0a4ded7c4" name="TestGetInstancesWithTypeGeneric" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="c91c2190-6937-2601-082c-bd16be3b32b3" name="TestBuildWithMultipleConstructorsNotMarkedWithAttribute" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="338297d7-6b11-e7b4-40dc-b1c1488c6241" name="TestOverwritingInterfaceClassWithOtherClass" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="affbe36b-6c08-7fc8-634d-7286e73ac659" name="TestGetInstanceWithType" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="3ee9c7e5-8201-4ddc-d5a2-e45c62d75f65" name="TestContainsClass" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="e8e85629-ee37-c675-3e01-ea15e105135b" name="TestGetInstanceWithParameters" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="9ef73523-82fa-7c8f-a014-99b8582153a3" name="TestUnregisterInstance" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="7bbf418c-7d20-7b55-88ac-770f0ede0cc3" name="TestCreationTimeWithInterfaceForNamedInstance" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="c2320ae1-8df7-2014-1584-4b03dbc11b9e" name="TestIsClassRegisteredWithFactoryAndKey" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="afd51ee4-e714-2d9e-acf3-3c81bea35145" name="TestGetAllInstancesWithInstanceGeneric" storage="galasoft.mvvmlight.test (net35)\bin\debug\galasoft.mvvmlight.test (net35).dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</TestLinks>
</TestList>
<TestList name="Lists of Tests" id="8c43106b-9dc1-4907-a29f-aa66a61bf5b6">
<RunConfiguration id="2d4ea1c9-9c2c-4e21-ba7a-1c783c04d914" name="Trace and Test Impact" storage="traceandtestimpact.testsettings" type="Microsoft.VisualStudio.TestTools.Common.TestRunConfiguration, Microsoft.VisualStudio.QualityTools.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</TestList>

Просмотреть файл

@ -0,0 +1,35 @@
// ****************************************************************************
// <copyright file="INotifyPropertyChanging.cs" company="GalaSoft Laurent Bugnion">
// Copyright © GalaSoft Laurent Bugnion 2012
// </copyright>
// ****************************************************************************
// <author>Laurent Bugnion</author>
// <email>laurent@galasoft.ch</email>
// <date>08.01.2012</date>
// <project>GalaSoft.MvvmLight</project>
// <web>http://www.galasoft.ch</web>
// <license>
// See license.txt in this solution or http://www.galasoft.ch/license_MIT.txt
// </license>
// <LastBaseLevel>BL0000</LastBaseLevel>
// ****************************************************************************
namespace System.ComponentModel
{
/// <summary>
/// Defines an event for notifying clients that a property value is changing.
/// </summary>
////[ClassInfo(typeof(INotifyPropertyChanging),
//// VersionString = "4.0.0.0/BL0000",
//// DateString = "201201081910",
//// Description = "A class allowing to store and invoke actions without keeping a hard reference to the action's target.",
//// UrlContacts = "http://www.galasoft.ch/contact_en.html",
//// Email = "laurent@galasoft.ch")]
public interface INotifyPropertyChanging
{
/// <summary>
/// Occurs when a property value is changing.
/// </summary>
event PropertyChangingEventHandler PropertyChanging;
}
}

Просмотреть файл

@ -0,0 +1,43 @@
// ****************************************************************************
// <copyright file="PropertyChangingEventArgs.cs" company="GalaSoft Laurent Bugnion">
// Copyright © GalaSoft Laurent Bugnion 2012
// </copyright>
// ****************************************************************************
// <author>Laurent Bugnion</author>
// <email>laurent@galasoft.ch</email>
// <date>08.01.2012</date>
// <project>GalaSoft.MvvmLight</project>
// <web>http://www.galasoft.ch</web>
// <license>
// See license.txt in this solution or http://www.galasoft.ch/license_MIT.txt
// </license>
// ****************************************************************************
namespace System.ComponentModel
{
/// <summary>
/// Provides data for the System.ComponentModel.INotifyPropertyChanging.PropertyChanging
/// event.
/// </summary>
////[ClassInfo(typeof(INotifyPropertyChanging))]
public class PropertyChangingEventArgs
{
/// <summary>
/// Gets the name of the property that is changing.
/// </summary>
public string PropertyName
{
get;
private set;
}
/// <summary>
/// Initializes a new instance of the System.ComponentModel.PropertyChangingEventArgs class.
/// </summary>
/// <param name="propertyName">The name of the property that is changing.</param>
public PropertyChangingEventArgs(string propertyName)
{
PropertyName = propertyName;
}
}
}

Просмотреть файл

@ -0,0 +1,25 @@
// ****************************************************************************
// <copyright file="PropertyChangingEventHandler.cs" company="GalaSoft Laurent Bugnion">
// Copyright © GalaSoft Laurent Bugnion 2012
// </copyright>
// ****************************************************************************
// <author>Laurent Bugnion</author>
// <email>laurent@galasoft.ch</email>
// <date>08.01.2012</date>
// <project>GalaSoft.MvvmLight</project>
// <web>http://www.galasoft.ch</web>
// <license>
// See license.txt in this solution or http://www.galasoft.ch/license_MIT.txt
// </license>
// ****************************************************************************
namespace System.ComponentModel
{
/// <summary>
/// Represents a method that will handle the System.ComponentModel.INotifyPropertyChanging.PropertyChanging event.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The event data.</param>
////[ClassInfo(typeof(INotifyPropertyChanging))]
public delegate void PropertyChangingEventHandler(object sender, PropertyChangingEventArgs e);
}

Просмотреть файл

@ -7,6 +7,16 @@ namespace GalaSoft.MvvmLight.Test.Command
[TestClass]
public class RelayCommandGenericTest
{
private bool _canExecute = true;
private WeakReference _reference;
private TemporaryClass _tempoInstance;
public RelayCommand<string> TestCommand
{
get;
private set;
}
[TestMethod]
public void CanExecuteChangedTest()
{
@ -24,13 +34,23 @@ namespace GalaSoft.MvvmLight.Test.Command
command.RaiseCanExecuteChanged();
Assert.AreEqual(1, canExecuteChangedCalled);
#if SILVERLIGHT
Assert.AreEqual(1, canExecuteChangedCalled);
#else
// In WPF, cannot trigger the CanExecuteChanged event like this
Assert.AreEqual(0, canExecuteChangedCalled);
#endif
command.CanExecuteChanged -= canExecuteChangedEventHandler;
command.RaiseCanExecuteChanged();
Assert.AreEqual(1, canExecuteChangedCalled);
#if SILVERLIGHT
Assert.AreEqual(1, canExecuteChangedCalled);
#else
// In WPF, cannot trigger the CanExecuteChanged event like this
Assert.AreEqual(0, canExecuteChangedCalled);
#endif
}
[TestMethod]
@ -105,7 +125,7 @@ namespace GalaSoft.MvvmLight.Test.Command
}
[TestMethod]
public void ExecuteTest()
public void TestExecute()
{
var dummy = "Not executed";
const string executed = "Executed";
@ -122,7 +142,25 @@ namespace GalaSoft.MvvmLight.Test.Command
Assert.AreEqual(executed + parameter, dummy);
}
private bool _canExecute = true;
private static string _dummyStatic;
[TestMethod]
public void TestExecuteStatic()
{
_dummyStatic = "Not executed";
const string executed = "Executed";
const string parameter = "Parameter";
var command = new RelayCommand<string>(
p =>
{
_dummyStatic = executed + p;
});
command.Execute(parameter);
Assert.AreEqual(executed + parameter, _dummyStatic);
}
[TestMethod]
public void TestCallingExecuteWhenCanExecuteIsFalse()
@ -144,5 +182,187 @@ namespace GalaSoft.MvvmLight.Test.Command
command.Execute(value2);
Assert.AreEqual(value2, result);
}
[TestMethod]
public void TestReleasingTargetForCanExecuteGeneric()
{
_tempoInstance = new TemporaryClass();
_reference = new WeakReference(_tempoInstance);
TestCommand = new RelayCommand<string>(
_tempoInstance.SetContent,
_tempoInstance.CheckEnabled);
Assert.IsTrue(_reference.IsAlive);
_tempoInstance = null;
GC.Collect();
#if NETFX_CORE
Assert.IsTrue(_reference.IsAlive);
TestCommand = null;
GC.Collect();
Assert.IsFalse(_reference.IsAlive);
#else
Assert.IsFalse(_reference.IsAlive);
#endif
}
[TestMethod]
public void TestReleasingTargetForCanExecuteGenericPrivate()
{
_tempoInstance = new TemporaryClass();
_reference = new WeakReference(_tempoInstance);
_tempoInstance.CreateCommandGenericCanExecutePrivate();
Assert.IsTrue(_reference.IsAlive);
_tempoInstance = null;
GC.Collect();
Assert.IsFalse(_reference.IsAlive);
}
[TestMethod]
public void TestReleasingTargetForCanExecuteGenericInternal()
{
_tempoInstance = new TemporaryClass();
_reference = new WeakReference(_tempoInstance);
_tempoInstance.CreateCommandGenericCanExecuteInternal();
Assert.IsTrue(_reference.IsAlive);
_tempoInstance = null;
GC.Collect();
Assert.IsFalse(_reference.IsAlive);
}
[TestMethod]
public void TestReleasingTargetForExecuteGeneric()
{
_tempoInstance = new TemporaryClass();
_reference = new WeakReference(_tempoInstance);
TestCommand = new RelayCommand<string>(
_tempoInstance.SetContent);
Assert.IsTrue(_reference.IsAlive);
_tempoInstance = null;
GC.Collect();
#if NETFX_CORE
Assert.IsTrue(_reference.IsAlive);
TestCommand = null;
GC.Collect();
Assert.IsFalse(_reference.IsAlive);
#else
Assert.IsFalse(_reference.IsAlive);
#endif
}
[TestMethod]
public void TestReleasingTargetForExecuteGenericPrivate()
{
_tempoInstance = new TemporaryClass();
_reference = new WeakReference(_tempoInstance);
_tempoInstance.CreateCommandGenericPrivate();
Assert.IsTrue(_reference.IsAlive);
_tempoInstance = null;
GC.Collect();
Assert.IsFalse(_reference.IsAlive);
}
[TestMethod]
public void TestReleasingTargetForExecuteGenericInternal()
{
_tempoInstance = new TemporaryClass();
_reference = new WeakReference(_tempoInstance);
_tempoInstance.CreateCommandGenericInternal();
Assert.IsTrue(_reference.IsAlive);
_tempoInstance = null;
GC.Collect();
Assert.IsFalse(_reference.IsAlive);
}
[TestMethod]
public void TestValueTypeCanExecute()
{
Reset();
var command = new RelayCommand<int>(
i => _methodWasExecuted = true,
i => true);
Assert.IsFalse(_methodWasExecuted);
command.Execute(null);
Assert.IsTrue(_methodWasExecuted);
}
[TestMethod]
#if NETFX_CORE
[ExpectedException(typeof(InvalidCastException))] // TODO Try to find a way in Win8
#endif
public void TestValueTypeConversion()
{
Reset();
const int inputInt = 1234;
const double inputDouble = 1234.5678;
const bool inputBool = true;
var resultInt = 0;
var resultBool = false;
var resultDouble = 0.0;
var commandInt = new RelayCommand<int>(
p =>
{
resultInt = p;
},
p => true);
var commandBool = new RelayCommand<bool>(
p =>
{
resultBool = p;
},
p => true);
var commandDouble = new RelayCommand<double>(
p =>
{
resultDouble = p;
},
p => true);
Assert.AreEqual(0, resultInt);
Assert.AreEqual(false, resultBool);
Assert.AreEqual(0.0, resultDouble);
commandInt.Execute(inputInt.ToString());
commandBool.Execute(inputBool.ToString());
commandDouble.Execute(inputDouble.ToString());
Assert.AreEqual(inputInt, resultInt);
Assert.AreEqual(inputBool, resultBool);
Assert.AreEqual(inputDouble, resultDouble);
}
private bool _methodWasExecuted;
private void Reset()
{
_methodWasExecuted = false;
}
}
}

Просмотреть файл

@ -7,6 +7,53 @@ namespace GalaSoft.MvvmLight.Test.Command
[TestClass]
public class RelayCommandTest
{
private bool _canExecute = true;
private WeakReference _reference;
private TemporaryClass _tempoInstance;
public RelayCommand TestCommand
{
get;
private set;
}
[TestMethod]
public void TestCallingExecuteWhenCanExecuteIsFalse()
{
var counter = 0;
var command = new RelayCommand(
() => counter++,
() => _canExecute);
command.Execute(null);
Assert.AreEqual(1, counter);
_canExecute = false;
command.Execute(null);
Assert.AreEqual(1, counter);
_canExecute = true;
command.Execute(null);
Assert.AreEqual(2, counter);
}
[TestMethod]
public void TestCanExecute()
{
var canExecute = true;
var command = new RelayCommand(
() =>
{
},
() => canExecute);
Assert.AreEqual(true, command.CanExecute(null));
canExecute = false;
Assert.AreEqual(false, command.CanExecute(null));
}
[TestMethod]
public void TestCanExecuteChanged()
{
@ -24,29 +71,22 @@ namespace GalaSoft.MvvmLight.Test.Command
command.RaiseCanExecuteChanged();
Assert.AreEqual(1, canExecuteChangedCalled);
#if SILVERLIGHT
Assert.AreEqual(1, canExecuteChangedCalled);
#else
// In WPF, cannot trigger the CanExecuteChanged event like this
Assert.AreEqual(0, canExecuteChangedCalled);
#endif
command.CanExecuteChanged -= canExecuteChangedEventHandler;
command.RaiseCanExecuteChanged();
Assert.AreEqual(1, canExecuteChangedCalled);
}
[TestMethod]
public void TestCanExecute()
{
var canExecute = true;
var command = new RelayCommand(() =>
{
},
() => canExecute);
Assert.AreEqual(true, command.CanExecute(null));
canExecute = false;
Assert.AreEqual(false, command.CanExecute(null));
#if SILVERLIGHT
Assert.AreEqual(1, canExecuteChangedCalled);
#else
// In WPF, cannot trigger the CanExecuteChanged event like this
Assert.AreEqual(0, canExecuteChangedCalled);
#endif
}
[TestMethod]
@ -91,25 +131,72 @@ namespace GalaSoft.MvvmLight.Test.Command
Assert.AreEqual(executed, dummy);
}
private bool _canExecute = true;
private static string _dummyStatic;
[TestMethod]
public void TestCallingExecuteWhenCanExecuteIsFalse()
public void TestExecuteStatic()
{
var counter = 0;
_dummyStatic = "Not executed";
const string executed = "Executed";
var command = new RelayCommand(
() => counter++,
() => _canExecute);
() =>
{
_dummyStatic = executed;
});
command.Execute(null);
Assert.AreEqual(1, counter);
_canExecute = false;
command.Execute(null);
Assert.AreEqual(1, counter);
_canExecute = true;
command.Execute(null);
Assert.AreEqual(2, counter);
Assert.AreEqual(executed, _dummyStatic);
}
[TestMethod]
public void TestReleasingTargetForCanExecute()
{
_tempoInstance = new TemporaryClass();
_reference = new WeakReference(_tempoInstance);
TestCommand = new RelayCommand(
_tempoInstance.SetContent,
_tempoInstance.CheckEnabled);
Assert.IsTrue(_reference.IsAlive);
_tempoInstance = null;
GC.Collect();
#if NETFX_CORE
Assert.IsTrue(_reference.IsAlive);
TestCommand = null;
GC.Collect();
Assert.IsFalse(_reference.IsAlive);
#else
Assert.IsFalse(_reference.IsAlive);
#endif
}
[TestMethod]
public void TestReleasingTargetForExecute()
{
_tempoInstance = new TemporaryClass();
_reference = new WeakReference(_tempoInstance);
TestCommand = new RelayCommand(
_tempoInstance.SetContent);
Assert.IsTrue(_reference.IsAlive);
_tempoInstance = null;
GC.Collect();
#if NETFX_CORE
Assert.IsTrue(_reference.IsAlive);
TestCommand = null;
GC.Collect();
Assert.IsFalse(_reference.IsAlive);
#else
Assert.IsFalse(_reference.IsAlive);
#endif
}
}
}

Просмотреть файл

@ -59,11 +59,40 @@
<Compile Include="Command\EventToCommandTest.cs" />
<Compile Include="Command\RelayCommandGenericTest.cs" />
<Compile Include="Command\RelayCommandTest.cs" />
<Compile Include="Command\TemporaryClass.cs" />
<Compile Include="Helpers\PublicTestClassGenericWithResult.cs" />
<Compile Include="Helpers\InternalTestClassGenericWithResult.cs" />
<Compile Include="Helpers\InternalTestClassWithResult.cs" />
<Compile Include="Helpers\PublicTestClassWithResult.cs" />
<Compile Include="Helpers\WeakActionGenericNestedTest.cs" />
<Compile Include="Helpers\WeakActionNestedTest.cs" />
<Compile Include="Helpers\WeakFuncGenericNestedTest.cs" />
<Compile Include="Helpers\WeakFuncGenericTest.cs" />
<Compile Include="Helpers\WeakFuncNestedTest.cs" />
<Compile Include="Helpers\WeakFuncTest.cs" />
<Compile Include="Messaging\ITestMessage.cs" />
<Compile Include="Messaging\MessengerTestConstrainingMessages.cs" />
<Compile Include="Messaging\TestMessageBase.cs" />
<Compile Include="Messaging\TestMessageImpl.cs" />
<Compile Include="ObservableObjectPropertyChangingTest.cs" />
<Compile Include="Ioc\SimpleIocTestAutoCreation.cs" />
<Compile Include="Helpers\CommonTestClass.cs" />
<Compile Include="Helpers\InternalTestClassGeneric.cs" />
<Compile Include="Helpers\InternalTestClass.cs" />
<Compile Include="Helpers\PublicTestClassGeneric.cs" />
<Compile Include="Helpers\PublicTestClass.cs" />
<Compile Include="Helpers\TestCase.cs" />
<Compile Include="Helpers\WeakActionGenericTest.cs" />
<Compile Include="Helpers\WeakActionTest.cs" />
<Compile Include="Ioc\SimpleIocTestIsRegistered.cs" />
<Compile Include="Ioc\SimpleIocTestContains.cs" />
<Compile Include="Ioc\SimpleIocTestCreationTime.cs" />
<Compile Include="Messaging\GarbageCollectionTest.cs" />
<Compile Include="Properties\AssemblyInfo.Net35.cs" />
<Compile Include="Stubs\TestPropertyDescriptor.cs" />
<Compile Include="Stubs\TestBindingTarget.cs" />
<Compile Include="Stubs\TestClassForCreationTime.cs" />
<Compile Include="Stubs\TestCustomTypeDescriptor.cs" />
<Compile Include="Stubs\TestViewModelWithBooleanSource.cs" />
<Compile Include="Ioc\SimpleIocTestMultipleConstructors.cs" />
<Compile Include="Ioc\SimpleIocTestMultipleInstances.cs" />
@ -82,7 +111,7 @@
<Compile Include="Messaging\NotificationMessageActionTest.cs" />
<Compile Include="Messaging\NotificationMessageTest.cs" />
<Compile Include="Messaging\PropertyChangedMessageTest.cs" />
<Compile Include="ObservableObjectTest.cs" />
<Compile Include="ObservableObjectPropertyChangedTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Stubs\TestClass6.cs" />
<Compile Include="Stubs\ITestClass.cs" />
@ -109,7 +138,6 @@
<Name>GalaSoft.MvvmLight.Extras %28NET35%29</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

Просмотреть файл

@ -0,0 +1,181 @@
using System;
using GalaSoft.MvvmLight.Test.ViewModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace GalaSoft.MvvmLight.Test
{
[TestClass]
public class ObservableObjectPropertyChangedTest
{
[TestMethod]
public void TestPropertyChangedNoBroadcast()
{
var receivedDateTimeLocal = DateTime.MinValue;
var vm = new TestClassWithObservableObject();
vm.PropertyChanged += (s, e) =>
{
if (e.PropertyName == TestClassWithObservableObject.LastChangedPropertyName)
{
receivedDateTimeLocal = vm.LastChanged;
}
};
var now = DateTime.Now;
vm.LastChanged = now;
Assert.AreEqual(now, vm.LastChanged);
Assert.AreEqual(now, receivedDateTimeLocal);
}
[TestMethod]
public void TestPropertyChangedNoMagicString()
{
var receivedDateTimeLocal = DateTime.MinValue;
var vm = new TestClassWithObservableObject();
vm.PropertyChanged += (s, e) =>
{
if (e.PropertyName == "LastChangedNoMagicString")
{
receivedDateTimeLocal = vm.LastChangedNoMagicString;
}
};
var now = DateTime.Now;
vm.LastChangedNoMagicString = now;
Assert.AreEqual(now, vm.LastChangedNoMagicString);
Assert.AreEqual(now, receivedDateTimeLocal);
}
[TestMethod]
#if !NET40
#if DEBUG
[ExpectedException(typeof(ArgumentException))]
#endif
#else
// For some reason the VS test adapter throws exception even in Release mode
[ExpectedException(typeof(ArgumentException))]
#endif
public void TestRaisePropertyChangedValidInvalidPropertyName()
{
var vm = new TestClassWithObservableObject();
var receivedPropertyChanged = false;
var invalidPropertyNameReceived = false;
vm.PropertyChanged += (s, e) =>
{
if (e.PropertyName == TestClassWithObservableObject.LastChangedPropertyName)
{
receivedPropertyChanged = true;
}
else
{
invalidPropertyNameReceived = true;
}
};
vm.RaisePropertyChangedPublic(TestClassWithObservableObject.LastChangedPropertyName);
Assert.IsTrue(receivedPropertyChanged);
Assert.IsFalse(invalidPropertyNameReceived);
vm.RaisePropertyChangedPublic(TestClassWithObservableObject.LastChangedPropertyName + "1");
Assert.IsTrue(invalidPropertyNameReceived);
}
[TestMethod]
public void TestSet()
{
var vm = new TestClassWithObservableObject();
const int expectedValue = 1234;
var receivedValue = 0;
vm.PropertyChanged += (s, e) =>
{
if (e.PropertyName == TestClassWithObservableObject.PropertyWithSetPropertyName)
{
receivedValue = expectedValue;
}
};
vm.PropertyWithSet = expectedValue;
Assert.AreEqual(expectedValue, receivedValue);
}
[TestMethod]
public void TestSetWithString()
{
var vm = new TestClassWithObservableObject();
const int expectedValue = 1234;
var receivedValue = 0;
vm.PropertyChanged += (s, e) =>
{
if (e.PropertyName == TestClassWithObservableObject.PropertyWithStringSetPropertyName)
{
receivedValue = expectedValue;
}
};
vm.PropertyWithStringSet = expectedValue;
Assert.AreEqual(expectedValue, receivedValue);
}
[TestMethod]
public void TestReturnValueWithSet()
{
var vm = new TestClassWithObservableObject();
const int firstValue = 1234;
var receivedValue = 0;
vm.PropertyChanged += (s, e) =>
{
if (e.PropertyName == TestClassWithObservableObject.PropertyWithSetPropertyName)
{
receivedValue = vm.PropertyWithSet;
}
};
vm.PropertyWithSet = firstValue;
Assert.AreEqual(firstValue, receivedValue);
Assert.IsTrue(vm.SetRaisedPropertyChangedEvent);
vm.PropertyWithSet = firstValue;
Assert.AreEqual(firstValue, receivedValue);
Assert.IsFalse(vm.SetRaisedPropertyChangedEvent);
vm.PropertyWithSet = firstValue + 1;
Assert.AreEqual(firstValue + 1, receivedValue);
Assert.IsTrue(vm.SetRaisedPropertyChangedEvent);
}
[TestMethod]
public void TestReturnValueWithSetWithString()
{
var vm = new TestClassWithObservableObject();
const int firstValue = 1234;
var receivedValue = 0;
vm.PropertyChanged += (s, e) =>
{
if (e.PropertyName == TestClassWithObservableObject.PropertyWithStringSetPropertyName)
{
receivedValue = vm.PropertyWithStringSet;
}
};
vm.PropertyWithStringSet = firstValue;
Assert.AreEqual(firstValue, receivedValue);
Assert.IsTrue(vm.SetRaisedPropertyChangedEvent);
vm.PropertyWithStringSet = firstValue;
Assert.AreEqual(firstValue, receivedValue);
Assert.IsFalse(vm.SetRaisedPropertyChangedEvent);
vm.PropertyWithStringSet = firstValue + 1;
Assert.AreEqual(firstValue + 1, receivedValue);
Assert.IsTrue(vm.SetRaisedPropertyChangedEvent);
}
}
}

Просмотреть файл

@ -0,0 +1,207 @@
using System;
using GalaSoft.MvvmLight.Test.ViewModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace GalaSoft.MvvmLight.Test
{
[TestClass]
public class ObservableObjectPropertyChangingTest
{
[TestMethod]
public void TestPropertyChangingNoBroadcast()
{
var receivedDateTimeLocal = DateTime.MinValue;
var vm = new TestClassWithObservableObject();
vm.PropertyChanged += (s, e) =>
{
if (e.PropertyName == TestClassWithObservableObject.LastChangedPropertyName)
{
receivedDateTimeLocal = vm.LastChanged;
}
};
var receivedPropertyChanging = false;
vm.PropertyChanging += (s, e) =>
{
if (e.PropertyName == TestClassWithObservableObject.LastChangedPropertyName)
{
Assert.AreEqual(DateTime.MinValue, receivedDateTimeLocal);
Assert.AreEqual(DateTime.MinValue, vm.LastChanged);
receivedPropertyChanging = true;
}
};
var now = DateTime.Now;
vm.LastChanged = now;
Assert.AreEqual(now, vm.LastChanged);
Assert.AreEqual(now, receivedDateTimeLocal);
Assert.IsTrue(receivedPropertyChanging);
}
[TestMethod]
public void TestPropertyChangingNoMagicString()
{
var receivedDateTimeLocal = DateTime.MinValue;
var vm = new TestClassWithObservableObject();
vm.PropertyChanged += (s, e) =>
{
if (e.PropertyName == "LastChangedNoMagicString")
{
receivedDateTimeLocal = vm.LastChangedNoMagicString;
}
};
var receivedPropertyChanging = false;
vm.PropertyChanging += (s, e) =>
{
if (e.PropertyName == "LastChangedNoMagicString")
{
Assert.AreEqual(DateTime.MinValue, receivedDateTimeLocal);
Assert.AreEqual(DateTime.MinValue, vm.LastChangedNoMagicString);
receivedPropertyChanging = true;
}
};
var now = DateTime.Now;
vm.LastChangedNoMagicString = now;
Assert.AreEqual(now, vm.LastChangedNoMagicString);
Assert.AreEqual(now, receivedDateTimeLocal);
Assert.IsTrue(receivedPropertyChanging);
}
[TestMethod]
#if !NET40
#if DEBUG
[ExpectedException(typeof(ArgumentException))]
#endif
#else
// For some reason the VS test adapter throws exception even in Release mode
[ExpectedException(typeof(ArgumentException))]
#endif
public void TestRaisePropertyChangingValidInvalidPropertyName()
{
var vm = new TestClassWithObservableObject();
var receivedPropertyChanging = false;
var invalidPropertyNameReceived = false;
vm.PropertyChanging += (s, e) =>
{
if (e.PropertyName == TestClassWithObservableObject.LastChangedPropertyName)
{
receivedPropertyChanging = true;
}
else
{
invalidPropertyNameReceived = true;
}
};
vm.RaisePropertyChangingPublic(TestClassWithObservableObject.LastChangedPropertyName);
Assert.IsTrue(receivedPropertyChanging);
Assert.IsFalse(invalidPropertyNameReceived);
vm.RaisePropertyChangingPublic(TestClassWithObservableObject.LastChangedPropertyName + "1");
Assert.IsTrue(invalidPropertyNameReceived);
}
[TestMethod]
public void TestPropertyChangingWithSet()
{
var instance = new TestClassWithObservableObject();
const int value1 = 1234;
const int value2 = 5678;
instance.PropertyWithSet = value1;
var changingWasRaised = false;
var changedWasRaised = false;
instance.PropertyChanging += (s, e) =>
{
if (e.PropertyName != TestClassWithObservableObject.PropertyWithSetPropertyName)
{
return;
}
var sender = (TestClassWithObservableObject)s;
Assert.AreSame(instance, sender);
Assert.AreEqual(value1, instance.PropertyWithSet);
changingWasRaised = true;
};
instance.PropertyChanged += (s, e) =>
{
if (e.PropertyName != TestClassWithObservableObject.PropertyWithSetPropertyName)
{
return;
}
var sender = (TestClassWithObservableObject)s;
Assert.AreSame(instance, sender);
Assert.AreEqual(value2, instance.PropertyWithSet);
changedWasRaised = true;
};
instance.PropertyWithSet = value2;
Assert.IsTrue(changingWasRaised);
Assert.IsTrue(changedWasRaised);
}
[TestMethod]
public void TestPropertyChangingWithStringWithSet()
{
var instance = new TestClassWithObservableObject();
const int value1 = 1234;
const int value2 = 5678;
instance.PropertyWithStringSet = value1;
var changingWasRaised = false;
var changedWasRaised = false;
instance.PropertyChanging += (s, e) =>
{
if (e.PropertyName != TestClassWithObservableObject.PropertyWithStringSetPropertyName)
{
return;
}
var sender = (TestClassWithObservableObject)s;
Assert.AreSame(instance, sender);
Assert.AreEqual(value1, instance.PropertyWithStringSet);
changingWasRaised = true;
};
instance.PropertyChanged += (s, e) =>
{
if (e.PropertyName != TestClassWithObservableObject.PropertyWithStringSetPropertyName)
{
return;
}
var sender = (TestClassWithObservableObject)s;
Assert.AreSame(instance, sender);
Assert.AreEqual(value2, instance.PropertyWithStringSet);
changedWasRaised = true;
};
instance.PropertyWithStringSet = value2;
Assert.IsTrue(changingWasRaised);
Assert.IsTrue(changedWasRaised);
}
}
}

Просмотреть файл

@ -29,6 +29,7 @@ namespace GalaSoft.MvvmLight.Test.ViewModel
return;
}
RaisePropertyChanging(LastChangedPropertyName);
_lastChanged = value;
RaisePropertyChanged(LastChangedPropertyName);
}
@ -54,11 +55,18 @@ namespace GalaSoft.MvvmLight.Test.ViewModel
return;
}
RaisePropertyChanging(() => LastChangedNoMagicString);
_lastChangedNoMagicString = value;
RaisePropertyChanged(() => LastChangedNoMagicString);
}
}
public bool SetRaisedPropertyChangedEvent
{
get;
set;
}
public const string PropertyWithSetPropertyName = "PropertyWithSet";
private int _propertyWithSet;
public int PropertyWithSet
@ -69,7 +77,8 @@ namespace GalaSoft.MvvmLight.Test.ViewModel
}
set
{
Set(() => PropertyWithSet, ref _propertyWithSet, value);
SetRaisedPropertyChangedEvent = false;
SetRaisedPropertyChangedEvent = Set(() => PropertyWithSet, ref _propertyWithSet, value);
}
}
@ -83,7 +92,8 @@ namespace GalaSoft.MvvmLight.Test.ViewModel
}
set
{
Set(PropertyWithStringSetPropertyName, ref _propertyWithStringSet, value);
SetRaisedPropertyChangedEvent = false;
SetRaisedPropertyChangedEvent = Set(PropertyWithStringSetPropertyName, ref _propertyWithStringSet, value);
}
}
@ -91,5 +101,10 @@ namespace GalaSoft.MvvmLight.Test.ViewModel
{
RaisePropertyChanged(propertyName);
}
public void RaisePropertyChangingPublic(string propertyName)
{
RaisePropertyChanging(propertyName);
}
}
}

Просмотреть файл

@ -1,11 +1,12 @@
using System;
using System.ComponentModel;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Test.Stubs;
using GalaSoft.MvvmLight.Test.ViewModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
#if WIN8
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml;
#else
using System.Windows.Controls;
@ -26,21 +27,21 @@ namespace GalaSoft.MvvmLight.Test
var vm = new TestViewModel();
Messenger.Default.Register<string>(vm, vm.HandleStringMessage);
const string Content1 = "Hello world";
const string Content2 = "Another message";
const string content1 = "Hello world";
const string content2 = "Another message";
Messenger.Default.Send(Content1);
Messenger.Default.Send(content1);
Assert.AreEqual(Content1, vm.ReceivedContent);
Assert.AreEqual(content1, vm.ReceivedContent);
var cleanupVm = vm as ICleanup;
cleanupVm.Cleanup();
Assert.IsTrue(vm.CleanupWasCalled);
Messenger.Default.Send(Content2);
Messenger.Default.Send(content2);
Assert.AreEqual(Content1, vm.ReceivedContent);
Assert.AreEqual(content1, vm.ReceivedContent);
}
[TestMethod]
@ -216,8 +217,13 @@ namespace GalaSoft.MvvmLight.Test
}
[TestMethod]
#if !NET40
#if DEBUG
[ExpectedException(typeof(ArgumentException))]
#endif
#else
// For some reason the VS10 .NET4 test adapter throws exception even in Release mode
[ExpectedException(typeof(ArgumentException))]
#endif
public void TestRaiseValidInvalidPropertyName()
{
@ -407,5 +413,128 @@ namespace GalaSoft.MvvmLight.Test
Assert.AreEqual(expectedValue, receivedValue);
Assert.AreEqual(0, receivedValueWithMessenger);
}
static void InstancePropertyChanged(object sender, PropertyChangedEventArgs e)
{
// Void
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void TestRaisePropertyChangedNoMagicStringNullExpression()
{
var instance = new TestViewModelNoMagicString();
instance.PropertyChanged += InstancePropertyChanged;
instance.RaisePropertyChangedPublic<string>(null);
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void TestRaisePropertyChangedNoMagicStringNullExpressionWithBroadcast()
{
var instance = new TestViewModelNoMagicString();
instance.PropertyChanged += InstancePropertyChanged;
instance.RaisePropertyChangedPublic(null, "12", "34", true);
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void TestRaisePropertyChangedNoMagicStringNonPropertyExpression()
{
var instance = new TestViewModelNoMagicString();
instance.PropertyChanged += InstancePropertyChanged;
instance.RaisePropertyChangedPublic(() => DummyStringMethod());
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void TestRaisePropertyChangedNoMagicStringNonPropertyExpressionWithBroadcast()
{
var instance = new TestViewModelNoMagicString();
instance.PropertyChanged += InstancePropertyChanged;
instance.RaisePropertyChangedPublic(() => DummyStringMethod(), "12", "34", true);
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void TestRaisePropertyChangingNoMagicStringNullExpression()
{
var instance = new TestViewModelNoMagicString();
instance.PropertyChanging += InstancePropertyChanging;
instance.RaisePropertyChangingPublic<string>(null);
}
private static void InstancePropertyChanging(object sender, PropertyChangingEventArgs e)
{
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void TestRaisePropertyChangingNoMagicStringNonPropertyExpression()
{
var instance = new TestViewModelNoMagicString();
instance.PropertyChanging += InstancePropertyChanging;
instance.RaisePropertyChangingPublic(() => DummyStringMethod());
}
private static string DummyStringMethod()
{
return string.Empty;
}
#if !SILVERLIGHT
#if !WIN8
[TestMethod]
public void TestTypeDescriptor()
{
var instance = new TestCustomTypeDescriptor();
var argumentExceptionWasThrown = false;
try
{
instance.RaisePropertyChangedPublic(
TestCustomTypeDescriptor.TestPropertyPropertyName);
}
catch (ArgumentException)
{
argumentExceptionWasThrown = true;
}
Assert.IsFalse(argumentExceptionWasThrown);
try
{
instance.RaisePropertyChangedPublic(
TestCustomTypeDescriptor.TestPropertyPropertyName + TestCustomTypeDescriptor.PropertyNameSuffix);
}
catch (ArgumentException)
{
argumentExceptionWasThrown = true;
}
Assert.IsFalse(argumentExceptionWasThrown);
try
{
instance.RaisePropertyChangedPublic(
TestCustomTypeDescriptor.TestPropertyPropertyName + "abcd");
}
catch (ArgumentException)
{
argumentExceptionWasThrown = true;
}
#if NET40
Assert.IsTrue(argumentExceptionWasThrown);
#else
#if DEBUG
Assert.IsTrue(argumentExceptionWasThrown);
#else
Assert.IsFalse(argumentExceptionWasThrown);
#endif
#endif
}
#endif
#endif
}
}