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
|
#!/usr/bin/perl -w
# test for client data handling in various controls
use strict;
use Wx;
use lib './t';
use Tests_Helper qw(in_frame);
use Wx::Event qw(EVT_BUTTON);
package MyClass;
sub new {
my $class = shift;
my $code = shift;
die "want a CODE reference" unless ref $code eq 'CODE';
return bless [ $code ], $class;
}
sub DESTROY { &{$_[0][0]} }
package main;
use Test::More 'tests' => 61;
use strict;
#use base 'Wx::Frame';
use vars '$TODO';
sub tdata($) { Wx::TreeItemData->new( MyClass->new( $_[0] ) ) }
sub cdata($) { MyClass->new( $_[0] ) }
sub tests {
my $this = shift;
############################################################################
# wxTreeCtrl
############################################################################
my $tree = Wx::TreeCtrl->new( $this, -1 );
my $root = $tree->AddRoot( 'Root', -1, -1,
Wx::TreeItemData->new( 'Frobnicate' ) );
my $trdata = $tree->GetItemData( $root );
my $data = $trdata->GetData();
is( $data, 'Frobnicate', "Wx::TreeItemData::GetData" );
$data = $trdata->GetData();
is( $data, 'Frobnicate', "Wx::TreeItemData::GetData (again)" );
$data = $tree->GetPlData( $root );
is( $data, 'Frobnicate', "Wx::TreeCtrl::GetPlData" );
$trdata = $tree->GetItemData( $root );
$trdata->SetData( 'Baz' );
$trdata = $tree->GetItemData( $root );
$data = $trdata->GetData();
is( $data, 'Baz', "Wx::TreeItemData::SetData" );
$tree->SetItemData( $root, Wx::TreeItemData->new( 'Boo' ) );
$data = $tree->GetPlData( $root );
is( $data, 'Boo', "Wx::TreeCtrl::SetItemData" );
$tree->SetPlData( $root, 'XyZ' );
$data = $tree->GetPlData( $root );
is( $data, 'XyZ', "Wx::TreeCtrl::SetPlData" );
# test deleting and setting again
my( $deleting, $setting, $ctrldelete ) = ( 0, 0, 0 );
my $item1 = $tree->AppendItem( $root, 'An item', -1, -1,
tdata sub { $deleting = 1 } );
my $item2 = $tree->AppendItem( $root, 'An item', -1, -1,
tdata sub { $setting = 1 } );
my $item3 = $tree->AppendItem( $root, 'An item', -1, -1,
tdata sub { $ctrldelete = 1 } );
$tree->Delete( $item1 );
ok( $deleting, 'WxTreeCtrl: deleting an item deletes the data' );
$tree->SetItemData( $item2, Wx::TreeItemData->new( 'foo' ) );
ok( $setting, 'Wx::TreeCtrl: setting again item data deletes old data' );
# and hope the tree is deleted NOW
$tree->Destroy;
ok( $ctrldelete, 'Wx::TreeCtrl: deleting the tree deletes the data' );
############################################################################
# wxTreeListCtrl
############################################################################
SKIP: {
skip 'No Native Wx::TreeListCtrl', 8 unless defined(&Wx::TreeListCtrl::new);
my $treelist = Wx::TreeListCtrl->new( $this, -1, );
$treelist->AppendColumn("Component",
&Wx::wxCOL_WIDTH_AUTOSIZE,
&Wx::wxALIGN_LEFT,
&Wx::wxCOL_RESIZABLE | &Wx::wxCOL_SORTABLE);
$treelist->AppendColumn("# Files",
$treelist->WidthFor("1,000,000"),
&Wx::wxALIGN_RIGHT,
&Wx::wxCOL_RESIZABLE | &Wx::wxCOL_SORTABLE);
$treelist->AppendColumn("Size",
$treelist->WidthFor("1,000,000 KiB"),
&Wx::wxALIGN_RIGHT,
&Wx::wxCOL_RESIZABLE | &Wx::wxCOL_SORTABLE);
my $tlroot = $treelist->GetRootItem();
ok( $tlroot->IsOk, 'Wx::TreeListCtrl Root item OK');
my $tldata = \'Hubris';
$treelist->SetItemData($tlroot, $tldata);
my $outdata = $treelist->GetItemData($tlroot);
is( $$outdata, 'Hubris', "Wx::TreeListCtrl::GetItemData" );
$outdata = $treelist->GetItemData($tlroot);
is( $$outdata, 'Hubris', "Wx::TreeListCtrl::GetItemData again" );
$treelist->SetItemData($tlroot, Wx::TreeItemData->new( 'Aghast' ) );
$outdata = $treelist->GetItemData($tlroot)->GetData;
is( $outdata, 'Aghast', "Wx::TreeListCtrl::GetItemData From Wx::TreeItemData" );
## test deleting and setting again
my( $tldeleting, $tlsetting, $tlctrldelete ) = ( 0, 0, 0 );
my $tlitem1 = $treelist->AppendItem( $tlroot, 'An item', -1, -1,
cdata sub { $tldeleting = 1 } );
my $tlitem2 = $treelist->AppendItem( $tlroot, 'An item', -1, -1,
cdata sub { $tlsetting = 1 } );
my $tlitem3 = $treelist->AppendItem( $tlroot, 'An item', -1, -1,
cdata sub { $tlctrldelete = 1 } );
is( ref($treelist->GetItemData($tlitem1)), 'MyClass', 'Wx::TreeListCtrl Item Data is class');
$treelist->DeleteItem( $tlitem1 );
ok( $tldeleting, 'Wx::TreeListCtrl: deleting an item deletes the data' );
$treelist->SetItemData( $tlitem2, Wx::TreeItemData->new( 'foo' ) );
ok( $tlsetting, 'Wx::TreeListCtrl: setting again item data deletes old data' );
## and hope the tree is deleted NOW
$treelist->Destroy;
ok( $tlctrldelete, 'Wx::TreeListCtrl: deleting the tree deletes the data' );
};
############################################################################
# wxListBox & co.
############################################################################
my $list = Wx::ListBox->new( $this, -1 );
my $combo = Wx::ComboBox->new( $this, -1, 'foo' );
my $choice = Wx::Choice->new( $this, -1 );
my $checklist = Wx::CheckListBox->new( $this, -1, [-1, -1], [-1, -1], [1] );
my $odncombo = undef;
if( defined &Wx::PlOwnerDrawnComboBox::new ) {
$odncombo = Wx::PlOwnerDrawnComboBox->new( $this, -1, 'foo', [-1, -1],
[-1, -1], [] );
}
# test deleting and setting again
for my $x ( [ $list, 'Wx::ListBox' ],
[ $choice, 'Wx::Choice' ],
[ $combo, 'Wx::ComboBox' ],
[ $checklist, 'Wx::CheckListBox' ],
[ $odncombo, 'Wx::OwnerDrawnComboBox' ],
) {
SKIP: {
my( $list, $name ) = @$x;
( $deleting, $setting, $ctrldelete ) = ( 0, 0, 0 );
skip( $x->[1] . ": not available", 8 )
if !defined $x->[0];
skip( "wxMSW wxCheckListBox can't store client data yet", 8 )
if Wx::wxMSW && $name eq 'Wx::CheckListBox';
$list->Clear;
# diag "starting tests for $name";
my $data = 'Foo';
$list->Append( 'An item', $data );
$list->SetClientData( 0, $data ); # workaround bug in HEAD
$list->Append( 'An item' );
$data = 'Frobnication';
is( $list->GetClientData( 0 ), 'Foo', "$name: some client data" );
is( $list->GetClientData( 1 ), undef, "$name: no client data" );
$list->SetClientData( 0, 'Bar' );
$list->SetClientData( 1, 'Baz' );
is( $list->GetClientData( 0 ), 'Bar', "$name: setting client data" );
is( $list->GetClientData( 1 ), 'Baz',
"$name: setting client data (again)" );
my $x = 1;
$list->SetClientData( 0, \$x );
$x = 2;
is( ${$list->GetClientData( 0 )}, 2,
"$name: client data is a reference" );
$list->Append( 'An item', cdata sub { $setting = 1 } );
$list->Append( 'An item', cdata sub { $ctrldelete = 1 } );
$list->Append( 'An item', cdata sub { $deleting = 1 } );
SKIP: {
skip "delayed on Mac", 1 if Wx::wxMAC && $list->isa( 'Wx::ListBox' );
$list->Delete( 4 );
ok( $deleting, "$name: deleting an item deletes the data" );
}
$list->SetClientData( 2, 'foo' );
ok( $setting, "$name: setting again item data deletes old data" );
SKIP: {
# and hope the control is deleted NOW
$list->Destroy;
# TODO: is it correct to skip as below ? - Fails on osx-cocoa & 2.9.2 all platforms?
skip "delete delayed", 1 if ( $list->isa( 'Wx::ListBox' ) || $list->isa( 'Wx::ComboBox' ) || $list->isa( 'Wx::Choice' ) );
ok( $ctrldelete, "$name: deleting the control deletes the data" );
}
}
}
############################################################################
# wxListCtrl
############################################################################
my $listctrl = Wx::ListCtrl->new( $this, -1, [-1, -1], [-1, -1],
Wx::wxLC_REPORT() );
$listctrl->InsertColumn( 1, "Type" );
$listctrl->InsertStringItem( 0, 'text0' );
$listctrl->InsertStringItem( 1, 'text1' );
$listctrl->InsertStringItem( 2, 'text2' );
$listctrl->SetItemData( 0, 123 );
$listctrl->SetItemData( 1, 456 );
$listctrl->SetItemData( 2, 789 );
is( $listctrl->GetItemData( 0 ), 123, "Wx::ListCtrl first item data" );
is( $listctrl->GetItemData( 1 ), 456, "Wx::ListCtrl second item data" );
is( $listctrl->GetItemData( 2 ), 789, "Wx::ListCtrl third item data" );
$listctrl->SetItemData( 1, 135 );
is( $listctrl->GetItemData( 1 ), 135, "Wx::ListCtrl, changing item data" );
}
in_frame( \&tests );
# local variables:
# mode: cperl
# end:
|