2008-07-01 22:57:50 +00:00
|
|
|
/* GTK - The GIMP Toolkit
|
2008-06-17 13:37:59 +00:00
|
|
|
* gtkrecentmanager.c: a manager for the recently used resources
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Emmanuele Bassi
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-02-27 13:01:10 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2008-06-17 13:37:59 +00:00
|
|
|
*/
|
|
|
|
|
2010-10-22 15:12:16 +00:00
|
|
|
#include <glib/gstdio.h>
|
2008-06-17 13:37:59 +00:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *uri = "file:///tmp/testrecentchooser.txt";
|
|
|
|
const char *uri2 = "file:///tmp/testrecentchooser2.txt";
|
2008-06-17 13:37:59 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
recent_manager_get_default (void)
|
|
|
|
{
|
|
|
|
GtkRecentManager *manager;
|
|
|
|
GtkRecentManager *manager2;
|
|
|
|
|
|
|
|
manager = gtk_recent_manager_get_default ();
|
|
|
|
g_assert (manager != NULL);
|
|
|
|
|
|
|
|
manager2 = gtk_recent_manager_get_default ();
|
|
|
|
g_assert (manager == manager2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
recent_manager_add (void)
|
|
|
|
{
|
|
|
|
GtkRecentManager *manager;
|
|
|
|
GtkRecentData *recent_data;
|
|
|
|
gboolean res;
|
|
|
|
|
|
|
|
manager = gtk_recent_manager_get_default ();
|
|
|
|
|
|
|
|
recent_data = g_slice_new0 (GtkRecentData);
|
|
|
|
|
2013-06-23 23:13:44 +00:00
|
|
|
G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
|
|
|
|
|
2008-06-17 13:37:59 +00:00
|
|
|
/* mime type is mandatory */
|
|
|
|
recent_data->mime_type = NULL;
|
2020-03-07 14:16:36 +00:00
|
|
|
recent_data->app_name = (char *)"testrecentchooser";
|
|
|
|
recent_data->app_exec = (char *)"testrecentchooser %u";
|
2008-06-17 13:37:59 +00:00
|
|
|
if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
|
|
|
|
{
|
|
|
|
res = gtk_recent_manager_add_full (manager,
|
|
|
|
uri,
|
|
|
|
recent_data);
|
|
|
|
}
|
|
|
|
g_test_trap_assert_failed ();
|
|
|
|
|
|
|
|
/* app name is mandatory */
|
2020-03-07 14:16:36 +00:00
|
|
|
recent_data->mime_type = (char *)"text/plain";
|
2008-06-17 13:37:59 +00:00
|
|
|
recent_data->app_name = NULL;
|
2020-03-07 14:16:36 +00:00
|
|
|
recent_data->app_exec = (char *)"testrecentchooser %u";
|
2008-06-17 13:37:59 +00:00
|
|
|
if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
|
|
|
|
{
|
|
|
|
res = gtk_recent_manager_add_full (manager,
|
|
|
|
uri,
|
|
|
|
recent_data);
|
|
|
|
}
|
|
|
|
g_test_trap_assert_failed ();
|
|
|
|
|
|
|
|
/* app exec is mandatory */
|
2020-03-07 14:16:36 +00:00
|
|
|
recent_data->mime_type = (char *)"text/plain";
|
|
|
|
recent_data->app_name = (char *)"testrecentchooser";
|
2008-06-17 13:37:59 +00:00
|
|
|
recent_data->app_exec = NULL;
|
|
|
|
if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
|
|
|
|
{
|
|
|
|
res = gtk_recent_manager_add_full (manager,
|
|
|
|
uri,
|
|
|
|
recent_data);
|
|
|
|
}
|
|
|
|
g_test_trap_assert_failed ();
|
|
|
|
|
2013-06-23 23:13:44 +00:00
|
|
|
G_GNUC_END_IGNORE_DEPRECATIONS;
|
|
|
|
|
2020-03-07 14:16:36 +00:00
|
|
|
recent_data->mime_type = (char *)"text/plain";
|
|
|
|
recent_data->app_name = (char *)"testrecentchooser";
|
|
|
|
recent_data->app_exec = (char *)"testrecentchooser %u";
|
2008-06-17 13:37:59 +00:00
|
|
|
res = gtk_recent_manager_add_full (manager,
|
|
|
|
uri,
|
|
|
|
recent_data);
|
|
|
|
g_assert (res == TRUE);
|
|
|
|
|
|
|
|
g_slice_free (GtkRecentData, recent_data);
|
|
|
|
}
|
|
|
|
|
2010-10-22 15:12:16 +00:00
|
|
|
typedef struct {
|
2019-02-08 14:37:21 +00:00
|
|
|
GtkRecentManager *manager;
|
2010-10-22 15:12:16 +00:00
|
|
|
GMainLoop *main_loop;
|
2020-07-24 13:54:49 +00:00
|
|
|
int counter;
|
2010-10-22 15:12:16 +00:00
|
|
|
} AddManyClosure;
|
|
|
|
|
|
|
|
static void
|
|
|
|
check_bulk (GtkRecentManager *manager,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
AddManyClosure *closure = data;
|
|
|
|
|
|
|
|
if (g_test_verbose ())
|
|
|
|
g_print (G_STRLOC ": counter = %d\n", closure->counter);
|
|
|
|
|
|
|
|
g_assert_cmpint (closure->counter, ==, 100);
|
|
|
|
|
|
|
|
if (g_main_loop_is_running (closure->main_loop))
|
|
|
|
g_main_loop_quit (closure->main_loop);
|
|
|
|
}
|
|
|
|
|
2019-02-08 14:37:21 +00:00
|
|
|
static gboolean
|
|
|
|
add_bulk (gpointer data_)
|
2010-10-22 15:12:16 +00:00
|
|
|
{
|
2019-02-08 14:37:21 +00:00
|
|
|
AddManyClosure *closure = data_;
|
2010-10-22 15:12:16 +00:00
|
|
|
GtkRecentData *data = g_slice_new0 (GtkRecentData);
|
2019-02-08 14:37:21 +00:00
|
|
|
int i;
|
2010-10-22 15:12:16 +00:00
|
|
|
|
|
|
|
for (i = 0; i < 100; i++)
|
|
|
|
{
|
2019-02-08 14:37:21 +00:00
|
|
|
char *new_uri;
|
2010-10-22 15:12:16 +00:00
|
|
|
|
2020-03-07 14:16:36 +00:00
|
|
|
data->mime_type = (char *)"text/plain";
|
|
|
|
data->app_name = (char *)"testrecentchooser";
|
|
|
|
data->app_exec = (char *)"testrecentchooser %u";
|
2010-10-22 15:12:16 +00:00
|
|
|
|
|
|
|
if (g_test_verbose ())
|
|
|
|
g_print (G_STRLOC ": adding item %d\n", i);
|
|
|
|
|
|
|
|
new_uri = g_strdup_printf ("file:///doesnotexist-%d.txt", i);
|
2019-02-08 14:37:21 +00:00
|
|
|
gtk_recent_manager_add_full (closure->manager, new_uri, data);
|
2010-10-22 15:12:16 +00:00
|
|
|
g_free (new_uri);
|
|
|
|
|
|
|
|
closure->counter += 1;
|
|
|
|
}
|
|
|
|
|
2019-02-08 14:37:21 +00:00
|
|
|
g_slice_free (GtkRecentData, data);
|
|
|
|
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
recent_manager_add_many (void)
|
|
|
|
{
|
|
|
|
GtkRecentManager *manager = g_object_new (GTK_TYPE_RECENT_MANAGER,
|
|
|
|
"filename", "recently-used.xbel",
|
|
|
|
NULL);
|
|
|
|
AddManyClosure *closure = g_new (AddManyClosure, 1);
|
|
|
|
|
|
|
|
closure->main_loop = g_main_loop_new (NULL, FALSE);
|
|
|
|
closure->counter = 0;
|
|
|
|
closure->manager = manager;
|
|
|
|
|
|
|
|
g_signal_connect (manager, "changed", G_CALLBACK (check_bulk), closure);
|
|
|
|
|
|
|
|
g_idle_add (add_bulk, closure);
|
|
|
|
|
2010-10-22 15:12:16 +00:00
|
|
|
g_main_loop_run (closure->main_loop);
|
|
|
|
|
|
|
|
g_main_loop_unref (closure->main_loop);
|
2019-02-08 14:37:21 +00:00
|
|
|
g_object_unref (closure->manager);
|
2010-10-22 15:12:16 +00:00
|
|
|
g_free (closure);
|
|
|
|
|
|
|
|
g_assert_cmpint (g_unlink ("recently-used.xbel"), ==, 0);
|
|
|
|
}
|
|
|
|
|
2008-06-17 13:37:59 +00:00
|
|
|
static void
|
|
|
|
recent_manager_has_item (void)
|
|
|
|
{
|
|
|
|
GtkRecentManager *manager;
|
|
|
|
gboolean res;
|
|
|
|
|
|
|
|
manager = gtk_recent_manager_get_default ();
|
|
|
|
|
|
|
|
res = gtk_recent_manager_has_item (manager, "file:///tmp/testrecentdoesnotexist.txt");
|
|
|
|
g_assert (res == FALSE);
|
|
|
|
|
|
|
|
res = gtk_recent_manager_has_item (manager, uri);
|
|
|
|
g_assert (res == TRUE);
|
|
|
|
}
|
|
|
|
|
2008-06-17 15:46:40 +00:00
|
|
|
static void
|
|
|
|
recent_manager_move_item (void)
|
|
|
|
{
|
|
|
|
GtkRecentManager *manager;
|
|
|
|
gboolean res;
|
|
|
|
GError *error;
|
|
|
|
|
|
|
|
manager = gtk_recent_manager_get_default ();
|
|
|
|
|
|
|
|
error = NULL;
|
|
|
|
res = gtk_recent_manager_move_item (manager,
|
|
|
|
"file:///tmp/testrecentdoesnotexist.txt",
|
|
|
|
uri2,
|
|
|
|
&error);
|
|
|
|
g_assert (res == FALSE);
|
|
|
|
g_assert (error != NULL);
|
|
|
|
g_assert (error->domain == GTK_RECENT_MANAGER_ERROR);
|
|
|
|
g_assert (error->code == GTK_RECENT_MANAGER_ERROR_NOT_FOUND);
|
|
|
|
g_error_free (error);
|
|
|
|
|
|
|
|
error = NULL;
|
|
|
|
res = gtk_recent_manager_move_item (manager, uri, uri2, &error);
|
|
|
|
g_assert (res == TRUE);
|
|
|
|
g_assert (error == NULL);
|
|
|
|
|
|
|
|
res = gtk_recent_manager_has_item (manager, uri);
|
|
|
|
g_assert (res == FALSE);
|
|
|
|
|
|
|
|
res = gtk_recent_manager_has_item (manager, uri2);
|
|
|
|
g_assert (res == TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
recent_manager_lookup_item (void)
|
|
|
|
{
|
|
|
|
GtkRecentManager *manager;
|
|
|
|
GtkRecentInfo *info;
|
|
|
|
GError *error;
|
|
|
|
|
|
|
|
manager = gtk_recent_manager_get_default ();
|
|
|
|
|
|
|
|
error = NULL;
|
|
|
|
info = gtk_recent_manager_lookup_item (manager,
|
|
|
|
"file:///tmp/testrecentdoesnotexist.txt",
|
|
|
|
&error);
|
|
|
|
g_assert (info == NULL);
|
|
|
|
g_assert (error != NULL);
|
|
|
|
g_assert (error->domain == GTK_RECENT_MANAGER_ERROR);
|
|
|
|
g_assert (error->code == GTK_RECENT_MANAGER_ERROR_NOT_FOUND);
|
|
|
|
g_error_free (error);
|
|
|
|
|
|
|
|
error = NULL;
|
|
|
|
info = gtk_recent_manager_lookup_item (manager, uri2, &error);
|
|
|
|
g_assert (info != NULL);
|
|
|
|
g_assert (error == NULL);
|
|
|
|
|
|
|
|
g_assert (gtk_recent_info_has_application (info, "testrecentchooser"));
|
|
|
|
|
|
|
|
gtk_recent_info_unref (info);
|
|
|
|
}
|
|
|
|
|
2008-06-17 13:37:59 +00:00
|
|
|
static void
|
|
|
|
recent_manager_remove_item (void)
|
|
|
|
{
|
|
|
|
GtkRecentManager *manager;
|
|
|
|
gboolean res;
|
|
|
|
GError *error;
|
|
|
|
|
|
|
|
manager = gtk_recent_manager_get_default ();
|
|
|
|
|
|
|
|
error = NULL;
|
|
|
|
res = gtk_recent_manager_remove_item (manager,
|
|
|
|
"file:///tmp/testrecentdoesnotexist.txt",
|
|
|
|
&error);
|
|
|
|
g_assert (res == FALSE);
|
|
|
|
g_assert (error != NULL);
|
|
|
|
g_assert (error->domain == GTK_RECENT_MANAGER_ERROR);
|
|
|
|
g_assert (error->code == GTK_RECENT_MANAGER_ERROR_NOT_FOUND);
|
|
|
|
g_error_free (error);
|
|
|
|
|
|
|
|
/* remove an item that's actually there */
|
|
|
|
error = NULL;
|
2008-06-17 15:46:40 +00:00
|
|
|
res = gtk_recent_manager_remove_item (manager, uri2, &error);
|
2008-06-17 13:37:59 +00:00
|
|
|
g_assert (res == TRUE);
|
|
|
|
g_assert (error == NULL);
|
|
|
|
|
2008-06-17 15:46:40 +00:00
|
|
|
res = gtk_recent_manager_has_item (manager, uri2);
|
2008-06-17 13:37:59 +00:00
|
|
|
g_assert (res == FALSE);
|
|
|
|
}
|
|
|
|
|
2008-06-17 15:46:40 +00:00
|
|
|
static void
|
|
|
|
recent_manager_purge (void)
|
|
|
|
{
|
|
|
|
GtkRecentManager *manager;
|
|
|
|
GtkRecentData *recent_data;
|
2020-07-24 13:54:49 +00:00
|
|
|
int n;
|
2008-06-17 15:46:40 +00:00
|
|
|
GError *error;
|
|
|
|
|
|
|
|
manager = gtk_recent_manager_get_default ();
|
|
|
|
|
|
|
|
/* purge, add 1, purge again and check that 1 item has been purged */
|
|
|
|
error = NULL;
|
|
|
|
n = gtk_recent_manager_purge_items (manager, &error);
|
|
|
|
g_assert (error == NULL);
|
|
|
|
|
|
|
|
recent_data = g_slice_new0 (GtkRecentData);
|
2020-03-07 14:16:36 +00:00
|
|
|
recent_data->mime_type = (char *)"text/plain";
|
|
|
|
recent_data->app_name = (char *)"testrecentchooser";
|
|
|
|
recent_data->app_exec = (char *)"testrecentchooser %u";
|
2008-06-17 15:46:40 +00:00
|
|
|
gtk_recent_manager_add_full (manager, uri, recent_data);
|
|
|
|
g_slice_free (GtkRecentData, recent_data);
|
|
|
|
|
|
|
|
error = NULL;
|
|
|
|
n = gtk_recent_manager_purge_items (manager, &error);
|
|
|
|
g_assert (error == NULL);
|
|
|
|
g_assert (n == 1);
|
|
|
|
}
|
|
|
|
|
2008-06-17 13:37:59 +00:00
|
|
|
int
|
|
|
|
main (int argc,
|
|
|
|
char **argv)
|
|
|
|
{
|
|
|
|
gtk_test_init (&argc, &argv, NULL);
|
|
|
|
|
2010-10-22 15:12:16 +00:00
|
|
|
g_test_add_func ("/recent-manager/get-default", recent_manager_get_default);
|
|
|
|
g_test_add_func ("/recent-manager/add", recent_manager_add);
|
|
|
|
g_test_add_func ("/recent-manager/add-many", recent_manager_add_many);
|
|
|
|
g_test_add_func ("/recent-manager/has-item", recent_manager_has_item);
|
|
|
|
g_test_add_func ("/recent-manager/move-item", recent_manager_move_item);
|
|
|
|
g_test_add_func ("/recent-manager/lookup-item", recent_manager_lookup_item);
|
|
|
|
g_test_add_func ("/recent-manager/remove-item", recent_manager_remove_item);
|
|
|
|
g_test_add_func ("/recent-manager/purge", recent_manager_purge);
|
2008-06-17 13:37:59 +00:00
|
|
|
|
|
|
|
return g_test_run ();
|
|
|
|
}
|