Skip to content

Commit

Permalink
Adding ViewModelBase and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
brentedwards committed Dec 13, 2010
1 parent 52db865 commit f065290
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions BrentEdwards.MVVM.Test/BrentEdwards.MVVM.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<Compile Include="Navigation\ViewResultTests.cs" />
<Compile Include="Navigation\ViewTargetsTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ViewModelBaseTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BrentEdwards.MVVM\BrentEdwards.MVVM.csproj">
Expand Down
43 changes: 43 additions & 0 deletions BrentEdwards.MVVM.Test/ViewModelBaseTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.ComponentModel;

namespace BrentEdwards.MVVM.Test
{
[TestClass]
public sealed class ViewModelBaseTests
{
private class MockViewModel : ViewModelBase
{
public void TriggerPropertyChanged(string propertyName)
{
NotifyPropertyChanged(propertyName);
}
}

private List<string> _propertiesChanged;
private void PropertyChanged(object sender, PropertyChangedEventArgs e)
{
_propertiesChanged.Add(e.PropertyName);
}

[TestMethod]
public void NotifyPropertyChanged()
{
_propertiesChanged = new List<string>();

var viewModel = new MockViewModel();
var propertyName = Guid.NewGuid().ToString();

viewModel.PropertyChanged += PropertyChanged;
viewModel.TriggerPropertyChanged(propertyName);
viewModel.PropertyChanged -= PropertyChanged;

Assert.AreEqual(1, _propertiesChanged.Count);
Assert.IsTrue(_propertiesChanged.Contains(propertyName));
}
}
}
1 change: 1 addition & 0 deletions BrentEdwards.MVVM/BrentEdwards.MVVM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<Compile Include="Navigation\ViewResult.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Navigation\ViewTargets.cs" />
<Compile Include="ViewModelBase.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
22 changes: 22 additions & 0 deletions BrentEdwards.MVVM/ViewModelBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace BrentEdwards.MVVM
{
public abstract class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

protected void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null)
{
var args = new PropertyChangedEventArgs(propertyName);
PropertyChanged(this, args);
}
}
}
}

0 comments on commit f065290

Please sign in to comment.