-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
DeviceUnitTests.cs
145 lines (121 loc) · 5.05 KB
/
DeviceUnitTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.Threading.Tasks;
using NUnit.Framework;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms.Core.UnitTests
{
[TestFixture]
public class DeviceUnitTests : BaseTestFixture
{
[Test]
public void TestBeginInvokeOnMainThread()
{
bool calledFromMainThread = false;
Device.PlatformServices = MockPlatformServices(() => calledFromMainThread = true);
bool invoked = false;
Device.BeginInvokeOnMainThread(() => invoked = true);
Assert.True(invoked, "Action not invoked.");
Assert.True(calledFromMainThread, "Action not invoked from main thread.");
}
[Test]
public async Task TestInvokeOnMainThreadWithSyncFunc()
{
bool calledFromMainThread = false;
Device.PlatformServices = MockPlatformServices(() => calledFromMainThread = true);
bool invoked = false;
var result = await Device.InvokeOnMainThreadAsync(() => { invoked = true; return true; });
Assert.True(invoked, "Action not invoked.");
Assert.True(calledFromMainThread, "Action not invoked from main thread.");
Assert.True(result, "Unexpected result.");
}
[Test]
public async Task TestInvokeOnMainThreadWithSyncAction()
{
bool calledFromMainThread = false;
Device.PlatformServices = MockPlatformServices(() => calledFromMainThread = true);
bool invoked = false;
await Device.InvokeOnMainThreadAsync(() => { invoked = true; });
Assert.True(invoked, "Action not invoked.");
Assert.True(calledFromMainThread, "Action not invoked from main thread.");
}
[Test]
public async Task TestInvokeOnMainThreadWithAsyncFunc()
{
bool calledFromMainThread = false;
Device.PlatformServices = MockPlatformServices(() => calledFromMainThread = true,
invokeOnMainThread: action => Task.Delay(50).ContinueWith(_ => action()));
bool invoked = false;
var task = Device.InvokeOnMainThreadAsync(async () => { invoked = true; return true; });
Assert.True(calledFromMainThread, "Action not invoked from main thread.");
Assert.False(invoked, "Action invoked early.");
var result = await task;
Assert.True(invoked, "Action not invoked.");
Assert.True(result, "Unexpected result.");
}
[Test]
public async Task TestInvokeOnMainThreadWithAsyncFuncError()
{
bool calledFromMainThread = false;
Device.PlatformServices = MockPlatformServices(() => calledFromMainThread = true,
invokeOnMainThread: action => Task.Delay(50).ContinueWith(_ => action()));
bool invoked = false;
async Task<bool> boom() { invoked = true; throw new ApplicationException(); }
var task = Device.InvokeOnMainThreadAsync(boom);
Assert.True(calledFromMainThread, "Action not invoked from main thread.");
Assert.False(invoked, "Action invoked early.");
async Task MethodThatThrows() => await task;
Assert.ThrowsAsync<ApplicationException>(MethodThatThrows);
Assert.True(invoked, "Action not invoked.");
}
[Test]
public async Task TestInvokeOnMainThreadWithAsyncAction()
{
bool calledFromMainThread = false;
Device.PlatformServices = MockPlatformServices(() => calledFromMainThread = true,
invokeOnMainThread: action => Task.Delay(50).ContinueWith(_ => action()));
bool invoked = false;
var task = Device.InvokeOnMainThreadAsync(async () => { invoked = true; });
Assert.True(calledFromMainThread, "Action not invoked from main thread.");
Assert.False(invoked, "Action invoked early.");
await task;
Assert.True(invoked, "Action not invoked.");
}
[Test]
public async Task TestInvokeOnMainThreadWithAsyncActionError()
{
bool calledFromMainThread = false;
Device.PlatformServices = MockPlatformServices(() => calledFromMainThread = true,
invokeOnMainThread: action => Task.Delay(50).ContinueWith(_ => action()));
bool invoked = false;
async Task boom() { invoked = true; throw new ApplicationException(); }
var task = Device.InvokeOnMainThreadAsync(boom);
Assert.True(calledFromMainThread, "Action not invoked from main thread.");
Assert.False(invoked, "Action invoked early.");
async Task MethodThatThrows() => await task;
Assert.ThrowsAsync<ApplicationException>(MethodThatThrows);
Assert.True(invoked, "Action not invoked.");
}
[Test]
public void InvokeOnMainThreadThrowsWhenNull()
{
Device.PlatformServices = null;
Assert.Throws<InvalidOperationException>(() => Device.BeginInvokeOnMainThread(() => { }));
Assert.Throws<InvalidOperationException>(() => Device.InvokeOnMainThreadAsync(() => { }).Wait(100));
Assert.Throws<InvalidOperationException>(() => Device.InvokeOnMainThreadAsync(() => true).Wait(100));
Assert.Throws<InvalidOperationException>(() => Device.InvokeOnMainThreadAsync(async () => { }).Wait(100));
Assert.Throws<InvalidOperationException>(() => Device.InvokeOnMainThreadAsync(async () => true).Wait(100));
}
private IPlatformServices MockPlatformServices(Action onInvokeOnMainThread, Action<Action> invokeOnMainThread = null)
{
return new MockPlatformServices(
invokeOnMainThread: action =>
{
onInvokeOnMainThread();
if (invokeOnMainThread == null)
action();
else
invokeOnMainThread(action);
});
}
}
}