This commit is contained in:
Michael Lamers 2018-08-06 17:38:24 +00:00 committed by GitHub
commit 6c6efaa120
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 141 additions and 87 deletions

View file

@ -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<TQmlRegistered>(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();

View file

@ -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>(
"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>(
"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>(
"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>(
"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>(
"viewModelContainer",
@"
var vm = viewModelContainer.viewModel
vm.customNotifyIntPropertyChangedSignal.connect(function() {
viewModelContainer.testResult = true
})
viewModelContainer.changeCustomNotifyOnlyIntPropertyTo(3)
");
Instance.TestResult.Should().Be(true);
}

View file

@ -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<UIntTestsQml>(
"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<UIntTestsQml>(
"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<UIntTestsQml>(
"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<uint>(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<UIntTestsQml>(
"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<uint>(y => y == uint.MaxValue)), Times.Once);
}