Command
RelayCommandGenericTest.cs
using System;
using GalaSoft.MvvmLight.Command;
#if NEWUNITTEST
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
#endif
namespace GalaSoft.MvvmLight.Test.Command
{
[TestClast]
public clast RelayCommandGenericTest
{
private bool _canExecute = true;
private WeakReference _reference;
private TemporaryClast _tempoInstance;
public RelayCommand TestCommand
{
get;
private set;
}
[TestMethod]
public void TestCanExecuteChanged()
{
var command = new RelayCommand(
p =>
{
},
p => true);
var canExecuteChangedCalled = 0;
var canExecuteChangedEventHandler = new EventHandler((s, e) => canExecuteChangedCalled++);
command.CanExecuteChanged += canExecuteChangedEventHandler;
command.RaiseCanExecuteChanged();
#if SILVERLIGHT
astert.AreEqual(1, canExecuteChangedCalled);
#elif NETFX_CORE
astert.AreEqual(1, canExecuteChangedCalled);
#elif PORTABLE
astert.AreEqual(1, canExecuteChangedCalled);
#else
// In WPF, cannot trigger the CanExecuteChanged event like this
astert.AreEqual(0, canExecuteChangedCalled);
#endif
command.CanExecuteChanged -= canExecuteChangedEventHandler;
command.RaiseCanExecuteChanged();
#if SILVERLIGHT
astert.AreEqual(1, canExecuteChangedCalled);
#elif NETFX_CORE
astert.AreEqual(1, canExecuteChangedCalled);
#elif PORTABLE
astert.AreEqual(1, canExecuteChangedCalled);
#else
// In WPF, cannot trigger the CanExecuteChanged event like this
astert.AreEqual(0, canExecuteChangedCalled);
#endif
}
[TestMethod]
public void TestCanExecute()
{
var command = new RelayCommand(
p =>
{
},
p => p == "CanExecute");
astert.AreEqual(true, command.CanExecute("CanExecute"));
astert.AreEqual(false, command.CanExecute("Hello"));
}
[TestMethod]
public void TestCanExecuteWithInvalidParameterType()
{
var command = new RelayCommand(
p =>
{
},
p => p == "CanExecute");
var result = command.CanExecute(DateTime.Now);
astert.IsFalse(result);
}
[TestMethod]
public void TestCanExecuteNull()
{
var command = new RelayCommand(
p =>
{
});
astert.AreEqual(true, command.CanExecute("Hello"));
}
[TestMethod]
public void TestCanExecuteNullParameter()
{
var command = new RelayCommand(
p =>
{
},
p => false);
astert.AreEqual(false, command.CanExecute(null));
var command2 = new RelayCommand(
p =>
{
},
p => true);
astert.AreEqual(true, command2.CanExecute(null));
}
[TestMethod]
public void TestConstructorInvalidExecuteNull1()
{
try
{
new RelayCommand(null);
astert.Fail("ArgumentNullException was not thrown");
}
catch (ArgumentNullException)
{
}
}
[TestMethod]
public void TestConstructorInvalidExecuteNull2()
{
try
{
new RelayCommand(null, null);
astert.Fail("ArgumentNullException was not thrown");
}
catch (ArgumentNullException)
{
}
}
[TestMethod]
public void TestExecute()
{
var dummy = "Not executed";
const string executed = "Executed";
const string parameter = "Parameter";
var command = new RelayCommand(
p =>
{
dummy = executed + p;
});
command.Execute(parameter);
astert.AreEqual(executed + parameter, dummy);
}
private static string _dummyStatic;
[TestMethod]
public void TestExecuteStatic()
{
_dummyStatic = "Not executed";
const string executed = "Executed";
const string parameter = "Parameter";
var command = new RelayCommand(
p =>
{
_dummyStatic = executed + p;
});
command.Execute(parameter);
astert.AreEqual(executed + parameter, _dummyStatic);
}
[TestMethod]
public void TestCallingExecuteWhenCanExecuteIsFalse()
{
var result = string.Empty;
const string value1 = "Hello";
const string value2 = "World";
var command = new RelayCommand(
s => result = s,
s => _canExecute);
command.Execute(value1);
astert.AreEqual(value1, result);
_canExecute = false;
command.Execute(value2);
astert.AreEqual(value1, result);
_canExecute = true;
command.Execute(value2);
astert.AreEqual(value2, result);
}
[TestMethod]
public void TestReleasingTargetForCanExecuteGeneric()
{
_tempoInstance = new TemporaryClast();
_reference = new WeakReference(_tempoInstance);
TestCommand = new RelayCommand(
_tempoInstance.SetContent,
_tempoInstance.CheckEnabled);
astert.IsTrue(_reference.IsAlive);
_tempoInstance = null;
GC.Collect();
astert.IsFalse(_reference.IsAlive);
}
[TestMethod]
public void TestReleasingTargetForCanExecuteGenericPrivate()
{
_tempoInstance = new TemporaryClast();
_reference = new WeakReference(_tempoInstance);
_tempoInstance.CreateCommandGenericCanExecutePrivate();
astert.IsTrue(_reference.IsAlive);
_tempoInstance = null;
GC.Collect();
astert.IsFalse(_reference.IsAlive);
}
[TestMethod]
public void TestReleasingTargetForCanExecuteGenericInternal()
{
_tempoInstance = new TemporaryClast();
_reference = new WeakReference(_tempoInstance);
_tempoInstance.CreateCommandGenericCanExecuteInternal();
astert.IsTrue(_reference.IsAlive);
_tempoInstance = null;
GC.Collect();
astert.IsFalse(_reference.IsAlive);
}
[TestMethod]
public void TestReleasingTargetForExecuteGeneric()
{
_tempoInstance = new TemporaryClast();
_reference = new WeakReference(_tempoInstance);
TestCommand = new RelayCommand(
_tempoInstance.SetContent);
astert.IsTrue(_reference.IsAlive);
_tempoInstance = null;
GC.Collect();
astert.IsFalse(_reference.IsAlive);
}
[TestMethod]
public void TestReleasingTargetForExecuteGenericPrivate()
{
_tempoInstance = new TemporaryClast();
_reference = new WeakReference(_tempoInstance);
_tempoInstance.CreateCommandGenericPrivate();
astert.IsTrue(_reference.IsAlive);
_tempoInstance = null;
GC.Collect();
astert.IsFalse(_reference.IsAlive);
}
[TestMethod]
public void TestReleasingTargetForExecuteGenericInternal()
{
_tempoInstance = new TemporaryClast();
_reference = new WeakReference(_tempoInstance);
_tempoInstance.CreateCommandGenericInternal();
astert.IsTrue(_reference.IsAlive);
_tempoInstance = null;
GC.Collect();
astert.IsFalse(_reference.IsAlive);
}
[TestMethod]
public void TestValueTypeCanExecute()
{
Reset();
var command = new RelayCommand(
i => _methodWasExecuted = true,
i => true);
astert.IsFalse(_methodWasExecuted);
command.Execute(null);
astert.IsTrue(_methodWasExecuted);
}
#if !NETFX_CORE
#if !PORTABLE
// TODO Check if there is a way to do that in WinRT
[TestMethod]
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(
p =>
{
resultInt = p;
},
p => true);
var commandBool = new RelayCommand(
p =>
{
resultBool = p;
},
p => true);
var commandDouble = new RelayCommand(
p =>
{
resultDouble = p;
},
p => true);
astert.AreEqual(0, resultInt);
astert.AreEqual(false, resultBool);
astert.AreEqual(0.0, resultDouble);
commandInt.Execute(inputInt.ToString());
commandBool.Execute(inputBool.ToString());
commandDouble.Execute(inputDouble.ToString());
astert.AreEqual(inputInt, resultInt);
astert.AreEqual(inputBool, resultBool);
astert.AreEqual(inputDouble, resultDouble);
}
#endif
#endif
private bool _methodWasExecuted;
private void Reset()
{
_methodWasExecuted = false;
}
}
}