Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit aa8b615

Browse filesBrowse files
committed
Removed the need for TestContext. Simplified some tests.
1 parent 1786cf9 commit aa8b615
Copy full SHA for aa8b615

File tree

4 files changed

+72
-230
lines changed
Filter options

4 files changed

+72
-230
lines changed

‎src/native/QmlNet/QmlNet/qml/NetTestHelper.cpp

Copy file name to clipboardExpand all lines: src/native/QmlNet/QmlNet/qml/NetTestHelper.cpp
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ Q_DECL_EXPORT uchar net_test_helper_runQml(QQmlApplicationEngineContainer* qmlEn
232232
QMetaObject::invokeMethod(object, "runTest");
233233

234234
if(runEvents == 1) {
235+
QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete);
235236
QCoreApplication::processEvents(QEventLoop::AllEvents);
236237
}
237238

‎src/net/Qml.Net.Tests/Qml/BaseQmlTests.cs

Copy file name to clipboardExpand all lines: src/net/Qml.Net.Tests/Qml/BaseQmlTests.cs
-23Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,6 @@
99

1010
namespace Qml.Net.Tests.Qml
1111
{
12-
public class TestContext
13-
{
14-
public void Quit()
15-
{
16-
Quited = true;
17-
}
18-
19-
public bool Quited { get; set; }
20-
}
21-
2212
public abstract class AbstractBaseQmlTests<TTypeToRegister> : BaseTests
2313
{
2414
private readonly QGuiApplication _coreApplication;
@@ -27,20 +17,13 @@ public abstract class AbstractBaseQmlTests<TTypeToRegister> : BaseTests
2717
protected MockTypeCreator TypeCreator { get; private set; }
2818

2919
readonly List<Type> _registeredTypes = new List<Type>();
30-
static bool _testContextRegistered = false;
3120

3221
protected AbstractBaseQmlTests()
3322
{
3423
_coreApplication = new QGuiApplication(new[] { "-platform", "offscreen" });
3524
qmlApplicationEngine = new QQmlApplicationEngine();
3625
TypeCreator = new MockTypeCreator();
3726
Net.TypeCreator.Current = TypeCreator;
38-
TypeCreator.SetInstance(typeof(TestContext), new TestContext());
39-
if (!_testContextRegistered)
40-
{
41-
Net.Qml.RegisterType<TestContext>("testContext");
42-
_testContextRegistered = true;
43-
}
4427
}
4528

4629
protected virtual void RegisterType<T>()
@@ -50,12 +33,6 @@ protected virtual void RegisterType<T>()
5033
Net.Qml.RegisterType<T>("tests");
5134
}
5235

53-
protected bool ExecApplicationWithTimeout(int timeoutMs)
54-
{
55-
var testContext = TypeCreator.Create(typeof(TestContext)) as TestContext;
56-
return QTest.QWaitFor(() => testContext.Quited, timeoutMs);
57-
}
58-
5936
protected void RunQmlTest(string instanceId, string componentOnCompletedCode, bool runEvents = false)
6037
{
6138
var result = NetTestHelper.RunQml(

‎src/net/Qml.Net.Tests/Qml/LifetimeTests.cs

Copy file name to clipboardExpand all lines: src/net/Qml.Net.Tests/Qml/LifetimeTests.cs
+45-158Lines changed: 45 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
using FluentAssertions;
33
using Xunit;
44

5-
// ReSharper disable MemberCanBePrivate.Global
6-
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
7-
// ReSharper disable UnusedMember.Global
8-
// ReSharper disable UnusedAutoPropertyAccessor.Global
95
namespace Qml.Net.Tests.Qml
106
{
117
public class LifetimeTests : BaseQmlTestsWithInstance<LifetimeTests.NetInteropTestQml>
@@ -27,6 +23,12 @@ public NetInteropTestQml()
2723
_parameterWeakRef = new WeakReference<SecondLevelType>(Parameter);
2824
}
2925

26+
public void CollectGc()
27+
{
28+
GC.Collect(GC.MaxGeneration);
29+
GC.WaitForPendingFinalizers();
30+
}
31+
3032
public SecondLevelType Parameter { get; set; }
3133

3234
public SecondLevelType Parameter2 { get; set; }
@@ -51,25 +53,14 @@ public bool CheckIsParameterAlive()
5153
[Fact]
5254
public void Can_handle_multiple_instances_equality_qml()
5355
{
54-
qmlApplicationEngine.LoadData(@"
55-
import QtQuick 2.0
56-
import tests 1.0
57-
import testContext 1.0
58-
59-
Item {
60-
NetInteropTestQml {
61-
id: test
62-
Component.onCompleted: function() {
63-
var instance1 = test.parameter
64-
var instance2 = test.parameter
56+
RunQmlTest("test",
57+
@"
58+
var instance1 = test.parameter
59+
var instance2 = test.parameter
6560
66-
test.testResult = instance1.isSame(instance2)
67-
}
68-
}
69-
}
61+
test.testResult = instance1.isSame(instance2)
7062
");
71-
//ExecApplicationWithTimeout(2000).Should().BeTrue();
72-
63+
7364
Assert.True(Instance.TestResult);
7465
}
7566

@@ -97,9 +88,7 @@ public void Can_handle_instance_deref_of_one_ref_in_qml()
9788
9889
//deref Parameter
9990
instance2 = null;
100-
101-
gc();
102-
91+
10392
Qt.callLater(function() {
10493
test.testResult = test.checkIsParameterAlive();
10594
})
@@ -113,162 +102,60 @@ public void Can_handle_instance_deref_of_all_refs_in_qml()
113102
{
114103
RunQmlTest("test",
115104
@"
116-
var instance1 = test.parameter;
117-
var instance2 = test.parameter;
118-
119-
//deref Parameter
120-
instance1 = null;
121-
instance2 = null;
105+
var instance = test.parameter;
106+
instance = null;
122107
123-
gc();
108+
gc()
124109
125110
Qt.callLater(function() {
111+
test.releaseNetReferenceParameter()
112+
test.collectGc()
126113
test.testResult = test.checkIsParameterAlive();
127114
});
128115
", true);
129116

130-
Instance.TestResult.Should().BeTrue();
131-
}
132-
133-
[Fact]
134-
public void Can_handle_instance_deref_of_all_refs_in_qml_and_net()
135-
{
136-
qmlApplicationEngine.LoadData(@"
137-
import QtQuick 2.0
138-
import tests 1.0
139-
import testContext 1.0
140-
141-
Item {
142-
TestContext {
143-
id: tc
144-
}
145-
146-
Timer {
147-
id: checkAndQuitTimer
148-
running: false
149-
interval: 1000
150-
onTriggered: {
151-
test.testResult = test.checkIsParameterAlive();
152-
153-
tc.quit()
154-
}
155-
}
156-
157-
NetInteropTestQml {
158-
id: test
159-
Component.onCompleted: function() {
160-
var instance1 = test.parameter
161-
var instance2 = test.parameter
162-
163-
//deref Parameter
164-
instance1 = null
165-
instance2 = null
166-
test.releaseNetReferenceParameter()
167-
168-
gc()
169-
Net.gcCollect(2)
170-
171-
checkAndQuitTimer.running = true
172-
}
173-
}
174-
}
175-
");
176-
177-
ExecApplicationWithTimeout(3000).Should().BeTrue();
178-
179-
Assert.False(Instance.TestResult);
117+
Instance.TestResult.Should().BeFalse();
180118
}
181-
119+
182120
[Fact]
183-
public void Can_handle_qml_reference_keeps_net_object_alive()
121+
public void Can_handle_instance_ref_of_all_refs_in_qml()
184122
{
185-
qmlApplicationEngine.LoadData(@"
186-
import QtQuick 2.0
187-
import tests 1.0
188-
import testContext 1.0
189-
190-
Item {
191-
TestContext {
192-
id: tc
193-
}
194-
195-
Timer {
196-
id: checkAndQuitTimer
197-
running: false
198-
interval: 1000
199-
onTriggered: {
200-
test.testResult = test.checkIsParameterAlive();
201-
202-
tc.quit()
203-
}
204-
}
205-
206-
NetInteropTestQml {
207-
id: test
208-
Component.onCompleted: function() {
209-
var instance1 = test.parameter
210-
211-
test.releaseNetReferenceParameter()
212-
Net.gcCollect(2)
123+
RunQmlTest("test",
124+
@"
125+
var instance = test.parameter;
213126
214-
checkAndQuitTimer.running = true
215-
}
216-
}
217-
}
218-
");
127+
gc()
219128
220-
ExecApplicationWithTimeout(3000).Should().BeTrue();
129+
Qt.callLater(function() {
130+
test.releaseNetReferenceParameter()
131+
test.collectGc()
132+
test.testResult = test.checkIsParameterAlive();
133+
});
134+
", true);
221135

222-
Assert.True(Instance.TestResult);
136+
Instance.TestResult.Should().BeTrue();
223137
}
224138

225139
[Fact]
226140
public void Can_handle_deleting_one_qml_ref_does_not_release()
227141
{
228-
qmlApplicationEngine.LoadData(@"
229-
import QtQuick 2.0
230-
import tests 1.0
231-
import testContext 1.0
232-
233-
Item {
234-
property var instanceRef: null
235-
TestContext {
236-
id: tc
237-
}
238-
239-
Timer {
240-
id: checkAndQuitTimer
241-
running: false
242-
interval: 1000
243-
onTriggered: {
244-
test.testResult = test.checkIsParameterAlive();
245-
246-
tc.quit()
247-
}
248-
}
249-
250-
NetInteropTestQml {
251-
id: test
252-
Component.onCompleted: function() {
253-
instanceRef = test.parameter
254-
var instance2 = test.parameter
142+
RunQmlTest("test",
143+
@"
144+
var instance1 = test.parameter;
145+
var instance2 = test.parameter;
255146
256-
test.releaseNetReferenceParameter()
147+
instance1 = null;
257148
258-
//release second QML ref
259-
instance2 = null
260-
gc()
261-
Net.gcCollect(2)
149+
gc()
262150
263-
checkAndQuitTimer.running = true
264-
}
265-
}
266-
}
267-
");
268-
269-
ExecApplicationWithTimeout(3000).Should().BeTrue();
151+
Qt.callLater(function() {
152+
test.releaseNetReferenceParameter()
153+
test.collectGc()
154+
test.testResult = test.checkIsParameterAlive();
155+
});
156+
", true);
270157

271-
Assert.True(Instance.TestResult);
158+
Instance.TestResult.Should().BeTrue();
272159
}
273160
}
274161
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.