bovender framework
C# framework that implements MVVM and more
ProcessViewModelBaseTest.cs
1 /* ProcessViewModelBaseTest.cs
2  * part of Bovender framework
3  *
4  * Copyright 2014-2018 Daniel Kraus
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 using System;
19 using System.Collections.Generic;
20 using System.Linq;
21 using System.Text;
22 using NUnit.Framework;
24 using System.Threading;
25 
26 namespace Bovender.UnitTests.Mvvm
27 {
28  [TestFixture]
30  {
31  [SetUp]
32  public void Setup()
33  {
34  SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
35  Logging.LogFile.Default.EnableDebugLogging();
36  _model = new ProcessModelForTesting();
37  _viewModel = new ProcessViewModelForTesting(_model);
38  _messageContent = null;
39  }
40 
41  [Test]
42  public void ShowProgress()
43  {
44  bool showProgressWasSent = false;
45  bool completed = false;
46  _viewModel.ProcessFinishedMessage.Sent += (s, a) =>
47  {
48  Logger.Info("ProcessFinishedMessage was sent");
49  completed = true;
50  };
51  _viewModel.ShowProgressMessage.Sent += (sender, args) =>
52  {
53  Logger.Info("ShowProgressMessage was sent");
54  _messageContent = args.Content;
55  showProgressWasSent = true;
56  };
57  bool abort = false;
58  Timer t = new Timer((obj) =>
59  {
60  if (_messageContent != null && _messageContent.Processing)
61  {
62  Logger.Info("aborting...");
63  abort = true;
64  }
65  }, null, 5000, Timeout.Infinite);
66  _viewModel.StartProcess();
67  while (!completed && !abort) ;
68  t.Dispose();
69  if (abort)
70  {
71  Logger.Info("Cancelling...");
72  _viewModel.CancelProcess();
73  }
74  Logger.Info("Asserting...");
75  Assert.IsFalse(abort, "Task was aborted");
76  Assert.IsTrue(_model.Duration > 1500, "Process took less than 1.5 seconds - increase faculty loop?");
77  Assert.IsTrue(showProgressWasSent, "ShowProgress message should have been sent");
78  Assert.IsTrue(_messageContent.WasSuccessful, "WasSuccessful should be true");
79  Assert.IsNull(_messageContent.Exception, "Exception should be null");
80  }
81 
82  [Test]
83  public void ProcessException()
84  {
85  bool showProgressWasSent = false;
86  bool completed = false;
87  _model = new ExceptionProcessModelForTesting();
88  _viewModel = new ProcessViewModelForTesting(_model);
89  _viewModel.ProcessFinishedMessage.Sent += (s, a) =>
90  {
91  Logger.Info("ProcessFinishedMessage was sent");
92  completed = true;
93  };
94  _viewModel.ShowProgressMessage.Sent += (sender, args) =>
95  {
96  Logger.Info("ShowProgressMessage was sent");
97  _messageContent = args.Content;
98  showProgressWasSent = true;
99  };
100  bool abort = false;
101  Timer t = new Timer((obj) =>
102  {
103  if (_messageContent.Processing)
104  {
105  Logger.Info("aborting...");
106  abort = true;
107  }
108  }, null, 5000, Timeout.Infinite);
109  _viewModel.StartProcess();
110  while (!completed && !abort) ;
111  t.Dispose();
112  if (abort)
113  {
114  Logger.Info("Cancelling...");
115  _viewModel.CancelProcess();
116  }
117  Logger.Info("Asserting...");
118  Assert.IsFalse(abort, "Task was aborted");
119  Assert.IsTrue(_model.Duration > 1500, "Process took less than 1.5 seconds - increase faculty loop?");
120  Assert.IsTrue(showProgressWasSent, "ShowProgress message should have been sent");
121  Assert.IsFalse(_messageContent.WasSuccessful, "WasSuccessful should be false");
122  Assert.IsInstanceOf<ExceptionForTestingPurposes>(_messageContent.Exception, "Exception was not handed over.");
123  }
124 
125  [Test]
126  public void CancelProcessViaViewModel()
127  {
128  bool completed = false;
129  _viewModel.ProcessFinishedMessage.Sent += (s, a) =>
130  {
131  Logger.Info("ProcessFinishedMessage was sent");
132  completed = true;
133  };
134  _viewModel.ShowProgressMessage.Sent += (sender, args) =>
135  {
136  Logger.Info("ShowProgressMessage was sent");
137  _messageContent = args.Content;
138  };
139  bool abort = false;
140  Timer t = new Timer((obj) =>
141  {
142  if (_messageContent.Processing)
143  {
144  Logger.Info("aborting...");
145  abort = true;
146  }
147  }, null, 5000, Timeout.Infinite);
148  Timer abortTimer = new Timer((obj) =>
149  {
150  Logger.Info("Now simulating abort...");
151  _viewModel.CancelProcess();
152  }, null, 1000, Timeout.Infinite);
153  _viewModel.StartProcess();
154  while (!completed && !abort) ;
155  t.Dispose();
156  if (abort)
157  {
158  Logger.Info("Cancelling...");
159  _viewModel.CancelProcess();
160  }
161  abortTimer.Dispose();
162  Logger.Info("Asserting...");
163  Assert.IsFalse(abort, "Task was aborted");
164  Assert.IsTrue(_model.Duration < 1300, "Process took more than 1.3 seconds - was not aborted?");
165  Assert.IsTrue(_messageContent.WasCancelled, "WasCancelled should be true");
166  Assert.IsNull(_messageContent.Exception, "Exception should be null");
167  }
168 
169  [Test]
170  public void CancelProcessViaMessageContent()
171  {
172  bool completed = false;
173  _viewModel.ProcessFinishedMessage.Sent += (s, a) =>
174  {
175  Logger.Info("ProcessFinishedMessage was sent");
176  completed = true;
177  };
178  _viewModel.ShowProgressMessage.Sent += (sender, args) =>
179  {
180  Logger.Info("ShowProgressMessage was sent");
181  _messageContent = args.Content;
182  };
183  bool abort = false;
184  Timer t = new Timer((obj) =>
185  {
186  if (_messageContent != null && _messageContent.Processing)
187  {
188  Logger.Info("aborting...");
189  abort = true;
190  }
191  }, null, 5000, 2000);
192  Timer abortTimer = new Timer((obj) =>
193  {
194  if (_messageContent != null)
195  {
196  Logger.Info("Now simulating cancellation...");
197  _messageContent.CancelCommand.Execute(null);
198  }
199  }, null, 1200, 100);
200  _viewModel.StartProcess();
201  while (!completed && !abort) ;
202  t.Dispose();
203  if (abort)
204  {
205  Logger.Info("Cancelling...");
206  _viewModel.CancelProcess();
207  }
208  abortTimer.Dispose();
209  Logger.Info("Asserting...");
210  Assert.IsFalse(abort, "Task was aborted");
211  Assert.IsTrue(_model.Duration < 1500, "Process took more than 1.5 seconds - was not aborted?");
212  Assert.IsTrue(_messageContent.WasCancelled, "WasCancelled should be true");
213  Assert.IsNull(_messageContent.Exception, "Exception should be null");
214  }
215 
216  #region Private fields
217 
218  ProcessModelForTesting _model;
219  ProcessViewModelForTesting _viewModel;
221 
222  #endregion
223 
224  #region Class logger
225 
226  private static NLog.Logger Logger { get { return _logger.Value; } }
227 
228  private static readonly Lazy<NLog.Logger> _logger = new Lazy<NLog.Logger>(() => NLog.LogManager.GetCurrentClassLogger());
229 
230  #endregion
231  }
232 }
Holds information about percent completion of a process and defines events that occur when the proces...