1 package prop
2
3 import (
4 "reflect"
5 "testing"
6
7 "github.com/godbus/dbus/v5"
8 )
9
10 type Foo struct {
11 Id int
12 Value string
13 }
14
15 func comparePropValue(obj dbus.BusObject, name string, want interface{}, t *testing.T) {
16 r, err := obj.GetProperty("org.guelfey.DBus.Test." + name)
17 if err != nil {
18 t.Fatal(err)
19 }
20 haveValue := reflect.New(reflect.TypeOf(want)).Interface()
21 dbus.Store([]interface{}{r.Value()}, haveValue)
22 have := reflect.ValueOf(haveValue).Elem().Interface()
23 if !reflect.DeepEqual(have, want) {
24 t.Errorf("struct comparison failed: got '%v', want '%v'", have, want)
25 }
26 }
27
28 func TestValidateStructsAsProp(t *testing.T) {
29 srv, err := dbus.SessionBus()
30 if err != nil {
31 t.Fatal(err)
32 }
33 defer srv.Close()
34
35 cli, err := dbus.SessionBus()
36 if err != nil {
37 t.Fatal(err)
38 }
39 defer cli.Close()
40
41 foo := Foo{Id: 1, Value: "First"}
42 fooPtr := &Foo{Id: 1, Value: "1st"}
43 foos := make([]Foo, 2)
44 foos[0] = Foo{Id: 1, Value: "Ones"}
45 foos[1] = Foo{Id: 2, Value: "Twos"}
46
47 propsSpec := map[string]map[string]*Prop{
48 "org.guelfey.DBus.Test": {
49 "FooStruct": {
50 foo,
51 true,
52 EmitTrue,
53 nil,
54 },
55 "FooStructPtr": {
56 &fooPtr,
57 true,
58 EmitTrue,
59 nil,
60 },
61 "SliceOfFoos": {
62 foos,
63 true,
64 EmitTrue,
65 nil,
66 },
67 },
68 }
69 props := New(srv, "/org/guelfey/DBus/Test", propsSpec)
70
71 obj := cli.Object(srv.Names()[0], "/org/guelfey/DBus/Test")
72 comparePropValue(obj, "FooStruct", foo, t)
73 comparePropValue(obj, "FooStructPtr", *fooPtr, t)
74 comparePropValue(obj, "SliceOfFoos", foos, t)
75
76 yoo := Foo{Id: 2, Value: "Second"}
77 yooPtr := &Foo{Id: 2, Value: "2nd"}
78 yoos := make([]Foo, 2)
79 yoos[0] = Foo{Id: 3, Value: "Threes"}
80 yoos[1] = Foo{Id: 4, Value: "Fours"}
81 obj.SetProperty("org.guelfey.DBus.Test.FooStruct", dbus.MakeVariant(yoo))
82 obj.SetProperty("org.guelfey.DBus.Test.FooStructPtr", dbus.MakeVariant(yooPtr))
83 obj.SetProperty("org.guelfey.DBus.Test.SliceOfFoos", dbus.MakeVariant(yoos))
84 comparePropValue(obj, "FooStruct", yoo, t)
85 comparePropValue(obj, "FooStructPtr", *yooPtr, t)
86 comparePropValue(obj, "SliceOfFoos", yoos, t)
87
88 props.SetMust("org.guelfey.DBus.Test", "SliceOfFoos", foos)
89 comparePropValue(obj, "SliceOfFoos", foos, t)
90
91 zoo := Foo{Id: 3, Value: "Third"}
92 zooPtr := &Foo{Id: 3, Value: "3th"}
93 zoos := make([]Foo, 2)
94 zoos[0] = Foo{Id: 5, Value: "Sevens"}
95 zoos[1] = Foo{Id: 6, Value: "Sixes"}
96 obj.SetProperty("org.guelfey.DBus.Test.FooStruct", dbus.MakeVariant(zoo))
97 obj.SetProperty("org.guelfey.DBus.Test.FooStructPtr", dbus.MakeVariant(zooPtr))
98 obj.SetProperty("org.guelfey.DBus.Test.SliceOfFoos", dbus.MakeVariant(zoos))
99 comparePropValue(obj, "FooStruct", zoo, t)
100 comparePropValue(obj, "FooStructPtr", *zooPtr, t)
101 comparePropValue(obj, "SliceOfFoos", zoos, t)
102 }
103
104 func TestInt32(t *testing.T) {
105 srv, err := dbus.SessionBus()
106 if err != nil {
107 t.Fatal(err)
108 }
109 defer srv.Close()
110
111 cli, err := dbus.SessionBus()
112 if err != nil {
113 t.Fatal(err)
114 }
115 defer cli.Close()
116
117 propsSpec := map[string]map[string]*Prop{
118 "org.guelfey.DBus.Test": {
119 "int32": {
120 int32(100),
121 true,
122 EmitTrue,
123 nil,
124 },
125 },
126 }
127 props := New(srv, "/org/guelfey/DBus/Test", propsSpec)
128
129 obj := cli.Object(srv.Names()[0], "/org/guelfey/DBus/Test")
130
131 comparePropValue(obj, "int32", int32(100), t)
132 r := props.GetMust("org.guelfey.DBus.Test", "int32")
133 if r != int32(100) {
134 t.Errorf("expected r to be int32(100), but was %#v", r)
135 }
136
137 if err := props.Set("org.guelfey.DBus.Test", "int32", dbus.MakeVariant(int32(101))); err != nil {
138 t.Fatalf("failed to set prop int32 to 101")
139 }
140
141 comparePropValue(obj, "int32", int32(101), t)
142 r = props.GetMust("org.guelfey.DBus.Test", "int32")
143 if r != int32(101) {
144 t.Errorf("expected r to be int32(101), but was %#v", r)
145 }
146 }
147
View as plain text