Chandra

 view release on metacpan or  search on metacpan

t/35_tray.t  view on Meta::CPAN

	$tray->add_item('Quit' => sub {});
	my $id = $tray->items->[0]{id};
	$tray->update_item($id, disabled => 1);

	my $items = $tray->items;
	is($items->[0]{disabled}, 1, 'disabled flag set by id');
}

# --- on_click ---
{
	my $tray = Chandra::Tray->new;
	my $called = 0;
	$tray->on_click(sub { $called = 1 });
	is(ref $tray->{_on_click}, 'CODE', 'on_click handler stored');
}

# --- Chaining ---
{
	my $tray = Chandra::Tray->new;
	my $result = $tray
		->add_item('A' => sub {})
		->add_separator
		->add_item('B' => sub {})
		->set_icon('icon.png')
		->set_tooltip('Tip');
	is($result, $tray, 'chaining works');
	is($tray->item_count, 3, '3 items after chain');
}

# --- Callback dispatch ---
{
	my $tray = Chandra::Tray->new;
	my @called_ids;
	$tray->add_item('A' => sub { push @called_ids, 'A' });
	$tray->add_item('B' => sub { push @called_ids, 'B' });

	my $cb = $tray->_make_dispatch_callback;
	ok(ref $cb eq 'CODE', 'dispatch callback is a coderef');

	my $id_a = $tray->items->[0]{id};
	my $id_b = $tray->items->[1]{id};

	$cb->($id_a);
	is_deeply(\@called_ids, ['A'], 'first handler called');

	$cb->($id_b);
	is_deeply(\@called_ids, ['A', 'B'], 'second handler called');
}

# --- on_click dispatch ---
{
	my $tray = Chandra::Tray->new;
	my $click_called = 0;
	$tray->on_click(sub { $click_called = 1 });

	my $cb = $tray->_make_dispatch_callback;
	$cb->(-1);
	is($click_called, 1, 'on_click handler called with id -1');
}

# --- Menu JSON output ---
{
	my $tray = Chandra::Tray->new;
	$tray->add_item('Show' => sub {});
	$tray->add_separator;
	$tray->add_item('Quit' => sub {});

	my $menu_json = $tray->_menu_json;
	ok(defined $menu_json, 'menu_json produced');

	require Cpanel::JSON::XS;
	my $decoded = Cpanel::JSON::XS::decode_json($menu_json);
	is(ref $decoded, 'ARRAY', 'decoded JSON is array');
	is(scalar @$decoded, 3, '3 entries');
	is($decoded->[0]{label}, 'Show', 'first item label');
	ok($decoded->[1]{separator}, 'separator present');
	is($decoded->[2]{label}, 'Quit', 'third item label');
}

# --- show without app returns self ---
{
	my $tray = Chandra::Tray->new;
	my $ret = $tray->show;
	is($ret, $tray, 'show without app returns self');
	is($tray->is_active, 0, 'not active without app');
}

# --- remove when not active ---
{
	my $tray = Chandra::Tray->new;
	my $ret = $tray->remove;
	is($ret, $tray, 'remove when not active returns self');
}

# --- show guard: no _webview returns self ---
{
	my $mock_app = bless { _started => 1 }, 'MockTrayApp1';

	my $tray = Chandra::Tray->new(
		app     => $mock_app,
		icon    => 'icon.png',
		tooltip => 'Test',
	);
	$tray->add_item('Quit' => sub {});
	my $ret = $tray->show;
	is($ret, $tray, 'show returns self when no _webview');
	is($tray->is_active, 0, 'not active without _webview');
}

# --- remove when not active returns self ---
{
	my $mock_app = bless { _started => 1 }, 'MockTrayApp2';

	my $tray = Chandra::Tray->new(app => $mock_app);
	$tray->show;	# won't activate (no _webview)
	my $ret = $tray->remove;
	is($ret, $tray, 'remove returns self');
	is($tray->is_active, 0, 'not active after remove');
}

# --- _sync on update when active (verify menu JSON updates) ---



( run in 1.029 second using v1.01-cache-2.11-cpan-39bf76dae61 )