| 1 |
/* |
| 2 |
* testing.c |
| 3 |
* This file is part of libpeas |
| 4 |
* |
| 5 |
* Copyright (C) 2010 - Garrett Regier |
| 6 |
* |
| 7 |
* libpeas is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU Lesser General Public |
| 9 |
* License as published by the Free Software Foundation; either |
| 10 |
* version 2.1 of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* libpeas is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* Lesser General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General Public |
| 18 |
* License along with this library; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifdef HAVE_CONFIG_H |
| 23 |
#include <config.h> |
| 24 |
#endif |
| 25 |
|
| 26 |
#include <stdlib.h> |
| 27 |
|
| 28 |
#include <glib.h> |
| 29 |
#include <girepository/girepository.h> |
| 30 |
#include <testing-util.h> |
| 31 |
|
| 32 |
#include "testing.h" |
| 33 |
|
| 34 |
void |
| 35 |
testing_init (gint *argc, |
| 36 |
gchar ***argv) |
| 37 |
{ |
| 38 |
GError *error = NULL; |
| 39 |
static gboolean initialized = FALSE; |
| 40 |
GIRepository *repository; |
| 41 |
|
| 42 |
if (initialized) |
| 43 |
return; |
| 44 |
|
| 45 |
/* Must set environment variables first */ |
| 46 |
testing_util_envars (); |
| 47 |
|
| 48 |
gtk_test_init (argc, argv, NULL); |
| 49 |
|
| 50 |
/* Must be called after gtk_test_init() |
| 51 |
* because it changed the log settings |
| 52 |
*/ |
| 53 |
testing_util_init (); |
| 54 |
|
| 55 |
repository = gi_repository_dup_default (); |
| 56 |
gi_repository_require_private (repository, |
| 57 |
BUILDDIR "/libpeas-gtk", |
| 58 |
"PeasGtk", "1.0", 0, &error); |
| 59 |
g_assert_no_error (error); |
| 60 |
g_clear_object (&repository); |
| 61 |
|
| 62 |
initialized = TRUE; |
| 63 |
} |
| 64 |
|
| 65 |
PeasEngine * |
| 66 |
testing_engine_new (void) |
| 67 |
{ |
| 68 |
PeasEngine *engine; |
| 69 |
|
| 70 |
engine = testing_util_engine_new (); |
| 71 |
peas_engine_add_search_path (engine, BUILDDIR "/tests/libpeas-gtk/plugins", |
| 72 |
SRCDIR "/tests/libpeas-gtk/plugins"); |
| 73 |
|
| 74 |
return engine; |
| 75 |
} |
| 76 |
|
| 77 |
PeasPluginInfo * |
| 78 |
testing_get_plugin_info_for_iter (PeasGtkPluginManagerView *view, |
| 79 |
GtkTreeIter *iter) |
| 80 |
{ |
| 81 |
GtkTreeSelection *selection; |
| 82 |
GtkTreeIter selected_iter; |
| 83 |
gboolean had_selection; |
| 84 |
PeasPluginInfo *info; |
| 85 |
|
| 86 |
/* This is annoying but the only way to get the plugin info |
| 87 |
* is to ask the view for the info of the selected plugin |
| 88 |
*/ |
| 89 |
|
| 90 |
selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view)); |
| 91 |
|
| 92 |
had_selection = gtk_tree_selection_get_selected (selection, |
| 93 |
NULL, &selected_iter); |
| 94 |
|
| 95 |
gtk_tree_selection_select_iter (selection, iter); |
| 96 |
|
| 97 |
info = peas_gtk_plugin_manager_view_get_selected_plugin (view); |
| 98 |
|
| 99 |
if (had_selection) |
| 100 |
gtk_tree_selection_select_iter (selection, &selected_iter); |
| 101 |
|
| 102 |
return info; |
| 103 |
} |
| 104 |
|
| 105 |
gboolean |
| 106 |
testing_get_iter_for_plugin_info (PeasGtkPluginManagerView *view, |
| 107 |
PeasPluginInfo *info, |
| 108 |
GtkTreeIter *iter) |
| 109 |
{ |
| 110 |
GtkTreeModel *model; |
| 111 |
GtkTreeIter pos_iter; |
| 112 |
|
| 113 |
model = gtk_tree_view_get_model (GTK_TREE_VIEW (view)); |
| 114 |
|
| 115 |
g_assert (gtk_tree_model_get_iter_first (model, &pos_iter)); |
| 116 |
|
| 117 |
do |
| 118 |
{ |
| 119 |
if (testing_get_plugin_info_for_iter (view, &pos_iter) == info) |
| 120 |
{ |
| 121 |
if (iter != NULL) |
| 122 |
*iter = pos_iter; |
| 123 |
|
| 124 |
return TRUE; |
| 125 |
} |
| 126 |
} |
| 127 |
while (gtk_tree_model_iter_next (model, &pos_iter)); |
| 128 |
|
| 129 |
return FALSE; |
| 130 |
} |
| 131 |
|
| 132 |
static gboolean |
| 133 |
delete_event_cb (GtkWidget *window, |
| 134 |
GdkEvent *event, |
| 135 |
GtkWidget *widget) |
| 136 |
{ |
| 137 |
gtk_main_quit (); |
| 138 |
|
| 139 |
return TRUE; |
| 140 |
} |
| 141 |
|
| 142 |
/* This takes a gpointer for ease of debugging */ |
| 143 |
void |
| 144 |
testing_show_widget (gpointer widget) |
| 145 |
{ |
| 146 |
GtkWidget *window; |
| 147 |
GLogFunc orig_log_handler; |
| 148 |
|
| 149 |
g_assert (GTK_IS_WIDGET (widget)); |
| 150 |
|
| 151 |
widget = gtk_widget_get_toplevel (GTK_WIDGET (widget)); |
| 152 |
|
| 153 |
if (gtk_widget_is_toplevel (GTK_WIDGET (widget))) |
| 154 |
window = widget; |
| 155 |
else |
| 156 |
{ |
| 157 |
window = gtk_window_new (GTK_WINDOW_TOPLEVEL); |
| 158 |
gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (widget)); |
| 159 |
} |
| 160 |
|
| 161 |
gtk_window_set_default_size (GTK_WINDOW (window), 200, 100); |
| 162 |
|
| 163 |
gtk_widget_show_all (window); |
| 164 |
|
| 165 |
g_signal_connect (window, |
| 166 |
"delete-event", |
| 167 |
G_CALLBACK (delete_event_cb), |
| 168 |
widget); |
| 169 |
|
| 170 |
/* Do not abort if a warning occurs while running the widget */ |
| 171 |
orig_log_handler = g_log_set_default_handler (g_log_default_handler, NULL); |
| 172 |
|
| 173 |
gtk_main (); |
| 174 |
|
| 175 |
g_log_set_default_handler (orig_log_handler, NULL); |
| 176 |
} |