ocarina/tests/gui/sidebar.c

255 lines
8.7 KiB
C

/*
* Copyright 2015 (c) Anna Schumaker.
*/
#include <core/core.h>
#include <core/idle.h>
#include <core/settings.h>
#include <gui/filter.h>
#include <gui/model.h>
#include <gui/sidebar.h>
#include <gui/treeview.h>
#include <tests/test.h>
const gchar *test_pl_names[8] = { "Collection",
"History",
"", "Playlists",
"", "Dynamic",
"", "Library" };
const gchar *test_dyn_names[3] = { "Least Played", "Most Played", "Unplayed" };
struct core_init_data init_data;
static void test_sidebar()
{
GtkTreeIter iter, child;
unsigned int i;
g_assert_nonnull(gui_sidebar_store());
g_assert_true(GTK_IS_TREE_STORE(gui_sidebar_store()));
g_assert_nonnull(gui_sidebar_model());
g_assert_true(GTK_IS_TREE_MODEL(gui_sidebar_model()));
g_assert_nonnull(gui_sidebar_filter());
g_assert_true(GTK_IS_TREE_MODEL_FILTER(gui_sidebar_filter()));
g_assert_nonnull(gui_sidebar_treeview());
g_assert_true(GTK_IS_TREE_VIEW(gui_sidebar_treeview()));
g_assert_nonnull(gui_random_button());
g_assert_true(GTK_IS_TOGGLE_BUTTON(gui_random_button()));
g_assert_true(gui_sidebar_iter_first(&iter));
gui_sidebar_iter_add(&iter, playlist_get(PL_SYSTEM, "Collection"), NULL);
gui_sidebar_iter_add(&iter, playlist_get(PL_SYSTEM, "History"), NULL);
g_assert_true(gui_sidebar_iter_first(&iter));
for (i = 0; i < 8; i++) {
g_assert_cmpstr_free(gui_sidebar_iter_name(&iter), ==,
test_pl_names[i]);
g_assert_cmpuint(gui_sidebar_iter_type(&iter), ==,
(i < 2) ? PL_SYSTEM: PL_MAX_TYPE);
if (i < 7)
g_assert_true(gui_sidebar_iter_next(&iter));
}
g_assert_false(gui_sidebar_iter_next(&iter));
gui_sidebar_iter_first(&iter);
g_assert_true(gui_sidebar_iter_find(&iter, "Playlists", PL_MAX_TYPE));
g_assert_cmpstr_free(gui_sidebar_iter_name(&iter), ==, "Playlists");
g_assert_cmpuint(gui_sidebar_iter_type(&iter), ==, PL_MAX_TYPE);
g_assert_false(gui_sidebar_iter_down(&iter, &child));
gui_sidebar_iter_append_child(&iter, playlist_get(PL_SYSTEM, "Favorites"),
NULL);
gui_sidebar_iter_append_child(&iter, playlist_get(PL_SYSTEM, "Hidden"),
NULL);
g_assert_true(gui_sidebar_iter_down(&iter, &child));
for (i = 0; i < 2; i++) {
g_assert_cmpstr_free(gui_sidebar_iter_name(&child), ==,
(i == 0) ? "Favorites" : "Hidden");
g_assert_cmpuint(gui_sidebar_iter_type(&child), ==, PL_SYSTEM);
gui_sidebar_iter_next(&child);
}
g_assert_false(gui_sidebar_iter_find(&iter, "History", PL_SYSTEM));
gui_sidebar_iter_first(&iter);
g_assert_true(gui_sidebar_iter_find(&iter, "History", PL_SYSTEM));
g_assert_cmpstr_free(gui_sidebar_iter_name(&iter), ==, "History");
g_assert_cmpuint(gui_sidebar_iter_type(&iter), ==, PL_SYSTEM);
g_assert(gui_sidebar_iter_playlist(&iter) ==
playlist_get(PL_SYSTEM, "History"));
g_assert_true(gui_sidebar_iter_find(&iter, "Dynamic", PL_MAX_TYPE));
g_assert_false(gui_sidebar_iter_down(&iter, &child));
gui_sidebar_iter_sort_child(&iter, playlist_get(PL_SYSTEM, "Most Played"),
NULL);
gui_sidebar_iter_sort_child(&iter, playlist_get(PL_SYSTEM, "Least Played"),
NULL);
gui_sidebar_iter_sort_child(&iter, playlist_get(PL_SYSTEM, "Unplayed"),
NULL);
g_assert_true(gui_sidebar_iter_down(&iter, &child));
g_assert_true(gui_sidebar_iter_down(&iter, &child));
for (i = 0; i < 3; i++) {
g_assert_cmpstr_free(gui_sidebar_iter_name(&child), ==,
test_dyn_names[i]);
g_assert_cmpuint(gui_sidebar_iter_type(&child), ==, PL_SYSTEM);
gui_sidebar_iter_next(&child);
}
}
static void test_sidebar_selection()
{
struct playlist *collection;
GtkTreeSelection *selection;
GtkToggleButton *random;
GtkTreeModel *filter;
GtkTreePath *path;
GtkTreeIter iter;
unsigned int i, n;
collection = playlist_get(PL_SYSTEM, "Collection");
selection = gtk_tree_view_get_selection(gui_sidebar_treeview());
filter = GTK_TREE_MODEL(gui_sidebar_filter());
random = gui_random_button();
g_assert_cmpuint(gtk_tree_model_iter_n_children(filter, NULL), ==, 6);
playlist_new(PL_LIBRARY, "tests/Music/Hyrule Symphony");
while (idle_run_task()) {}
playlist_add(playlist_get(PL_SYSTEM, "History"), track_get(0));
playlist_add(playlist_get(PL_SYSTEM, "Favorites"), track_get(0));
gtk_tree_model_filter_refilter(gui_sidebar_filter());
g_assert_cmpuint(gtk_tree_model_iter_n_children(filter, NULL), ==, 8);
playlist_set_random(collection, true);
g_assert_true(playlist_select(PL_SYSTEM, "Favorites"));
g_assert(playlist_cur() == playlist_get(PL_SYSTEM, "Favorites"));
g_assert_true(gui_sidebar_iter_first(&iter));
path = gtk_tree_model_get_path(gui_sidebar_model(), &iter);
for (i = 0; i < 8; i++) {
gtk_tree_selection_select_path(selection, path);
n = gtk_tree_selection_count_selected_rows(selection);
g_assert_cmpuint(n, ==, (i < 2) ? 1 : 0);
if (i == 0) {
g_assert(gui_model_get_playlist() == collection);
g_assert_true(gtk_toggle_button_get_active(random));
g_assert_true(gtk_widget_get_sensitive(
GTK_WIDGET(random)));
gtk_toggle_button_set_active(random, false);
g_assert_false(playlist_get_random(collection));
gtk_toggle_button_set_active(random, true);
g_assert_true(playlist_get_random(collection));
} else if (i == 1) {
g_assert(gui_model_get_playlist() ==
playlist_get(PL_SYSTEM, "History"));
g_assert_false(gtk_toggle_button_get_active(random));
g_assert_false(gtk_widget_get_sensitive(
GTK_WIDGET(random)));
} else {
g_assert_null(gui_model_get_playlist());
g_assert_false(gtk_toggle_button_get_active(random));
g_assert_false(gtk_widget_get_sensitive(
GTK_WIDGET(random)));
}
gui_sidebar_filter_path_select(path);
g_assert(playlist_cur() == playlist_get(PL_SYSTEM, "Collection"));
gtk_tree_selection_unselect_all(selection);
gui_sidebar_iter_next(&iter);
gtk_tree_path_next(path);
}
gtk_tree_path_free(path);
}
static void test_sidebar_pos()
{
g_assert_false(settings_has("gui.sidebar.pos"));
gtk_paned_set_position(gui_sidebar(), 300);
g_assert_true(settings_has("gui.sidebar.pos"));
g_assert_cmpuint(settings_get("gui.sidebar.pos"), ==, 300);
settings_set("gui.sidebar.pos", 250);
gui_sidebar_init();
g_assert_cmpuint(settings_get("gui.sidebar.pos"), ==, 250);
g_assert_cmpuint(gtk_paned_get_position(gui_sidebar()), ==, 250);
}
static void test_sidebar_expand()
{
GtkTreeModel *model = GTK_TREE_MODEL(gui_sidebar_filter());
GtkTreeIter iter;
g_assert_true(gtk_tree_model_get_iter_first(model, &iter));
g_assert_false( settings_has("gui.sidebar.expand.Collection"));
gui_sidebar_filter_row_expanded(&iter, true);
g_assert_cmpuint(settings_get("gui.sidebar.expand.Collection"), ==, 1);
g_assert_true( settings_has("gui.sidebar.expand.Collection"));
gui_sidebar_filter_row_expanded(&iter, false);
g_assert_cmpuint(settings_get("gui.sidebar.expand.Collection"), ==, 0);
gtk_tree_model_iter_next(model, &iter);
gtk_tree_model_iter_next(model, &iter);
gtk_tree_model_iter_next(model, &iter);
g_assert_false( settings_has("gui.sidebar.expand.Playlists"));
gui_sidebar_filter_row_expanded(&iter, true);
g_assert_cmpuint(settings_get("gui.sidebar.expand.Playlists"), ==, 1);
g_assert_true( settings_has("gui.sidebar.expand.Playlists"));
gui_sidebar_filter_row_expanded(&iter, false);
g_assert_cmpuint(settings_get("gui.sidebar.expand.Playlists"), ==, 0);
gtk_tree_model_iter_next(model, &iter);
gtk_tree_model_iter_next(model, &iter);
g_assert_false( settings_has("gui.sidebar.expand.Dynamic"));
gui_sidebar_filter_row_expanded(&iter, true);
g_assert_cmpuint(settings_get("gui.sidebar.expand.Dynamic"), ==, 1);
g_assert_true( settings_has("gui.sidebar.expand.Dynamic"));
gui_sidebar_filter_row_expanded(&iter, false);
g_assert_cmpuint(settings_get("gui.sidebar.expand.Dynamic"), ==, 0);
gtk_tree_model_iter_next(model, &iter);
gtk_tree_model_iter_next(model, &iter);
g_assert_false( settings_has("gui.sidebar.expand.Library"));
gui_sidebar_filter_row_expanded(&iter, true);
g_assert_cmpuint(settings_get("gui.sidebar.expand.Library"), ==, 1);
g_assert_true( settings_has("gui.sidebar.expand.Library"));
gui_sidebar_filter_row_expanded(&iter, false);
g_assert_cmpuint(settings_get("gui.sidebar.expand.Library"), ==, 0);
}
int main(int argc, char **argv)
{
int ret;
gtk_init(&argc, NULL);
core_init(&argc, &argv, &init_data);
gui_builder_init("share/ocarina/ocarina.ui");
gui_model_init();
gui_filter_init();
gui_treeview_init();
gui_sidebar_init();
g_test_init(&argc, &argv, NULL);
g_test_add_func("/Gui/Sidebar", test_sidebar);
g_test_add_func("/Gui/Sidebar/Selection", test_sidebar_selection);
g_test_add_func("/Gui/Sidebar/Pos", test_sidebar_pos);
g_test_add_func("/Gui/Sidebar/Expand", test_sidebar_expand);
ret = g_test_run();
core_deinit();
gui_treeview_deinit();
gui_filter_deinit();
gui_model_deinit();
gui_builder_deinit();
return ret;
}