diff --git a/src/net/Qml.Net.Tests/Qml/BaseQmlTests.cs b/src/net/Qml.Net.Tests/Qml/BaseQmlTests.cs index aabc65e9..4c847cb1 100644 --- a/src/net/Qml.Net.Tests/Qml/BaseQmlTests.cs +++ b/src/net/Qml.Net.Tests/Qml/BaseQmlTests.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using Moq; using Qml.Net.Internal; using Qml.Net.Internal.Behaviors; +using Qml.Net.Internal.Qml; namespace Qml.Net.Tests.Qml { @@ -74,6 +75,24 @@ namespace Qml.Net.Tests.Qml return result; } + protected void RunQmlTest(string instanceId, string componentOnCompletedCode) + { + NetTestHelper.RunQml(qmlApplicationEngine, + string.Format(@" + import QtQuick 2.0 + import tests 1.0 + {0} {{ + id: {1} + Component.onCompleted: function() {{ + {2} + }} + }} + ", + typeof(TQmlRegistered).Name, + instanceId, + componentOnCompletedCode)); + } + public override void Dispose() { qmlApplicationEngine.Dispose(); diff --git a/src/net/Qml.Net.Tests/Qml/MvvmInteropBehaviorTests.cs b/src/net/Qml.Net.Tests/Qml/MvvmInteropBehaviorTests.cs index 38160974..c9abbf7b 100644 --- a/src/net/Qml.Net.Tests/Qml/MvvmInteropBehaviorTests.cs +++ b/src/net/Qml.Net.Tests/Qml/MvvmInteropBehaviorTests.cs @@ -30,7 +30,17 @@ namespace Qml.Net.Tests.Qml ViewModel.CustomMvvmStyleIntProperty = newValue; } - public bool? TestResult { get; set; } = false; + public void ChangeNotifyOnlyIntPropertyTo(int newValue) + { + ViewModel.NotifyOnlyIntProperty = newValue; + } + + public void ChangeCustomNotifyOnlyIntPropertyTo(int newValue) + { + ViewModel.CustomNotifyOnlyIntProperty = newValue; + } + + public bool? TestResult { get; set; } = null; } public class ViewModel : INotifyPropertyChanged @@ -103,6 +113,42 @@ namespace Qml.Net.Tests.Qml } } } + + private int _notifyOnlyIntProperty; + [NotifySignal] + public int NotifyOnlyIntProperty + { + get + { + return _notifyOnlyIntProperty; + } + set + { + if (!Equals(value, _notifyOnlyIntProperty)) + { + _notifyOnlyIntProperty = value; + this.ActivateNotifySignal(); + } + } + } + + private int _customNotifyOnlyIntProperty; + [NotifySignal("customNotifyIntPropertyChangedSignal")] + public int CustomNotifyOnlyIntProperty + { + get + { + return _customNotifyOnlyIntProperty; + } + set + { + if (!Equals(value, _customNotifyOnlyIntProperty)) + { + _customNotifyOnlyIntProperty = value; + this.ActivateNotifySignal(); + } + } + } private void FirePropertyChanged([CallerMemberName]string propertyName = "") { @@ -115,21 +161,15 @@ namespace Qml.Net.Tests.Qml [Fact] public void Does_register_property_changed_signal() { - NetTestHelper.RunQml(qmlApplicationEngine, - @" - import QtQuick 2.0 - import tests 1.0 - ViewModelContainer { - id: viewModelContainer - Component.onCompleted: function() { - var vm = viewModelContainer.viewModel - vm.stringPropertyChanged.connect(function() { - viewModelContainer.testResult = true - }) - viewModelContainer.changeStringPropertyTo('new value') - } - } - "); + RunQmlTest( + "viewModelContainer", + @" + var vm = viewModelContainer.viewModel + vm.stringPropertyChanged.connect(function() { + viewModelContainer.testResult = true + }) + viewModelContainer.changeStringPropertyTo('new value') + "); Instance.TestResult.Should().Be(true); } @@ -180,45 +220,65 @@ namespace Qml.Net.Tests.Qml } [Fact] - public void Does_not_interfer_with_completely_custom_notify_signals() + public void Does_play_nicely_with_completely_custom_notify_signals() { - NetTestHelper.RunQml(qmlApplicationEngine, + RunQmlTest( + "viewModelContainer", @" - import QtQuick 2.0 - import tests 1.0 - ViewModelContainer { - id: viewModelContainer - Component.onCompleted: function() { - var vm = viewModelContainer.viewModel - vm.customIntPropertyChangedSignal.connect(function() { - viewModelContainer.testResult = true - }) - viewModelContainer.changeCustomIntPropertyTo(3) - } - } - "); + var vm = viewModelContainer.viewModel + vm.customIntPropertyChangedSignal.connect(function() { + viewModelContainer.testResult = true + }) + viewModelContainer.changeCustomIntPropertyTo(3) + "); Instance.TestResult.Should().Be(true); } [Fact] - public void Does_not_interfer_with_custom_notify_signals() + public void Does_play_nicely_with_custom_notify_signals() { - NetTestHelper.RunQml(qmlApplicationEngine, + RunQmlTest( + "viewModelContainer", @" - import QtQuick 2.0 - import tests 1.0 - ViewModelContainer { - id: viewModelContainer - Component.onCompleted: function() { - var vm = viewModelContainer.viewModel - vm.customMvvmStyleIntPropertyChanged.connect(function() { - viewModelContainer.testResult = true - }) - viewModelContainer.changeCustomMvvmStyleIntPropertyTo(3) - } - } - "); + var vm = viewModelContainer.viewModel + vm.customMvvmStyleIntPropertyChanged.connect(function() { + viewModelContainer.testResult = true + }) + viewModelContainer.changeCustomMvvmStyleIntPropertyTo(3) + "); + + Instance.TestResult.Should().Be(true); + } + + [Fact] + public void Does_not_interfer_with_properties_only_using_notify_signals() + { + RunQmlTest( + "viewModelContainer", + @" + var vm = viewModelContainer.viewModel + vm.notifyOnlyIntPropertyChanged.connect(function() { + viewModelContainer.testResult = true + }) + viewModelContainer.changeNotifyOnlyIntPropertyTo(3) + "); + + Instance.TestResult.Should().Be(true); + } + + [Fact] + public void Does_not_interfer_with_properties_only_using_custom_notify_signals() + { + RunQmlTest( + "viewModelContainer", + @" + var vm = viewModelContainer.viewModel + vm.customNotifyIntPropertyChangedSignal.connect(function() { + viewModelContainer.testResult = true + }) + viewModelContainer.changeCustomNotifyOnlyIntPropertyTo(3) + "); Instance.TestResult.Should().Be(true); } diff --git a/src/net/Qml.Net.Tests/Qml/UIntTests.cs b/src/net/Qml.Net.Tests/Qml/UIntTests.cs index 5d18adb8..cb9eb747 100644 --- a/src/net/Qml.Net.Tests/Qml/UIntTests.cs +++ b/src/net/Qml.Net.Tests/Qml/UIntTests.cs @@ -25,18 +25,11 @@ namespace Qml.Net.Tests.Qml public void Can_read_write_int_min_value() { Mock.Setup(x => x.Property).Returns(uint.MinValue); - - NetTestHelper.RunQml(qmlApplicationEngine, + RunQmlTest( + "test", @" - import QtQuick 2.0 - import tests 1.0 - UIntTestsQml { - id: test - Component.onCompleted: function() { - test.property = test.property - } - } - "); + test.property = test.property + "); Mock.VerifyGet(x => x.Property, Times.Once); Mock.VerifySet(x => x.Property = uint.MinValue, Times.Once); @@ -47,17 +40,11 @@ namespace Qml.Net.Tests.Qml { Mock.Setup(x => x.Property).Returns(uint.MaxValue); - NetTestHelper.RunQml(qmlApplicationEngine, + RunQmlTest( + "test", @" - import QtQuick 2.0 - import tests 1.0 - UIntTestsQml { - id: test - Component.onCompleted: function() { - test.property = test.property - } - } - "); + test.property = test.property + "); Mock.VerifyGet(x => x.Property, Times.Once); Mock.VerifySet(x => x.Property = uint.MaxValue, Times.Once); @@ -66,17 +53,11 @@ namespace Qml.Net.Tests.Qml [Fact] public void Can_call_method_with_parameter() { - NetTestHelper.RunQml(qmlApplicationEngine, + RunQmlTest( + "test", @" - import QtQuick 2.0 - import tests 1.0 - UIntTestsQml { - id: test - Component.onCompleted: function() { - test.methodParameter(3) - } - } - "); + test.methodParameter(3) + "); Mock.Verify(x => x.MethodParameter(It.Is(y => y == 3)), Times.Once); } @@ -86,17 +67,11 @@ namespace Qml.Net.Tests.Qml { Mock.Setup(x => x.MethodReturn()).Returns(uint.MaxValue); - NetTestHelper.RunQml(qmlApplicationEngine, + RunQmlTest( + "test", @" - import QtQuick 2.0 - import tests 1.0 - UIntTestsQml { - id: test - Component.onCompleted: function() { - test.methodParameter(test.methodReturn()) - } - } - "); + test.methodParameter(test.methodReturn()) + "); Mock.Verify(x => x.MethodParameter(It.Is(y => y == uint.MaxValue)), Times.Once); }