Skip to content

Commit

Permalink
WPF + UnitTest added
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnemaf committed Aug 3, 2020
1 parent 46778d6 commit ec952f4
Show file tree
Hide file tree
Showing 16 changed files with 340 additions and 22 deletions.
23 changes: 13 additions & 10 deletions MvvmDemo/MvvmDemo.csproj → MvvmDemo/MvvmDemoUWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,10 @@
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
</Compile>
<Compile Include="Messages\AsyncYesNoMessage.cs" />
<Compile Include="Services\DebugLogger.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Views\Pages\MainPage.xaml.cs">
<DependentUpon>MainPage.xaml</DependentUpon>
</Compile>
<Compile Include="Models\Employee.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\ILogger.cs" />
<Compile Include="ViewModels\MainViewModel.cs" />
</ItemGroup>
<ItemGroup>
<AppxManifest Include="Package.appxmanifest">
Expand All @@ -150,10 +145,6 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Page Include="Views\Pages\MainPage.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
Expand All @@ -163,6 +154,18 @@
<Version>7.0.0-build.149</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MvvmDemoLibrary\MvvmDemoLibrary.csproj">
<Project>{18f24147-d592-4735-b3ec-bb0415b0d5dd}</Project>
<Name>MvvmDemoLibrary</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Page Include="Views\Pages\MainPage.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
</ItemGroup>
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">
<VisualStudioVersion>14.0</VisualStudioVersion>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using Microsoft.Toolkit.Mvvm.Messaging.Messages;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Popups;

namespace MvvmDemo.Messages {
public sealed class AsyncYesNoMessage : AsyncRequestMessage<bool> {
Expand Down
File renamed without changes.
11 changes: 11 additions & 0 deletions MvvmDemoLibrary/MvvmDemoLibrary.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Toolkit.Mvvm" Version="7.0.0-build.149" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Diagnostics;

namespace MvvmDemo.Services {
class DebugLogger : ILogger {
public class DebugLogger : ILogger {
public void Log(string message) {
Debug.WriteLine(message);
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@
using MvvmDemo.Models;
using MvvmDemo.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel.Resources.Core;

namespace MvvmDemo.ViewModels {
public class MainViewModel {
Expand Down
20 changes: 20 additions & 0 deletions MvvmDemoTest/MvvmDemoTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MvvmDemoLibrary\MvvmDemoLibrary.csproj" />
</ItemGroup>

</Project>
77 changes: 77 additions & 0 deletions MvvmDemoTest/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using Microsoft.Toolkit.Mvvm.Messaging;
using MvvmDemo.Messages;
using MvvmDemo.Services;
using MvvmDemo.ViewModels;
using System;
using Xunit;

namespace MvvmDemoTest {

public class MainViewModelTest {

[Fact]
public void DeleteYes() {
// Arrange
var vm = new MainViewModel(new DebugLogger());

Messenger.Default.Register<AsyncYesNoMessage>(this, m => {
m.Reply(true);
});

// Act
vm.DeleteCommand.Execute(vm.Employees[1]);

Messenger.Default.Unregister<AsyncYesNoMessage>(this);

// Assert
Assert.Equal(2, vm.Employees.Count);
}

[Fact]
public void RaiseSalary() {
// Arrange
var vm = new MainViewModel(new DebugLogger());
var emp = vm.Employees[1];
var oldSal = emp.Salary;

// Act
vm.RaiseSalaryCommand.Execute(emp);

// Assert
Assert.Equal(oldSal + 100, emp.Salary);
}

[Fact]
public void RaiseSalaryAboveLimit() {
// Arrange
var vm = new MainViewModel(new DebugLogger());
var emp = vm.Employees[1];
emp.Salary = 6000;
var oldSal = emp.Salary;

// Act
vm.RaiseSalaryCommand.Execute(emp);

// Assert
Assert.Equal(oldSal, emp.Salary);
}

[Fact]
public void DeleteNo() {
// Arrange
var vm = new MainViewModel(new DebugLogger());

Messenger.Default.Register<AsyncYesNoMessage>(this, m => {
m.Reply(false);
});

// Act
vm.DeleteCommand.Execute(vm.Employees[1]);

Messenger.Default.Unregister<AsyncYesNoMessage>(this);

// Assert
Assert.Equal(3, vm.Employees.Count);
}
}
}
9 changes: 9 additions & 0 deletions MvvmDemoWpf/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Application x:Class="MvvmDemoWpf.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MvvmDemoWpf"
StartupUri="Views/Windows/MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
39 changes: 39 additions & 0 deletions MvvmDemoWpf/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Toolkit.Mvvm.DependencyInjection;
using Microsoft.Toolkit.Mvvm.Messaging;
using MvvmDemo.Messages;
using MvvmDemo.Services;
using MvvmDemo.ViewModels;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace MvvmDemoWpf {
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application {

public App() {
Ioc.Default.ConfigureServices(services => {
services.AddSingleton<ILogger, DebugLogger>();
services.AddSingleton<MainViewModel>();
});

Messenger.Default.Register<AsyncYesNoMessage>(this, m => {

Task<bool> GetResult() {
var result = MessageBox.Show(m.Text, "Confirm", MessageBoxButton.YesNo );
return Task.FromResult(result == MessageBoxResult.Yes);
}

m.Reply(GetResult());
});
}

}
}
10 changes: 10 additions & 0 deletions MvvmDemoWpf/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Windows;

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
13 changes: 13 additions & 0 deletions MvvmDemoWpf/MvvmDemoWPF.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UseWPF>true</UseWPF>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\MvvmDemoLibrary\MvvmDemoLibrary.csproj" />
</ItemGroup>

</Project>
44 changes: 44 additions & 0 deletions MvvmDemoWpf/Views/Windows/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<Window x:Class="MvvmDemoWpf.Views.Windows.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MvvmDemoWpf"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:MvvmDemo.ViewModels;assembly=MvvmDemoLibrary"
Title="MainWindow"
Width="800"
FontSize="20"
Height="450"
DataContext="{x:Static vm:MainViewModel.Current}"
mc:Ignorable="d">
<Grid Margin="4">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<Button Command="{Binding RaiseSalaryCommand, Mode=OneTime}"
CommandParameter="{Binding ElementName=listViewEmployees, Path=SelectedItem, Mode=OneWay}"
Margin="4,0"
Content="Raise Salary" />
<Button Command="{Binding DeleteCommand, Mode=OneTime}"
CommandParameter="{Binding ElementName=listViewEmployees, Path=SelectedItem, Mode=OneWay}"
Content="Delete" />
</StackPanel>
<ListBox x:Name="listViewEmployees"
Margin="4"
SelectionChanged="ListViewEmployees_SelectionChanged"
Grid.Row="1"
ItemsSource="{Binding Employees, Mode=OneTime}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,4">
<TextBlock FontWeight="Bold"
Text="{Binding Name, Mode=OneTime}" />
<TextBlock Text="{Binding Salary, Mode=OneWay}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
32 changes: 32 additions & 0 deletions MvvmDemoWpf/Views/Windows/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using MvvmDemo.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MvvmDemoWpf.Views.Windows {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}

private void ListViewEmployees_SelectionChanged(object sender, SelectionChangedEventArgs e) {
var vm = MainViewModel.Current;
vm.RaiseSalaryCommand.NotifyCanExecuteChanged();
vm.DeleteCommand.NotifyCanExecuteChanged();
}
}
}
Loading

0 comments on commit ec952f4

Please sign in to comment.