2
2
using System . IO ;
3
3
using System . Linq ;
4
4
using System . Reactive . Linq ;
5
+ using System . Reactive . Subjects ;
5
6
using System . Threading ;
6
7
using System . Windows . Media . Imaging ;
7
8
using GitHub . Collections ;
@@ -17,78 +18,79 @@ public class AccountModelTests : TestBaseClass
17
18
[ Fact ]
18
19
public void CopyFromDoesNotLoseAvatar ( )
19
20
{
20
- //A function that will return this image in an observable after X seconds
21
21
var userImage = AvatarProvider . CreateBitmapImage ( "pack://application:,,,/GitHub.App;component/Images/default_user_avatar.png" ) ;
22
22
var orgImage = AvatarProvider . CreateBitmapImage ( "pack://application:,,,/GitHub.App;component/Images/default_org_avatar.png" ) ;
23
23
24
- Func < int , BitmapImage , IObservable < BitmapImage > > generateObservable = ( seconds , image ) =>
25
- {
26
- return Observable . Generate (
27
- initialState : 0 ,
28
- condition : i => i < 1 ,
29
- iterate : state => state + 1 ,
30
- resultSelector : i => image ,
31
- timeSelector : i => TimeSpan . FromSeconds ( seconds ) ) ;
32
- } ;
24
+ var bitmapImageObservable = new Subject < BitmapImage > ( ) ;
33
25
34
- var evt = new ManualResetEvent ( false ) ;
26
+ var collectionEvent = new ManualResetEvent ( false ) ;
27
+ var avatarPropertyEvent = new ManualResetEvent ( false ) ;
35
28
36
29
//Creating an initial account with an observable that returns immediately
37
30
const string login = "foo" ;
38
31
const int initialOwnedPrivateRepositoryCount = 1 ;
39
32
40
- var initialAccount = new Account ( login , true , false , initialOwnedPrivateRepositoryCount , 0 , generateObservable ( 0 , userImage ) ) ;
33
+ var initialAccount = new Account ( login , true , false , initialOwnedPrivateRepositoryCount , 0 , bitmapImageObservable ) ;
41
34
42
35
//Creating the test collection
43
36
var col = new TrackingCollection < IAccount > ( Observable . Empty < IAccount > ( ) , OrderedComparer < IAccount > . OrderByDescending ( x => x . Login ) . Compare ) ;
44
37
col . Subscribe ( account =>
45
38
{
46
- evt . Set ( ) ;
39
+ collectionEvent . Set ( ) ;
47
40
} , ( ) => { } ) ;
48
41
49
42
//Adding that account to the TrackingCollection
50
43
col . AddItem ( initialAccount ) ;
51
44
52
45
//Waiting for the collection add the item
53
- evt . WaitOne ( ) ;
54
- evt . Reset ( ) ;
46
+ collectionEvent . WaitOne ( ) ;
47
+ collectionEvent . Reset ( ) ;
55
48
56
49
//Checking some initial properties
57
50
Assert . Equal ( login , col [ 0 ] . Login ) ;
58
51
Assert . Equal ( initialOwnedPrivateRepositoryCount , col [ 0 ] . OwnedPrivateRepos ) ;
59
52
60
- //Demonstrating that the avatar is present
61
- Assert . NotNull ( col [ 0 ] . Avatar ) ;
62
- Assert . True ( BitmapSourcesAreEqual ( col [ 0 ] . Avatar , userImage ) ) ;
63
- Assert . False ( BitmapSourcesAreEqual ( col [ 0 ] . Avatar , orgImage ) ) ;
64
-
65
- //Creating an observable that will return in one second
66
- var updatedBitmapSourceObservable = generateObservable ( 1 , orgImage ) ;
67
-
68
- //Creating an account update with an observable
69
- const int updatedOwnedPrivateRepositoryCount = 2 ;
70
-
71
- var updatedAccount = new Account ( login , true , false , updatedOwnedPrivateRepositoryCount , 0 , updatedBitmapSourceObservable ) ;
53
+ //Demonstrating that the avatar is not yet present
54
+ Assert . Null ( col [ 0 ] . Avatar ) ;
72
55
73
56
//Adding a listener to check for the changing of the Avatar property
74
57
initialAccount . Changed . Subscribe ( args =>
75
58
{
76
59
if ( args . PropertyName == "Avatar" )
77
60
{
78
- evt . Set ( ) ;
61
+ avatarPropertyEvent . Set ( ) ;
79
62
}
80
63
} ) ;
81
64
82
- //Updating the accout in the collection
65
+ //Providing the first avatar
66
+ bitmapImageObservable . OnNext ( userImage ) ;
67
+
68
+ //Waiting for the avatar to be added
69
+ avatarPropertyEvent . WaitOne ( ) ;
70
+ avatarPropertyEvent . Reset ( ) ;
71
+
72
+ //Demonstrating that the avatar is present
73
+ Assert . NotNull ( col [ 0 ] . Avatar ) ;
74
+ Assert . True ( BitmapSourcesAreEqual ( col [ 0 ] . Avatar , userImage ) ) ;
75
+ Assert . False ( BitmapSourcesAreEqual ( col [ 0 ] . Avatar , orgImage ) ) ;
76
+
77
+ //Creating an account update
78
+ const int updatedOwnedPrivateRepositoryCount = 2 ;
79
+ var updatedAccount = new Account ( login , true , false , updatedOwnedPrivateRepositoryCount , 0 , bitmapImageObservable ) ;
80
+
81
+ //Updating the account in the collection
83
82
col . AddItem ( updatedAccount ) ;
84
83
85
84
//Waiting for the collection to process the update
86
- evt . WaitOne ( ) ;
87
- evt . Reset ( ) ;
85
+ collectionEvent . WaitOne ( ) ;
86
+ collectionEvent . Reset ( ) ;
87
+
88
+ //Providing the second avatar
89
+ bitmapImageObservable . OnNext ( orgImage ) ;
88
90
89
91
//Waiting for the delayed bitmap image observable
90
- evt . WaitOne ( ) ;
91
- evt . Reset ( ) ;
92
+ avatarPropertyEvent . WaitOne ( ) ;
93
+ avatarPropertyEvent . Reset ( ) ;
92
94
93
95
//Login is the id, so that should be the same
94
96
Assert . Equal ( login , col [ 0 ] . Login ) ;
0 commit comments