-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
MarshalingObservableCollectionTests.cs
341 lines (274 loc) · 8.52 KB
/
MarshalingObservableCollectionTests.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms.Core.UnitTests
{
[TestFixture]
public class MarshalingObservableCollectionTests
{
MarshalingTestPlatformServices _services;
[SetUp]
public void Setup()
{
_services = new MarshalingTestPlatformServices();
Device.PlatformServices = _services;
_services.Start();
}
[TearDown]
public void TearDown()
{
_services.Stop();
_services = null;
}
[Test]
[Description("Added items don't show up until they've been processed on the UI thread")]
public async Task AddOffUIThread()
{
int insertCount = 0;
var countFromThreadPool = -1;
var source = new ObservableCollection<int>();
var moc = new MarshalingObservableCollection(source);
moc.CollectionChanged += (sender, args) => {
if (args.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
{
insertCount += 1;
}
};
// Add an item from a threadpool thread
await Task.Run(() =>
{
source.Add(1);
countFromThreadPool = moc.Count;
});
// Check the result on the main thread
var onMainThreadCount = await Device.InvokeOnMainThreadAsync<int>(() =>
{
return moc.Count;
});
Assert.That(countFromThreadPool, Is.EqualTo(0), "Count should be zero because the update on the UI thread hasn't run yet");
Assert.That(onMainThreadCount, Is.EqualTo(1), "Count should be 1 because the UI thread has updated");
Assert.That(insertCount, Is.EqualTo(1), "The CollectionChanged event should have fired with an Add exactly 1 time");
}
[Test]
[Description("Intial item count should match wrapped collection.")]
public async Task InitialItemCountsMatch()
{
var source = new ObservableCollection<int> { 1, 2 };
var moc = new MarshalingObservableCollection(source);
Assert.That(source.Count, Is.EqualTo(moc.Count));
}
[Test]
[Description("Clears don't show up until they've been processed on the UI thread")]
public async Task ClearOnUIThread()
{
var countFromThreadPool = -1;
var source = new ObservableCollection<int>
{
1,
2
};
var moc = new MarshalingObservableCollection(source);
// Call Clear from a threadpool thread
await Task.Run(() =>
{
source.Clear();
countFromThreadPool = moc.Count;
});
// Check the result on the main thread
var onMainThreadCount = await Device.InvokeOnMainThreadAsync<int>(() => moc.Count);
Assert.That(countFromThreadPool, Is.EqualTo(2), "Count should be pre-clear");
Assert.That(onMainThreadCount, Is.EqualTo(0), "Count should be zero because the Clear has been processed");
}
[Test]
[Description("A Reset should reflect the state at the time of the Reset")]
public async Task ClearAndAddOffUIThread()
{
var countFromThreadPool = -1;
var source = new ObservableCollection<int>
{
1,
2
};
var moc = new MarshalingObservableCollection(source);
// Call Clear from a threadpool thread
await Task.Run(() =>
{
source.Clear();
source.Add(4);
countFromThreadPool = moc.Count;
});
// Check the result on the main thread
var onMainThreadCount = await Device.InvokeOnMainThreadAsync<int>(() => moc.Count);
Assert.That(countFromThreadPool, Is.EqualTo(2), "Count should be pre-clear");
Assert.That(onMainThreadCount, Is.EqualTo(1), "Should have processed a Clear and an Add");
}
[Test]
[Description("Removed items are still there until they're removed on the UI thread")]
public async Task RemoveOffUIThread()
{
var countFromThreadPool = -1;
var source = new ObservableCollection<int> { 1, 2 };
var moc = new MarshalingObservableCollection(source);
// Call Clear from a threadpool thread
await Task.Run(() =>
{
source.Remove(1);
countFromThreadPool = moc.Count;
});
// Check the result on the main thread
var onMainThreadCount = await Device.InvokeOnMainThreadAsync<int>(() => moc.Count);
Assert.That(countFromThreadPool, Is.EqualTo(2), "Count should be pre-remove");
Assert.That(onMainThreadCount, Is.EqualTo(1), "Remove has now processed");
}
[Test]
[Description("Until the UI thread processes a change, the indexer should remain consistent")]
public async Task IndexerConsistent()
{
int itemFromThreadPool = -1;
var source = new ObservableCollection<int> { 1, 2 };
var moc = new MarshalingObservableCollection(source);
// Call Remove from a threadpool thread
await Task.Run(() =>
{
source.Remove(1);
itemFromThreadPool = (int)moc[1];
});
Assert.That(itemFromThreadPool, Is.EqualTo(2), "Should have indexer value from before remove");
}
[Test]
[Description("Don't show replacements until the UI thread has processed them")]
public async Task ReplaceOffUIThread()
{
int itemFromThreadPool = -1;
var source = new ObservableCollection<int> { 1, 2 };
var moc = new MarshalingObservableCollection(source);
// Replace a value from a threadpool thread
await Task.Run(() =>
{
source[0] = 42;
itemFromThreadPool = (int)moc[0];
});
// Check the result on the main thread
var onMainThreadValue = await Device.InvokeOnMainThreadAsync(() => moc[0]);
Assert.That(itemFromThreadPool, Is.EqualTo(1), "Should have value from before replace");
Assert.That(onMainThreadValue, Is.EqualTo(42), "Should have value from after replace");
}
[Test]
[Description("Don't show moves until the UI thread has processed them")]
public async Task MoveOffUIThread()
{
int itemFromThreadPool = -1;
var source = new ObservableCollection<int> { 1, 2 };
var moc = new MarshalingObservableCollection(source);
// Replace a value from a threadpool thread
await Task.Run(() =>
{
source.Move(1, 0);
itemFromThreadPool = (int)moc[0];
});
// Check the result on the main thread
var onMainThreadValue = await Device.InvokeOnMainThreadAsync(() => moc[0]);
Assert.That(itemFromThreadPool, Is.EqualTo(1), "Should have value from before move");
Assert.That(onMainThreadValue, Is.EqualTo(2), "Should have value from after move");
}
// This class simulates running a single UI thread with a queue and non-UI threads;
// this allows us to test IsInvokeRequired/BeginInvoke without having to be on an actual device
class MarshalingTestPlatformServices : IPlatformServices
{
int _threadId;
bool _running;
Queue<Action> _todo = new Queue<Action>();
public void Stop()
{
_running = false;
}
public void Start()
{
_running = true;
Task.Run(() => {
if (_threadId == 0)
{
_threadId = Thread.CurrentThread.ManagedThreadId;
}
while (_running)
{
try
{
Thread.Sleep(100);
while (_todo.Count > 0)
{
_todo.Dequeue().Invoke();
}
}
catch (Exception ex)
{
Stop();
}
}
});
}
public bool IsInvokeRequired => Thread.CurrentThread.ManagedThreadId != _threadId;
public void BeginInvokeOnMainThread(Action action)
{
_todo.Enqueue(action);
}
public OSAppTheme RequestedTheme { get; }
public string RuntimePlatform { get; }
public Ticker CreateTicker()
{
throw new NotImplementedException();
}
public Assembly[] GetAssemblies()
{
throw new NotImplementedException();
}
public Color GetNamedColor(string name)
{
throw new NotImplementedException();
}
public double GetNamedSize(NamedSize size, Type targetElementType, bool useOldSizes)
{
throw new NotImplementedException();
}
public SizeRequest GetNativeSize(VisualElement view, double widthConstraint, double heightConstraint)
{
throw new NotImplementedException();
}
public Task<Stream> GetStreamAsync(Uri uri, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public IIsolatedStorageFile GetUserStoreForApplication()
{
throw new NotImplementedException();
}
public void OpenUriAction(Uri uri)
{
throw new NotImplementedException();
}
public void QuitApplication()
{
throw new NotImplementedException();
}
public void StartTimer(TimeSpan interval, Func<bool> callback)
{
throw new NotImplementedException();
}
public string GetHash(string input)
{
throw new NotImplementedException();
}
public string GetMD5Hash(string input)
{
throw new NotImplementedException();
}
}
}
}