11using System ;
2+ using System . IO ;
3+ using System . Linq ;
24using System . Reactive . Linq ;
35using System . Threading ;
46using System . Windows . Media . Imaging ;
@@ -16,8 +18,10 @@ public class AccountModelTests : TestBaseClass
1618 public void CopyFromDoesNotLoseAvatar ( )
1719 {
1820 //A function that will return this image in an observable after X seconds
19- var image = AvatarProvider . CreateBitmapImage ( "pack://application:,,,/GitHub.App;component/Images/default_user_avatar.png" ) ;
20- Func < int , IObservable < BitmapImage > > generateObservable = seconds =>
21+ var userImage = AvatarProvider . CreateBitmapImage ( "pack://application:,,,/GitHub.App;component/Images/default_user_avatar.png" ) ;
22+ var orgImage = AvatarProvider . CreateBitmapImage ( "pack://application:,,,/GitHub.App;component/Images/default_org_avatar.png" ) ;
23+
24+ Func < int , BitmapImage , IObservable < BitmapImage > > generateObservable = ( seconds , image ) =>
2125 {
2226 return Observable . Generate (
2327 initialState : 0 ,
@@ -33,8 +37,7 @@ public void CopyFromDoesNotLoseAvatar()
3337 const string login = "foo" ;
3438 const int initialOwnedPrivateRepositoryCount = 1 ;
3539
36- var createdAt = DateTime . Now ;
37- var initialAccount = new Account ( login , true , false , initialOwnedPrivateRepositoryCount , 0 , generateObservable ( 0 ) ) ;
40+ var initialAccount = new Account ( login , true , false , initialOwnedPrivateRepositoryCount , 0 , generateObservable ( 0 , userImage ) ) ;
3841
3942 //Creating the test collection
4043 var col = new TrackingCollection < IAccount > ( Observable . Empty < IAccount > ( ) , OrderedComparer < IAccount > . OrderByDescending ( x => x . Login ) . Compare ) ;
@@ -56,27 +59,32 @@ public void CopyFromDoesNotLoseAvatar()
5659
5760 //Demonstrating that the avatar is present
5861 Assert . NotNull ( col [ 0 ] . Avatar ) ;
62+ Assert . Equal ( true , BitmapImageExtensions . IsEqual ( col [ 0 ] . Avatar , userImage ) ) ;
5963
6064 //Creating an observable that will return in one second
61- var updatedBitmapSourceObservable = generateObservable ( 1 ) ;
65+ var updatedBitmapSourceObservable = generateObservable ( 1 , orgImage ) ;
6266
6367 //Creating an account update with an observable
6468 const int updatedOwnedPrivateRepositoryCount = 2 ;
6569
6670 var updatedAccount = new Account ( login , true , false , updatedOwnedPrivateRepositoryCount , 0 , updatedBitmapSourceObservable ) ;
6771
72+ //Adding a listener to check for the changing of the Avatar property
73+ initialAccount . Changed . Subscribe ( args =>
74+ {
75+ if ( args . PropertyName == "Avatar" )
76+ {
77+ evt . Set ( ) ;
78+ }
79+ } ) ;
80+
6881 //Updating the accout in the collection
6982 col . AddItem ( updatedAccount ) ;
7083
7184 //Waiting for the collection to process the update
7285 evt . WaitOne ( ) ;
7386 evt . Reset ( ) ;
7487
75- updatedBitmapSourceObservable . Subscribe ( bitmapImage =>
76- {
77- evt . Set ( ) ;
78- } ) ;
79-
8088 //Waiting for the delayed bitmap image observable
8189 evt . WaitOne ( ) ;
8290 evt . Reset ( ) ;
@@ -89,6 +97,44 @@ public void CopyFromDoesNotLoseAvatar()
8997
9098 //CopyFrom() should not cause a race condition here
9199 Assert . NotNull ( col [ 0 ] . Avatar ) ;
100+ Assert . Equal ( true , BitmapImageExtensions . IsEqual ( ( col [ 0 ] . Avatar as BitmapImage ) , orgImage ) ) ;
101+ }
102+ }
103+
104+ public static class BitmapImageExtensions
105+ {
106+ //http://stackoverflow.com/questions/15558107/quickest-way-to-compare-two-bitmapimages-to-check-if-they-are-different-in-wpf
107+
108+ public static bool IsEqual ( BitmapSource image1 , BitmapSource image2 )
109+ {
110+ if ( image1 == null || image2 == null )
111+ {
112+ return false ;
113+ }
114+ return ToBytes ( image1 ) . SequenceEqual ( ToBytes ( image2 ) ) ;
115+ }
116+
117+ public static byte [ ] ToBytes ( BitmapSource image )
118+ {
119+ byte [ ] data = new byte [ ] { } ;
120+ if ( image != null )
121+ {
122+ try
123+ {
124+ var encoder = new BmpBitmapEncoder ( ) ;
125+ encoder . Frames . Add ( BitmapFrame . Create ( image ) ) ;
126+ using ( MemoryStream ms = new MemoryStream ( ) )
127+ {
128+ encoder . Save ( ms ) ;
129+ data = ms . ToArray ( ) ;
130+ }
131+ return data ;
132+ }
133+ catch ( Exception ex )
134+ {
135+ }
136+ }
137+ return data ;
92138 }
93139 }
94140}
0 commit comments