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