Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright (c) 2019-2026 ReactiveUI and Contributors. All rights reserved.
// ReactiveUI and Contributors licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using System.Runtime.CompilerServices;
using System.Windows;

namespace ReactiveUI.Binding.Wpf.Tests;

/// <summary>Tests observing a read-only <see cref="DependencyProperty"/>, which only its key can write (ReactiveUI #4547).</summary>
/// <remarks>
/// Serialized: the tests share one static <see cref="DependencyProperty"/>, and the value-changed subscriptions
/// on it live in a process-wide table keyed by that property.
/// </remarks>
[NotInParallel]
public class ReadOnlyDependencyPropertyTests
{
/// <summary>A generated observation delivers the current value, then each value written through the key.</summary>
/// <returns>A task representing the asynchronous test operation.</returns>
[Test]
public async Task WhenAnyValue_ReadOnlyDependencyProperty_EmitsEachValueWrittenThroughTheKey()
{
var button = new ReadOnlyPropertyButton();
var recorder = new ValueRecorder();

using (button.WhenAnyValue(static x => x.IsButtonEnabled).Subscribe(recorder))
{
button.Enable(true);
button.Enable(false);
}

button.Enable(true);

await Assert.That(recorder.Values).IsEquivalentTo([false, true, false]);
}

/// <summary>The registered WPF observer scores a read-only dependency property and reports its changes.</summary>
/// <returns>A task representing the asynchronous test operation.</returns>
[Test]
public async Task RegisteredObserver_ReadOnlyDependencyProperty_ScoresAndNotifies()
{
var button = new ReadOnlyPropertyButton();
var observer = new DependencyObjectObservableForProperty();
var recorder = new ChangeRecorder();
var expression = ((System.Linq.Expressions.Expression<Func<ReadOnlyPropertyButton, bool>>)(x => x.IsButtonEnabled)).Body;

var affinity = observer.GetAffinityForObject(typeof(ReadOnlyPropertyButton), nameof(ReadOnlyPropertyButton.IsButtonEnabled), false);
using (observer
.GetNotificationForProperty(button, expression, nameof(ReadOnlyPropertyButton.IsButtonEnabled), false, false)
.Subscribe(recorder))
{
button.Enable(true);
}

await Assert.That(affinity).IsEqualTo(BindingAffinity.WpfDependencyObject);
await Assert.That(recorder.Count).IsEqualTo(1);
}

/// <summary>Records the values an observation delivers.</summary>
private sealed class ValueRecorder : IObserver<bool>
{
/// <summary>Gets the values observed, in order.</summary>
public List<bool> Values { get; } = [];

/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void OnNext(bool value) => Values.Add(value);

/// <inheritdoc/>
public void OnError(Exception error)
{
}

/// <inheritdoc/>
public void OnCompleted()
{
}
}

/// <summary>Counts the changes a registered observer reports.</summary>
private sealed class ChangeRecorder : IObserver<IObservedChange<object, object?>>
{
/// <summary>Gets the number of changes observed.</summary>
public int Count { get; private set; }

/// <inheritdoc/>
public void OnNext(IObservedChange<object, object?> value) => Count++;

/// <inheritdoc/>
public void OnError(Exception error)
{
}

/// <inheritdoc/>
public void OnCompleted()
{
}
}
}
37 changes: 37 additions & 0 deletions src/tests/ReactiveUI.Binding.Wpf.Tests/ReadOnlyPropertyButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2019-2026 ReactiveUI and Contributors. All rights reserved.
// ReactiveUI and Contributors licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using System.Diagnostics;
using System.Windows;

namespace ReactiveUI.Binding.Wpf.Tests;

/// <summary>
/// A dependency object with a read-only dependency property, registered the way ReactiveUI #4547 does: the public
/// field is assigned from the key in the static constructor, and only the key can write the value.
/// </summary>
[DebuggerDisplay("ReadOnlyPropertyButton: IsButtonEnabled = {IsButtonEnabled}")]
public sealed class ReadOnlyPropertyButton : DependencyObject
{
/// <summary>Identifies the read-only <see cref="IsButtonEnabled"/> dependency property.</summary>
public static readonly DependencyProperty IsButtonEnabledProperty;

/// <summary>The key that writes <see cref="IsButtonEnabled"/>.</summary>
internal static readonly DependencyPropertyKey IsButtonEnabledPropertyKey =
DependencyProperty.RegisterReadOnly(nameof(IsButtonEnabled), typeof(bool), typeof(ReadOnlyPropertyButton), new(false));

/// <summary>Initializes static members of the <see cref="ReadOnlyPropertyButton"/> class.</summary>
static ReadOnlyPropertyButton() => IsButtonEnabledProperty = IsButtonEnabledPropertyKey.DependencyProperty;

/// <summary>Gets a value indicating whether the button is enabled.</summary>
public bool IsButtonEnabled
{
get => (bool)GetValue(IsButtonEnabledPropertyKey.DependencyProperty);
private set => SetValue(IsButtonEnabledPropertyKey, value);
}

/// <summary>Writes <see cref="IsButtonEnabled"/> through its key.</summary>
/// <param name="enabled">The value to write.</param>
public void Enable(bool enabled) => IsButtonEnabled = enabled;
}
Loading