Merge branch 'matthiasc/for-master' into 'master'

Matthiasc/for master

See merge request GNOME/gtk!2283
This commit is contained in:
Matthias Clasen 2020-07-25 00:05:28 +00:00
commit f2bdb1fb87
5 changed files with 157 additions and 4 deletions

View File

@ -379,6 +379,7 @@ gtk_drop_down_size_allocate (GtkWidget *widget,
gtk_widget_size_allocate (self->button, &(GtkAllocation) { 0, 0, width, height }, baseline);
gtk_widget_set_size_request (self->popup, width, -1);
gtk_widget_queue_resize (self->popup);
gtk_native_check_resize (GTK_NATIVE (self->popup));
}

View File

@ -148,6 +148,9 @@ gtk_sort_list_model_get_item (GListModel *list,
if (self->model == NULL)
return NULL;
if (position >= self->n_items)
return NULL;
if (self->positions)
position = pos_from_key (self, self->positions[position]);

View File

@ -85,7 +85,7 @@ gtk_tim_sort_init (GtkTimSort *self,
void
gtk_tim_sort_finish (GtkTimSort *self)
{
g_free (self->tmp);
g_clear_pointer (&self->tmp, g_free);
}
void

View File

@ -167,6 +167,29 @@ sort_list_model_new (GListModel *source,
#define N_MODELS 8
static char *
create_test_name (guint id)
{
GString *s = g_string_new ("");
if (id & (1 << 0))
g_string_append (s, "set-model");
else
g_string_append (s, "construct-with-model");
if (id & (1 << 1))
g_string_append (s, "/set-sorter");
else
g_string_append (s, "/construct-with-sorter");
if (id & (1 << 2))
g_string_append (s, "/incremental");
else
g_string_append (s, "/non-incremental");
return g_string_free (s, FALSE);
}
static GtkSortListModel *
create_sort_list_model (gconstpointer model_id,
gboolean track_changes,
@ -187,7 +210,7 @@ create_sort_list_model (gconstpointer model_id,
break;
case 1:
//gtk_sort_list_model_set_incremental (model, TRUE);
gtk_sort_list_model_set_incremental (model, TRUE);
break;
default:
@ -418,12 +441,15 @@ add_test_for_all_models (const char *name,
GTestDataFunc test_func)
{
guint i;
char *test;
for (i = 0; i < N_MODELS; i++)
{
char *path = g_strdup_printf ("/sorterlistmodel/model%u/%s", i, name);
test = create_test_name (i);
char *path = g_strdup_printf ("/sorterlistmodel/%s/%s", test, name);
g_test_add_data_func (path, GUINT_TO_POINTER (i), test_func);
g_free (path);
g_free (test);
}
}

View File

@ -91,6 +91,22 @@ add (GListStore *store,
g_object_unref (object);
}
static void
insert (GListStore *store,
guint position,
guint number)
{
GObject *object;
/* 0 cannot be differentiated from NULL, so don't use it */
g_assert (number != 0);
object = g_object_new (G_TYPE_OBJECT, NULL);
g_object_set_qdata (object, number_quark, GUINT_TO_POINTER (number));
g_list_store_insert (store, position, object);
g_object_unref (object);
}
#define assert_model(model, expected) G_STMT_START{ \
char *s = model_to_string (G_LIST_MODEL (model)); \
if (!g_str_equal (s, expected)) \
@ -107,6 +123,11 @@ add (GListStore *store,
g_string_set_size (changes, 0); \
}G_STMT_END
#define ignore_changes(model) G_STMT_START{ \
GString *changes = g_object_get_qdata (G_OBJECT (model), changes_quark); \
g_string_set_size (changes, 0); \
}G_STMT_END
static GListStore *
new_empty_store (void)
{
@ -398,7 +419,7 @@ test_stability (void)
GtkSortListModel *sort;
GListStore *store;
GtkSorter *sorter;
store = new_store ((guint[]) { 11, 31, 21, 1, 0 });
sort = new_model (store);
assert_model (sort, "1 11 21 31");
@ -414,6 +435,106 @@ test_stability (void)
g_object_unref (sort);
}
static GListStore *
new_shuffled_store (guint size)
{
GListStore *store = new_empty_store ();
guint i;
add (store, 1);
for (i = 1; i < size; i++)
insert (store, g_random_int_range (0, i), i + 1);
return store;
}
/* Test that we don't crash when things are removed from the
* model while it is incrementally sorting.
*/
static void
test_incremental_remove (void)
{
GListStore *store;
GtkSortListModel *model;
GtkSorter *sorter;
guint i;
GListStore *removed;
const guint n_items = 100000;
store = new_shuffled_store (n_items);
model = new_model (NULL);
gtk_sort_list_model_set_incremental (model, TRUE);
gtk_sort_list_model_set_model (model, G_LIST_MODEL (store));
sorter = gtk_custom_sorter_new (compare, NULL, NULL);
gtk_sort_list_model_set_sorter (model, sorter);
g_object_unref (sorter);
removed = g_list_store_new (G_TYPE_OBJECT);
while (gtk_sort_list_model_get_pending (model) != 0)
{
g_main_context_iteration (NULL, TRUE);
/* randomly remove items while the sort is ongoing */
if (g_list_model_get_n_items (G_LIST_MODEL (removed)) < 100)
{
guint position;
position = g_random_int_range (0, g_list_model_get_n_items (G_LIST_MODEL (store)) - 10);
for (i = 0; i < 10; i++)
{
GObject *item = g_list_model_get_item (G_LIST_MODEL (store), position + i);
g_list_store_append (removed, item);
g_object_unref (item);
}
g_list_store_splice (store, position, 10, NULL, 0);
}
}
g_assert_cmpuint (gtk_sort_list_model_get_pending (model), ==, 0);
gtk_sort_list_model_set_incremental (model, FALSE);
/* add them back */
for (i = 0; i < g_list_model_get_n_items (G_LIST_MODEL (removed)); i++)
{
GObject *item = g_list_model_get_item (G_LIST_MODEL (removed), i);
g_list_store_append (store, item);
g_object_unref (item);
}
g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (model)), ==, n_items);
for (i = 0; i < g_list_model_get_n_items (G_LIST_MODEL (model)); i++)
g_assert_cmpuint (i + 1, ==, get (G_LIST_MODEL (model), i));
ignore_changes (model);
g_object_unref (store);
g_object_unref (model);
g_object_unref (removed);
}
static void
test_out_of_bounds_access (void)
{
GtkSortListModel *sort;
GListStore *store;
gpointer item;
store = new_store ((guint[]) { 4, 8, 2, 6, 10, 0 });
sort = new_model (store);
item = g_list_model_get_item (G_LIST_MODEL (sort), GTK_INVALID_LIST_POSITION);
g_assert_null (item);
g_object_unref (store);
g_object_unref (sort);
}
int
main (int argc, char *argv[])
{
@ -432,6 +553,8 @@ main (int argc, char *argv[])
g_test_add_func ("/sortlistmodel/remove_items", test_remove_items);
#endif
g_test_add_func ("/sortlistmodel/stability", test_stability);
g_test_add_func ("/sortlistmodel/incremental/remove", test_incremental_remove);
g_test_add_func ("/sortlistmodel/oob-access", test_out_of_bounds_access);
return g_test_run ();
}