tests/plover-gtk/treemodel.c
author J. Ali Harlow <ali@juiblex.co.uk>
Fri Jul 08 07:50:42 2016 +0100 (2016-07-08)
changeset 57 e0f448fb54a5
permissions -rw-r--r--
Tests shouldn't fail if RAZOR_ROOT is set in the environment
     1 /*
     2  * Copyright (C) 2011, 2016  J. Ali Harlow <ali@juiblex.co.uk>
     3  *
     4  * This program is free software; you can redistribute it and/or modify
     5  * it under the terms of the GNU General Public License as published by
     6  * the Free Software Foundation; either version 2 of the License, or
     7  * (at your option) any later version.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License along
    15  * with this program; if not, write to the Free Software Foundation, Inc.,
    16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    17  */
    18 
    19 #include <stdlib.h>
    20 #include <string.h>
    21 #include <glib.h>
    22 #include <gtk/gtk.h>
    23 
    24 static void test_tree_model_iter(GtkTreeModel *model,GtkTreeIter *iter)
    25 {
    26     GtkTreePath *path,*path2;
    27     GtkTreeIter iter2,child;
    28     gchar *string,*string2,*s;
    29     int i,n;
    30     if (iter)   /* Not all tests make sense on the virtual root */
    31     {
    32 	/* Check we can convert to a path and back */
    33 	path=gtk_tree_model_get_path(model,iter);
    34 	g_assert(path!=NULL);
    35 	g_assert(gtk_tree_model_get_iter(model,&iter2,path));
    36 	path2=gtk_tree_model_get_path(model,&iter2);
    37 	g_assert(path2!=NULL);
    38 	g_assert(gtk_tree_path_compare(path,path2)==0);
    39 	gtk_tree_path_free(path2);
    40 	/* Check we can convert to a path string and back */
    41 	string=gtk_tree_model_get_string_from_iter(model,iter);
    42 	g_assert(string!=NULL);
    43 	g_assert(gtk_tree_model_get_iter_from_string(model,&iter2,string));
    44 	string2=gtk_tree_model_get_string_from_iter(model,&iter2);
    45 	g_assert(string2!=NULL);
    46 	g_assert(strcmp(string,string2)==0);
    47 	g_free(string2);
    48     }
    49     else
    50     {
    51 	path=NULL;
    52 	string=NULL;
    53     }
    54     if (string)
    55 	g_debug("Checking iter %s",string);
    56     else
    57 	g_debug("Checking virtual root iter");
    58     n=gtk_tree_model_iter_n_children(model,iter);
    59     g_assert(n>=0);
    60     if (n>0)
    61     {
    62 	/* Check that GTK_TREE_MODEL_LIST_ONLY is not set inappropriately */
    63 	if (iter)
    64 	{
    65 	    gboolean list_only_with_grandchildren;
    66 	    list_only_with_grandchildren=
    67 	      gtk_tree_model_get_flags(model)&GTK_TREE_MODEL_LIST_ONLY;
    68 	    g_assert(!list_only_with_grandchildren);
    69 	}
    70 	/* Check that gtk_tree_model_iter_has_child() agrees */
    71 	if (iter)
    72 	    g_assert(gtk_tree_model_iter_has_child(model,iter));
    73 	/* Check that gtk_tree_model_iter_children() returns the first child */
    74 	g_assert(gtk_tree_model_iter_children(model,&child,iter));
    75 	string2=gtk_tree_model_get_string_from_iter(model,&child);
    76 	g_assert(string2!=NULL);
    77 	if (string)
    78 	{
    79 	    g_assert(g_str_has_prefix(string2,string));
    80 	    g_assert(strlen(string2)==strlen(string)+2);
    81 	    g_assert(g_str_has_suffix(string2,":0"));
    82 	}
    83 	else
    84 	    g_assert(strcmp(string2,"0")==0);
    85 	g_free(string2);
    86 	for(i=0;i<n;i++)
    87 	{
    88 	    /*
    89 	     * Check that gtk_tree_model_iter_nth_child() returns the nth child
    90 	     */
    91 	    g_assert(gtk_tree_model_iter_nth_child(model,&child,iter,i));
    92 	    string2=gtk_tree_model_get_string_from_iter(model,&child);
    93 	    g_assert(string2!=NULL);
    94 	    if (string)
    95 		s=g_strdup_printf("%s:%d",string,i);
    96 	    else
    97 		s=g_strdup_printf("%d",i);
    98 	    g_assert(strcmp(string2,s)==0);
    99 	    g_free(s);
   100 	    g_free(string2);
   101 	    /* Check that my son's father is me */
   102 	    if (iter)
   103 	    {
   104 		g_assert(gtk_tree_model_iter_parent(model,&iter2,&child));
   105 		string2=gtk_tree_model_get_string_from_iter(model,&iter2);
   106 		g_assert(strcmp(string,string2)==0);
   107 		g_free(string2);
   108 	    }
   109 	    else
   110 		g_assert(!gtk_tree_model_iter_parent(model,&iter2,&child));
   111 	    /* Check next sibling */
   112 	    if (i+1<n)
   113 	    {
   114 		iter2=child;
   115 		g_assert(gtk_tree_model_iter_next(model,&iter2));
   116 		string2=gtk_tree_model_get_string_from_iter(model,&iter2);
   117 		g_assert(string2!=NULL);
   118 		if (string)
   119 		s=g_strdup_printf("%s:%d",string,i+1);
   120 		    else
   121 		s=g_strdup_printf("%d",i+1);
   122 		g_assert(strcmp(string2,s)==0);
   123 		g_free(s);
   124 		g_free(string2);
   125 	    }
   126 	    else
   127 	    {
   128 		iter2=child;
   129 		g_assert(!gtk_tree_model_iter_next(model,&iter2));
   130 	    }
   131 	    /* Check child */
   132 	    test_tree_model_iter(model,&child);
   133 	}
   134 	/*
   135 	 * Check that gtk_tree_model_iter_nth_child() returns no more children
   136 	 */
   137 	g_assert(!gtk_tree_model_iter_nth_child(model,&child,iter,n));
   138     }
   139     else
   140     {
   141 	/* Check that all APIs agree that iter has no children */
   142 	if (iter)
   143 	    g_assert(!gtk_tree_model_iter_has_child(model,iter));
   144 	g_assert(!gtk_tree_model_iter_children(model,&child,iter));
   145 	g_assert(!gtk_tree_model_iter_nth_child(model,&child,iter,0));
   146     }
   147     gtk_tree_path_free(path);
   148     g_free(string);
   149 }
   150 
   151 void test_tree_model(GtkTreeModel *model)
   152 {
   153     GtkTreeModelFlags undefined_flags;
   154     undefined_flags=gtk_tree_model_get_flags(model);
   155     undefined_flags&=~GTK_TREE_MODEL_ITERS_PERSIST;
   156     undefined_flags&=~GTK_TREE_MODEL_LIST_ONLY;
   157     g_assert(!undefined_flags);
   158     test_tree_model_iter(model,NULL);
   159 }