~kubuntu-members/perlkde/4.11

« back to all changes in this revision

Viewing changes to qtcore/t/f_handlers.t

  • Committer: Arno Rehn
  • Date: 2011-01-06 17:49:41 UTC
  • Revision ID: git-v1:ada9e4b459cf6fabd0f3b9b164387469cb19d9bc
A patch by Ian Monroe and myself to modularize the repository.
See README.MODULARIZATION for details.

svn path=/trunk/KDE/kdebindings/perl/; revision=1212365

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
use Test::More tests => 34;
2
 
 
3
 
use strict;
4
 
use warnings;
5
 
use Devel::Peek;
6
 
use QtCore4;
7
 
use QtGui4;
8
 
use QtNetwork4;
9
 
 
10
 
my $app = Qt::Application( \@ARGV );
11
 
 
12
 
{
13
 
    my $widget = Qt::Widget();
14
 
    # Check refcount
15
 
    is ( Devel::Peek::SvREFCNT($widget), 1, 'refcount' );
16
 
    # Test Qt::String marshalling
17
 
    my $wt = 'Qt::String marshalling works!';
18
 
    $widget->setWindowTitle( $wt );
19
 
    is ( $widget->windowTitle(), $wt, 'Qt::String' );
20
 
}
21
 
 
22
 
{
23
 
    my $widget = Qt::Widget();
24
 
    # Test a string that has non-latin characters
25
 
    use utf8;
26
 
    my $wt = 'ターミナル';
27
 
    utf8::upgrade($wt);
28
 
    $widget->setWindowTitle( $wt );
29
 
    is ( $widget->windowTitle(), $wt, 'Qt::String unicode' );
30
 
    no utf8;
31
 
}
32
 
 
33
 
{
34
 
    # Test int marshalling
35
 
    my $widget = Qt::Widget();
36
 
    my $int = 341;
37
 
    $widget->resize( $int, $int );
38
 
    is ( $widget->height(), $int, 'int' );
39
 
 
40
 
    # Test marshalling to int from enum value
41
 
    my $textFormat = Qt::TextCharFormat();
42
 
    $textFormat->setFontWeight( Qt::Font::Bold() );
43
 
    is ( $textFormat->fontWeight(), ${Qt::Font::Bold()}, 'enum to int' );
44
 
}
45
 
 
46
 
{
47
 
    # Test double marshalling
48
 
    my $double = 3/7;
49
 
    my $doubleValidator = Qt::DoubleValidator( $double, $double * 2, 5, undef );
50
 
    is ( $doubleValidator->bottom(), $double, 'double' );
51
 
    is ( $doubleValidator->top(), $double * 2, 'double' );
52
 
}
53
 
 
54
 
{
55
 
    # Test bool marshalling
56
 
    my $widget = Qt::Widget();
57
 
    my $bool = !$widget->isEnabled();
58
 
    $widget->setEnabled( $bool );
59
 
    is ( $widget->isEnabled(), $bool, 'bool' );
60
 
}
61
 
 
62
 
{
63
 
    # Test int* marshalling
64
 
    my ( $x1, $y1, $w1, $h1, $x2, $y2, $w2, $h2 ) = ( 5, 4, 50, 40 );
65
 
    my $rect = Qt::Rect( $x1, $y1, $w1, $h1 );
66
 
    $rect->getRect( $x2, $y2, $w2, $h2 );
67
 
    ok ( $x1 == $x2 &&
68
 
         $y1 == $y2 &&
69
 
         $w1 == $w2 &&
70
 
         $h1 == $h2,
71
 
         'int*' );
72
 
}
73
 
 
74
 
{
75
 
    # Test unsigned int marshalling
76
 
    my $label = Qt::Label();
77
 
    my $hcenter = ${Qt::AlignHCenter()};
78
 
    my $top = ${Qt::AlignTop()};
79
 
    $label->setAlignment(Qt::AlignHCenter() | Qt::AlignTop());
80
 
    my $alignment = $label->alignment();
81
 
    is( $alignment, $hcenter|$top, 'unsigned int' );
82
 
}
83
 
 
84
 
{
85
 
    # Test unsigned int * marshalling
86
 
    my $option = Qt::StyleOption();
87
 
    my $state = Qt::Style::State_On() | Qt::Style::State_HasFocus();
88
 
    $option->setState( $state );
89
 
    is( $option->state(), ${$state}, 'unsigned int *' );
90
 
}
91
 
 
92
 
{
93
 
    # Test char and uchar marshalling
94
 
    my $char = Qt::Char( Qt::Int(87) );
95
 
    is ( $char->toAscii(), 87, 'signed char' );
96
 
    $char = Qt::Char( Qt::Uchar('f') );
97
 
    is ( $char->toAscii(), ord('f'), 'unsigned char' );
98
 
    $char = Qt::Char( 'f', 3 );
99
 
    is ( $char->row(), 3, 'unsigned char' );
100
 
    is ( $char->cell(), ord('f'), 'unsigned char' );
101
 
}
102
 
 
103
 
{
104
 
    # Test uchar* marshalling
105
 
    my $red = chr(64);
106
 
    my $green = chr(128);
107
 
    my $blue = chr(196);
108
 
    my $alpha = chr(255);
109
 
    my $val = $red . $green . $blue . $alpha;
110
 
    my $w = 1;
111
 
    my $h = 1;
112
 
    my $buffer = $val x ($w*$h);
113
 
    my $image = Qt::Image( $buffer, $w, $h, Qt::Image::Format_RGB32() );
114
 
    my $result = $image->pixel(0,0);
115
 
    is( $result, ord($red) << 0 | ord($green) << 8 | ord($blue) << 16 | ord($alpha) << 24, 'unsigned char*' );
116
 
}
117
 
 
118
 
{
119
 
    # Test short, ushort, and long marshalling
120
 
    my $shortSize = length( pack 'S', 0 );
121
 
    my $num = 5;
122
 
    my $gotNum = 0;
123
 
    my $block = Qt::ByteArray();
124
 
    my $stream = Qt::DataStream($block, Qt::IODevice::ReadWrite());
125
 
    no warnings qw(void);
126
 
    $stream << Qt::Short($num);
127
 
    my $streamPos = $stream->device()->pos();
128
 
    $stream->device()->seek(0);
129
 
    $stream >> Qt::Short($gotNum);
130
 
    use warnings;
131
 
    is ( $gotNum, $num, 'signed short' );
132
 
 
133
 
    $gotNum = 0;
134
 
    no warnings qw(void);
135
 
    $stream->device()->seek(0);
136
 
    $stream << Qt::Ushort($num);
137
 
    $stream->device()->seek(0);
138
 
    $stream >> Qt::Ushort($gotNum);
139
 
    use warnings;
140
 
    is ( $gotNum, $num, 'unsigned short' );
141
 
    is ( $streamPos, $shortSize, 'long' );
142
 
}
143
 
 
144
 
{
145
 
    # Test ushort marshalling
146
 
    my $tcpSocket = Qt::TcpSocket(undef);
147
 
    my $port = 29031;
148
 
    $tcpSocket->setPeerPort($port);
149
 
    is( $tcpSocket->peerPort(), $port, 'unsigned short' );
150
 
}
151
 
 
152
 
{
153
 
    # Test length of QByteArray::data
154
 
    my $buf = Qt::Buffer();
155
 
    $buf->open( Qt::Buffer::ReadWrite() );
156
 
    my $var = "Hello, \0World!";
157
 
    $buf->write( $var, length $var );
158
 
    $buf->seek(0);
159
 
    my $ba = $buf->read( 14 );
160
 
    is( length( $ba->data() ), length( $var ), "QByteArray::data length" );
161
 
}
162
 
 
163
 
{
164
 
    # Test double& marshalling
165
 
    my @dimensions = ( 97.84, 105.934, 43.9025, 408.32 );
166
 
    my $rect = Qt::RectF( @dimensions );
167
 
    my ( $left, $top, $right, $bottom );
168
 
    $rect->getRect( $left, $top, $right, $bottom );
169
 
    is_deeply( [ $left, $top, $right, $bottom ], \@dimensions, 'double&' );
170
 
}
171
 
 
172
 
{
173
 
    # Test some QLists
174
 
    my $action1 = Qt::Action( 'foo', undef );
175
 
    my $action2 = Qt::Action( 'bar', undef );
176
 
    my $action3 = Qt::Action( 'baz', undef );
177
 
 
178
 
    # Add some stuff to them...
179
 
    $action1->{The} = 'quick';
180
 
    $action2->{brown} = 'fox';
181
 
    $action3->{jumped} = 'over';
182
 
 
183
 
    my $actions = [ $action1, $action2, $action3 ]; 
184
 
 
185
 
    my $widget = Qt::Widget();
186
 
    $widget->addActions( $actions );
187
 
 
188
 
    my $gotactions = $widget->actions();
189
 
 
190
 
    is_deeply( $actions, $gotactions, 'marshall_ItemList<>' );
191
 
}
192
 
 
193
 
{
194
 
    # Test QIntList
195
 
    my $w = Qt::ColumnView();
196
 
    $w->setModel(Qt::DirModel());
197
 
    my $widths = [237];
198
 
    $w->setColumnWidths( $widths );
199
 
    is_deeply( $w->columnWidths, $widths, 'marshall_QIntList' );
200
 
}
201
 
 
202
 
{
203
 
    # Test ambiguous list call
204
 
    my $strings = [ qw( The quick brown fox jumped over the lazy dog ) ];
205
 
    my $var = Qt::Variant( $strings );
206
 
    my $newStrings = $var->toStringList();
207
 
    is_deeply( $strings, $newStrings, 'Ambiguous list resolution' );
208
 
}
209
 
 
210
 
{
211
 
    # Test marshall_ValueListItem ToSV
212
 
    my $shortcut1 = Qt::KeySequence( 'Enter' );
213
 
    my $shortcut2 = Qt::KeySequence( 'Tab' );
214
 
    my $shortcut3 = Qt::KeySequence( 'Space' );
215
 
    my $shortcuts = [ $shortcut1, $shortcut2, $shortcut3 ];
216
 
    my $action = Qt::Action( 'Foobar', undef );
217
 
 
218
 
    $action->setShortcuts( $shortcuts );
219
 
    my $gotshortcuts = $action->shortcuts();
220
 
 
221
 
    is( scalar @{$gotshortcuts}, scalar @{$shortcuts}, 'marshall_ValueListItem<> FromSV array size' );
222
 
    is_deeply( [ map{ $gotshortcuts->[$_]->matches( $shortcuts->[$_] ) } 0..$#{$gotshortcuts} ],
223
 
               [ map{ Qt::KeySequence::ExactMatch() } (0..$#{$gotshortcuts}) ],
224
 
               'marshall_ValueListItem<> FromSV array content' );
225
 
}
226
 
 
227
 
{
228
 
    my $tree = Qt::TableView( undef );
229
 
    my $model = Qt::DirModel();
230
 
 
231
 
    $tree->setModel( $model );
232
 
    my $top = $model->index( Qt::Dir::currentPath() );
233
 
    $tree->setRootIndex( $top );
234
 
 
235
 
    my $selectionModel = $tree->selectionModel();
236
 
    my $child0 = $top->child(0,0);
237
 
    my $child1 = $top->child(0,1);
238
 
    my $child2 = $top->child(0,2);
239
 
    my $child3 = $top->child(0,3);
240
 
    $selectionModel->select( $child0, Qt::ItemSelectionModel::Select() );
241
 
    $selectionModel->select( $child1, Qt::ItemSelectionModel::Select() );
242
 
    $selectionModel->select( $child2, Qt::ItemSelectionModel::Select() );
243
 
    $selectionModel->select( $child3, Qt::ItemSelectionModel::Select() );
244
 
 
245
 
    my $selection = $selectionModel->selection();
246
 
    my $indexes = $selection->indexes();
247
 
 
248
 
    # Run $indexes->[0] == $child0, which should return '1', for each returned
249
 
    # index.
250
 
    is_deeply( [ map{ eval "\$indexes->[$_] == \$child$_" } (0..$#{$indexes}) ],
251
 
               [ map{ 1 } (0..$#{$indexes}) ],
252
 
               'marshall_ValueListItem<> ToSV' );
253
 
}
254
 
 
255
 
{
256
 
    # Test QHash marshalling
257
 
    # This will test EITHER QMap<QString,QVariant> or
258
 
    # QHash<QString,QVariant>
259
 
    # On my system with Qt 4.6.1 and perl 5.10.0, it calls the QMap one
260
 
    my $hash = {
261
 
        string => Qt::Variant( Qt::String( 'bar' ) ),
262
 
        integer => Qt::Variant( Qt::Int( 5 ) )
263
 
    };
264
 
 
265
 
    my $variant = Qt::Variant( $hash );
266
 
    my $rethash = $variant->value();
267
 
    is_deeply( $rethash, $hash, 'QMap<QString,QVariant>' );
268
 
}
269
 
 
270
 
{
271
 
    # Test Qt::Object::findChildren
272
 
    my $widget = Qt::Widget();
273
 
    my $childWidget = Qt::Widget($widget);
274
 
    $childWidget->setObjectName('childWidget');
275
 
    my $childPushButton = Qt::PushButton($childWidget);
276
 
    my $children = $widget->findChildren('Qt::Widget');
277
 
    is_deeply( $children, [$childWidget, $childPushButton], 'Qt::Object::findChildren' );
278
 
    $children = $widget->findChildren('Qt::Widget', 'childWidget');
279
 
    is_deeply( $children, [$childWidget], 'Qt::Object::findChildren' );
280
 
    $children = $widget->findChildren('Qt::PushButton');
281
 
    is_deeply( $children, [$childPushButton], 'Qt::Object::findChildren' );
282
 
}
283
 
 
284
 
{
285
 
# Test Qt::IODevice::read
286
 
    my $buf = Qt::Buffer();
287
 
    $buf->open( Qt::Buffer::ReadWrite() );
288
 
    my $var = "Hello, \0World!";
289
 
    $buf->write( $var, length $var );
290
 
    $buf->seek(0);
291
 
    my $str = undef;
292
 
    my $size = $buf->read( \$str, length $var );
293
 
 
294
 
    is( $str, $var, "Qt::IODevice::read(char*, qint64)" );
295
 
 
296
 
    $buf->seek(0);
297
 
    my $ba = $buf->read( length $var );
298
 
    is( $ba->data(), $var, 'Qt::IODevice::read(qint64)' );
299
 
}