File: 65-object-magic.t

package info (click to toggle)
libnet-dbus-perl 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 720 kB
  • sloc: perl: 4,919; sh: 34; makefile: 6
file content (182 lines) | stat: -rw-r--r-- 5,485 bytes parent folder | download | duplicates (5)
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
# -*- perl -*-
use Test::More tests => 13;

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);

dbus_method("test_set_serial", ["serial"]);
sub test_set_serial {
    my $self = shift;
    my @args = @_;
    $self->{lastargs} = \@args;
}

dbus_method("test_set_caller", ["caller"]);
sub test_set_caller {
    my $self = shift;
    my @args = @_;
    $self->{lastargs} = \@args;
}

dbus_method("test_set_multi_args1", ["string", "caller"]);
sub test_set_multi_args1 {
    my $self = shift;
    my @args = @_;
    $self->{lastargs} = \@args;
}

dbus_method("test_set_multi_args2", ["caller", "string"]);
sub test_set_multi_args2 {
    my $self = shift;
    my @args = @_;
    $self->{lastargs} = \@args;
}

dbus_method("test_set_multi_args3", ["string", "caller", "string"]);
sub test_set_multi_args3 {
    my $self = shift;
    my @args = @_;
    $self->{lastargs} = \@args;
}

package main;

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">
    <method name="test_set_caller">
    </method>
    <method name="test_set_multi_args1">
      <arg type="s" direction="in"/>
    </method>
    <method name="test_set_multi_args2">
      <arg type="s" direction="in"/>
    </method>
    <method name="test_set_multi_args3">
      <arg type="s" direction="in"/>
      <arg type="s" direction="in"/>
    </method>
    <method name="test_set_serial">
    </method>
  </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");

CALLER: {
    my $msg = Net::DBus::Binding::Message::MethodCall->new(service_name => "org.example.MyService",
							   object_path => "/org/example/MyObject",
							   interface => "org.example.MyObject",
							   method_name => "test_set_caller");
    $msg->set_sender(":1.1");

    my $reply = $bus->get_connection->send_with_reply_and_block($msg);
    is($reply->get_type, &Net::DBus::Binding::Message::MESSAGE_TYPE_METHOD_RETURN);

    is_deeply($object->{lastargs}, [":1.1"], "caller is :1.1");
}


SERIAL: {
    my $msg = Net::DBus::Binding::Message::MethodCall->new(service_name => "org.example.MyService",
							   object_path => "/org/example/MyObject",
							   interface => "org.example.MyObject",
							   method_name => "test_set_serial");

    my $reply = $bus->get_connection->send_with_reply_and_block($msg);

    is($reply->get_type, &Net::DBus::Binding::Message::MESSAGE_TYPE_METHOD_RETURN);

    is_deeply($object->{lastargs}, [$msg->get_serial], "serial matches");
}

MULTI_ARGS1: {
    my $msg = Net::DBus::Binding::Message::MethodCall->new(service_name => "org.example.MyService",
							   object_path => "/org/example/MyObject",
							   interface => "org.example.MyObject",
							   method_name => "test_set_multi_args1");
    $msg->set_sender(":1.1");
    my $iter = $msg->iterator(1);
    $iter->append_string("one");

    my $reply = $bus->get_connection->send_with_reply_and_block($msg);

    is($reply->get_type, &Net::DBus::Binding::Message::MESSAGE_TYPE_METHOD_RETURN);

    is_deeply($object->{lastargs}, ["one",":1.1"], "caller matches");
}

MULTI_ARGS2: {
    my $msg = Net::DBus::Binding::Message::MethodCall->new(service_name => "org.example.MyService",
							   object_path => "/org/example/MyObject",
							   interface => "org.example.MyObject",
							   method_name => "test_set_multi_args2");
    $msg->set_sender(":1.1");
    my $iter = $msg->iterator(1);
    $iter->append_string("one");

    my $reply = $bus->get_connection->send_with_reply_and_block($msg);

    is($reply->get_type, &Net::DBus::Binding::Message::MESSAGE_TYPE_METHOD_RETURN);

    is_deeply($object->{lastargs}, [":1.1", "one"], "caller matches");
}

MULTI_ARGS3: {
    my $msg = Net::DBus::Binding::Message::MethodCall->new(service_name => "org.example.MyService",
							   object_path => "/org/example/MyObject",
							   interface => "org.example.MyObject",
							   method_name => "test_set_multi_args3");
    $msg->set_sender(":1.1");
    my $iter = $msg->iterator(1);
    $iter->append_string("one");
    $iter->append_string("two");

    my $reply = $bus->get_connection->send_with_reply_and_block($msg);

    is($reply->get_type, &Net::DBus::Binding::Message::MESSAGE_TYPE_METHOD_RETURN);

    is_deeply($object->{lastargs}, ["one",":1.1", "two"], "caller matches");
}