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
12 changes: 11 additions & 1 deletion src/Controls/src/Core/VisualStateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,17 @@ public void RemoveAt(int index)
public VisualStateGroup this[int index]
{
get => _internalList[index];
set => _internalList[index] = value;
set
{
if (value == null)
throw new ArgumentNullException(nameof(value));

var oldGroup = _internalList[index];
oldGroup.StatesChanged -= ValidateAndNotify;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 AI-Generated Review (multi-model)

[moderate] Logic and Correctness — The -=/+= pair is unconditional and does not account for the same VisualStateGroup instance appearing at more than one index. Two concrete cases: (1) groups[1] = groups[0] subscribes ValidateAndNotify to that instance a second time, so every later States mutation on it runs Validate + OnStatesChanged (and therefore ChangeVisualState) twice; (2) if oldGroup is also stored at another index, oldGroup.StatesChanged -= ValidateAndNotify removes the only handler, so the surviving copy's later States mutations are no longer validated or propagated to the element. Guard with if (ReferenceEquals(oldGroup, value)) return; and only unsubscribe/subscribe when the instance is not otherwise present in _internalList.

_internalList[index] = value;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 AI-Generated Review (multi-model)

[major] Logic and Correctness — The replaced group's currently-applied setters are never un-applied. GoToState only calls setter.UnApply(...) for group.CurrentState of groups still present in the list (see GoToState, this file), and the subsequent ValidateAndNotify -> OnStatesChanged -> VisualElement.ChangeVisualState() iterates the new list, so oldGroup (now detached) never gets its active state un-applied. Concrete scenario: group 0's Normal state sets BackgroundColor=Red and Margin=1; groups[0] = replacement where the replacement's Normal sets only Margin=2. After the assignment Margin becomes 2 but BackgroundColor stays Red permanently at VSM specificity, because nothing ever un-applies the removed group's setters. Un-apply oldGroup.CurrentState's setters (using the same specificity GoToState applies) and clear oldGroup.CurrentState/oldGroup.VisualElement before installing the replacement.

value.StatesChanged += ValidateAndNotify;
ValidateAndNotify(_internalList);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 AI-Generated Review (multi-model)

[moderate] Logic and Correctness — ValidateAndNotify runs after the list and both subscriptions have already been mutated, and it can throw. Concrete scenario: groups[0] contains a state named Normal; assigning groups[1] = groupWithNormalState reaches Validate, which throws InvalidOperationException("VisualState Names must be unique"). The exception escapes the setter with the invalid group already installed at index 1, oldGroup already unsubscribed, and the new group already subscribed — a caller that catches the exception is left with a corrupted list rather than the pre-assignment state. Validate the candidate list before committing the swap (or restore oldGroup and its subscription in a catch/finally) so the setter is atomic. Note this is a new failure mode for this indexer: before this change the assignment was never validated.

}
}

WeakReference<VisualElement> _visualElement;
Expand Down
48 changes: 48 additions & 0 deletions src/Controls/tests/Core.UnitTests/VisualStateManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,54 @@ public void GroupWithDuplicateNameReplacesExisting()
Assert.Same(secondGroup, vsgs[0]);
}

[Fact]
public void ReplacingGroupUpdatesVisualStateAndValidationSubscription()
{
var label = new Label();
var originalMargin = new Thickness(1);
var replacementMargin = new Thickness(2);
var groups = new VisualStateGroupList
Comment on lines +156 to +162
{
new VisualStateGroup
{
States =
{
new VisualState
{
Name = NormalStateName,
Setters =
{
new Setter { Property = View.MarginProperty, Value = originalMargin }
}
}
}
}
};

VisualStateManager.SetVisualStateGroups(label, groups);
Assert.Equal(originalMargin, label.Margin);

var replacementGroup = new VisualStateGroup
{
States =
{
new VisualState
{
Name = NormalStateName,
Setters =
{
new Setter { Property = View.MarginProperty, Value = replacementMargin }
}
}
}
};

groups[0] = replacementGroup;

Assert.Equal(replacementMargin, label.Margin);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 AI-Generated Review (multi-model)

[moderate] Regression Prevention and Test Coverage — The fixture conflates two signals: the original and replacement states both set the same property (View.MarginProperty), so Assert.Equal(replacementMargin, label.Margin) cannot distinguish "replacement setters were applied" from "the removed group's setters were correctly un-applied" — the replacement's Apply at the same specificity simply overwrites the stale value. This is exactly why the leaked-setter behavior flagged on VisualStateManager.cs:425 is invisible here. Give the original group a setter for a property the replacement does not set (e.g. original sets BackgroundColor + Margin, replacement sets only Margin) and assert that property returns to its default after the swap.

Assert.Throws<InvalidOperationException>(() => replacementGroup.States.Add(new VisualState { Name = NormalStateName }));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 AI-Generated Review (multi-model)

[major] Regression Prevention and Test Coverage — This test does not prove the stale-subscription half of the fix. Assert.Throws<InvalidOperationException> on replacementGroup.States.Add(...) only exercises the new subscription added on line 426; deleting line 424 (oldGroup.StatesChanged -= ValidateAndNotify) leaves this test fully green, so the regression it is supposed to lock in is untested. Add the negative assertion the guard exists for: keep a reference to the original group, and after groups[0] = replacementGroup assert that mutating the detached group (e.g. originalGroup.States.Add(new VisualState { Name = "Foo" })) neither throws the uniqueness InvalidOperationException nor changes label.Margin — that is the only assertion that fails if the -= is removed. The new ArgumentNullException path on lines 420-421 is likewise uncovered.

}

[Fact]
public void StateNamesInGroupMayNotBeNull()
{
Expand Down
Loading