Skip to content

Commit 3d6b98f

Browse files
committed
Add syncronus and asyncronus tests for the incremtnetal loading collection
1 parent 10bc0ab commit 3d6b98f

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Microsoft.Toolkit.Collections;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
11+
namespace UnitTests.UI
12+
{
13+
public class DataSource<T> : IIncrementalSource<T>
14+
{
15+
private readonly IEnumerable<T> _data;
16+
17+
public DataSource(IEnumerable<T> items)
18+
{
19+
_data = items;
20+
}
21+
22+
public async Task<IEnumerable<T>> GetPagedItemsAsync(int pageIndex, int pageSize, CancellationToken cancellationToken = default(CancellationToken))
23+
{
24+
// Gets items from the collection according to pageIndex and pageSize parameters.
25+
var result = (from p in _data
26+
select p).Skip(pageIndex * pageSize).Take(pageSize);
27+
28+
// Simulates a longer request...
29+
// Make sure the list is still in order after a refresh,
30+
// even if the first page takes longer to load
31+
if (pageIndex == 0)
32+
{
33+
await Task.Delay(2500);
34+
}
35+
else
36+
{
37+
await Task.Delay(1000);
38+
}
39+
40+
return result;
41+
}
42+
}
43+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Threading.Tasks;
9+
using Microsoft.Toolkit.Uwp;
10+
using Microsoft.VisualStudio.TestTools.UnitTesting;
11+
12+
namespace UnitTests.UI
13+
{
14+
[TestClass]
15+
public class Test_IncrementalLoadingCollection
16+
{
17+
[TestMethod]
18+
public async Task SequentialRequests()
19+
{
20+
const int pageSize = 20;
21+
const int pages = 5;
22+
var collection = new IncrementalLoadingCollection<DataSource<int>, int>(new DataSource<int>(Enumerable.Range(0, pageSize * pages)), pageSize);
23+
24+
for (int pageNum = 1; pageNum <= pages; pageNum++)
25+
{
26+
var rez1 = await collection.LoadMoreItemsAsync(0);
27+
Assert.AreEqual((uint)pageSize, rez1.Count);
28+
CollectionAssert.AreEquivalent(Enumerable.Range(0, pageSize * pageNum).ToArray(), collection);
29+
}
30+
}
31+
32+
[TestMethod]
33+
public void ConcurentRequests()
34+
{
35+
const int pageSize = 20;
36+
const int pages = 5;
37+
var collection = new IncrementalLoadingCollection<DataSource<int>, int>(new DataSource<int>(Enumerable.Range(0, pageSize * pages)), pageSize);
38+
39+
var requests = new List<Task>();
40+
41+
for (int pageNum = 1; pageNum <= pages; pageNum++)
42+
{
43+
requests.Add(collection.LoadMoreItemsAsync(0).AsTask().ContinueWith(t =>
44+
{
45+
Assert.AreEqual(TaskStatus.RanToCompletion, t.Status);
46+
Assert.AreEqual((uint)pageSize, t.Result.Count);
47+
}));
48+
}
49+
50+
Task.WaitAll(requests.ToArray());
51+
CollectionAssert.AreEquivalent(Enumerable.Range(0, pageSize * pages).ToArray(), collection);
52+
}
53+
}
54+
}

UnitTests/UnitTests.UWP/UnitTests.UWP.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@
197197
<Compile Include="Properties\AssemblyInfo.cs" />
198198
<Compile Include="Helpers\Test_WeakEventListener.cs" />
199199
<Compile Include="UI\Animations\Test_AnimationBuilderStart.cs" />
200+
<Compile Include="UI\Collection\DataSource.cs" />
201+
<Compile Include="UI\Collection\Test_IncrementalLoadingCollection.cs" />
200202
<Compile Include="UI\Controls\Test_Carousel.cs" />
201203
<Compile Include="UI\Controls\Test_BladeView.cs" />
202204
<Compile Include="UI\Controls\Test_RadialGauge.cs" />
@@ -548,4 +550,4 @@
548550
<Target Name="AfterBuild">
549551
</Target>
550552
-->
551-
</Project>
553+
</Project>

0 commit comments

Comments
 (0)