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
|
package prop
import (
"reflect"
"testing"
"github.com/godbus/dbus/v5"
)
type Foo struct {
Id int
Value string
}
func comparePropValue(obj dbus.BusObject, name string, want interface{}, t *testing.T) {
r, err := obj.GetProperty("org.guelfey.DBus.Test." + name)
if err != nil {
t.Fatal(err)
}
haveValue := reflect.New(reflect.TypeOf(want)).Interface()
dbus.Store([]interface{}{r.Value()}, haveValue)
have := reflect.ValueOf(haveValue).Elem().Interface()
if !reflect.DeepEqual(have, want) {
t.Errorf("struct comparison failed: got '%v', want '%v'", have, want)
}
}
func TestValidateStructsAsProp(t *testing.T) {
srv, err := dbus.SessionBus()
if err != nil {
t.Fatal(err)
}
defer srv.Close()
cli, err := dbus.SessionBus()
if err != nil {
t.Fatal(err)
}
defer cli.Close()
foo := Foo{Id: 1, Value: "First"}
fooPtr := &Foo{Id: 1, Value: "1st"}
foos := make([]Foo, 2)
foos[0] = Foo{Id: 1, Value: "Ones"}
foos[1] = Foo{Id: 2, Value: "Twos"}
propsSpec := map[string]map[string]*Prop{
"org.guelfey.DBus.Test": {
"FooStruct": {
foo,
true,
EmitTrue,
nil,
},
"FooStructPtr": {
&fooPtr,
true,
EmitTrue,
nil,
},
"SliceOfFoos": {
foos,
true,
EmitTrue,
nil,
},
},
}
props := New(srv, "/org/guelfey/DBus/Test", propsSpec)
obj := cli.Object(srv.Names()[0], "/org/guelfey/DBus/Test")
comparePropValue(obj, "FooStruct", foo, t)
comparePropValue(obj, "FooStructPtr", *fooPtr, t)
comparePropValue(obj, "SliceOfFoos", foos, t)
yoo := Foo{Id: 2, Value: "Second"}
yooPtr := &Foo{Id: 2, Value: "2nd"}
yoos := make([]Foo, 2)
yoos[0] = Foo{Id: 3, Value: "Threes"}
yoos[1] = Foo{Id: 4, Value: "Fours"}
obj.SetProperty("org.guelfey.DBus.Test.FooStruct", dbus.MakeVariant(yoo))
obj.SetProperty("org.guelfey.DBus.Test.FooStructPtr", dbus.MakeVariant(yooPtr))
obj.SetProperty("org.guelfey.DBus.Test.SliceOfFoos", dbus.MakeVariant(yoos))
comparePropValue(obj, "FooStruct", yoo, t)
comparePropValue(obj, "FooStructPtr", *yooPtr, t)
comparePropValue(obj, "SliceOfFoos", yoos, t)
props.SetMust("org.guelfey.DBus.Test", "SliceOfFoos", foos)
comparePropValue(obj, "SliceOfFoos", foos, t)
zoo := Foo{Id: 3, Value: "Third"}
zooPtr := &Foo{Id: 3, Value: "3th"}
zoos := make([]Foo, 2)
zoos[0] = Foo{Id: 5, Value: "Sevens"}
zoos[1] = Foo{Id: 6, Value: "Sixes"}
obj.SetProperty("org.guelfey.DBus.Test.FooStruct", dbus.MakeVariant(zoo))
obj.SetProperty("org.guelfey.DBus.Test.FooStructPtr", dbus.MakeVariant(zooPtr))
obj.SetProperty("org.guelfey.DBus.Test.SliceOfFoos", dbus.MakeVariant(zoos))
comparePropValue(obj, "FooStruct", zoo, t)
comparePropValue(obj, "FooStructPtr", *zooPtr, t)
comparePropValue(obj, "SliceOfFoos", zoos, t)
}
func TestInt32(t *testing.T) {
srv, err := dbus.SessionBus()
if err != nil {
t.Fatal(err)
}
defer srv.Close()
cli, err := dbus.SessionBus()
if err != nil {
t.Fatal(err)
}
defer cli.Close()
propsSpec := map[string]map[string]*Prop{
"org.guelfey.DBus.Test": {
"int32": {
int32(100),
true,
EmitTrue,
nil,
},
},
}
props := New(srv, "/org/guelfey/DBus/Test", propsSpec)
obj := cli.Object(srv.Names()[0], "/org/guelfey/DBus/Test")
comparePropValue(obj, "int32", int32(100), t)
r := props.GetMust("org.guelfey.DBus.Test", "int32")
if r != int32(100) {
t.Errorf("expected r to be int32(100), but was %#v", r)
}
if err := props.Set("org.guelfey.DBus.Test", "int32", dbus.MakeVariant(int32(101))); err != nil {
t.Fatalf("failed to set prop int32 to 101")
}
comparePropValue(obj, "int32", int32(101), t)
r = props.GetMust("org.guelfey.DBus.Test", "int32")
if r != int32(101) {
t.Errorf("expected r to be int32(101), but was %#v", r)
}
}
|