From bc1c462d36366db17d1cea35d129e07d7fbc6d80 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 6 Sep 2016 08:02:50 -0400 Subject: [PATCH] gui/artwork: Add an accessor function for the artwork image This patch also adds a unit test checking that the image is initialized properly. In addition, I simplify things by changing the image widget to be a direct child of the GtkButton. Signed-off-by: Anna Schumaker --- gui/artwork.c | 11 ++++---- include/gui/artwork.h | 7 +++++ share/ocarina/ocarina.ui | 11 ++++---- tests/gui/.gitignore | 1 + tests/gui/CMakeLists.txt | 1 + tests/gui/artwork.c | 57 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 78 insertions(+), 10 deletions(-) create mode 100644 tests/gui/artwork.c diff --git a/gui/artwork.c b/gui/artwork.c index 050a557e..e795b1af 100644 --- a/gui/artwork.c +++ b/gui/artwork.c @@ -2,6 +2,7 @@ * Copyright 2016 (c) Anna Schumaker. */ #include +#include #include #include @@ -17,7 +18,7 @@ static cairo_surface_t *__artwork_scale_pixbuf(GdkPixbuf *pix) int new_h = gui_builder_widget_height("position") + gui_builder_widget_height("o_tags"); int new_w = (old_w * new_h) / old_h; - int scale = gtk_widget_get_scale_factor(gui_builder_widget("o_cover")); + int scale = gtk_widget_get_scale_factor(GTK_WIDGET(gui_artwork())); cairo_surface_t *orig = gdk_cairo_surface_create_from_pixbuf(pix, 0, NULL); cairo_content_t content = cairo_surface_get_content(orig); @@ -56,7 +57,6 @@ static cairo_surface_t *__artwork_get_surface(struct track *track) static bool __artwork_set_pixbuf(void) { - GtkImage *cover = GTK_IMAGE(gui_builder_widget("o_cover")); struct track *track = audio_cur_track(); cairo_surface_t *surface; @@ -65,12 +65,13 @@ static bool __artwork_set_pixbuf(void) surface = __artwork_get_surface(track); if (surface) { - gtk_image_set_from_surface(cover, surface); + gtk_image_set_from_surface(gui_artwork(), surface); cairo_surface_destroy(surface); } else - gtk_image_set_from_icon_name(cover, "image-missing", GTK_ICON_SIZE_DIALOG); + gtk_image_set_from_icon_name(gui_artwork(), "image-missing", + GTK_ICON_SIZE_DIALOG); - gtk_widget_set_sensitive(GTK_WIDGET(cover), surface != NULL); + gtk_widget_set_sensitive(GTK_WIDGET(gui_artwork()), surface != NULL); __artwork_cur_album = surface ? track->tr_album : NULL; return (surface != NULL); } diff --git a/include/gui/artwork.h b/include/gui/artwork.h index ea09f7d1..c9523982 100644 --- a/include/gui/artwork.h +++ b/include/gui/artwork.h @@ -3,8 +3,15 @@ */ #ifndef OCARINA_GUI_ARTWORK_H #define OCARINA_GUI_ARTWORK_H +#include /* Called to set artwork for a track. */ void gui_artwork_set_cover(void); +/* Called to get the cover image. */ +static inline GtkImage *gui_artwork(void) +{ + return GTK_IMAGE(gui_builder_widget("artwork")); +} + #endif /* OCARINA_GUI_ARTWORK_H */ diff --git a/share/ocarina/ocarina.ui b/share/ocarina/ocarina.ui index 01280c32..bde1a78e 100644 --- a/share/ocarina/ocarina.ui +++ b/share/ocarina/ocarina.ui @@ -158,8 +158,8 @@ media-skip-backward - + False @@ -193,8 +193,8 @@ 5 - + False @@ -227,8 +227,8 @@ 5 - + False @@ -261,8 +261,8 @@ media-skip-forward - + False @@ -286,9 +286,10 @@ none - + 100 True + False False image-missing 6 diff --git a/tests/gui/.gitignore b/tests/gui/.gitignore index 7e125424..dac2affd 100644 --- a/tests/gui/.gitignore +++ b/tests/gui/.gitignore @@ -6,4 +6,5 @@ filter treeview sidebar playlist +artwork audio diff --git a/tests/gui/CMakeLists.txt b/tests/gui/CMakeLists.txt index 220062e5..1fc28598 100644 --- a/tests/gui/CMakeLists.txt +++ b/tests/gui/CMakeLists.txt @@ -14,4 +14,5 @@ gui_unit_test(Sidebar) add_subdirectory(playlists/) gui_unit_test(Playlist) +gui_unit_test(Artwork) gui_unit_test(Audio) diff --git a/tests/gui/artwork.c b/tests/gui/artwork.c new file mode 100644 index 00000000..6095c879 --- /dev/null +++ b/tests/gui/artwork.c @@ -0,0 +1,57 @@ +/* + * Copyright 2016 (c) Anna Schumaker. + */ +#include +#include +#include +#include +#include +#include +#include +#include + +struct core_init_data init_data = { + .playlist_ops = &playlist_ops, +}; + +static void test_artwork(void) +{ + const gchar *name; + GtkIconSize size; + + g_assert_true(GTK_IS_IMAGE(gui_artwork())); + + gtk_image_get_icon_name(gui_artwork(), &name, &size); + g_assert_cmpuint(gtk_image_get_storage_type(gui_artwork()), ==, + GTK_IMAGE_ICON_NAME); + g_assert_cmpstr(name, ==, "image-missing"); + g_assert_cmpuint(size, ==, GTK_ICON_SIZE_DIALOG); + g_assert_false(gtk_widget_is_sensitive(GTK_WIDGET(gui_artwork()))); +} + +int main(int argc, char **argv) +{ + int ret; + + gtk_init(&argc, NULL); + core_init(&argc, NULL, &init_data); + gui_builder_init("share/ocarina/ocarina.ui"); + gui_model_init(); + gui_filter_init(); + gui_treeview_init(); + gui_sidebar_init(); + gui_playlist_init(); + gui_pl_library_add("tests/Music/Hyrule Symphony"); + while (idle_run_task()) {} + + g_test_init(&argc, &argv, NULL); + g_test_add_func("/Gui/Artwork", test_artwork); + ret = g_test_run(); + + core_deinit(); + gui_treeview_deinit(); + gui_filter_deinit(); + gui_model_deinit(); + gui_builder_deinit(); + return ret; +}