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 ;
9+ using System . Threading . Tasks ;
10+ using Microsoft . Toolkit . Uwp ;
11+ using Microsoft . VisualStudio . TestTools . UnitTesting ;
12+
13+ namespace UnitTests . UI
14+ {
15+ [ TestClass ]
16+ public class Test_IncrementalLoadingCollection
17+ {
18+ private const int PageSize = 20 ;
19+ private const int Pages = 5 ;
20+
21+ private static readonly DataSource < int > . PageOperation [ ] FailPassSequence
22+ = new DataSource < int > . PageOperation [ ]
23+ {
24+ DataSource < int > . ThrowException , DataSource < int > . PassThrough ,
25+ DataSource < int > . ThrowException , DataSource < int > . PassThrough ,
26+ DataSource < int > . ThrowException , DataSource < int > . PassThrough ,
27+ DataSource < int > . ThrowException , DataSource < int > . PassThrough ,
28+ DataSource < int > . ThrowException , DataSource < int > . PassThrough ,
29+ } ;
30+
31+ private static readonly int [ ] AllData
32+ = Enumerable . Range ( 0 , Pages * PageSize ) . ToArray ( ) ;
33+
34+ [ DataRow ]
35+ [ DataRow ( 2500 , 1000 , 1000 , 1000 , 1000 ) ]
36+ [ TestMethod ]
37+ public async Task Requests ( params int [ ] pageDelays )
38+ {
39+ var source = new DataSource < int > ( AllData , pageDelays . Select ( DataSource < int > . MakeDelayOp ) ) ;
40+ var collection = new IncrementalLoadingCollection < DataSource < int > , int > ( source , PageSize ) ;
41+
42+ for ( int pageNum = 1 ; pageNum <= Pages ; pageNum ++ )
43+ {
44+ await collection . LoadMoreItemsAsync ( 0 ) ;
45+ CollectionAssert . AreEqual ( Enumerable . Range ( 0 , PageSize * pageNum ) . ToArray ( ) , collection ) ;
46+ }
47+ }
48+
49+ [ DataRow ]
50+ [ DataRow ( 2500 , 1000 , 1000 , 1000 , 1000 ) ]
51+ [ TestMethod ]
52+ public async Task RequestsAsync ( params int [ ] pageDelays )
53+ {
54+ var source = new DataSource < int > ( AllData , pageDelays . Select ( DataSource < int > . MakeDelayOp ) ) ;
55+ var collection = new IncrementalLoadingCollection < DataSource < int > , int > ( source , PageSize ) ;
56+
57+ var requests = new List < Task > ( ) ;
58+
59+ for ( int pageNum = 1 ; pageNum <= Pages ; pageNum ++ )
60+ {
61+ requests . Add ( collection . LoadMoreItemsAsync ( 0 ) . AsTask ( )
62+ . ContinueWith ( t => Assert . IsTrue ( t . IsCompletedSuccessfully ) ) ) ;
63+ }
64+
65+ await Task . WhenAll ( requests ) ;
66+
67+ CollectionAssert . AreEqual ( AllData , collection ) ;
68+ }
69+
70+ [ TestMethod ]
71+ public async Task FirstRequestFails ( )
72+ {
73+ var source = new DataSource < int > ( AllData , DataSource < int > . ThrowException ) ;
74+ var collection = new IncrementalLoadingCollection < DataSource < int > , int > ( source , PageSize ) ;
75+
76+ await Assert . ThrowsExceptionAsync < AggregateException > ( collection . LoadMoreItemsAsync ( 0 ) . AsTask ) ;
77+
78+ Assert . IsTrue ( ! collection . Any ( ) ) ;
79+
80+ var requests = new List < Task > ( ) ;
81+
82+ for ( int pageNum = 1 ; pageNum <= Pages ; pageNum ++ )
83+ {
84+ requests . Add ( collection . LoadMoreItemsAsync ( 0 ) . AsTask ( )
85+ . ContinueWith ( t => Assert . IsTrue ( t . IsCompletedSuccessfully ) ) ) ;
86+ }
87+
88+ await Task . WhenAll ( requests ) ;
89+
90+ CollectionAssert . AreEqual ( AllData , collection ) ;
91+ }
92+
93+ [ TestMethod ]
94+ public async Task EveryOtherRequestFails ( )
95+ {
96+ var source = new DataSource < int > ( AllData , FailPassSequence ) ;
97+ var collection = new IncrementalLoadingCollection < DataSource < int > , int > ( source , PageSize ) ;
98+
99+ var willFail = true ;
100+ for ( int submitedRequests = 0 ; submitedRequests < Pages * 2 ; submitedRequests ++ )
101+ {
102+ if ( willFail )
103+ {
104+ await Assert . ThrowsExceptionAsync < AggregateException > ( collection . LoadMoreItemsAsync ( 0 ) . AsTask ) ;
105+ }
106+ else
107+ {
108+ await collection . LoadMoreItemsAsync ( 0 ) ;
109+ }
110+
111+ willFail = ! willFail ;
112+ }
113+
114+ CollectionAssert . AreEqual ( AllData , collection ) ;
115+ }
116+
117+ [ TestMethod ]
118+ public async Task EveryOtherRequestFailsAsync ( )
119+ {
120+ var source = new DataSource < int > ( AllData , FailPassSequence ) ;
121+ var collection = new IncrementalLoadingCollection < DataSource < int > , int > ( source , PageSize ) ;
122+
123+ var requests = new List < Task > ( ) ;
124+
125+ var willFail = true ;
126+ for ( int submitedRequests = 0 ; submitedRequests < Pages * 2 ; submitedRequests ++ )
127+ {
128+ if ( willFail )
129+ {
130+ requests . Add ( Assert . ThrowsExceptionAsync < AggregateException > ( collection . LoadMoreItemsAsync ( 0 ) . AsTask ) ) ;
131+ }
132+ else
133+ {
134+ requests . Add ( collection . LoadMoreItemsAsync ( 0 ) . AsTask ( ) . ContinueWith ( t => Assert . IsTrue ( t . IsCompletedSuccessfully ) ) ) ;
135+ }
136+
137+ willFail = ! willFail ;
138+ }
139+
140+ await Task . WhenAll ( requests ) ;
141+
142+ CollectionAssert . AreEqual ( AllData , collection ) ;
143+ }
144+ }
145+ }
0 commit comments