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
|
# -*- perl -*-
use Test::More tests => 18;
use strict;
use warnings;
BEGIN {
use_ok('Net::DBus::Binding::Introspector');
use_ok('Net::DBus::Object');
};
package MyObject;
use base qw(Net::DBus::Object);
use Net::DBus::Exporter qw(org.example.MyObject);
# Typically one would use Class::MethodMaker, but I don't
# want to add a hard dependancy for the test suite.
#use Class::MethodMaker [ scalar => ["name", "email", "age" ]];
sub name {
my $self = shift;
$self->{name} = shift if @_;
return $self->{name};
}
sub email {
my $self = shift;
$self->{email} = shift if @_;
return $self->{email};
}
sub age {
my $self = shift;
$self->{age} = shift if @_;
return $self->{age};
}
sub parents {
my $self = shift;
$self->{parents} = shift if @_;
return $self->{parents};
}
sub height {
my $self = shift;
$self->{height} = shift if @_;
return $self->{height};
}
dbus_property("name", "string");
dbus_property("email", "string", "read");
dbus_property("age", "int32" ,"write");
dbus_property("parents", ["array", "string"]);
dbus_property("height", "double", "write");
package main;
use Net::DBus qw(:typing);
my $bus = Net::DBus->test;
my $service = $bus->export_service("org.cpan.Net.Bus.test");
my $object = MyObject->new($service, "/org/example/MyObject");
my $introspector = $object->_introspector;
my $xml_got = $introspector->format($object);
my $xml_expect = <<EOF;
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node name="/org/example/MyObject">
<interface name="org.example.MyObject">
<property name="age" type="i" access="write"/>
<property name="email" type="s" access="read"/>
<property name="height" type="d" access="write"/>
<property name="name" type="s" access="readwrite"/>
<property name="parents" type="as" access="readwrite"/>
</interface>
<interface name="org.freedesktop.DBus.Introspectable">
<method name="Introspect">
<arg type="s" direction="out"/>
</method>
</interface>
<interface name="org.freedesktop.DBus.Properties">
<method name="Get">
<arg type="s" direction="in"/>
<arg type="s" direction="in"/>
<arg type="v" direction="out"/>
</method>
<method name="GetAll">
<arg type="s" direction="in"/>
<arg type="a{sv}" direction="out"/>
</method>
<method name="Set">
<arg type="s" direction="in"/>
<arg type="s" direction="in"/>
<arg type="v" direction="in"/>
</method>
</interface>
</node>
EOF
is($xml_got, $xml_expect, "xml data matches");
GET_NAME: {
my $msg = Net::DBus::Binding::Message::MethodCall->new(service_name => "org.example.MyService",
object_path => "/org/example/MyObject",
interface => "org.freedesktop.DBus.Properties",
method_name => "Get");
my $iter = $msg->iterator(1);
$iter->append_string("org.example.MyObject");
$iter->append_string("name");
$object->name("John Doe");
my $reply = $bus->get_connection->send_with_reply_and_block($msg);
is($reply->get_type, &Net::DBus::Binding::Message::MESSAGE_TYPE_METHOD_RETURN);
my ($value) = $reply->get_args_list;
is($value, "John Doe", "name is John Doe");
}
GET_BOGUS: {
my $msg = Net::DBus::Binding::Message::MethodCall->new(service_name => "org.example.MyService",
object_path => "/org/example/MyObject",
interface => "org.freedesktop.DBus.Properties",
method_name => "Get");
my $iter = $msg->iterator(1);
$iter->append_string("org.example.MyObject");
$iter->append_string("bogus");
$object->name("John Doe");
my $reply = eval {
$bus->get_connection->send_with_reply_and_block($msg);
};
ok($@, "error is set");
}
sub GET_SET_NAME: {
my $msg1 = Net::DBus::Binding::Message::MethodCall->new(service_name => "org.example.MyService",
object_path => "/org/example/MyObject",
interface => "org.freedesktop.DBus.Properties",
method_name => "Get");
my $iter1 = $msg1->iterator(1);
$iter1->append_string("org.example.MyObject");
$iter1->append_string("name");
$object->name("John Doe");
my $reply1 = $bus->get_connection->send_with_reply_and_block($msg1);
is($reply1->get_type, &Net::DBus::Binding::Message::MESSAGE_TYPE_METHOD_RETURN);
my ($value1) = $reply1->get_args_list;
is($value1, "John Doe", "name is John Doe");
my $msg2 = Net::DBus::Binding::Message::MethodCall->new(service_name => "org.example.MyService",
object_path => "/org/example/MyObject",
interface => "org.freedesktop.DBus.Properties",
method_name => "Set");
my $iter2 = $msg2->iterator(1);
$iter2->append_string("org.example.MyObject");
$iter2->append_string("name");
$iter2->append_variant("Jane Doe");
my $reply2 = $bus->get_connection->send_with_reply_and_block($msg2);
is($reply2->get_type, &Net::DBus::Binding::Message::MESSAGE_TYPE_METHOD_RETURN);
my $reply3 = $bus->get_connection->send_with_reply_and_block($msg1);
is($reply3->get_type, &Net::DBus::Binding::Message::MESSAGE_TYPE_METHOD_RETURN);
my ($value2) = $reply3->get_args_list;
is($value2, "Jane Doe", "name is Jane Doe");
}
SET_AGE: {
my $msg1 = Net::DBus::Binding::Message::MethodCall->new(service_name => "org.example.MyService",
object_path => "/org/example/MyObject",
interface => "org.freedesktop.DBus.Properties",
method_name => "Get");
my $iter1 = $msg1->iterator(1);
$iter1->append_string("org.example.MyObject");
$iter1->append_string("age");
my $msg2 = Net::DBus::Binding::Message::MethodCall->new(service_name => "org.example.MyService",
object_path => "/org/example/MyObject",
interface => "org.freedesktop.DBus.Properties",
method_name => "Set");
my $iter2 = $msg2->iterator(1);
$iter2->append_string("org.example.MyObject");
$iter2->append_string("age");
$iter2->append_variant(21);
my $reply1 = $bus->get_connection->send_with_reply_and_block($msg2);
is($reply1->get_type, &Net::DBus::Binding::Message::MESSAGE_TYPE_METHOD_RETURN);
my $reply2 = eval {
$bus->get_connection->send_with_reply_and_block($msg1);
};
ok($@, "error is set");
is($object->age, 21, "age is 21");
}
GET_EMAIL: {
my $msg1 = Net::DBus::Binding::Message::MethodCall->new(service_name => "org.example.MyService",
object_path => "/org/example/MyObject",
interface => "org.freedesktop.DBus.Properties",
method_name => "Get");
my $iter1 = $msg1->iterator(1);
$iter1->append_string("org.example.MyObject");
$iter1->append_string("email");
$object->email('john@example.com');
my $msg2 = Net::DBus::Binding::Message::MethodCall->new(service_name => "org.example.MyService",
object_path => "/org/example/MyObject",
interface => "org.freedesktop.DBus.Properties",
method_name => "Set");
my $iter2 = $msg2->iterator(1);
$iter2->append_string("org.example.MyObject");
$iter2->append_string("email");
$iter2->append_variant('jane@example.com');
my $reply1 = eval {
$bus->get_connection->send_with_reply_and_block($msg2);
};
ok($@, "error is set");
my $reply2 = $bus->get_connection->send_with_reply_and_block($msg1);
is($reply2->get_type, &Net::DBus::Binding::Message::MESSAGE_TYPE_METHOD_RETURN);
is($object->age, 21, "age is 21");
my ($value) = $reply2->get_args_list;
is($value, 'john@example.com', 'email is john@example.com');
}
SET_HEIGHT: {
my $msg = $bus->get_connection()->make_method_call_message("org.example.MyService",
"/org/example/MyObject",
"org.freedesktop.DBus.Properties",
"Set");
$introspector->encode($msg, "methods", "Set", "params", "org.example.MyObject", "height", dbus_double(1.414));
is($msg->get_signature, "ssv", "signature is ssvd");
my $reply = $bus->get_connection->send_with_reply_and_block($msg);
is($reply->get_type, &Net::DBus::Binding::Message::MESSAGE_TYPE_METHOD_RETURN);
ok($object->height > 1.410 &&
$object->height < 1.420, "height is 1.414");
}
GET_ALL: {
my $msg = Net::DBus::Binding::Message::MethodCall->new(service_name => "org.example.MyService",
object_path => "/org/example/MyObject",
interface => "org.freedesktop.DBus.Properties",
method_name => "GetAll");
my $iter = $msg->iterator(1);
$iter->append_string("org.example.MyObject");
$iter->append_string("name");
my $reply = $bus->get_connection->send_with_reply_and_block($msg);
is($reply->get_type, &Net::DBus::Binding::Message::MESSAGE_TYPE_METHOD_RETURN);
my ($value) = $reply->get_args_list;
# we use sort because there is no strict order of keys(...) call result
is_deeply([sort(keys(%$value))], [sort("name", "email", "parents")], "all readable properties have been received");
}
|