|
| 1 | +using System; |
| 2 | +using System.Reactive.Linq; |
| 3 | +using System.Threading; |
| 4 | +using System.Windows.Media.Imaging; |
| 5 | +using GitHub.Collections; |
| 6 | +using GitHub.Models; |
| 7 | +using GitHub.Services; |
| 8 | +using ReactiveUI; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace UnitTests.GitHub.App.Models |
| 12 | +{ |
| 13 | + public class AccountModelTests : TestBaseClass |
| 14 | + { |
| 15 | + [Fact] |
| 16 | + public void CopyFromDoesNotLoseAvatar() |
| 17 | + { |
| 18 | + //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 | + { |
| 22 | + return Observable.Generate( |
| 23 | + initialState: 0, |
| 24 | + condition: i => i < 1, |
| 25 | + iterate: state => state + 1, |
| 26 | + resultSelector: i => image, |
| 27 | + timeSelector: i => TimeSpan.FromSeconds(seconds)); |
| 28 | + }; |
| 29 | + |
| 30 | + var evt = new ManualResetEvent(false); |
| 31 | + |
| 32 | + //Creating an initial account with an observable that returns immediately |
| 33 | + const string login = "foo"; |
| 34 | + const int initialOwnedPrivateRepositoryCount = 1; |
| 35 | + |
| 36 | + var createdAt = DateTime.Now; |
| 37 | + var initialAccount = new Account(login, true, false, initialOwnedPrivateRepositoryCount, 0, generateObservable(0)); |
| 38 | + |
| 39 | + //Creating the test collection |
| 40 | + var col = new TrackingCollection<IAccount>(Observable.Empty<IAccount>(), OrderedComparer<IAccount>.OrderByDescending(x => x.Login).Compare); |
| 41 | + col.Subscribe(account => |
| 42 | + { |
| 43 | + evt.Set(); |
| 44 | + }, () => { }); |
| 45 | + |
| 46 | + //Adding that account to the TrackingCollection |
| 47 | + col.AddItem(initialAccount); |
| 48 | + |
| 49 | + //Waiting for the collection add the item |
| 50 | + evt.WaitOne(); |
| 51 | + evt.Reset(); |
| 52 | + |
| 53 | + //Checking some initial properties |
| 54 | + Assert.Equal(login, col[0].Login); |
| 55 | + Assert.Equal(initialOwnedPrivateRepositoryCount, col[0].OwnedPrivateRepos); |
| 56 | + |
| 57 | + //Demonstrating that the avatar is present |
| 58 | + Assert.NotNull(col[0].Avatar); |
| 59 | + |
| 60 | + //Creating an observable that will return in one second |
| 61 | + var updatedBitmapSourceObservable = generateObservable(1); |
| 62 | + |
| 63 | + //Creating an account update with an observable |
| 64 | + const int updatedOwnedPrivateRepositoryCount = 2; |
| 65 | + |
| 66 | + var updatedAccount = new Account(login, true, false, updatedOwnedPrivateRepositoryCount, 0, updatedBitmapSourceObservable); |
| 67 | + |
| 68 | + //Updating the accout in the collection |
| 69 | + col.AddItem(updatedAccount); |
| 70 | + |
| 71 | + //Waiting for the collection to process the update |
| 72 | + evt.WaitOne(); |
| 73 | + evt.Reset(); |
| 74 | + |
| 75 | + updatedBitmapSourceObservable.Subscribe(bitmapImage => |
| 76 | + { |
| 77 | + evt.Set(); |
| 78 | + }); |
| 79 | + |
| 80 | + //Waiting for the delayed bitmap image observable |
| 81 | + evt.WaitOne(); |
| 82 | + evt.Reset(); |
| 83 | + |
| 84 | + //Login is the id, so that should be the same |
| 85 | + Assert.Equal(login, col[0].Login); |
| 86 | + |
| 87 | + //CopyFrom() should have updated this field |
| 88 | + Assert.Equal(updatedOwnedPrivateRepositoryCount, col[0].OwnedPrivateRepos); |
| 89 | + |
| 90 | + //CopyFrom() should not cause a race condition here |
| 91 | + Assert.NotNull(col[0].Avatar); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments