-
-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix an event handler leak in
Binding.RemovePropertyEvent
Removing the handler in `RemovePropertyEvent` doesn't work, as the `Target` of the handler is not the `PropertyNotifyHelper` instance; it is the object that the handler is bound to. Instead, find the correct handler to remove by checking all invocations of the `PropertyChanged` event of the bound object, and comparing the internal handler of the invocation with the method's argument. Also, add unit tests to verify the correct behavior.
- Loading branch information
Showing
3 changed files
with
195 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
154 changes: 154 additions & 0 deletions
154
test/Eto.Test/UnitTests/Forms/Bindings/BindingHelpersTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
using NUnit.Framework; | ||
|
||
namespace Eto.Test.UnitTests.Forms.Bindings; | ||
|
||
[TestFixture] | ||
public class BindingHelpersTests | ||
{ | ||
[Test] | ||
public void AddingAPropertyEventByNameShouldWork() | ||
{ | ||
var propertyValueChanged = false; | ||
void Handler(object sender, EventArgs e) => propertyValueChanged = true; | ||
|
||
var bindObject = new BindObject { BoolProperty = true }; | ||
Binding.AddPropertyEvent(bindObject, "BoolProperty", Handler); | ||
|
||
// Act | ||
bindObject.BoolProperty = false; | ||
|
||
// Assert | ||
Assert.That(propertyValueChanged, Is.True); | ||
} | ||
|
||
[Test] | ||
public void AddingAPropertyEventByExpressionShouldWork() | ||
{ | ||
var propertyValueChanged = false; | ||
void Handler(object sender, EventArgs e) => propertyValueChanged = true; | ||
|
||
var bindObject = new BindObject { BoolProperty = true }; | ||
Binding.AddPropertyEvent(bindObject, obj => obj.BoolProperty, Handler); | ||
|
||
// Act | ||
bindObject.BoolProperty = false; | ||
|
||
// Assert | ||
Assert.That(propertyValueChanged, Is.True); | ||
} | ||
|
||
[Test] | ||
public void AddingMultiplePropertyEventsShouldWork() | ||
{ | ||
var boolPropertyValueChanged = false; | ||
var intPropertyValueChanged = false; | ||
var stringPropertyValueChanged = false; | ||
void BoolHandler(object sender, EventArgs e) => boolPropertyValueChanged = true; | ||
void IntHandler(object sender, EventArgs e) => intPropertyValueChanged = true; | ||
void StringHandler(object sender, EventArgs e) => stringPropertyValueChanged = true; | ||
|
||
var bindObject = new BindObject { BoolProperty = true, IntProperty = 3, StringProperty = "Test1" }; | ||
Binding.AddPropertyEvent(bindObject, obj => obj.BoolProperty, BoolHandler); | ||
Binding.AddPropertyEvent(bindObject, obj => obj.IntProperty, IntHandler); | ||
Binding.AddPropertyEvent(bindObject, obj => obj.StringProperty, StringHandler); | ||
|
||
// Act | ||
bindObject.BoolProperty = false; | ||
bindObject.IntProperty = 4; | ||
bindObject.StringProperty = "Test2"; | ||
|
||
// Assert | ||
Assert.That(boolPropertyValueChanged, Is.True); | ||
Assert.That(intPropertyValueChanged, Is.True); | ||
Assert.That(stringPropertyValueChanged, Is.True); | ||
} | ||
|
||
[Test] | ||
public void APropertyEventShouldNotRespondToADifferentProperty() | ||
{ | ||
var propertyValueChanged = false; | ||
void Handler(object sender, EventArgs e) => propertyValueChanged = true; | ||
|
||
var bindObject = new BindObject { BoolProperty = true, IntProperty = 1 }; | ||
Binding.AddPropertyEvent(bindObject, obj => obj.BoolProperty, Handler); | ||
|
||
// Act | ||
bindObject.IntProperty = 2; | ||
|
||
// Assert | ||
Assert.That(propertyValueChanged, Is.False); | ||
} | ||
|
||
[Test] | ||
public void RemovingAPropertyEventShouldWork() | ||
{ | ||
var propertyValueChanged = false; | ||
void Handler(object sender, EventArgs e) => propertyValueChanged = true; | ||
|
||
var bindObject = new BindObject { BoolProperty = true }; | ||
Binding.AddPropertyEvent(bindObject, obj => obj.BoolProperty, Handler); | ||
|
||
// Act | ||
Binding.RemovePropertyEvent(bindObject, Handler); | ||
bindObject.BoolProperty = false; | ||
|
||
// Assert | ||
Assert.That(propertyValueChanged, Is.False); | ||
} | ||
|
||
[Test] | ||
public void RemovingAPropertyEventShouldKeepOtherEvents() | ||
{ | ||
var boolPropertyValueChanged = false; | ||
var intPropertyValueChanged = false; | ||
var stringPropertyValueChanged = false; | ||
void BoolHandler(object sender, EventArgs e) => boolPropertyValueChanged = true; | ||
void IntHandler(object sender, EventArgs e) => intPropertyValueChanged = true; | ||
void StringHandler(object sender, EventArgs e) => stringPropertyValueChanged = true; | ||
|
||
var bindObject = new BindObject { BoolProperty = true, IntProperty = 3, StringProperty = "Test1" }; | ||
Binding.AddPropertyEvent(bindObject, obj => obj.BoolProperty, BoolHandler); | ||
Binding.AddPropertyEvent(bindObject, obj => obj.IntProperty, IntHandler); | ||
Binding.AddPropertyEvent(bindObject, obj => obj.StringProperty, StringHandler); | ||
|
||
// Act | ||
Binding.RemovePropertyEvent(bindObject, IntHandler); | ||
bindObject.BoolProperty = false; | ||
bindObject.IntProperty = 4; | ||
bindObject.StringProperty = "Test2"; | ||
|
||
// Assert | ||
Assert.That(boolPropertyValueChanged, Is.True); | ||
Assert.That(intPropertyValueChanged, Is.False); | ||
Assert.That(stringPropertyValueChanged, Is.True); | ||
} | ||
|
||
[Test] | ||
public void RemovingAllPropertyEventsShouldWork() | ||
{ | ||
var boolPropertyValueChanged = false; | ||
var intPropertyValueChanged = false; | ||
var stringPropertyValueChanged = false; | ||
void BoolHandler(object sender, EventArgs e) => boolPropertyValueChanged = true; | ||
void IntHandler(object sender, EventArgs e) => intPropertyValueChanged = true; | ||
void StringHandler(object sender, EventArgs e) => stringPropertyValueChanged = true; | ||
|
||
var bindObject = new BindObject { BoolProperty = true, IntProperty = 3, StringProperty = "Test1" }; | ||
Binding.AddPropertyEvent(bindObject, obj => obj.BoolProperty, BoolHandler); | ||
Binding.AddPropertyEvent(bindObject, obj => obj.IntProperty, IntHandler); | ||
Binding.AddPropertyEvent(bindObject, obj => obj.StringProperty, StringHandler); | ||
|
||
// Act | ||
Binding.RemovePropertyEvent(bindObject, StringHandler); | ||
Binding.RemovePropertyEvent(bindObject, BoolHandler); | ||
Binding.RemovePropertyEvent(bindObject, IntHandler); | ||
bindObject.BoolProperty = false; | ||
bindObject.IntProperty = 4; | ||
bindObject.StringProperty = "Test2"; | ||
|
||
// Assert | ||
Assert.That(boolPropertyValueChanged, Is.False); | ||
Assert.That(intPropertyValueChanged, Is.False); | ||
Assert.That(stringPropertyValueChanged, Is.False); | ||
} | ||
} |