Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 4724715

Browse files
Improving the unit test
1 parent a14f685 commit 4724715

File tree

1 file changed

+35
-33
lines changed

1 file changed

+35
-33
lines changed

src/UnitTests/GitHub.App/Models/AccountModelTests.cs

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.IO;
33
using System.Linq;
44
using System.Reactive.Linq;
5+
using System.Reactive.Subjects;
56
using System.Threading;
67
using System.Windows.Media.Imaging;
78
using GitHub.Collections;
@@ -17,78 +18,79 @@ public class AccountModelTests : TestBaseClass
1718
[Fact]
1819
public void CopyFromDoesNotLoseAvatar()
1920
{
20-
//A function that will return this image in an observable after X seconds
2121
var userImage = AvatarProvider.CreateBitmapImage("pack://application:,,,/GitHub.App;component/Images/default_user_avatar.png");
2222
var orgImage = AvatarProvider.CreateBitmapImage("pack://application:,,,/GitHub.App;component/Images/default_org_avatar.png");
2323

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>();
3325

34-
var evt = new ManualResetEvent(false);
26+
var collectionEvent = new ManualResetEvent(false);
27+
var avatarPropertyEvent = new ManualResetEvent(false);
3528

3629
//Creating an initial account with an observable that returns immediately
3730
const string login = "foo";
3831
const int initialOwnedPrivateRepositoryCount = 1;
3932

40-
var initialAccount = new Account(login, true, false, initialOwnedPrivateRepositoryCount, 0, generateObservable(0, userImage));
33+
var initialAccount = new Account(login, true, false, initialOwnedPrivateRepositoryCount, 0, bitmapImageObservable);
4134

4235
//Creating the test collection
4336
var col = new TrackingCollection<IAccount>(Observable.Empty<IAccount>(), OrderedComparer<IAccount>.OrderByDescending(x => x.Login).Compare);
4437
col.Subscribe(account =>
4538
{
46-
evt.Set();
39+
collectionEvent.Set();
4740
}, () => { });
4841

4942
//Adding that account to the TrackingCollection
5043
col.AddItem(initialAccount);
5144

5245
//Waiting for the collection add the item
53-
evt.WaitOne();
54-
evt.Reset();
46+
collectionEvent.WaitOne();
47+
collectionEvent.Reset();
5548

5649
//Checking some initial properties
5750
Assert.Equal(login, col[0].Login);
5851
Assert.Equal(initialOwnedPrivateRepositoryCount, col[0].OwnedPrivateRepos);
5952

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);
7255

7356
//Adding a listener to check for the changing of the Avatar property
7457
initialAccount.Changed.Subscribe(args =>
7558
{
7659
if (args.PropertyName == "Avatar")
7760
{
78-
evt.Set();
61+
avatarPropertyEvent.Set();
7962
}
8063
});
8164

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
8382
col.AddItem(updatedAccount);
8483

8584
//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);
8890

8991
//Waiting for the delayed bitmap image observable
90-
evt.WaitOne();
91-
evt.Reset();
92+
avatarPropertyEvent.WaitOne();
93+
avatarPropertyEvent.Reset();
9294

9395
//Login is the id, so that should be the same
9496
Assert.Equal(login, col[0].Login);

0 commit comments

Comments
 (0)