2014-05-20 05:05:15 -07:00
using Moq ;
using NUnit.Framework ;
using Stylet ;
using System ;
using System.Collections.Generic ;
2014-05-20 13:24:10 -07:00
using System.Drawing ;
2014-05-20 05:05:15 -07:00
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
2014-05-20 13:24:10 -07:00
using System.Windows ;
2014-05-20 05:05:15 -07:00
namespace StyletUnitTests
{
[TestFixture]
public class MessageBoxTests
{
2014-05-20 13:24:10 -07:00
private class MyMessageBoxViewModel : MessageBoxViewModel
{
2014-05-21 04:04:19 -07:00
public new void OnViewLoaded ( )
2014-05-20 13:24:10 -07:00
{
base . OnViewLoaded ( ) ;
}
}
private MessageBoxViewModel vm ;
[SetUp]
public void SetUp ( )
{
this . vm = new MessageBoxViewModel ( ) ;
}
2014-05-20 05:05:15 -07:00
[Test]
public void ShowMessageBoxShowsMessageBox ( )
{
var vm = new Mock < IMessageBoxViewModel > ( ) ;
IoC . GetInstance = ( o , k ) = >
{
Assert . AreEqual ( typeof ( IMessageBoxViewModel ) , o ) ;
Assert . AreEqual ( null , k ) ;
return vm . Object ;
} ;
var windowManager = new Mock < IWindowManager > ( ) ;
2014-05-20 13:24:10 -07:00
vm . SetupGet ( x = > x . ClickedButton ) . Returns ( MessageBoxResult . OK ) ;
var result = MessageBoxWindowManagerExtensions . ShowMessageBox ( windowManager . Object , "this is the text" , "this is the title" , MessageBoxButton . OKCancel , MessageBoxImage . Exclamation , MessageBoxResult . OK , MessageBoxResult . Cancel ) ;
2014-05-20 05:05:15 -07:00
2014-05-21 03:48:48 -07:00
vm . Verify ( x = > x . Setup ( "this is the text" , "this is the title" , MessageBoxButton . OKCancel , MessageBoxImage . Exclamation , MessageBoxResult . OK , MessageBoxResult . Cancel , null ) ) ;
2014-05-20 05:05:15 -07:00
windowManager . Verify ( x = > x . ShowDialog ( vm . Object ) ) ;
2014-05-20 13:24:10 -07:00
Assert . AreEqual ( MessageBoxResult . OK , result ) ;
}
[Test]
public void SetsTextCorrectly ( )
{
2014-05-21 03:48:48 -07:00
this . vm . Setup ( "this is the text" , null , MessageBoxButton . OK , System . Windows . MessageBoxImage . None , MessageBoxResult . None , MessageBoxResult . None , null ) ;
2014-05-20 13:24:10 -07:00
Assert . AreEqual ( "this is the text" , this . vm . Text ) ;
}
[Test]
public void SetsTitleCorrectly ( )
{
2014-05-21 03:48:48 -07:00
this . vm . Setup ( null , "this is the title" , MessageBoxButton . OK , System . Windows . MessageBoxImage . None , MessageBoxResult . None , MessageBoxResult . None , null ) ;
2014-05-20 13:24:10 -07:00
Assert . AreEqual ( "this is the title" , this . vm . DisplayName ) ;
}
[Test]
public void DisplaysRequestedButtons ( )
{
2014-05-21 03:48:48 -07:00
this . vm . Setup ( null , null , MessageBoxButton . OKCancel , System . Windows . MessageBoxImage . None , MessageBoxResult . None , MessageBoxResult . None , null ) ;
2014-05-20 13:24:10 -07:00
var buttons = vm . ButtonList . ToList ( ) ;
Assert . AreEqual ( 2 , buttons . Count ) ;
Assert . AreEqual ( "OK" , buttons [ 0 ] . Label ) ;
Assert . AreEqual ( MessageBoxResult . OK , buttons [ 0 ] . Value ) ;
Assert . AreEqual ( "Cancel" , buttons [ 1 ] . Label ) ;
Assert . AreEqual ( MessageBoxResult . Cancel , buttons [ 1 ] . Value ) ;
}
[Test]
public void SetsDefaultButtonToTheRequestedButton ( )
{
2014-05-21 03:48:48 -07:00
this . vm . Setup ( null , null , MessageBoxButton . OKCancel , System . Windows . MessageBoxImage . None , MessageBoxResult . Cancel , MessageBoxResult . None , null ) ;
2014-05-20 13:24:10 -07:00
Assert . AreEqual ( this . vm . ButtonList . ElementAt ( 1 ) , this . vm . DefaultButton ) ;
}
[Test]
public void SetsDefaultToLeftmostButtonIfDefaultRequested ( )
{
2014-05-21 03:48:48 -07:00
this . vm . Setup ( null , null , MessageBoxButton . YesNoCancel , System . Windows . MessageBoxImage . None , MessageBoxResult . None , MessageBoxResult . None , null ) ;
2014-05-20 13:24:10 -07:00
Assert . AreEqual ( this . vm . ButtonList . ElementAt ( 0 ) , this . vm . DefaultButton ) ;
}
[Test]
public void ThrowsIfTheRequestedDefaultButtonIsNotDisplayed ( )
{
2014-05-21 03:48:48 -07:00
Assert . Throws < ArgumentException > ( ( ) = > this . vm . Setup ( null , null , MessageBoxButton . OKCancel , MessageBoxImage . None , MessageBoxResult . Yes , MessageBoxResult . None , null ) ) ;
2014-05-20 13:24:10 -07:00
}
[Test]
public void SetsCancelButtonToTheRequestedButton ( )
{
2014-05-21 03:48:48 -07:00
this . vm . Setup ( null , null , MessageBoxButton . YesNoCancel , MessageBoxImage . None , MessageBoxResult . None , MessageBoxResult . No , null ) ;
2014-05-20 13:24:10 -07:00
Assert . AreEqual ( this . vm . ButtonList . ElementAt ( 1 ) , this . vm . CancelButton ) ;
}
[Test]
public void SetsCancelToRighmostButtonIfDefaultRequested ( )
{
2014-05-21 03:48:48 -07:00
this . vm . Setup ( null , null , MessageBoxButton . OKCancel , MessageBoxImage . None , MessageBoxResult . None , MessageBoxResult . None , null ) ;
2014-05-20 13:24:10 -07:00
Assert . AreEqual ( this . vm . ButtonList . ElementAt ( 1 ) , this . vm . CancelButton ) ;
}
[Test]
public void ThrowsIfRequestedCancelButtonIsNotDisplayed ( )
{
2014-05-21 03:48:48 -07:00
Assert . Throws < ArgumentException > ( ( ) = > this . vm . Setup ( null , null , MessageBoxButton . OK , MessageBoxImage . None , MessageBoxResult . None , MessageBoxResult . No , null ) ) ;
2014-05-20 13:24:10 -07:00
}
[Test]
public void SetsIconCorrectly ( )
{
2014-05-21 03:48:48 -07:00
this . vm . Setup ( null , null , MessageBoxButton . OK , MessageBoxImage . Exclamation , MessageBoxResult . None , MessageBoxResult . None , null ) ;
2014-05-20 13:24:10 -07:00
Assert . AreEqual ( SystemIcons . Exclamation , this . vm . ImageIcon ) ;
}
[Test]
public void SetsResultAndClosesWhenButtonClicked ( )
{
var parent = new Mock < IChildDelegate > ( ) ;
this . vm . Parent = parent . Object ;
2014-05-21 03:48:48 -07:00
this . vm . Setup ( null , null , MessageBoxButton . OK , MessageBoxImage . None , MessageBoxResult . None , MessageBoxResult . None , null ) ;
2014-05-20 13:24:10 -07:00
this . vm . ButtonClicked ( MessageBoxResult . No ) ;
parent . Verify ( x = > x . CloseItem ( this . vm , true ) ) ;
Assert . AreEqual ( MessageBoxResult . No , this . vm . ClickedButton ) ;
}
[Test]
public void PlaysNoSoundIfNoSoundToPlay ( )
{
var vm = new MyMessageBoxViewModel ( ) ;
// Can't test it actually playing the sound
2014-05-21 03:48:48 -07:00
vm . Setup ( null , null , MessageBoxButton . OK , MessageBoxImage . None , MessageBoxResult . None , MessageBoxResult . None , null ) ;
2014-05-20 13:24:10 -07:00
Assert . DoesNotThrow ( ( ) = > vm . OnViewLoaded ( ) ) ;
2014-05-20 05:05:15 -07:00
}
2014-05-21 03:48:48 -07:00
[Test]
public void ButtonTextOverridesWork ( )
{
this . vm . Setup ( null , null , MessageBoxButton . OKCancel , MessageBoxImage . None , MessageBoxResult . None , MessageBoxResult . None , new Dictionary < MessageBoxResult , string > ( )
{
{ MessageBoxResult . Cancel , "YAY!" } ,
} ) ;
Assert . AreEqual ( "OK" , this . vm . ButtonList . ElementAt ( 0 ) . Label ) ;
Assert . AreEqual ( "YAY!" , this . vm . ButtonList . ElementAt ( 1 ) . Label ) ;
}
2014-05-20 05:05:15 -07:00
}
}