Skip to content

Commit c7f5ca0

Browse files
committed
Added ViewModelTest
1 parent cabd55b commit c7f5ca0

File tree

5 files changed

+106
-18
lines changed

5 files changed

+106
-18
lines changed

ConcurrentProgramming.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ViewModel", "ViewModel\View
1919
EndProject
2020
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ModelTest", "ModelTest\ModelTest.csproj", "{657654E1-24E6-441D-A97E-61BD723DA688}"
2121
EndProject
22+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ViewModelTest", "ViewModelTest\ViewModelTest.csproj", "{645ED22D-4434-4FAA-AEC8-09B5B5144C3D}"
23+
EndProject
2224
Global
2325
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2426
Debug|Any CPU = Debug|Any CPU
@@ -57,6 +59,10 @@ Global
5759
{657654E1-24E6-441D-A97E-61BD723DA688}.Debug|Any CPU.Build.0 = Debug|Any CPU
5860
{657654E1-24E6-441D-A97E-61BD723DA688}.Release|Any CPU.ActiveCfg = Release|Any CPU
5961
{657654E1-24E6-441D-A97E-61BD723DA688}.Release|Any CPU.Build.0 = Release|Any CPU
62+
{645ED22D-4434-4FAA-AEC8-09B5B5144C3D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
63+
{645ED22D-4434-4FAA-AEC8-09B5B5144C3D}.Debug|Any CPU.Build.0 = Debug|Any CPU
64+
{645ED22D-4434-4FAA-AEC8-09B5B5144C3D}.Release|Any CPU.ActiveCfg = Release|Any CPU
65+
{645ED22D-4434-4FAA-AEC8-09B5B5144C3D}.Release|Any CPU.Build.0 = Release|Any CPU
6066
EndGlobalSection
6167
GlobalSection(SolutionProperties) = preSolution
6268
HideSolutionNode = FALSE

ViewModel/MainWindowViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ public int viewWidth
6363
get { return _width; }
6464
}
6565

66-
private async void CreateHandler()
66+
public async void CreateHandler()
6767
{
6868
ballsGroup = _modelLayer.createBalls(_ballsAmount, 25);
6969
await Task.Run(() => _modelLayer.StartSimulation());
7070
}
7171

72-
private void StopHandler()
72+
public void StopHandler()
7373
{
7474
_modelLayer.StopSimulation();
7575
}

ViewModelTest/UnitTest1.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

ViewModelTest/ViewModelTest.cs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using Logic;
2+
using Model;
3+
using System.Collections.ObjectModel;
4+
using ViewModel;
5+
6+
namespace ViewModelTest
7+
{
8+
public class MainWindowViewModelTests
9+
{
10+
private class ModelMockClass : ModelAbstractAPI
11+
{
12+
public int StartSimulationCallCount { get; set; }
13+
public int StopSimulationCallCount { get; set; }
14+
public int CreateBallsCallCount { get; set; }
15+
public override int Width => 500;
16+
public override int Height => 500;
17+
18+
public override ObservableCollection<BallLogic> createBalls(int amount, int radius)
19+
{
20+
CreateBallsCallCount++;
21+
return new ObservableCollection<BallLogic>();
22+
}
23+
24+
public override void StartSimulation()
25+
{
26+
StartSimulationCallCount++;
27+
}
28+
29+
public override void StopSimulation()
30+
{
31+
StopSimulationCallCount++;
32+
}
33+
}
34+
35+
[Test]
36+
public void CreateHandler_StartsSimulationTest()
37+
{
38+
var mockModel = new ModelMockClass();
39+
var viewModel = new MainWindowViewModel(mockModel);
40+
viewModel.ballsAmount = 5;
41+
42+
viewModel.CreateHandler();
43+
44+
Assert.AreEqual(1, mockModel.CreateBallsCallCount);
45+
Assert.AreEqual(1, mockModel.StartSimulationCallCount);
46+
}
47+
48+
[Test]
49+
public void StopHandler_StopsSimulationTest()
50+
{
51+
var mockModel = new ModelMockClass();
52+
var viewModel = new MainWindowViewModel(mockModel);
53+
54+
viewModel.StopHandler();
55+
56+
Assert.AreEqual(1, mockModel.StopSimulationCallCount);
57+
}
58+
59+
[Test]
60+
public void BallsGroup_Setter_RaisesPropertyChangedEventTest()
61+
{
62+
var mockModel = new ModelMockClass();
63+
var viewModel = new MainWindowViewModel(mockModel);
64+
65+
bool eventRaised = false;
66+
viewModel.PropertyChanged += (sender, args) =>
67+
{
68+
if (args.PropertyName == "ballsGroup")
69+
eventRaised = true;
70+
};
71+
72+
viewModel.ballsGroup = new ObservableCollection<BallLogic>();
73+
Assert.IsTrue(eventRaised);
74+
}
75+
76+
[Test]
77+
public void BallsAmount_Setter_RaisesPropertyChangedEventTest()
78+
{
79+
var mockModel = new ModelMockClass();
80+
var viewModel = new MainWindowViewModel(mockModel);
81+
82+
bool eventRaised = false;
83+
viewModel.PropertyChanged += (sender, args) =>
84+
{
85+
if (args.PropertyName == "ballsAmount")
86+
eventRaised = true;
87+
};
88+
89+
viewModel.ballsAmount = 10;
90+
Assert.IsTrue(eventRaised);
91+
}
92+
}
93+
}

ViewModelTest/ViewModelTest.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
1818
</ItemGroup>
1919

20+
<ItemGroup>
21+
<ProjectReference Include="..\Model\Model.csproj" />
22+
<ProjectReference Include="..\ViewModel\ViewModel.csproj" />
23+
</ItemGroup>
24+
2025
<ItemGroup>
2126
<Using Include="NUnit.Framework" />
2227
</ItemGroup>

0 commit comments

Comments
 (0)