Skip to content

Commit

Permalink
Add tests for icon scaling math & loading
Browse files Browse the repository at this point in the history
  • Loading branch information
xkr47 committed Dec 17, 2019
1 parent 03253e8 commit ad5d20b
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 22 deletions.
25 changes: 3 additions & 22 deletions test/dbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gio/gio.h>

#include "helpers.h"
#include "queues.h"

extern const char *base;
Expand Down Expand Up @@ -252,33 +253,13 @@ bool dbus_notification_fire(struct dbus_notification *n, uint *id)

void dbus_notification_set_raw_image(struct dbus_notification *n_dbus, const char *path)
{
GdkPixbuf *pb = gdk_pixbuf_new_from_file(path, NULL);

if (!pb)
GVariant *hint = notification_setup_raw_image(path);
if (!hint)
return;

GVariant *hint_data = g_variant_new_from_data(
G_VARIANT_TYPE("ay"),
gdk_pixbuf_read_pixels(pb),
gdk_pixbuf_get_byte_length(pb),
TRUE,
(GDestroyNotify) g_object_unref,
g_object_ref(pb));

GVariant *hint = g_variant_new(
"(iiibii@ay)",
gdk_pixbuf_get_width(pb),
gdk_pixbuf_get_height(pb),
gdk_pixbuf_get_rowstride(pb),
gdk_pixbuf_get_has_alpha(pb),
gdk_pixbuf_get_bits_per_sample(pb),
gdk_pixbuf_get_n_channels(pb),
hint_data);

g_hash_table_insert(n_dbus->hints,
g_strdup("image-data"),
g_variant_ref_sink(hint));
g_object_unref(pb);
}

/////// TESTS
Expand Down
35 changes: 35 additions & 0 deletions test/helpers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <gdk-pixbuf/gdk-pixbuf.h>

#include "helpers.h"

GVariant *notification_setup_raw_image(const char *path)
{
GdkPixbuf *pb = gdk_pixbuf_new_from_file(path, NULL);

if (!pb)
return NULL;

GVariant *hint_data = g_variant_new_from_data(
G_VARIANT_TYPE("ay"),
gdk_pixbuf_read_pixels(pb),
gdk_pixbuf_get_byte_length(pb),
TRUE,
(GDestroyNotify) g_object_unref,
g_object_ref(pb));

GVariant *hint = g_variant_new(
"(iiibii@ay)",
gdk_pixbuf_get_width(pb),
gdk_pixbuf_get_height(pb),
gdk_pixbuf_get_rowstride(pb),
gdk_pixbuf_get_has_alpha(pb),
gdk_pixbuf_get_bits_per_sample(pb),
gdk_pixbuf_get_n_channels(pb),
hint_data);

g_object_unref(pb);

return hint;
}

/* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */
9 changes: 9 additions & 0 deletions test/helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef DUNST_TEST_HELPERS_H
#define DUNST_TEST_HELPERS_H

#include <glib.h>

GVariant *notification_setup_raw_image(const char *path);

#endif
/* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */
80 changes: 80 additions & 0 deletions test/icon.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,61 @@ TEST test_get_pixbuf_from_icon_fileuri(void)
PASS();
}

TEST test_icon_size_clamp_too_small(void)
{
int w = 12, h = 24;
bool resized = icon_size_clamp(&w, &h);
ASSERT(resized);
ASSERT_EQ(w, 16);
ASSERT_EQ(h, 32);

PASS();
}

TEST test_icon_size_clamp_not_necessary(void)
{
int w = 20, h = 30;
bool resized = icon_size_clamp(&w, &h);
ASSERT(!resized);
ASSERT_EQ(w, 20);
ASSERT_EQ(h, 30);

PASS();
}

TEST test_icon_size_clamp_too_big(void)
{
int w = 75, h = 150;
bool resized = icon_size_clamp(&w, &h);
ASSERT(resized);
ASSERT_EQ(w, 50);
ASSERT_EQ(h, 100);

PASS();
}

TEST test_icon_size_clamp_too_small_then_too_big(void)
{
int w = 8, h = 80;
bool resized = icon_size_clamp(&w, &h);
ASSERT(resized);
ASSERT_EQ(w, 10);
ASSERT_EQ(h, 100);

PASS();
}

TEST test_get_pixbuf_from_icon_both_is_scaled(void)
{
GdkPixbuf *pixbuf = get_pixbuf_from_icon("onlypng");
ASSERT(pixbuf);
ASSERT_EQ(gdk_pixbuf_get_width(pixbuf), 16);
ASSERT_EQ(gdk_pixbuf_get_height(pixbuf), 16);
g_clear_pointer(&pixbuf, g_object_unref);

PASS();
}

SUITE(suite_icon)
{
settings.icon_path = g_strconcat(
Expand All @@ -129,6 +184,31 @@ SUITE(suite_icon)
RUN_TEST(test_get_pixbuf_from_icon_onlypng);
RUN_TEST(test_get_pixbuf_from_icon_filename);
RUN_TEST(test_get_pixbuf_from_icon_fileuri);
RUN_TEST(test_icon_size_clamp_not_necessary);

settings.min_icon_size = 16;
settings.max_icon_size = 100;

RUN_TEST(test_get_pixbuf_from_icon_both_is_scaled);
RUN_TEST(test_icon_size_clamp_too_small);
RUN_TEST(test_icon_size_clamp_not_necessary);
RUN_TEST(test_icon_size_clamp_too_big);
RUN_TEST(test_icon_size_clamp_too_small_then_too_big);

settings.min_icon_size = 16;
settings.max_icon_size = 0;

RUN_TEST(test_icon_size_clamp_too_small);
RUN_TEST(test_icon_size_clamp_not_necessary);

settings.min_icon_size = 0;
settings.max_icon_size = 100;

RUN_TEST(test_icon_size_clamp_not_necessary);
RUN_TEST(test_icon_size_clamp_too_big);

settings.min_icon_size = 0;
settings.max_icon_size = 0;

g_clear_pointer(&settings.icon_path, g_free);
}
Expand Down
75 changes: 75 additions & 0 deletions test/notification.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "../src/notification.c"
#include "greatest.h"
#include "helpers.h"

#include "../src/option_parser.h"
#include "../src/settings.h"
Expand Down Expand Up @@ -124,6 +125,76 @@ TEST test_notification_referencing(void)
PASS();
}


static struct notification *notification_load_icon_with_scaling(int min_icon_size, int max_icon_size)
{
struct notification *n = notification_create();

char *path = g_strconcat(base, "/data/icons/valid.svg", NULL); // 16x16

GVariant *rawIcon = notification_setup_raw_image(path);

settings.min_icon_size = min_icon_size;
settings.max_icon_size = max_icon_size;
notification_icon_replace_data(n, rawIcon);
settings.min_icon_size = 0;
settings.max_icon_size = 0;

g_variant_unref(rawIcon);
g_free(path);

return n;
}

TEST test_notification_icon_scaling_toosmall(void)
{
struct notification *n = notification_load_icon_with_scaling(20, 100);

ASSERT_EQ(gdk_pixbuf_get_width(n->icon), 20);
ASSERT_EQ(gdk_pixbuf_get_height(n->icon), 20);

notification_unref(n);

PASS();
}


TEST test_notification_icon_scaling_toolarge(void)
{
struct notification *n = notification_load_icon_with_scaling(5, 10);

ASSERT_EQ(gdk_pixbuf_get_width(n->icon), 10);
ASSERT_EQ(gdk_pixbuf_get_height(n->icon), 10);

notification_unref(n);

PASS();
}

TEST test_notification_icon_scaling_notconfigured(void)
{
struct notification *n = notification_load_icon_with_scaling(0, 0);

ASSERT_EQ(gdk_pixbuf_get_width(n->icon), 16);
ASSERT_EQ(gdk_pixbuf_get_height(n->icon), 16);

notification_unref(n);

PASS();
}

TEST test_notification_icon_scaling_notneeded(void)
{
struct notification *n = notification_load_icon_with_scaling(10, 20);

ASSERT_EQ(gdk_pixbuf_get_width(n->icon), 16);
ASSERT_EQ(gdk_pixbuf_get_height(n->icon), 16);

notification_unref(n);

PASS();
}

TEST test_notification_format_message(struct notification *n, const char *format, const char *exp)
{
n->format = format;
Expand Down Expand Up @@ -167,6 +238,10 @@ SUITE(suite_notification)
RUN_TEST(test_notification_is_duplicate);
RUN_TEST(test_notification_replace_single_field);
RUN_TEST(test_notification_referencing);
RUN_TEST(test_notification_icon_scaling_toosmall);
RUN_TEST(test_notification_icon_scaling_toolarge);
RUN_TEST(test_notification_icon_scaling_notconfigured);
RUN_TEST(test_notification_icon_scaling_notneeded);

// TEST notification_format_message
struct notification *a = notification_create();
Expand Down

0 comments on commit ad5d20b

Please sign in to comment.