home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / perl5 / Gtk2 / TestHelper.pm < prev   
Encoding:
Perl POD Document  |  2005-10-15  |  4.5 KB  |  203 lines

  1. #
  2. # $Header: /cvsroot/gtk2-perl/gtk2-perl-xs/Gtk2/pm/TestHelper.pm,v 1.10 2005/07/19 16:35:37 kaffeetisch Exp $
  3. #
  4.  
  5. package Gtk2::TestHelper;
  6. use Test::More;
  7. use Carp;
  8.  
  9. our $VERSION = '0.02';
  10.  
  11. sub import
  12. {
  13.     shift;
  14.     my %opts = (@_);
  15.  
  16.     plan skip_all => $opts{skip_all} if ($opts{skip_all});
  17.  
  18.     croak "tests must be provided at import" unless (exists ($opts{tests}));
  19.  
  20.     if ($opts{nowin32} && $^O eq 'MSWin32')
  21.     {
  22.         plan skip_all => "not appliciable on win32";
  23.     }
  24.  
  25.     if ($opts{at_least_version})
  26.     {
  27.         my ($rmajor, $rminor, $rmicro, $text) = 
  28.                         @{$opts{at_least_version}};
  29.         unless (Gtk2->CHECK_VERSION ($rmajor, $rminor, $rmicro))
  30.         {
  31.             plan skip_all => $text;
  32.         }
  33.     }
  34.  
  35.     # gtk+ 2.0.x can use X fonts, and requires a connection to the
  36.     # display at all times; so, ignore noinit for those versions.
  37.     delete $opts{noinit} unless Gtk2->CHECK_VERSION (2, 2, 0);
  38.  
  39.     if( $opts{noinit} || Gtk2->init_check )
  40.     {
  41.         plan tests => $opts{tests};
  42.     }    
  43.     else
  44.     {    
  45.         plan skip_all => 'Gtk2->init_check failed, probably '
  46.                 .'unable to open DISPLAY';
  47.     }
  48.  
  49.     # ignore keyboard
  50.     Gtk2->key_snooper_install (sub { 1; });
  51. }
  52.  
  53. package main;
  54.  
  55. # these are to make people behave
  56. use strict;
  57. use warnings;
  58. # go ahead and use Gtk2 for them.
  59. use Gtk2;
  60. # and obviously they'll need Test::More
  61. use Test::More;
  62.  
  63. # encourage use of these constants in tests
  64. use Glib qw(TRUE FALSE);
  65.  
  66.  
  67. # useful wrappers
  68. sub run_main (;&) {
  69.     my $callback = shift;
  70.     Glib::Idle->add (sub {
  71.         if ($callback) {
  72.             #print "# Entering run_main shutdown callback\n";
  73.             $callback->();
  74.             #print "# Leaving run_main shutdown callback\n";
  75.         }
  76.         Gtk2->main_quit;
  77.         FALSE;
  78.     });
  79.     #print "# Entering main loop (run_main)\n";
  80.     Gtk2->main;
  81.     #print "# Leaving main loop (run_main)\n";
  82. }
  83. sub ok_idle ($;$) {
  84.     my ($testsub, $test_name) = @_;
  85.     run_main {
  86.         # 0 Test::More::ok
  87.         # 1 this block's ok() call
  88.         # 2 idle callback in run_main
  89.         # 3 Gtk2::main call in run_main
  90.         # 4 Gtk2::main call in run_main (again)
  91.         # 5 ok_idle
  92.         # 6 the caller we want to print
  93.         local $Test::Builder::Level = 6;
  94.         ok ($testsub->(), $test_name);
  95.     }
  96. }
  97. sub is_idle ($$;$) {
  98.     my ($asub, $b, $test_name) = @_;
  99.     run_main {
  100.         local $Test::Builder::Level = 6; # see ok_idle()
  101.         is ($asub->(), $b, $test_name);
  102.     }
  103. }
  104.  
  105.  
  106. 1;
  107. __END__
  108.  
  109. =head1 NAME
  110.  
  111. Gtk2::TestHelper - Code to make testing Gtk2 and friends simpler.
  112.  
  113. =head1 SYNOPSIS
  114.  
  115.   use Gtk2::TestHelper tests => 10;
  116.  
  117. =head1 DESCRIPTION
  118.  
  119. A simplistic module that brings together code that would otherwise have to be
  120. copied into each and every test. The magic happens during the importing process
  121. and therefore all options are passed to the use call. The module also use's
  122. strict, warnings, Gtk2, and Test::More so that the individual tests will not
  123. have to. The only required option is the number of tests. The module installs a
  124. key snooper that causes all keyboard input to be ignored.
  125.  
  126. =head1 OPTIONS
  127.  
  128. =over
  129.  
  130. =item tests
  131.  
  132. The number of tests to be completed.
  133.  
  134. =item noinit
  135.  
  136. Do not call Gtk2->init_check, assume that it is not necessary.
  137.  
  138. =item nowin32
  139.  
  140. Set to true if all tests are to be skipped on the win32 platform.
  141.  
  142. =item at_least_version
  143.  
  144. A reference to a list that is checked with Gtk2->CHECK_VERSION.
  145.  
  146. =item skip_all
  147.  
  148. Simply skip all tests with the reason provided.
  149.  
  150. =back
  151.  
  152. =head1 "EXPORTED" FUNCTIONS
  153.  
  154. This module also defines a few utility functions for use in tests; since
  155. we already override import and pull the dirty trick of calling use in
  156. the package main, these are defined in the package main rather than exported
  157. by Exporter.
  158.  
  159. =over
  160.  
  161. =item run_main
  162.  
  163. =item run_main (CODEREF)
  164.  
  165. =item run_main BLOCK
  166.  
  167. Run a main loop, and stop when all pending events are handled.  This is
  168. useful if you have a test that needs a main loop to run properly, because
  169. it allows your program to remain noninteractive.  Important for a test
  170. suite.
  171.  
  172. If the optional I<CODEREF> is supplied, it will be run right before killing
  173. the mainloop.  The function is prototyped to allow two styles of invocation:
  174.  
  175.   run_main (\&some_sub);    # explicit code reference
  176.   run_main { print "hi" };  # callback as a block
  177.  
  178. =item ok_idle (TEST_SUB [, TEST_NAME])
  179.  
  180. Run Test::Simple's ok() on the return value of I<TEST_SUB> after handling
  181. pending events.  Implemented with C<run_main> and other special trickery.
  182.  
  183. =item is_idle (THIS_SUB, THAT [, NAME])
  184.  
  185. Like ok_idle(), but compares the return value of I<THIS_SUB> with I<THAT>
  186. using Test::More's is().
  187.  
  188. =back
  189.  
  190. =head1 SEE ALSO
  191.  
  192. L<perl>(1), L<Gtk2>(3pm).
  193.  
  194. =head1 AUTHORS
  195.  
  196. The Gtk2-Perl Team.
  197.  
  198. =head1 COPYRIGHT AND LICENSE
  199.  
  200. Copyright 2003-2005 by the gtk2-perl team.
  201.  
  202. LGPL, See LICENSE file for more information.
  203.