Gtk2-Ex-TickerView

 view release on metacpan or  search on metacpan

devel/display.t  view on Meta::CPAN

# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with Gtk2-Ex-TickerView.  If not, see <http://www.gnu.org/licenses/>.


# The timer start and stop tests depend on the windows not being overlapped
# on screen, and on you not being switched away to another virtual terminal,
# so best not have them in the main tests for now.
#
# Some of the drawing tests are probably ok, or could be made so.
#
# 

use strict;
use warnings;
use Gtk2::Ex::TickerView;
use Test::More;

require Gtk2;
diag ("Perl-Gtk2 version ",Gtk2->VERSION);
diag ("Perl-Glib version ",Glib->VERSION);
diag ("Compiled against Glib version ",
      Glib::MAJOR_VERSION(), ".",
      Glib::MINOR_VERSION(), ".",
      Glib::MICRO_VERSION());
diag ("Running on       Glib version ",
      Glib::major_version(), ".",
      Glib::minor_version(), ".",
      Glib::micro_version());
diag ("Compiled against Gtk version ",
      Gtk2::MAJOR_VERSION(), ".",
      Gtk2::MINOR_VERSION(), ".",
      Gtk2::MICRO_VERSION());
diag ("Running on       Gtk version ",
      Gtk2::major_version(), ".",
      Gtk2::minor_version(), ".",
      Gtk2::micro_version());

my $have_display = Gtk2->init_check;
diag "have_display: ",($have_display ? "yes" : "no");

if (! $have_display) {
  plan skip_all => 'due to no DISPLAY available';
}

plan tests => 27;


#------------------------------------------------------------------------------

sub main_sync {
  my ($widget) = @_;
  $widget->get_display->sync;

  my $count = 0;
  while (Gtk2->events_pending) {
    $count++;
    Gtk2->main_iteration_do (0);
  }
  diag "main_sync(): ran $count events/iterations";
}

sub wait_for_event {
  my ($widget, $signame) = @_;
  my $done = 0;
  my $sig_id = $widget->signal_connect ($signame => sub {
                                          $done = 1;
                                          return 0; # Gtk2::EVENT_PROPAGATE
                                        });
  my $timer_id = Glib::Timeout->add (30_000, # 30 seconds
                                     sub {
                                       diag "Oops, timeout on $signame";
                                       exit 1;
                                     });
  $widget->get_display->sync;

  my $count = 0;
  while (! $done) {
    Gtk2->main_iteration;
    $count++;
  }
  while (Gtk2->events_pending) {
    $count++;
    Gtk2->main_iteration_do (0);
  }
  diag "wait_for_event('$signame'): ran $count events/iterations";

  $widget->signal_handler_disconnect ($sig_id);
  Glib::Source->remove ($timer_id);
}


#------------------------------------------------------------------------------
diag "pixmap drawing";

{
  # Resizing stuff in a container probably, maybe, only runs when the
  # container is realized.  Is there a flag directly in the child widget
  # though?
  #
  my $store = Gtk2::ListStore->new ('Glib::String');
  $store->set ($store->append, 0 => '111');
  $store->set ($store->append, 0 => '222');
  $store->set ($store->append, 0 => '333');
  $store->set ($store->append, 0 => '444');

  my $ticker = Gtk2::Ex::TickerView->new (model => $store);
  $ticker->set_size_request (30, 10);

  my $renderer = Gtk2::CellRendererText->new;
  $renderer->set_fixed_size (10, 10);
  $ticker->pack_start ($renderer, 0);

  my $toplevel = Gtk2::Window->new ('toplevel');
  $toplevel->add ($ticker);
  $toplevel->show_all;
  wait_for_event ($ticker, 'map_event');

  Gtk2::Ex::TickerView::_pixmap_empty ($ticker);
  Gtk2::Ex::TickerView::_pixmap_extend ($ticker, 30);
  is_deeply ($ticker->{'drawn_array'},
             [ 0,0, 1,10, 2,20 ]);
  is ($ticker->{'pixmap_end_x'}, 30);

  $ticker->{'want_index'} = 0;
  is (Gtk2::Ex::TickerView::_pixmap_find_want ($ticker, 1), 0);

  $ticker->{'want_index'} = 2;
  $ticker->{'want_x'} = -4;
  is (Gtk2::Ex::TickerView::_pixmap_find_want ($ticker, 1), 24);

  Gtk2::Ex::TickerView::_pixmap_shift ($ticker, 3);
  is_deeply ($ticker->{'drawn_array'},
             [ 0,-3, 1,7, 2,17 ]);
  is ($ticker->{'pixmap_end_x'}, 27);

  Gtk2::Ex::TickerView::_pixmap_shift ($ticker, 7);
  is_deeply ($ticker->{'drawn_array'},
             [ 1,0, 2,10 ]);
  is ($ticker->{'pixmap_end_x'}, 20);

  Gtk2::Ex::TickerView::_pixmap_extend ($ticker, 30);
  is_deeply ($ticker->{'drawn_array'},
             [ 1,0, 2,10, 3,20 ]);
  is ($ticker->{'pixmap_end_x'}, 30);



( run in 0.702 second using v1.01-cache-2.11-cpan-71847e10f99 )