Click here to Skip to main content
15,880,427 members
Articles / Programming Languages / C# 4.0

Unit Testing a Silverlight View Model Style Modal Popup

Rate me:
Please Sign up or sign in to vote.
4.76/5 (9 votes)
6 May 2010Ms-PL3 min read 53.4K   756   22  
An example of a View Model Style Modal Popup Unit Test.
  • mvvmunittestexample.zip
    • MVVMPopUp.sln
    • MVVMPopUp.Web
    • MVVMPopUp
      • App.xaml
      • App.xaml.cs
      • Bin
        • Debug
          • AppManifest.xaml
          • ar
            • System.Windows.Controls.resources.dll
          • bg
            • System.Windows.Controls.resources.dll
          • ca
            • System.Windows.Controls.resources.dll
          • cs
            • System.Windows.Controls.resources.dll
          • da
            • System.Windows.Controls.resources.dll
          • de
            • System.Windows.Controls.resources.dll
          • el
            • System.Windows.Controls.resources.dll
          • es
            • System.Windows.Controls.resources.dll
          • et
            • System.Windows.Controls.resources.dll
          • eu
            • System.Windows.Controls.resources.dll
          • fi
            • System.Windows.Controls.resources.dll
          • fr
            • System.Windows.Controls.resources.dll
          • he
            • System.Windows.Controls.resources.dll
          • hr
            • System.Windows.Controls.resources.dll
          • hu
            • System.Windows.Controls.resources.dll
          • id
            • System.Windows.Controls.resources.dll
          • it
            • System.Windows.Controls.resources.dll
          • ja
            • System.Windows.Controls.resources.dll
          • ko
            • System.Windows.Controls.resources.dll
          • lt
            • System.Windows.Controls.resources.dll
          • lv
            • System.Windows.Controls.resources.dll
          • Microsoft.Expression.Interactions.dll
          • Microsoft.Expression.Interactions.xml
          • ms
            • System.Windows.Controls.resources.dll
          • MVVMPopUp.dll
          • MVVMPopUp.xap
          • MVVMPopUpTestPage.html
          • nl
            • System.Windows.Controls.resources.dll
          • no
            • System.Windows.Controls.resources.dll
          • pl
            • System.Windows.Controls.resources.dll
          • pt
            • System.Windows.Controls.resources.dll
          • pt-BR
            • System.Windows.Controls.resources.dll
          • ro
            • System.Windows.Controls.resources.dll
          • ru
            • System.Windows.Controls.resources.dll
          • sk
            • System.Windows.Controls.resources.dll
          • sl
            • System.Windows.Controls.resources.dll
          • sr-Cyrl-CS
            • System.Windows.Controls.resources.dll
          • sr-Latn-CS
            • System.Windows.Controls.resources.dll
          • sv
            • System.Windows.Controls.resources.dll
          • System.Windows.Controls.dll
          • System.Windows.Controls.xml
          • System.Windows.Interactivity.dll
          • System.Windows.Interactivity.xml
          • th
            • System.Windows.Controls.resources.dll
          • tr
            • System.Windows.Controls.resources.dll
          • uk
            • System.Windows.Controls.resources.dll
          • vi
            • System.Windows.Controls.resources.dll
          • zh-Hans
            • System.Windows.Controls.resources.dll
          • zh-Hant
            • System.Windows.Controls.resources.dll
        • Release
      • Classes
      • Converters
      • MainPage.xaml
      • MainPage.xaml.cs
      • MVVMPopUp.csproj
      • MVVMPopUp.csproj.user
      • PopUps
      • Properties
      • ViewModel
    • SilverlightTest1
      • App.xaml
      • App.xaml.cs
      • Bin
        • Debug
          • AppManifest.xaml
          • ar
            • System.Windows.Controls.resources.dll
          • bg
            • System.Windows.Controls.resources.dll
          • ca
            • System.Windows.Controls.resources.dll
          • cs
            • System.Windows.Controls.resources.dll
          • da
            • System.Windows.Controls.resources.dll
          • de
            • System.Windows.Controls.resources.dll
          • el
            • System.Windows.Controls.resources.dll
          • es
            • System.Windows.Controls.resources.dll
          • et
            • System.Windows.Controls.resources.dll
          • eu
            • System.Windows.Controls.resources.dll
          • fi
            • System.Windows.Controls.resources.dll
          • fr
            • System.Windows.Controls.resources.dll
          • he
            • System.Windows.Controls.resources.dll
          • hr
            • System.Windows.Controls.resources.dll
          • hu
            • System.Windows.Controls.resources.dll
          • id
            • System.Windows.Controls.resources.dll
          • it
            • System.Windows.Controls.resources.dll
          • ja
            • System.Windows.Controls.resources.dll
          • ko
            • System.Windows.Controls.resources.dll
          • lt
            • System.Windows.Controls.resources.dll
          • lv
            • System.Windows.Controls.resources.dll
          • Microsoft.Expression.Interactions.dll
          • Microsoft.Expression.Interactions.xml
          • Microsoft.Silverlight.Testing.dll
          • Microsoft.Silverlight.Testing.xml
          • Microsoft.VisualStudio.QualityTools.UnitTesting.Silverlight.dll
          • Microsoft.VisualStudio.QualityTools.UnitTesting.Silverlight.xml
          • ms
            • System.Windows.Controls.resources.dll
          • MVVMPopUp.dll
          • nl
            • System.Windows.Controls.resources.dll
          • no
            • System.Windows.Controls.resources.dll
          • pl
            • System.Windows.Controls.resources.dll
          • pt
            • System.Windows.Controls.resources.dll
          • pt-BR
            • System.Windows.Controls.resources.dll
          • ro
            • System.Windows.Controls.resources.dll
          • ru
            • System.Windows.Controls.resources.dll
          • SilverlightTest1.dll
          • SilverlightTest1.xap
          • sk
            • System.Windows.Controls.resources.dll
          • sl
            • System.Windows.Controls.resources.dll
          • sr-Cyrl-CS
            • System.Windows.Controls.resources.dll
          • sr-Latn-CS
            • System.Windows.Controls.resources.dll
          • sv
            • System.Windows.Controls.resources.dll
          • System.Windows.Controls.dll
          • System.Windows.Controls.xml
          • System.Windows.Interactivity.dll
          • System.Windows.Interactivity.xml
          • TestPage.html
          • th
            • System.Windows.Controls.resources.dll
          • tr
            • System.Windows.Controls.resources.dll
          • uk
            • System.Windows.Controls.resources.dll
          • vi
            • System.Windows.Controls.resources.dll
          • zh-Hans
            • System.Windows.Controls.resources.dll
          • zh-Hant
            • System.Windows.Controls.resources.dll
      • Properties
      • SilverlightTest1.csproj
      • SilverlightTest1.csproj.user
      • Tests.cs
// Copyright (c) 2010
// by OpenLight Group
// http://openlightgroup.net/
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
//
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.

using System;
using System.ComponentModel;
using System.Windows.Input;
using System.Windows.Controls;

namespace MVVMPopUp
{
    public class PopUpViewModel : INotifyPropertyChanged
    {
        private ChildWindow PopUP;

        public PopUpViewModel()
        {
            // Set the command property
            SetPopUpCommand = new DelegateCommand(SetPopUp, CanSetPopUp);
            OKButtonCommand = new DelegateCommand(OKButton, CanOKButton);
            CancelButtonCommand = new DelegateCommand(CancelButton, CanCancelButton);
        }

        // Commands

        #region SetPopUpCommand
        public ICommand SetPopUpCommand { get; set; }
        public void SetPopUp(object param)
        {
            PopUP = (ChildWindow)param;
        }

        private bool CanSetPopUp(object param)
        {
            return true;
        }
        #endregion

        #region OKButtonCommand
        public ICommand OKButtonCommand { get; set; }
        public void OKButton(object param)
        {
            PopUP.DialogResult = true;
        }

        private bool CanOKButton(object param)
        {
            return true;
        }
        #endregion

        #region CancelButtonCommand
        public ICommand CancelButtonCommand { get; set; }
        public void CancelButton(object param)
        {
            PopUP.DialogResult = false;
        }

        private bool CanCancelButton(object param)
        {
            return true;
        }
        #endregion

        // Properties

        #region SelectedPopUpValueProperty
        private string _SelectedPopUpValueProperty;
        public string SelectedPopUpValueProperty
        {
            get
            {
                return this._SelectedPopUpValueProperty;
            }
            set
            {
                this._SelectedPopUpValueProperty = value;
                this.NotifyPropertyChanged("SelectedPopUpValueProperty");
            }
        }
        #endregion

        #region DefaultValueProperty
        private string _DefaultValueProperty;
        public string DefaultValueProperty
        {
            get
            {
                return this._DefaultValueProperty;
            }
            set
            {
                this._DefaultValueProperty = value;
                this.NotifyPropertyChanged("DefaultValueProperty");
                SelectedPopUpValueProperty = this._DefaultValueProperty;
            }
        }
        #endregion

        // Utility
		   
        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion        
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior) http://ADefWebserver.com
United States United States
Michael Washington is a Microsoft MVP. He is a ASP.NET and
C# programmer.
He is the founder of
AiHelpWebsite.com,
LightSwitchHelpWebsite.com, and
HoloLensHelpWebsite.com.

He has a son, Zachary and resides in Los Angeles with his wife Valerie.

He is the Author of:

Comments and Discussions