### ----------------------------------------------------
### -- Gnome::Gio
### -- Licenses: Artistic-2.0
### -- Authors: Marcel Timmerman
### -- File: t/DesktopAppInfo.rakutest
### ----------------------------------------------------
use v6;
use NativeCall;
use Test;

use Gnome::Gio::DesktopAppInfo:api<1>;

#use Gnome::N::GlibToRakuTypes:api<1>;
#use Gnome::N::N-GObject:api<1>;
#use Gnome::N::X:api<1>;
#Gnome::N::debug(:on);

#-------------------------------------------------------------------------------
my Gnome::Gio::DesktopAppInfo $dai;
#-------------------------------------------------------------------------------
subtest 'ISA test', {
  if $*HOME eq '/home/marcel' {
    $dai .= new(:filename</home/marcel/Languages/Raku/Projects/gnome-gio/t/data/XAppInfo.desktop>);
    nok $dai.is-valid, '.new(:filename); file does not exist';

    $dai .= new(:desktop-id<abcdef.desktop>);
    ok $dai.is-valid, '.new(:desktop-id); file exists';
  }
#`{{
#note "\n$cwd, {%*ENV<XDG_DATA_HOME>//''}, {%*ENV<XDG_DATA_DIRS>//''}";
  %*ENV<XDG_DATA_HOME> = "$cwd/t/data";
  %*ENV<XDG_DATA_DIRS> = "$cwd/t/data:%*ENV<XDG_DATA_DIRS>";
#note "\n$cwd, {%*ENV<XDG_DATA_HOME>//''}, {%*ENV<XDG_DATA_DIRS>//''}";

#note 'load-file: ', $dai.load-file("$cwd/t/data/AppInfo.desktop");
}}

  my Str $cwd = $*CWD.IO.absolute;
  $dai .= new(:filename("$cwd/t/data/AppInfo.desktop"));
  ok $dai.is-valid, '.new(:filename); file exists';
}

#-------------------------------------------------------------------------------
# set environment variable 'raku-test-all' if rest must be tested too.
unless %*ENV<raku_test_all>:exists {
  done-testing;
  exit;
}

#-------------------------------------------------------------------------------
subtest 'Manipulations', {
  is $dai.get-action-name('FullList'), 'Full-directory-list',
      '.get-action-name()';
  ok $dai.get-boolean('StartupNotify'), '.get-boolean()';
  is $dai.get-categories, 'X-Gnome;Graphics;', '.get-categories()';
  is $dai.get-filename, $*CWD.IO.absolute ~ "/t/data/AppInfo.desktop",
      '.get-filename()';

  is-deeply $dai.list-actions, [<FullList>,], '.list-actions()';
  lives-ok { note $dai.search('sett'); }, '.search()';
}

#-------------------------------------------------------------------------------
subtest 'Properties …', {
#  my Gnome::Gio::DesktopAppInfo $dai .= new;
  my @r = $dai.get-properties(
    'filename', Str
  );
  is-deeply @r, [
    $*CWD.IO.absolute ~ "/t/data/AppInfo.desktop"
  ], 'properties: ' ~ (
    'filename'
  ).join(', ');
}

#-------------------------------------------------------------------------------
done-testing;

=finish

#-------------------------------------------------------------------------------
subtest 'Inherit Gnome::Gio::DesktopAppInfo', {
  class MyClass is Gnome::Gio::DesktopAppInfo {
    method new ( |c ) {
      self.bless( :GDesktopAppInfo, |c);
    }

    submethod BUILD ( *%options ) {

    }
  }

  my MyClass $mgc .= new;
  isa-ok $mgc, Gnome::Gio::DesktopAppInfo, 'MyClass.new()';
}

#-------------------------------------------------------------------------------
subtest 'Properties …', {
#  my Gnome::Gio::DesktopAppInfo $dai .= new;
  my @r = $dai.get-properties(
#    name, type,  …
  );
  is-deeply @r, [
#    value, …
  ], 'properties: ' ~ (
#    name, …
  ).join(', ');
}

#-------------------------------------------------------------------------------
subtest 'Signals …', {
  use Gnome::Gtk3::Main:api<1>;
  use Gnome::N::GlibToRakuTypes:api<1>;

  my Gnome::Gtk3::Main $main .= new;

  class SignalHandlers {
    has Bool $!signal-processed = False;

    method … (
      'any-args',
      Gnome::Gio::DesktopAppInfo() :$_native-object, gulong :$_handler-id
      # --> …
    ) {

      isa-ok $_widget, Gnome::Gio::DesktopAppInfo;
      $!signal-processed = True;
    }

    method signal-emitter ( Gnome::Gio::DesktopAppInfo :$widget --> Str ) {

      while $main.gtk-events-pending() { $main.iteration-do(False); }

      $widget.emit-by-name(
        'signal',
      #  'any-args',
      #  :return-type(int32),
      #  :parameters([int32,])
      );
      is $!signal-processed, True, '\'…\' signal processed';

      while $main.gtk-events-pending() { $main.iteration-do(False); }

      #$!signal-processed = False;
      #$widget.emit-by-name(
      #  'signal',
      #  'any-args',
      #  :return-type(int32),
      #  :parameters([int32,])
      #);
      #is $!signal-processed, True, '\'…\' signal processed';

      while $main.gtk-events-pending() { $main.iteration-do(False); }
      sleep(0.4);
      $main.gtk-main-quit;

      'done'
    }
  }

  my Gnome::Gio::DesktopAppInfo $dai .= new;

  #my Gnome::Gtk3::Window $w .= new;
  #$w.add($m);

  my SignalHandlers $sh .= new;
  $dai.register-signal( $sh, 'method', 'signal');

  my Promise $p = $dai.start-thread(
    $sh, 'signal-emitter',
    # :!new-context,
    # :start-time(now + 1)
  );

  is $main.gtk-main-level, 0, "loop level 0";
  $main.gtk-main;
  #is $main.gtk-main-level, 0, "loop level is 0 again";

  is $p.result, 'done', 'emitter finished';
}

#-------------------------------------------------------------------------------
subtest 'Themes …', {
}

#-------------------------------------------------------------------------------
subtest 'Interface …', {
}


