public void Can_read_date_time_local() { var time = DateTime.Now; var timeReturned = (DateTime?)null; Mock.Setup(x => x.Property).Returns(time); Mock.Setup(x => x.MethodParameter(It.IsAny <DateTime>())) .Callback(new Action <DateTime>(result => { timeReturned = result; })); NetTestHelper.RunQml(qmlApplicationEngine, @" import QtQuick 2.0 import tests 1.0 DateTimeTestsQml { id: test Component.onCompleted: function() { test.MethodParameter(test.Property) } } "); Mock.VerifyGet(x => x.Property, Times.Once); Mock.Verify(x => x.MethodParameter(It.IsAny <DateTime>())); Assert.NotNull(timeReturned); Assert.Equal(time.ToString("MM/dd/yyyy hh:mm:ss"), timeReturned.Value.ToString("MM/dd/yyyy hh:mm:ss")); }
public void Can_call_correct_overload() { Mock.Setup(x => x.Overload()); Mock.Setup(x => x.Overload(It.IsAny <string>())); NetTestHelper.RunQml(qmlApplicationEngine, @" import QtQuick 2.0 import tests 1.0 ObjectTestsQml { id: test Component.onCompleted: function() { test.overload() } } "); Mock.Verify(x => x.Overload(), Times.Once); Mock.Verify(x => x.Overload(It.IsAny <string>()), Times.Never); NetTestHelper.RunQml(qmlApplicationEngine, @" import QtQuick 2.0 import tests 1.0 ObjectTestsQml { id: test Component.onCompleted: function() { test.overload('test') } } "); Mock.Verify(x => x.Overload(), Times.Once); Mock.Verify(x => x.Overload(It.IsAny <string>()), Times.Once); }
public void Can_read_write_property_nullable_with_value() { var value = DateTimeOffset.Now; // This trims some percision off of the milliseconds, makes the comparison accurate. value = new DateTimeOffset(value.Year, value.Month, value.Day, value.Hour, value.Minute, value.Second, value.Millisecond, value.Offset); Mock.SetupGet(x => x.Nullable).Returns(value); Mock.SetupSet(x => x.Nullable = value); NetTestHelper.RunQml(qmlApplicationEngine, @" import QtQuick 2.0 import tests 1.0 DateTimeOffsetTestsQml { id: test Component.onCompleted: function() { var v = test.nullable test.nullable = v } } "); Mock.VerifyGet(x => x.Nullable, Times.Once); Mock.VerifySet(x => x.Nullable = value); }
public void Can_pass_js_value_to_callback() { Mock.CallBase = true; Mock.Setup(x => x.MethodWithParameters("test1", 4)); NetTestHelper.RunQml(qmlApplicationEngine, @" import QtQuick 2.0 import tests 1.0 JsTestsQml { id: test Component.onCompleted: function() { var o = { testProperty1: 'test1', testProperty2: 4 } test.callMethodWithJsValue(o, function(passedIn) { test.methodWithParameters(passedIn.testProperty1, passedIn.testProperty2) }) } } "); Mock.Verify(x => x.CallMethodWithJsValue(It.IsAny <INetJsValue>(), It.IsAny <INetJsValue>()), Times.Once); Mock.Verify(x => x.MethodWithParameters("test1", 4), Times.Once); }
public void Can_read_date_time_utc() { var time = DateTime.UtcNow; var timeReturned = (DateTime?)null; Mock.Setup(x => x.Property).Returns(time); Mock.Setup(x => x.MethodParameter(It.IsAny <DateTime>())) .Callback(new Action <DateTime>(result => { timeReturned = result; })); NetTestHelper.RunQml(qmlApplicationEngine, @" import QtQuick 2.0 import tests 1.0 DateTimeTestsQml { id: test Component.onCompleted: function() { test.MethodParameter(test.Property) } } "); // NOTE: When qml handles dates, it always uses local time, so that is what we get back. // I'd like to get what I passed in, but there is no way to tell if original // DateTime value was UTC or not. Mock.VerifyGet(x => x.Property, Times.Once); Mock.Verify(x => x.MethodParameter(It.IsAny <DateTime>())); Assert.NotNull(timeReturned); Assert.Equal(time.ToUniversalTime().ToString("MM/dd/yyyy hh:mm:ss"), timeReturned.Value.ToUniversalTime().ToString("MM/dd/yyyy hh:mm:ss")); }
public void Can_invoke_js_callback_with_net_instance() { var testObject = new JsValueTests.JsTestsQml.TestObject(); Mock.Setup(x => x.Method(It.IsAny <INetJsValue>())) .Callback(new Action <dynamic>(x => { x(testObject); })); NetTestHelper.RunQml(qmlApplicationEngine, @" import QtQuick 2.0 import tests 1.0 JsTestsQml { id: test Component.onCompleted: function() { test.method(function(param1) { param1.testMethod() param1.testMethod() }) } } "); Mock.Verify(x => x.Method(It.IsAny <INetJsValue>()), Times.Once); testObject.CalledCount.Should().Be(2); }
public void Can_invoke_js_callback_with_parameters() { object result = null; Mock.Setup(x => x.Method(It.IsAny <INetJsValue>())) .Callback(new Action <dynamic>(x => { result = x("test1", 4); })); Mock.Setup(x => x.MethodWithParameters("test1", 4)); NetTestHelper.RunQml(qmlApplicationEngine, @" import QtQuick 2.0 import tests 1.0 JsTestsQml { id: test Component.onCompleted: function() { test.method(function(param1, param2) { test.methodWithParameters(param1, param2) }) } } "); Mock.Verify(x => x.Method(It.IsAny <INetJsValue>()), Times.Once); Mock.Verify(x => x.MethodWithParameters("test1", 4), Times.Once); result.Should().BeNull(); }
protected void RunQmlTest(string instanceId, string componentOnCompletedCode, bool runEvents = false, bool failOnQmlWarnings = true) { var result = NetTestHelper.RunQml( qmlApplicationEngine, string.Format( @" import QtQuick 2.0 import tests 1.0 {0} {{ id: {1} property var testQObject: null function runTest() {{ {2} }} }} ", typeof(TTypeToRegister).Name, instanceId, componentOnCompletedCode), runEvents, failOnQmlWarnings); if (result == false) { throw new Exception($"Couldn't execute qml: {componentOnCompletedCode}"); } }
public void Can_send_non_function() { INetJsValue jsValue = null; Mock.Setup(x => x.Method(It.IsAny <INetJsValue>())) .Callback(new Action <dynamic>(x => { jsValue = x; })); NetTestHelper.RunQml(qmlApplicationEngine, @" import QtQuick 2.0 import tests 1.0 JsTestsQml { id: test Component.onCompleted: function() { test.method({}) } } "); Mock.Verify(x => x.Method(It.IsAny <INetJsValue>()), Times.Once); jsValue.Should().NotBeNull(); jsValue.IsCallable.Should().BeFalse(); }
public void Can_await_on_task_with_result() { var oldContext = SynchronizationContext.Current; SynchronizationContext.SetSynchronizationContext(new NoAsyncSynchronizationContext()); try { Mock.Setup(x => x.TestAsync()).Returns(Task.FromResult("testt")); Mock.Setup(x => x.TestMethodWithArg("testt")); NetTestHelper.RunQml(qmlApplicationEngine, @" import QtQuick 2.0 import tests 1.0 AwaitTestsQml { id: test Component.onCompleted: function() { var task = test.testAsync() Net.await(task, function(result) { test.testMethodWithArg(result) }) } } "); Mock.Verify(x => x.TestMethodWithArg("testt"), Times.Once); } finally { SynchronizationContext.SetSynchronizationContext(oldContext); } }
public void GlobalSetup() { _guiApplication = new QGuiApplication(new[] { "-platform", "offscreen" }); _qmlApplicationEngine = new QQmlApplicationEngine(); NetTestHelper.RunQml( _qmlApplicationEngine, @" import QtQuick 2.0 import tests 1.0 Item {{ }}"); _qObject = Qt.BuildQObject("TestQObject*"); }
public void Can_get_return_value_from_js_function() { var testObject = new JsTestsQml.TestObject(); var results = new List <object>(); Mock.Setup(x => x.GetTestObject()).Returns(testObject); Mock.Setup(x => x.Method(It.IsAny <INetJsValue>())).Callback(new Action <dynamic>(jsValue => { results.Add((object)jsValue()); })); NetTestHelper.RunQml(qmlApplicationEngine, @" import QtQuick 2.0 import tests 1.0 JsTestsQml { id: test Component.onCompleted: function() { var jsObject = { } test.method(function() { return 'test' }) test.method(function() { return 32 }) test.method(function() { return jsObject }) test.method(function() { return function() {} }) var netObject = test.getTestObject() test.method(function() { return netObject }) } } "); Mock.Verify(x => x.Method(It.IsAny <INetJsValue>()), Times.Exactly(5)); Mock.Verify(x => x.GetTestObject(), Times.Once); results.Should().HaveCount(5); results[0].Should().Be("test"); results[1].Should().Be(32); results[2].Should().BeAssignableTo <INetJsValue>().And.Subject.As <INetJsValue>().IsCallable.Should().BeFalse(); results[3].Should().BeAssignableTo <INetJsValue>().And.Subject.As <INetJsValue>().IsCallable.Should().BeTrue(); results[4].Should().BeSameAs(testObject); }
public void Can_set_properties() { var testObject = new JsValueTests.JsTestsQml.TestObject(); dynamic result = null; Mock.Setup(x => x.GetTestObject()).Returns(testObject); Mock.Setup(x => x.Method(It.IsAny <INetJsValue>(), It.IsAny <INetJsValue>())) .Callback(new Action <dynamic, dynamic>((source, destination) => { destination.dest1 = source.source1; destination.dest2 = source.source2; destination.dest3 = source.source3; destination.dest4 = source.source4; })); Mock.Setup(x => x.Method(It.IsAny <INetJsValue>())) .Callback(new Action <dynamic>(value => { result = value; })); NetTestHelper.RunQml(qmlApplicationEngine, @" import QtQuick 2.0 import tests 1.0 JsTestsQml { id: test Component.onCompleted: function() { var netObject = test.getTestObject() var source = { source1: 123, source2: 'value', source3: netObject, source4: { } } var destination = { } test.method(source, destination) test.method(destination) } } "); Mock.Verify(x => x.Method(It.IsAny <INetJsValue>()), Times.Exactly(1)); Mock.Verify(x => x.Method(It.IsAny <INetJsValue>(), It.IsAny <INetJsValue>()), Times.Exactly(1)); Mock.Verify(x => x.GetTestObject(), Times.Exactly(1)); ((object)result).Should().NotBeNull(); ((int)result.dest1).Should().Be(123); ((string)result.dest2).Should().Be("value"); ((object)result.dest3).Should().BeSameAs(testObject); ((object)result.dest4).Should().BeAssignableTo <INetJsValue>(); }
public void Can_write_property_false() { NetTestHelper.RunQml(qmlApplicationEngine, @" import QtQuick 2.0 import tests 1.0 BoolTestsQml { id: test Component.onCompleted: function() { test.property = false } } "); Mock.VerifySet(x => x.Property = false, Times.Once); }
public void Can_register_singleton() { NetTestHelper.RunQml( qmlApplicationEngine, @" import QtQuick 2.0 import tests 1.0 Item { id: test Component.onCompleted: function() { SingletonTestObject.property = ""test"" } }"); Mock.VerifySet(x => x.Property = "test", Times.Once); }
public void Can_call_method_with_parameter() { NetTestHelper.RunQml(qmlApplicationEngine, @" import QtQuick 2.0 import tests 1.0 IntTestsQml { id: test Component.onCompleted: function() { test.methodParameter(3) } } "); Mock.Verify(x => x.MethodParameter(It.Is <int>(y => y == 3)), Times.Once); }
public void Can_set_method_parameter() { NetTestHelper.RunQml(qmlApplicationEngine, @" import QtQuick 2.0 import tests 1.0 CharTestsQml { id: test Component.onCompleted: function() { test.methodParameter(""Ώ"") } } "); Mock.Verify(x => x.MethodParameter(It.IsIn('Ώ')), Times.Once); }
static int Main(string[] args) { var path = System.Environment.GetEnvironmentVariable("PATH"); path += ";" + @"D:\Git\Github\pauldotknopf\net-core-qml\src\native\build-QtNetCoreQml-Desktop_Qt_5_9_1_MSVC2017_64bit-Debug\debug"; System.Environment.SetEnvironmentVariable("PATH", path); Task.Factory.StartNew(() => { while (true) { Thread.Sleep(1000); GC.Collect(GC.MaxGeneration); } }); using (var r = new StringVector(0)) { using (var app = new QGuiApplication(r)) { using (var engine = new QQmlApplicationEngine()) { QQmlApplicationEngine.RegisterType <TestQmlImport>("test", 1, 1); var method = NetTypeInfoManager.GetTypeInfo <TestQmlImport>().GetMethod(0); Console.WriteLine(method.GetMethodName()); NetTestHelper.RunMethod(engine, @" import QtQuick 2.0 import test 1.1 TestQmlImport { } ", method, null, null); //engine.loadFile("main.qml"); //return app.exec(); } } } return(0); }
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(TTypeToRegister).Name, instanceId, componentOnCompletedCode)); }
public void Can_call_method_with_return() { Mock.Setup(x => x.MethodReturn()).Returns(int.MaxValue); NetTestHelper.RunQml(qmlApplicationEngine, @" import QtQuick 2.0 import tests 1.0 IntTestsQml { id: test Component.onCompleted: function() { test.methodParameter(test.methodReturn()) } } "); Mock.Verify(x => x.MethodParameter(It.Is <int>(y => y == int.MaxValue)), Times.Once); }
public void Can_use_as_return_type() { Mock.Setup(x => x.MethodReturn()).Returns('Ώ'); NetTestHelper.RunQml(qmlApplicationEngine, @" import QtQuick 2.0 import tests 1.0 CharTestsQml { id: test Component.onCompleted: function() { test.methodParameter(test.methodReturn()) } } "); Mock.Verify(x => x.MethodParameter(It.IsIn('Ώ')), Times.Once); }
public void Can_get_items_from_list_model() { var list = new List <TestNetObject>(); list.Add(new TestNetObject()); list.Add(new TestNetObject()); list.Add(new TestNetObject()); var result = new List <TestNetObject>(); Mock.Setup(x => x.GetNetObjectList()).Returns(list); Mock.Setup(x => x.Test(It.IsAny <object>())).Callback(new Action <object>(o => result.Add((TestNetObject)o))); NetTestHelper.RunQml( qmlApplicationEngine, @" import QtQuick 2.0 import tests 1.0 Item { ListModelTestsQml { id: test Component.onCompleted: function() { var list = test.getNetObjectList() var listModel = Net.toListModel(list) rep.model = listModel } } Repeater { id: rep Item { Component.onCompleted: { test.test(modelData) } } } } "); Mock.Verify(x => x.GetNetObjectList(), Times.Once); Mock.Verify(x => x.Test(It.IsAny <object>()), Times.Exactly(3)); list.Count.Should().Be(result.Count); list[0].Prop.Should().Be(result[0].Prop); list[1].Prop.Should().Be(result[1].Prop); list[2].Prop.Should().Be(result[2].Prop); }
public void Can_read_write_int_max_value() { Mock.Setup(x => x.Property).Returns(int.MaxValue); NetTestHelper.RunQml(qmlApplicationEngine, @" import QtQuick 2.0 import tests 1.0 IntTestsQml { id: test Component.onCompleted: function() { test.property = test.property } } "); Mock.VerifyGet(x => x.Property, Times.Once); Mock.VerifySet(x => x.Property = int.MaxValue, Times.Once); }
public void Can_read_write_value_unicode() { Mock.Setup(x => x.Property).Returns("test Ώ value"); NetTestHelper.RunQml(qmlApplicationEngine, @" import QtQuick 2.0 import tests 1.0 StringTestsQml { id: test Component.onCompleted: function() { test.property = test.property } } "); Mock.VerifyGet(x => x.Property, Times.Once); Mock.VerifySet(x => x.Property = "test Ώ value", Times.Once); }
public void Can_read_property_true() { Mock.Setup(x => x.Property).Returns(true); NetTestHelper.RunQml(qmlApplicationEngine, @" import QtQuick 2.0 import tests 1.0 BoolTestsQml { id: test Component.onCompleted: function() { test.methodParameter(test.property) } } "); Mock.VerifyGet(x => x.Property, Times.Once); Mock.Verify(x => x.MethodParameter(It.Is <bool>(y => y))); }
public void Can_read_write_char_null() { Mock.Setup(x => x.Property).Returns((char)0); NetTestHelper.RunQml(qmlApplicationEngine, @" import QtQuick 2.0 import tests 1.0 CharTestsQml { id: test Component.onCompleted: function() { test.property = test.property } } "); Mock.VerifyGet(x => x.Property, Times.Once); Mock.VerifySet(x => x.Property = (char)0, Times.Once); }
public void Can_read_nullable_char_with_value() { Mock.Setup(x => x.Nullable).Returns('t'); Mock.Setup(x => x.MethodParameterNullable(It.Is <char>(y => y == 't'))); NetTestHelper.RunQml(qmlApplicationEngine, @" import QtQuick 2.0 import tests 1.0 CharTestsQml { id: test Component.onCompleted: function() { test.methodParameterNullable(test.nullable) } } "); Mock.VerifyGet(x => x.Nullable, Times.Once); Mock.Verify(x => x.MethodParameterNullable(It.Is <char?>(y => y == 't')), Times.Once); }
public void Can_read_write_property_nullable_without_value() { Mock.SetupGet(x => x.Nullable).Returns((DateTimeOffset?)null); Mock.SetupSet(x => x.Nullable = null); NetTestHelper.RunQml(qmlApplicationEngine, @" import QtQuick 2.0 import tests 1.0 DateTimeOffsetTestsQml { id: test Component.onCompleted: function() { var v = test.nullable test.nullable = v } } "); Mock.VerifyGet(x => x.Nullable, Times.Once); Mock.VerifySet(x => x.Nullable = null); }
public void Can_read_write_property() { var value = DateTime.Now; Mock.SetupGet(x => x.Property).Returns(value); Mock.SetupSet(x => x.Property = value); NetTestHelper.RunQml(qmlApplicationEngine, @" import QtQuick 2.0 import tests 1.0 DateTimeTestsQml { id: test Component.onCompleted: function() { test.property = test.property } } "); Mock.VerifyGet(x => x.Property, Times.Once); Mock.VerifySet(x => x.Property = value, Times.Once); }
public void Can_read_properties_from_js_object() { var testObject = new JsValueTests.JsTestsQml.TestObject(); dynamic result = null; Mock.Setup(x => x.Method(It.IsAny <INetJsValue>())).Callback(new Action <dynamic>(jsValue => { result = jsValue; })); Mock.Setup(x => x.GetTestObject()).Returns(testObject); NetTestHelper.RunQml(qmlApplicationEngine, @" import QtQuick 2.0 import tests 1.0 JsTestsQml { id: test Component.onCompleted: function() { var netObject = test.getTestObject() test.method({ test1: 34, test2: 'test3', test3: netObject, test4: { test5: 'test5' } }) } } "); Mock.Verify(x => x.Method(It.IsAny <INetJsValue>()), Times.Exactly(1)); Mock.Verify(x => x.GetTestObject(), Times.Exactly(1)); ((object)result.nonExistant).Should().BeNull(); ((int)result.test1).Should().Be(34); ((string)result.test2).Should().Be("test3"); ((object)result.test3).Should().BeSameAs(testObject); ((string)result.test4.test5).Should().Be("test5"); }