This repository was archived by the owner on Dec 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathall_test.go
More file actions
835 lines (772 loc) · 23.6 KB
/
all_test.go
File metadata and controls
835 lines (772 loc) · 23.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
package qml_test
import (
"encoding/base64"
"flag"
"fmt"
"github.com/niemeyer/qml"
"image/color"
"io/ioutil"
. "launchpad.net/gocheck"
"os"
"regexp"
"runtime"
"strings"
"testing"
"time"
)
func Test(t *testing.T) { TestingT(t) }
type S struct {
engine *qml.Engine
context *qml.Context
}
var _ = Suite(&S{})
func (s *S) SetUpSuite(c *C) {
qml.Init(nil)
}
func (s *S) SetUpTest(c *C) {
qml.SetLogger(c)
qml.CollectStats(true)
qml.ResetStats()
stats := qml.Stats()
if stats.EnginesAlive > 0 || stats.ValuesAlive > 0 || stats.ConnectionsAlive > 0 {
panic(fmt.Sprintf("Test started with values alive: %#v\n", stats))
}
s.engine = qml.NewEngine()
s.context = s.engine.Context()
}
func (s *S) TearDownTest(c *C) {
s.engine.Destroy()
retries := 30 // Three seconds top.
for {
// Do not call qml.Flush here. It creates a nested event loop that
// that attempts to process the deferred object deletes and cannot,
// because deferred deletes are only processed at the same loop level.
// So it *reposts* the deferred deletion event, in practice *preventing*
// these objects from being deleted.
runtime.GC()
stats := qml.Stats()
if stats.EnginesAlive == 0 && stats.ValuesAlive == 0 && stats.ConnectionsAlive == 0 {
break
}
if retries == 0 {
panic(fmt.Sprintf("there are values alive:\n%#v\n", stats))
}
retries--
time.Sleep(100 * time.Millisecond)
if retries%10 == 0 {
c.Logf("There are still objects alive; waiting for them to die: %#v\n", stats)
}
}
qml.SetLogger(nil)
}
type TestType struct {
private bool // Besides being private, also adds a gap in the reflect field index.
StringValue string
BoolValue bool
IntValue int
Int64Value int64
Int32Value int32
Float64Value float64
Float32Value float32
AnyValue interface{}
ObjectValue qml.Object
}
func (ts *TestType) StringMethod() string {
return ts.StringValue
}
func (ts *TestType) Mod(dividend, divisor int) (int, error) {
if divisor == 0 {
return 0, fmt.Errorf("<division by zero>")
}
return dividend % divisor, nil
}
func (ts *TestType) ChangeString(new string) (old string) {
old = ts.StringValue
ts.StringValue = new
return
}
func (ts *TestType) NotifyStringChanged() {
qml.Changed(ts, &ts.StringValue)
}
func (ts *TestType) IncrementInt() {
ts.IntValue++
}
func (s *S) TestEngineDestroyedUse(c *C) {
s.engine.Destroy()
s.engine.Destroy()
c.Assert(s.engine.Context, PanicMatches, "engine already destroyed")
}
var same = "<same>"
var getSetTests = []struct{ set, get interface{} }{
{"value", same},
{true, same},
{false, same},
{int64(42), int64(42)},
{float64(42), same},
{float32(42), same},
{new(TestType), same},
{nil, same},
{42, same},
}
func (s *S) TestContextGetSet(c *C) {
for i, t := range getSetTests {
want := t.get
if t.get == same {
want = t.set
}
s.context.SetVar("key", t.set)
c.Assert(s.context.Var("key"), Equals, want,
Commentf("entry %d is {%v (%T), %v (%T)}", i, t.set, t.set, t.get, t.get))
}
}
func (s *S) TestContextGetMissing(c *C) {
c.Assert(s.context.Var("missing"), Equals, nil)
}
func (s *S) TestContextSetVars(c *C) {
component, err := s.engine.LoadString("file.qml", "import QtQuick 2.0\nItem { width: 42 }")
c.Assert(err, IsNil)
root := component.Create(nil)
vars := TestType{
StringValue: "<content>",
BoolValue: true,
IntValue: 42,
Int64Value: 42,
Int32Value: 42,
Float64Value: 4.2,
Float32Value: 4.2,
AnyValue: nil,
ObjectValue: root,
}
s.context.SetVars(&vars)
c.Assert(s.context.Var("stringValue"), Equals, "<content>")
c.Assert(s.context.Var("boolValue"), Equals, true)
c.Assert(s.context.Var("intValue"), Equals, 42)
c.Assert(s.context.Var("int64Value"), Equals, int64(42))
c.Assert(s.context.Var("int32Value"), Equals, 42)
c.Assert(s.context.Var("float64Value"), Equals, float64(4.2))
c.Assert(s.context.Var("float32Value"), Equals, float32(4.2))
c.Assert(s.context.Var("anyValue"), Equals, nil)
vars.AnyValue = 42
c.Assert(s.context.Var("anyValue"), Equals, 42)
c.Assert(s.context.Var("objectValue").(qml.Object).Int("width"), Equals, 42)
}
func (s *S) TestComponentSetDataError(c *C) {
_, err := s.engine.LoadString("file.qml", "Item{}")
c.Assert(err, ErrorMatches, "file:.*/file.qml:1 Item is not a type")
}
func (s *S) TestComponentCreateWindow(c *C) {
data := `
import QtQuick 2.0
Item { width: 300; height: 200; }
`
component, err := s.engine.LoadString("file.qml", data)
c.Assert(err, IsNil)
// TODO How to test this more effectively?
window := component.CreateWindow(nil)
window.Show()
// Qt doesn't hide the Window if we call it too quickly. :-(
time.Sleep(100 * time.Millisecond)
window.Hide()
}
type TestData struct {
*C
engine *qml.Engine
context *qml.Context
component qml.Object
root qml.Object
value *TestType
}
var tests = []struct {
Summary string
Value TestType
Init func(d *TestData)
// The QML provided is run with the initial state above, and
// then checks are made to ensure the provided state is found.
QML string
QMLLog string
QMLValue TestType
// The function provided is run with the post-QML state above,
// and then checks are made to ensure the provided state is found.
Done func(d *TestData)
DoneLog string
DoneValue TestType
}{
{
Summary: "Setting and reading of context variables",
Value: TestType{StringValue: "<content>", IntValue: 42},
QML: `
Item {
Component.onCompleted: {
console.log("String is", value.stringValue)
console.log("Int is", value.intValue)
console.log("Any is", value.anyValue)
}
}
`,
QMLLog: "String is <content>.*Int is 42.*Any is undefined",
},
{
Summary: "Reading of nested field via a value (not pointer) in an interface",
Value: TestType{AnyValue: TestType{StringValue: "<content>"}},
QML: `Item { Component.onCompleted: console.log("String is", value.anyValue.stringValue) }`,
QMLLog: "String is <content>",
},
{
Summary: "Reading of value fields",
QML: `Item { width: 123 }`,
Done: func(d *TestData) { d.Check(d.root.Int("width"), Equals, 123) },
},
{
Summary: "Read object properties",
QML: `
Item {
property bool boolp: true
property int intp: 1
property var int64p: 4294967296
property real float32p: 1.1
property double float64p: 1.1
property string stringp: "<content>"
property var objectp: Rectangle { width: 123 }
property var nilp: null
}
`,
Done: func(d *TestData) {
obj := d.root
d.Check(obj.Bool("boolp"), Equals, true)
d.Check(obj.Int("intp"), Equals, 1)
d.Check(obj.Int64("intp"), Equals, int64(1))
d.Check(obj.Int64("int64p"), Equals, int64(4294967296))
d.Check(obj.Float64("intp"), Equals, float64(1))
d.Check(obj.Float64("int64p"), Equals, float64(4294967296))
d.Check(obj.Float64("float32p"), Equals, float64(1.1))
d.Check(obj.Float64("float64p"), Equals, float64(1.1))
d.Check(obj.String("stringp"), Equals, "<content>")
d.Check(obj.Object("objectp").Int("width"), Equals, 123)
d.Check(obj.Property("nilp"), Equals, nil)
d.Check(func() { obj.Bool("intp") }, Panics, `value of property "intp" is not a bool: 1`)
d.Check(func() { obj.Int("boolp") }, Panics, `value of property "boolp" cannot be represented as an int: true`)
d.Check(func() { obj.Int64("boolp") }, Panics, `value of property "boolp" cannot be represented as an int64: true`)
d.Check(func() { obj.Float64("boolp") }, Panics, `value of property "boolp" cannot be represented as a float64: true`)
d.Check(func() { obj.String("boolp") }, Panics, `value of property "boolp" is not a string: true`)
d.Check(func() { obj.Object("boolp") }, Panics, `value of property "boolp" is not a QML object: true`)
d.Check(func() { obj.Property("missing") }, Panics, `object does not have a "missing" property`)
},
},
{
Summary: "No access to private fields",
Value: TestType{private: true},
QML: `Item { Component.onCompleted: console.log("Private is", value.private); }`,
QMLLog: "Private is undefined",
},
{
Summary: "Setting of value fields",
QML: `
Item {
property var obj: null
onObjChanged: console.log("String is", obj.stringValue)
onWidthChanged: console.log("Width is", width)
onHeightChanged: console.log("Height is", height)
}
`,
Done: func(d *TestData) {
value := TestType{StringValue: "<content>"}
d.root.Set("obj", &value)
d.root.Set("width", 300)
d.root.Set("height", 200)
},
DoneLog: "String is <content>.*Width is 300.*Height is 200",
},
{
Summary: "Reading and setting of a QUrl property",
QML: `import QtWebKit 3.0; WebView {}`,
Done: func(d *TestData) {
d.Check(d.root.String("url"), Equals, "")
url := "http://localhost:54321"
d.root.Set("url", url)
d.Check(d.root.String("url"), Equals, url)
},
},
{
Summary: "Reading and setting of a QColor property",
QML: `Text{ color: Qt.rgba(1/16, 1/8, 1/4, 1/2); function hasColor(c) { return Qt.colorEqual(color, c) }}`,
Done: func(d *TestData) {
d.Assert(d.root.Color("color"), Equals, color.RGBA{256 / 16, 256 / 8, 256 / 4, 256 / 2})
d.root.Set("color", color.RGBA{256 / 2, 256 / 4, 256 / 8, 256 / 16})
d.Assert(d.root.Call("hasColor", color.RGBA{256 / 2, 256 / 4, 256 / 8, 256 / 16}), Equals, true)
},
},
{
Summary: "Identical values remain identical when possible",
Init: func(d *TestData) {
d.context.SetVar("a", d.value)
d.context.SetVar("b", d.value)
},
QML: `Item { Component.onCompleted: console.log('Identical:', a === b); }`,
QMLLog: "Identical: true",
},
{
Summary: "Object finding via objectName",
QML: `Item { Item { objectName: "subitem"; property string s: "<found>" } }`,
Done: func(d *TestData) {
obj := d.root.ObjectByName("subitem")
d.Check(obj.String("s"), Equals, "<found>")
d.Check(func() { d.root.ObjectByName("foo") }, Panics, `cannot find descendant with objectName == "foo"`)
},
},
{
Summary: "Register Go type",
Value: TestType{StringValue: "<content>"},
QML: `
import GoTest 4.2
GoType { Component.onCompleted: console.log("String is", stringValue) }
`,
QMLLog: "String is <content>",
},
{
Summary: "Write Go type property",
QML: `
import GoTest 4.2
GoType { stringValue: "<new>"; intValue: 300 }
`,
QMLValue: TestType{StringValue: "<new>", IntValue: 300},
},
{
Summary: "Singleton type registration",
Value: TestType{StringValue: "<content>"},
QML: `
import GoTest 4.2
Item { Component.onCompleted: console.log("String is", GoSingleton.stringValue) }
`,
QMLLog: "String is <content>",
},
{
Summary: "qml.Changed on unknown value is okay",
Value: TestType{StringValue: "<old>"},
Init: func(d *TestData) {
value := &TestType{}
qml.Changed(&value, &value.StringValue)
},
QML: `Item{}`,
},
{
Summary: "qml.Changed triggers a QML slot",
Value: TestType{StringValue: "<old>"},
QML: `
import GoTest 4.2
GoType { onStringValueChanged: console.log("String is", stringValue) }
`,
QMLLog: "!String is",
QMLValue: TestType{StringValue: "<old>"},
Done: func(d *TestData) {
d.value.StringValue = "<new>"
qml.Changed(d.value, &d.value.StringValue)
},
DoneLog: "String is <new>",
DoneValue: TestType{StringValue: "<new>"},
},
{
Summary: "qml.Changed must not trigger on the wrong field",
Value: TestType{StringValue: "<old>"},
QML: `
import GoTest 4.2
GoType { onStringValueChanged: console.log("String is", stringValue) }
`,
Done: func(d *TestData) {
d.value.StringValue = "<new>"
qml.Changed(d.value, &d.value.IntValue)
},
DoneLog: "!String is",
},
{
Summary: "qml.Changed triggers multiple wrappers of the same value",
Value: TestType{StringValue: "<old>"},
Init: func(d *TestData) {
d.context.SetVar("v1", d.value)
d.context.SetVar("v2", d.value)
d.context.SetVar("v3", d.value)
},
QML: `
import GoTest 4.2
Item {
property var p1: GoType { onStringValueChanged: console.log("p1 has", stringValue) }
property var p2: GoType { onStringValueChanged: console.log("p2 has", stringValue) }
property var p3: GoType { onStringValueChanged: console.log("p3 has", stringValue) }
Connections { target: v1; onStringValueChanged: console.log("v1 has", v1.stringValue) }
Connections { target: v2; onStringValueChanged: console.log("v2 has", v2.stringValue) }
Connections { target: v3; onStringValueChanged: console.log("v3 has", v3.stringValue) }
}
`,
QMLLog: "![pv][123] has <old>",
QMLValue: TestType{StringValue: "<old>"},
Done: func(d *TestData) {
d.value.StringValue = "<new>"
qml.Changed(d.value, &d.value.StringValue)
},
// Why are v3-v1 reversed? Is QML registering connections in reversed order?
DoneLog: "v3 has <new>.*v2 has <new>.*v1 has <new>.*p1 has <new>.*p2 has <new>.*p3 has <new>.*",
},
{
Summary: "qml.Changed updates bindings",
Value: TestType{StringValue: "<old>"},
QML: `Item { property string s: "String is " + value.stringValue }`,
Done: func(d *TestData) {
d.value.StringValue = "<new>"
qml.Changed(d.value, &d.value.StringValue)
d.Check(d.root.String("s"), Equals, "String is <new>")
},
},
{
Summary: "Call a Go method without arguments or result",
Value: TestType{IntValue: 42},
QML: `Item { Component.onCompleted: console.log("Undefined is", value.incrementInt()); }`,
QMLLog: "Undefined is undefined",
QMLValue: TestType{IntValue: 43},
},
{
Summary: "Call a Go method with one argument and one result",
Value: TestType{StringValue: "<old>"},
QML: `Item { Component.onCompleted: console.log("String was", value.changeString("<new>")); }`,
QMLLog: "String was <old>",
QMLValue: TestType{StringValue: "<new>"},
},
{
Summary: "Call a Go method with multiple results",
QML: `
Item {
Component.onCompleted: {
var r = value.mod(42, 4);
console.log("mod is", r[0], "and err is", r[1]);
}
}
`,
QMLLog: `mod is 2 and err is undefined`,
},
{
Summary: "Call a Go method that returns an error",
QML: `
Item {
Component.onCompleted: {
var r = value.mod(0, 0);
console.log("err is", r[1].error());
}
}
`,
QMLLog: `err is <division by zero>`,
},
{
Summary: "Call a Go method that recurses back into the GUI thread",
QML: `
Item {
Connections {
target: value
onStringValueChanged: console.log("Notification arrived")
}
Component.onCompleted: {
value.notifyStringChanged()
}
}
`,
QMLLog: "Notification arrived",
},
{
Summary: "Connect a QML signal to a Go method",
Value: TestType{StringValue: "<old>"},
QML: `
Item {
id: item
signal testSignal(string s)
Component.onCompleted: {
item.testSignal.connect(value.changeString)
item.testSignal("<new>")
}
}
`,
QMLValue: TestType{StringValue: "<new>"},
},
{
Summary: "Call a QML method with no result or parameters from Go",
QML: `Item { function f() { console.log("f was called"); } }`,
Done: func(d *TestData) { d.Check(d.root.Call("f"), IsNil) },
DoneLog: "f was called",
},
{
Summary: "Call a QML method with result and parameters from Go",
QML: `Item { function add(a, b) { return a+b; } }`,
Done: func(d *TestData) { d.Check(d.root.Call("add", 1, 2), Equals, 3) },
},
{
Summary: "Call a QML method with a custom type",
Value: TestType{StringValue: "<content>"},
QML: `Item { function log(value) { console.log("String is", value.stringValue) } }`,
Done: func(d *TestData) { d.root.Call("log", d.value) },
DoneLog: "String is <content>",
},
{
Summary: "Call a QML method that returns a QML object",
QML: `
Item {
property var custom: Rectangle { width: 300; }
function f() { return custom }
}
`,
Done: func(d *TestData) {
d.Check(d.root.Call("f").(qml.Object).Int("width"), Equals, 300)
},
},
{
Summary: "Call a QML method that holds a custom type past the return point",
QML: `
Item {
property var held
function hold(v) { held = v; gc(); gc(); }
function log() { console.log("String is", held.stringValue) }
}`,
Done: func(d *TestData) {
value := TestType{StringValue: "<content>"}
stats := qml.Stats()
d.root.Call("hold", &value)
d.Check(qml.Stats().ValuesAlive, Equals, stats.ValuesAlive+1)
d.root.Call("log")
d.root.Call("hold", nil)
d.Check(qml.Stats().ValuesAlive, Equals, stats.ValuesAlive)
},
DoneLog: "String is <content>",
},
{
Summary: "Ensure URL of provided file is correct by loading a local file",
Init: func(d *TestData) {
data, err := base64.StdEncoding.DecodeString("R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==")
d.Assert(err, IsNil)
err = ioutil.WriteFile("test.gif", data, 0644)
d.Check(err, IsNil)
},
QML: `Image { source: "test.gif"; Component.onCompleted: console.log("Ready:", status == Image.Ready) }`,
QMLLog: "Ready: true",
Done: func(d *TestData) { os.Remove("test.gif") },
},
{
Summary: "Create window with non-window root object",
QML: `Rectangle { width: 300; height: 200; function inc(x) { return x+1 } }`,
Done: func(d *TestData) {
win := d.component.CreateWindow(nil)
root := win.Root()
d.Check(root.Int("width"), Equals, 300)
d.Check(root.Int("height"), Equals, 200)
d.Check(root.Call("inc", 42), Equals, 43)
root.Destroy()
},
},
{
Summary: "Create window with window root object",
QML: `
import QtQuick.Window 2.0
Window { title: "<title>"; width: 300; height: 200 }
`,
Done: func(d *TestData) {
win := d.component.CreateWindow(nil)
root := win.Root()
d.Check(root.String("title"), Equals, "<title>")
d.Check(root.Int("width"), Equals, 300)
d.Check(root.Int("height"), Equals, 200)
},
},
{
Summary: "Window is object",
QML: `Item {}`,
Done: func(d *TestData) {
win := d.component.CreateWindow(nil)
d.Assert(win.Int("status"), Equals, 1) // Ready
},
},
{
Summary: "Pass a *Value back into a method",
QML: `Rectangle { width: 300; function log(r) { console.log("Width is", r.width) } }`,
Done: func(d *TestData) { d.root.Call("log", d.root) },
DoneLog: "Width is 300",
},
{
Summary: "Create a QML-defined component in Go",
QML: `Item { property var comp: Component { Rectangle { width: 300 } } }`,
Done: func(d *TestData) {
rect := d.root.Object("comp").Create(nil)
d.Check(rect.Int("width"), Equals, 300)
d.Check(func() { d.root.Create(nil) }, Panics, "object is not a component")
d.Check(func() { d.root.CreateWindow(nil) }, Panics, "object is not a component")
},
},
{
Summary: "Call a Qt method that has no result",
QML: `Item { Component.onDestruction: console.log("item destroyed") }`,
Done: func(d *TestData) {
// Create a local instance to avoid double-destroying it.
root := d.component.Create(nil)
root.Call("deleteLater")
time.Sleep(100 * time.Millisecond)
},
DoneLog: "item destroyed",
},
{
Summary: "Errors connecting to QML signals",
QML: `Item { signal doIt() }`,
Done: func(d *TestData) {
d.Check(func() { d.root.On("missing", func() {}) }, Panics, `object does not expose a "missing" signal`)
d.Check(func() { d.root.On("doIt", func(s string) {}) }, Panics, `signal "doIt" has too few parameters for provided function`)
},
},
{
Summary: "Connect to a QML signal without parameters",
QML: `
Item {
id: item
signal doIt()
function emitDoIt() { item.doIt() }
}
`,
Done: func(d *TestData) {
itWorks := false
d.root.On("doIt", func() { itWorks = true })
d.Check(itWorks, Equals, false)
d.root.Call("emitDoIt")
d.Check(itWorks, Equals, true)
},
},
{
Summary: "Connect to a QML signal with a parameters",
QML: `
Item {
id: item
signal doIt(string s, int n)
function emitDoIt() { item.doIt("<arg>", 123) }
}
`,
Done: func(d *TestData) {
var stack []interface{}
d.root.On("doIt", func() { stack = append(stack, "A") })
d.root.On("doIt", func(s string) { stack = append(stack, "B", s) })
d.root.On("doIt", func(s string, i int) { stack = append(stack, "C", s, i) })
d.Check(stack, IsNil)
d.root.Call("emitDoIt")
d.Check(stack, DeepEquals, []interface{}{"A", "B", "<arg>", "C", "<arg>", 123})
},
},
{
Summary: "Connect to a QML signal with an object parameter",
QML: `import QtWebKit 3.0; WebView{}`,
Done: func(d *TestData) {
url := "http://localhost:54321/"
done := make(chan bool)
d.root.On("navigationRequested", func(request qml.Object) {
d.Check(request.String("url"), Equals, url)
done <- true
})
d.root.Set("url", url)
<-done
},
},
}
var tablef = flag.String("tablef", "", "if provided, TestTable only runs tests with a summary matching the regexp")
func (s *S) TestTable(c *C) {
var goTypeValue *TestType = &TestType{}
typeSpec := qml.TypeSpec{
Location: "GoTest",
Major: 4,
Minor: 2,
Name: "GoType",
New: func() interface{} { return goTypeValue },
}
err := qml.RegisterType(&typeSpec)
c.Assert(err, IsNil)
singletonSpec := qml.TypeSpec{
Location: "GoTest",
Major: 4,
Minor: 2,
Name: "GoSingleton",
New: func() interface{} { return goTypeValue },
}
err = qml.RegisterSingleton(&singletonSpec)
c.Assert(err, IsNil)
filter := regexp.MustCompile("")
if tablef != nil {
filter = regexp.MustCompile(*tablef)
}
for i := range tests {
s.TearDownTest(c)
t := &tests[i]
header := fmt.Sprintf("----- Running table test %d: %s -----", i, t.Summary)
if !filter.MatchString(header) {
continue
}
c.Log(header)
s.SetUpTest(c)
value := t.Value
goTypeValue = &value
s.context.SetVar("value", &value)
testData := TestData{
C: c,
value: &value,
engine: s.engine,
context: s.context,
}
if t.Init != nil {
t.Init(&testData)
if c.Failed() {
c.FailNow()
}
}
component, err := s.engine.LoadString("file.qml", "import QtQuick 2.0\n"+strings.TrimSpace(t.QML))
c.Assert(err, IsNil)
logMark := c.GetTestLog()
// The component instance is destroyed before the loop ends below,
// but do a defer to ensure it will be destroyed if the test fails.
root := component.Create(nil)
defer root.Destroy()
testData.component = component
testData.root = root
if t.QMLLog != "" {
logged := c.GetTestLog()[len(logMark):]
if t.QMLLog[0] == '!' {
c.Check(logged, Not(Matches), "(?s).*"+t.QMLLog[1:]+".*")
} else {
c.Check(logged, Matches, "(?s).*"+t.QMLLog+".*")
}
}
if t.QMLValue != (TestType{}) {
c.Check(value.StringValue, Equals, t.QMLValue.StringValue)
c.Check(value.BoolValue, Equals, t.QMLValue.BoolValue)
c.Check(value.IntValue, Equals, t.QMLValue.IntValue)
c.Check(value.Int64Value, Equals, t.QMLValue.Int64Value)
c.Check(value.Int32Value, Equals, t.QMLValue.Int32Value)
c.Check(value.Float64Value, Equals, t.QMLValue.Float64Value)
c.Check(value.Float32Value, Equals, t.QMLValue.Float32Value)
c.Check(value.AnyValue, Equals, t.QMLValue.AnyValue)
}
if !c.Failed() {
logMark := c.GetTestLog()
if t.Done != nil {
t.Done(&testData)
}
if t.DoneLog != "" {
logged := c.GetTestLog()[len(logMark):]
if t.DoneLog[0] == '!' {
c.Check(logged, Not(Matches), "(?s).*"+t.DoneLog[1:]+".*")
} else {
c.Check(logged, Matches, "(?s).*"+t.DoneLog+".*")
}
}
if t.DoneValue != (TestType{}) {
c.Check(value.StringValue, Equals, t.DoneValue.StringValue)
c.Check(value.BoolValue, Equals, t.DoneValue.BoolValue)
c.Check(value.IntValue, Equals, t.DoneValue.IntValue)
c.Check(value.Int64Value, Equals, t.DoneValue.Int64Value)
c.Check(value.Int32Value, Equals, t.DoneValue.Int32Value)
c.Check(value.Float64Value, Equals, t.DoneValue.Float64Value)
c.Check(value.Float32Value, Equals, t.DoneValue.Float32Value)
c.Check(value.AnyValue, Equals, t.DoneValue.AnyValue)
}
}
root.Destroy()
if c.Failed() {
c.FailNow() // So relevant logs are at the bottom.
}
}
}