diff --git a/osu.Game.Tests/Database/RealmSubscriptionRegistrationTests.cs b/osu.Game.Tests/Database/RealmSubscriptionRegistrationTests.cs index 02d617d0e0..363a189f6e 100644 --- a/osu.Game.Tests/Database/RealmSubscriptionRegistrationTests.cs +++ b/osu.Game.Tests/Database/RealmSubscriptionRegistrationTests.cs @@ -47,6 +47,28 @@ namespace osu.Game.Tests.Database void onChanged(IRealmCollection sender, ChangeSet? changes, Exception error) => lastChanges = changes; } + [Test] + public void TestPropertyChangedSubscription() + { + RunTestWithRealm((realm, _) => + { + bool? receivedValue = null; + + realm.Write(r => r.Add(TestResources.CreateTestBeatmapSetInfo())); + + using (realm.SubscribeToPropertyChanged(r => r.All().First(), setInfo => setInfo.Protected, val => receivedValue = val)) + { + Assert.That(receivedValue, Is.False); + + realm.Write(r => r.All().First().Protected = true); + + realm.Run(r => r.Refresh()); + + Assert.That(receivedValue, Is.True); + } + }); + } + [Test] public void TestSubscriptionWithContextLoss() { @@ -163,5 +185,41 @@ namespace osu.Game.Tests.Database Assert.That(beatmapSetInfo, Is.Null); }); } + + [Test] + public void TestPropertyChangedSubscriptionWithContextLoss() + { + RunTestWithRealm((realm, _) => + { + bool? receivedValue = null; + + realm.Write(r => r.Add(TestResources.CreateTestBeatmapSetInfo())); + + var subscription = realm.SubscribeToPropertyChanged( + r => r.All().First(), + setInfo => setInfo.Protected, + val => receivedValue = val); + + Assert.That(receivedValue, Is.Not.Null); + receivedValue = null; + + using (realm.BlockAllOperations()) + { + } + + // re-registration after context restore. + realm.Run(r => r.Refresh()); + Assert.That(receivedValue, Is.Not.Null); + + subscription.Dispose(); + receivedValue = null; + + using (realm.BlockAllOperations()) + Assert.That(receivedValue, Is.Null); + + realm.Run(r => r.Refresh()); + Assert.That(receivedValue, Is.Null); + }); + } } }