Skip to content

Commit 75f7c65

Browse files
committed
Added unit tests for new DispatcherQueue extensions
1 parent 756dba9 commit 75f7c65

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed

UnitTests/UnitTests.UWP/Extensions/Test_DispatcherQueueExtensions.cs

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,5 +287,167 @@ public void Test_DispatcherQueueHelper_FuncOfTaskOfT_Exception()
287287
Assert.IsNotNull(task.Exception);
288288
Assert.IsInstanceOfType(task.Exception.InnerExceptions.FirstOrDefault(), typeof(ArgumentException));
289289
}
290+
291+
[TestCategory("DispatcherQueueExtensions")]
292+
[TestMethod]
293+
public async Task Test_DispatcherQueueExtensions_Stateful_TryEnqueueT1_Ok_NonUIThread()
294+
{
295+
TaskCompletionSource taskSource = new();
296+
297+
_ = CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
298+
CoreDispatcherPriority.Normal,
299+
async () =>
300+
{
301+
try
302+
{
303+
var dispatcherQueue = DispatcherQueue.GetForCurrentThread();
304+
305+
bool success = dispatcherQueue.TryEnqueue(x =>
306+
{
307+
Assert.AreEqual(x, nameof(Test_DispatcherQueueExtensions_Stateful_TryEnqueueT1_Ok_NonUIThread));
308+
309+
var textBlock = new TextBlock { Text = x };
310+
311+
Assert.AreEqual(textBlock.Text, x);
312+
313+
taskSource.SetResult();
314+
}, nameof(Test_DispatcherQueueExtensions_Stateful_TryEnqueueT1_Ok_NonUIThread));
315+
316+
if (!success)
317+
{
318+
taskSource.SetException(new Exception("Failed to enqueue task"));
319+
}
320+
}
321+
catch (Exception e)
322+
{
323+
taskSource.SetException(e);
324+
}
325+
});
326+
327+
return taskSource.Task;
328+
}
329+
330+
[TestCategory("DispatcherQueueExtensions")]
331+
[TestMethod]
332+
public async Task Test_DispatcherQueueExtensions_Stateful_TryEnqueueT1T2_Ok_NonUIThread()
333+
{
334+
TaskCompletionSource taskSource = new();
335+
336+
_ = CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
337+
CoreDispatcherPriority.Normal,
338+
async () =>
339+
{
340+
try
341+
{
342+
var dispatcherQueue = DispatcherQueue.GetForCurrentThread();
343+
344+
bool success = dispatcherQueue.TryEnqueue((x, tcs) =>
345+
{
346+
Assert.AreEqual(x, nameof(Test_DispatcherQueueExtensions_Stateful_TryEnqueueT1T2_Ok_NonUIThread));
347+
Assert.AreSame(tcs, taskSource);
348+
349+
var textBlock = new TextBlock { Text = x };
350+
351+
Assert.AreEqual(textBlock.Text, x);
352+
353+
tcs.SetResult();
354+
}, nameof(Test_DispatcherQueueExtensions_Stateful_TryEnqueueT1T2_Ok_NonUIThread), taskSource);
355+
356+
if (!success)
357+
{
358+
taskSource.SetException(new Exception("Failed to enqueue task"));
359+
}
360+
}
361+
catch (Exception e)
362+
{
363+
taskSource.SetException(e);
364+
}
365+
});
366+
367+
return taskSource.Task;
368+
}
369+
370+
[TestCategory("DispatcherQueueExtensions")]
371+
[TestMethod]
372+
public async Task Test_DispatcherQueueExtensions_Stateful_WithPriority_TryEnqueueT1_Ok_NonUIThread()
373+
{
374+
TaskCompletionSource taskSource = new();
375+
376+
_ = CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
377+
CoreDispatcherPriority.Normal,
378+
async () =>
379+
{
380+
try
381+
{
382+
var dispatcherQueue = DispatcherQueue.GetForCurrentThread();
383+
384+
bool success = dispatcherQueue.TryEnqueue(
385+
DispatcherQueuePriority.Normal,
386+
x =>
387+
{
388+
Assert.AreEqual(x, nameof(Test_DispatcherQueueExtensions_Stateful_WithPriority_TryEnqueueT1_Ok_NonUIThread));
389+
390+
var textBlock = new TextBlock { Text = x };
391+
392+
Assert.AreEqual(textBlock.Text, x);
393+
394+
taskSource.SetResult();
395+
}, nameof(Test_DispatcherQueueExtensions_Stateful_WithPriority_TryEnqueueT1_Ok_NonUIThread));
396+
397+
if (!success)
398+
{
399+
taskSource.SetException(new Exception("Failed to enqueue task"));
400+
}
401+
}
402+
catch (Exception e)
403+
{
404+
taskSource.SetException(e);
405+
}
406+
});
407+
408+
return taskSource.Task;
409+
}
410+
411+
[TestCategory("DispatcherQueueExtensions")]
412+
[TestMethod]
413+
public async Task Test_DispatcherQueueExtensions_Stateful_WithPriority_TryEnqueueT1T2_Ok_NonUIThread()
414+
{
415+
TaskCompletionSource taskSource = new();
416+
417+
_ = CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
418+
CoreDispatcherPriority.Normal,
419+
async () =>
420+
{
421+
try
422+
{
423+
var dispatcherQueue = DispatcherQueue.GetForCurrentThread();
424+
425+
bool success = dispatcherQueue.TryEnqueue(
426+
DispatcherQueuePriority.Normal,
427+
(x, tcs) =>
428+
{
429+
Assert.AreEqual(x, nameof(Test_DispatcherQueueExtensions_Stateful_WithPriority_TryEnqueueT1T2_Ok_NonUIThread));
430+
Assert.AreSame(tcs, taskSource);
431+
432+
var textBlock = new TextBlock { Text = x };
433+
434+
Assert.AreEqual(textBlock.Text, x);
435+
436+
tcs.SetResult();
437+
}, nameof(Test_DispatcherQueueExtensions_Stateful_WithPriority_TryEnqueueT1T2_Ok_NonUIThread), taskSource);
438+
439+
if (!success)
440+
{
441+
taskSource.SetException(new Exception("Failed to enqueue task"));
442+
}
443+
}
444+
catch (Exception e)
445+
{
446+
taskSource.SetException(e);
447+
}
448+
});
449+
450+
return taskSource.Task;
451+
}
290452
}
291453
}

0 commit comments

Comments
 (0)