1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tests/plover-gtk/treemodel.c Thu Jun 07 15:56:09 2018 +0100
1.3 @@ -0,0 +1,159 @@
1.4 +/*
1.5 + * Copyright (C) 2011, 2016 J. Ali Harlow <ali@juiblex.co.uk>
1.6 + *
1.7 + * This program is free software; you can redistribute it and/or modify
1.8 + * it under the terms of the GNU General Public License as published by
1.9 + * the Free Software Foundation; either version 2 of the License, or
1.10 + * (at your option) any later version.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License along
1.18 + * with this program; if not, write to the Free Software Foundation, Inc.,
1.19 + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1.20 + */
1.21 +
1.22 +#include <stdlib.h>
1.23 +#include <string.h>
1.24 +#include <glib.h>
1.25 +#include <gtk/gtk.h>
1.26 +
1.27 +static void test_tree_model_iter(GtkTreeModel *model,GtkTreeIter *iter)
1.28 +{
1.29 + GtkTreePath *path,*path2;
1.30 + GtkTreeIter iter2,child;
1.31 + gchar *string,*string2,*s;
1.32 + int i,n;
1.33 + if (iter) /* Not all tests make sense on the virtual root */
1.34 + {
1.35 + /* Check we can convert to a path and back */
1.36 + path=gtk_tree_model_get_path(model,iter);
1.37 + g_assert(path!=NULL);
1.38 + g_assert(gtk_tree_model_get_iter(model,&iter2,path));
1.39 + path2=gtk_tree_model_get_path(model,&iter2);
1.40 + g_assert(path2!=NULL);
1.41 + g_assert(gtk_tree_path_compare(path,path2)==0);
1.42 + gtk_tree_path_free(path2);
1.43 + /* Check we can convert to a path string and back */
1.44 + string=gtk_tree_model_get_string_from_iter(model,iter);
1.45 + g_assert(string!=NULL);
1.46 + g_assert(gtk_tree_model_get_iter_from_string(model,&iter2,string));
1.47 + string2=gtk_tree_model_get_string_from_iter(model,&iter2);
1.48 + g_assert(string2!=NULL);
1.49 + g_assert(strcmp(string,string2)==0);
1.50 + g_free(string2);
1.51 + }
1.52 + else
1.53 + {
1.54 + path=NULL;
1.55 + string=NULL;
1.56 + }
1.57 + if (string)
1.58 + g_debug("Checking iter %s",string);
1.59 + else
1.60 + g_debug("Checking virtual root iter");
1.61 + n=gtk_tree_model_iter_n_children(model,iter);
1.62 + g_assert(n>=0);
1.63 + if (n>0)
1.64 + {
1.65 + /* Check that GTK_TREE_MODEL_LIST_ONLY is not set inappropriately */
1.66 + if (iter)
1.67 + {
1.68 + gboolean list_only_with_grandchildren;
1.69 + list_only_with_grandchildren=
1.70 + gtk_tree_model_get_flags(model)>K_TREE_MODEL_LIST_ONLY;
1.71 + g_assert(!list_only_with_grandchildren);
1.72 + }
1.73 + /* Check that gtk_tree_model_iter_has_child() agrees */
1.74 + if (iter)
1.75 + g_assert(gtk_tree_model_iter_has_child(model,iter));
1.76 + /* Check that gtk_tree_model_iter_children() returns the first child */
1.77 + g_assert(gtk_tree_model_iter_children(model,&child,iter));
1.78 + string2=gtk_tree_model_get_string_from_iter(model,&child);
1.79 + g_assert(string2!=NULL);
1.80 + if (string)
1.81 + {
1.82 + g_assert(g_str_has_prefix(string2,string));
1.83 + g_assert(strlen(string2)==strlen(string)+2);
1.84 + g_assert(g_str_has_suffix(string2,":0"));
1.85 + }
1.86 + else
1.87 + g_assert(strcmp(string2,"0")==0);
1.88 + g_free(string2);
1.89 + for(i=0;i<n;i++)
1.90 + {
1.91 + /*
1.92 + * Check that gtk_tree_model_iter_nth_child() returns the nth child
1.93 + */
1.94 + g_assert(gtk_tree_model_iter_nth_child(model,&child,iter,i));
1.95 + string2=gtk_tree_model_get_string_from_iter(model,&child);
1.96 + g_assert(string2!=NULL);
1.97 + if (string)
1.98 + s=g_strdup_printf("%s:%d",string,i);
1.99 + else
1.100 + s=g_strdup_printf("%d",i);
1.101 + g_assert(strcmp(string2,s)==0);
1.102 + g_free(s);
1.103 + g_free(string2);
1.104 + /* Check that my son's father is me */
1.105 + if (iter)
1.106 + {
1.107 + g_assert(gtk_tree_model_iter_parent(model,&iter2,&child));
1.108 + string2=gtk_tree_model_get_string_from_iter(model,&iter2);
1.109 + g_assert(strcmp(string,string2)==0);
1.110 + g_free(string2);
1.111 + }
1.112 + else
1.113 + g_assert(!gtk_tree_model_iter_parent(model,&iter2,&child));
1.114 + /* Check next sibling */
1.115 + if (i+1<n)
1.116 + {
1.117 + iter2=child;
1.118 + g_assert(gtk_tree_model_iter_next(model,&iter2));
1.119 + string2=gtk_tree_model_get_string_from_iter(model,&iter2);
1.120 + g_assert(string2!=NULL);
1.121 + if (string)
1.122 + s=g_strdup_printf("%s:%d",string,i+1);
1.123 + else
1.124 + s=g_strdup_printf("%d",i+1);
1.125 + g_assert(strcmp(string2,s)==0);
1.126 + g_free(s);
1.127 + g_free(string2);
1.128 + }
1.129 + else
1.130 + {
1.131 + iter2=child;
1.132 + g_assert(!gtk_tree_model_iter_next(model,&iter2));
1.133 + }
1.134 + /* Check child */
1.135 + test_tree_model_iter(model,&child);
1.136 + }
1.137 + /*
1.138 + * Check that gtk_tree_model_iter_nth_child() returns no more children
1.139 + */
1.140 + g_assert(!gtk_tree_model_iter_nth_child(model,&child,iter,n));
1.141 + }
1.142 + else
1.143 + {
1.144 + /* Check that all APIs agree that iter has no children */
1.145 + if (iter)
1.146 + g_assert(!gtk_tree_model_iter_has_child(model,iter));
1.147 + g_assert(!gtk_tree_model_iter_children(model,&child,iter));
1.148 + g_assert(!gtk_tree_model_iter_nth_child(model,&child,iter,0));
1.149 + }
1.150 + gtk_tree_path_free(path);
1.151 + g_free(string);
1.152 +}
1.153 +
1.154 +void test_tree_model(GtkTreeModel *model)
1.155 +{
1.156 + GtkTreeModelFlags undefined_flags;
1.157 + undefined_flags=gtk_tree_model_get_flags(model);
1.158 + undefined_flags&=~GTK_TREE_MODEL_ITERS_PERSIST;
1.159 + undefined_flags&=~GTK_TREE_MODEL_LIST_ONLY;
1.160 + g_assert(!undefined_flags);
1.161 + test_tree_model_iter(model,NULL);
1.162 +}