| 1 |
#!/usr/bin/perl -w |
| 2 |
# -*- perl -*- |
| 3 |
|
| 4 |
# |
| 5 |
# $Id: $ |
| 6 |
# Author: Slaven Rezic |
| 7 |
# |
| 8 |
|
| 9 |
use strict; |
| 10 |
|
| 11 |
use Tk; |
| 12 |
use Data::Dumper; |
| 13 |
|
| 14 |
BEGIN { |
| 15 |
if (!eval q{ |
| 16 |
use Test::More; |
| 17 |
1; |
| 18 |
}) { |
| 19 |
print "1..0 # skip: no Test::More module\n"; |
| 20 |
exit; |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
unless ($Tk::platform eq 'unix') { |
| 25 |
plan skip_all => 'property only work on X11'; |
| 26 |
exit 0; |
| 27 |
} |
| 28 |
|
| 29 |
plan tests => 38; |
| 30 |
|
| 31 |
my $mw = tkinit; |
| 32 |
$mw->geometry("+10+10"); |
| 33 |
|
| 34 |
{ |
| 35 |
my @prop = $mw->property('list'); |
| 36 |
# Here a test used to check if @prop is empty. But KDE defines a |
| 37 |
# _KDE_NET_WM_USER_CREATION_TIME property, so the test cannot be |
| 38 |
# used anymore. |
| 39 |
pass("property list call on windows"); |
| 40 |
ok(!$mw->property('exists','_PERL_TK_ThisPropertyDoesNotExist'), "Not-existent property"); |
| 41 |
} |
| 42 |
|
| 43 |
{ |
| 44 |
my @prop = $mw->property('list','root'); |
| 45 |
cmp_ok(scalar(@prop), ">=", 1, "It's very likely that there are properties on the root window"); |
| 46 |
ok(!$mw->property('exists','_PERL_TK_ThisPropertyDoesNotExist'.rand(10),'root'), "Not-existent property"); |
| 47 |
} |
| 48 |
|
| 49 |
# format=8 differs from format=16/32: The value to set and get is a |
| 50 |
# string. If getting the property value, then a "\0" is appended (I |
| 51 |
# don't know if this is intentional, this may change). With |
| 52 |
# format=16/32 the values are array references with integers. |
| 53 |
{ |
| 54 |
my $format = 8; |
| 55 |
|
| 56 |
$mw->property('set', '_PERL_TK_TestProperty_1', "CARDINAL", $format, "a"); |
| 57 |
ok($mw->property('exists', '_PERL_TK_TestProperty_1'), "Format=$format, Property exists"); |
| 58 |
is($mw->property('get', '_PERL_TK_TestProperty_1'), "a\0", "Expected get result") |
| 59 |
or diag(Dumper [$mw->property('get', '_PERL_TK_TestProperty_1')]); |
| 60 |
is_xprop('_PERL_TK_TestProperty_1', [map { ord } split //, "a\0"], "Expected xprop result"); |
| 61 |
|
| 62 |
$mw->property('set', '_PERL_TK_TestProperty_1', "CARDINAL", $format, "abc"); |
| 63 |
ok($mw->property('exists', '_PERL_TK_TestProperty_1'), "Format=$format, Property exists, longer value"); |
| 64 |
is($mw->property('get', '_PERL_TK_TestProperty_1'), "abc\0") |
| 65 |
or diag(Dumper [$mw->property('get', '_PERL_TK_TestProperty_1')]); |
| 66 |
is_xprop('_PERL_TK_TestProperty_1', [map { ord } split //, "abc\0"]); |
| 67 |
|
| 68 |
my @list = $mw->property('list'); |
| 69 |
ok((grep { $_ eq '_PERL_TK_TestProperty_1' } @list), "Found test property in list"); |
| 70 |
|
| 71 |
my @list2 = $mw->property('list', hex $mw->id); |
| 72 |
is_deeply(\@list, \@list2, "Same list with explicite window id"); |
| 73 |
|
| 74 |
$mw->property('delete', '_PERL_TK_TestProperty_1'); |
| 75 |
ok(!$mw->property('exists', '_PERL_TK_TestProperty_1'), "Property deleted"); |
| 76 |
|
| 77 |
my @list_after_delete = $mw->property('list'); |
| 78 |
ok(!(grep { $_ eq '_PERL_TK_TestProperty_1' } @list_after_delete), "Not appearing in list anymore"); |
| 79 |
} |
| 80 |
|
| 81 |
for my $format (16, 32) { |
| 82 |
$mw->property('set', '_PERL_TK_TestProperty_1', "CARDINAL", $format, [1]); |
| 83 |
ok($mw->property('exists', '_PERL_TK_TestProperty_1'), "Format=$format, Property exists"); |
| 84 |
is_deeply([$mw->property('get', '_PERL_TK_TestProperty_1')], ["CARDINAL", 1], "Expected get result") |
| 85 |
or diag(Dumper [$mw->property('get', '_PERL_TK_TestProperty_1')]); |
| 86 |
is_xprop('_PERL_TK_TestProperty_1', [1], "Expected xprop result"); |
| 87 |
|
| 88 |
$mw->property('set', '_PERL_TK_TestProperty_1', "CARDINAL", $format, [1,2,3]); |
| 89 |
ok($mw->property('exists', '_PERL_TK_TestProperty_1'), "Format=$format, Property exists, longer value"); |
| 90 |
is_deeply([$mw->property('get', '_PERL_TK_TestProperty_1')], ["CARDINAL", 1,2,3]) |
| 91 |
or diag(Dumper [$mw->property('get', '_PERL_TK_TestProperty_1')]); |
| 92 |
is_xprop('_PERL_TK_TestProperty_1', [1,2,3]); |
| 93 |
|
| 94 |
$mw->property('delete', '_PERL_TK_TestProperty_1'); |
| 95 |
ok(!$mw->property('exists', '_PERL_TK_TestProperty_1'), "Property deleted"); |
| 96 |
} |
| 97 |
|
| 98 |
# Test with ATOMs |
| 99 |
{ |
| 100 |
$mw->property('set', '_PERL_TK_TestProperty_2', "ATOM", 32, ['_PERL_TK_TestAtom_1']); |
| 101 |
ok($mw->property('exists', '_PERL_TK_TestProperty_2'), "Property with ATOM exists"); |
| 102 |
is_deeply([$mw->property('get', '_PERL_TK_TestProperty_2')], ['ATOM', '_PERL_TK_TestAtom_1'], "Expected get result") |
| 103 |
or diag(Dumper [$mw->property('get', '_PERL_TK_TestProperty_2')]); |
| 104 |
is_xprop('_PERL_TK_TestProperty_2', ['_PERL_TK_TestAtom_1'], "Expected xprop result"); |
| 105 |
|
| 106 |
my @list = $mw->property('list'); |
| 107 |
ok((grep { $_ eq '_PERL_TK_TestProperty_2' } @list), "Found test property in list"); |
| 108 |
|
| 109 |
$mw->property('delete', '_PERL_TK_TestProperty_2'); |
| 110 |
ok(!$mw->property('exists', '_PERL_TK_TestProperty_2'), "Property deleted"); |
| 111 |
} |
| 112 |
|
| 113 |
# Test with STRINGs |
| 114 |
{ |
| 115 |
$mw->property('set', '_PERL_TK_TestProperty_3', "STRING", 8, 'TestString'); |
| 116 |
ok($mw->property('exists', '_PERL_TK_TestProperty_3'), "Property with STRING exists"); |
| 117 |
is($mw->property('get', '_PERL_TK_TestProperty_3'), "TestString\0", "Expected get result") |
| 118 |
or diag(Dumper [$mw->property('get', '_PERL_TK_TestProperty_3')]); |
| 119 |
is_xprop('_PERL_TK_TestProperty_3', ['"TestString"'], "Expected xprop result"); |
| 120 |
|
| 121 |
my @list = $mw->property('list'); |
| 122 |
ok((grep { $_ eq '_PERL_TK_TestProperty_3' } @list), "Found test property in list"); |
| 123 |
|
| 124 |
$mw->property('delete', '_PERL_TK_TestProperty_3'); |
| 125 |
ok(!$mw->property('exists', '_PERL_TK_TestProperty_3'), "Property deleted"); |
| 126 |
} |
| 127 |
|
| 128 |
sub is_xprop { |
| 129 |
my($prop, $expected, $testname) = @_; |
| 130 |
SKIP: { |
| 131 |
skip("xprop not in PATH", 1) |
| 132 |
if !xprop_is_in_path(); |
| 133 |
my $mw_id = $mw->id; |
| 134 |
chomp(my $res = `xprop -notype -id $mw_id $prop`); |
| 135 |
$res =~ s{^\Q$prop\E\s*=\s*}{}; |
| 136 |
my(@bytes) = split /\s*,\s*/, $res; |
| 137 |
is_deeply(\@bytes, $expected, $testname) |
| 138 |
or diag(Dumper \@bytes); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
{ |
| 143 |
my $xprop_is_in_path; |
| 144 |
sub xprop_is_in_path { |
| 145 |
my($prog) = "xprop"; |
| 146 |
my $path = eval { |
| 147 |
require File::Spec; |
| 148 |
require Config; |
| 149 |
return $prog if (File::Spec->file_name_is_absolute($prog) and -f $prog and -x $prog); |
| 150 |
my $sep = $Config::Config{'path_sep'} || ':'; |
| 151 |
foreach (split(/$sep/o, $ENV{PATH})) { |
| 152 |
return "$_/$prog" if (-x "$_/$prog" && !-d "$_/$prog"); |
| 153 |
} |
| 154 |
undef; |
| 155 |
}; |
| 156 |
warn $@ if $@; # unlikely to happen |
| 157 |
$xprop_is_in_path = $path; # cache |
| 158 |
$path; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
__END__ |