1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
|
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 2009 Martin Nordholts <martinn@src.gnome.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include <gegl.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include "libgimpbase/gimpbase.h"
#include "libgimpmath/gimpmath.h"
#include "libgimpwidgets/gimpwidgets.h"
#include "tools/tools-types.h"
#include "tools/gimprectangleoptions.h"
#include "tools/tool_manager.h"
#include "display/gimpdisplay.h"
#include "display/gimpdisplayshell.h"
#include "display/gimpdisplayshell-callbacks.h"
#include "display/gimpdisplayshell-scale.h"
#include "display/gimpdisplayshell-tool-events.h"
#include "display/gimpdisplayshell-transform.h"
#include "display/gimpimagewindow.h"
#include "widgets/gimpdialogfactory.h"
#include "widgets/gimpdock.h"
#include "widgets/gimpdockable.h"
#include "widgets/gimpdockbook.h"
#include "widgets/gimpdocked.h"
#include "widgets/gimpdockwindow.h"
#include "widgets/gimphelp-ids.h"
#include "widgets/gimpsessioninfo.h"
#include "widgets/gimptoolbox.h"
#include "widgets/gimptooloptionseditor.h"
#include "widgets/gimpuimanager.h"
#include "widgets/gimpwidgets-utils.h"
#include "core/gimp.h"
#include "core/gimpchannel.h"
#include "core/gimpcontext.h"
#include "core/gimpimage.h"
#include "core/gimplayer.h"
#include "core/gimptoolinfo.h"
#include "core/gimptooloptions.h"
#include "tests.h"
#include "gimp-app-test-utils.h"
#define GIMP_TEST_IMAGE_WIDTH 150
#define GIMP_TEST_IMAGE_HEIGHT 267
/* Put this in the code below when you want the test to pause so you
* can do measurements of widgets on the screen for example
*/
#define GIMP_PAUSE (g_usleep (2 * 1000 * 1000))
#define ADD_TEST(function) \
g_test_add ("/gimp-tools/" #function, \
GimpTestFixture, \
gimp, \
gimp_tools_setup_image, \
function, \
gimp_tools_teardown_image);
typedef struct
{
int avoid_sizeof_zero;
} GimpTestFixture;
static void gimp_tools_setup_image (GimpTestFixture *fixture,
gconstpointer data);
static void gimp_tools_teardown_image (GimpTestFixture *fixture,
gconstpointer data);
static void gimp_tools_synthesize_image_click_drag_release (GimpDisplayShell *shell,
gdouble start_image_x,
gdouble start_image_y,
gdouble end_image_x,
gdouble end_image_y,
gint button,
GdkModifierType modifiers);
static GimpDisplay * gimp_test_get_only_display (Gimp *gimp);
static GimpImage * gimp_test_get_only_image (Gimp *gimp);
static GimpDisplayShell * gimp_test_get_only_display_shell (Gimp *gimp);
static void
gimp_tools_setup_image (GimpTestFixture *fixture,
gconstpointer data)
{
Gimp *gimp = GIMP (data);
gimp_test_utils_create_image (gimp,
GIMP_TEST_IMAGE_WIDTH,
GIMP_TEST_IMAGE_HEIGHT);
gimp_test_run_mainloop_until_idle ();
}
static void
gimp_tools_teardown_image (GimpTestFixture *fixture,
gconstpointer data)
{
Gimp *gimp = GIMP (data);
g_object_unref (gimp_test_get_only_image (gimp));
gimp_display_close (gimp_test_get_only_display (gimp));
gimp_test_run_mainloop_until_idle ();
}
/**
* gimp_tools_set_tool:
* @gimp:
* @tool_id:
* @display:
*
* Makes sure the given tool is the active tool and that the passed
* display is the focused tool display.
**/
static void
gimp_tools_set_tool (Gimp *gimp,
const gchar *tool_id,
GimpDisplay *display)
{
/* Activate tool and setup active display for the new tool */
gimp_context_set_tool (gimp_get_user_context (gimp),
gimp_get_tool_info (gimp, tool_id));
tool_manager_focus_display_active (gimp, display);
}
/**
* gimp_test_get_only_display:
* @gimp:
*
* Asserts that there only is one image and display and then
* returns the display.
*
* Returns: The #GimpDisplay.
**/
static GimpDisplay *
gimp_test_get_only_display (Gimp *gimp)
{
g_assert (g_list_length (gimp_get_image_iter (gimp)) == 1);
g_assert (g_list_length (gimp_get_display_iter (gimp)) == 1);
return GIMP_DISPLAY (gimp_get_display_iter (gimp)->data);
}
/**
* gimp_test_get_only_display_shell:
* @gimp:
*
* Asserts that there only is one image and display shell and then
* returns the display shell.
*
* Returns: The #GimpDisplayShell.
**/
static GimpDisplayShell *
gimp_test_get_only_display_shell (Gimp *gimp)
{
return gimp_display_get_shell (gimp_test_get_only_display (gimp));
}
/**
* gimp_test_get_only_image:
* @gimp:
*
* Asserts that there is only one image and returns that.
*
* Returns: The #GimpImage.
**/
static GimpImage *
gimp_test_get_only_image (Gimp *gimp)
{
g_assert (g_list_length (gimp_get_image_iter (gimp)) == 1);
g_assert (g_list_length (gimp_get_display_iter (gimp)) == 1);
return GIMP_IMAGE (gimp_get_image_iter (gimp)->data);
}
static void
gimp_test_synthesize_tool_button_event (GimpDisplayShell *shell,
gint x,
gint y,
gint button,
gint modifiers,
GdkEventType button_event_type)
{
GdkEvent *event = gdk_event_new (button_event_type);
GdkWindow *window = gtk_widget_get_window (GTK_WIDGET (shell->canvas));
GdkDisplay *display = gdk_window_get_display (window);
g_assert (button_event_type == GDK_BUTTON_PRESS ||
button_event_type == GDK_BUTTON_RELEASE);
event->button.window = g_object_ref (window);
event->button.send_event = TRUE;
event->button.time = gtk_get_current_event_time ();
event->button.x = x;
event->button.y = y;
event->button.axes = NULL;
event->button.state = 0;
event->button.button = button;
event->button.device = gdk_display_get_core_pointer (display);
event->button.x_root = -1;
event->button.y_root = -1;
gimp_display_shell_canvas_tool_events (shell->canvas,
event,
shell);
gdk_event_free (event);
}
static void
gimp_test_synthesize_tool_motion_event (GimpDisplayShell *shell,
gint x,
gint y,
gint modifiers)
{
GdkEvent *event = gdk_event_new (GDK_MOTION_NOTIFY);
GdkWindow *window = gtk_widget_get_window (GTK_WIDGET (shell->canvas));
GdkDisplay *display = gdk_window_get_display (window);
event->motion.window = g_object_ref (window);
event->motion.send_event = TRUE;
event->motion.time = gtk_get_current_event_time ();
event->motion.x = x;
event->motion.y = y;
event->motion.axes = NULL;
event->motion.state = GDK_BUTTON1_MASK | modifiers;
event->motion.is_hint = FALSE;
event->motion.device = gdk_display_get_core_pointer (display);
event->motion.x_root = -1;
event->motion.y_root = -1;
gimp_display_shell_canvas_tool_events (shell->canvas,
event,
shell);
gdk_event_free (event);
}
static void
gimp_test_synthesize_tool_crossing_event (GimpDisplayShell *shell,
gint x,
gint y,
gint modifiers,
GdkEventType crossing_event_type)
{
GdkEvent *event = gdk_event_new (crossing_event_type);
GdkWindow *window = gtk_widget_get_window (GTK_WIDGET (shell->canvas));
g_assert (crossing_event_type == GDK_ENTER_NOTIFY ||
crossing_event_type == GDK_LEAVE_NOTIFY);
event->crossing.window = g_object_ref (window);
event->crossing.send_event = TRUE;
event->crossing.subwindow = NULL;
event->crossing.time = gtk_get_current_event_time ();
event->crossing.x = x;
event->crossing.y = y;
event->crossing.x_root = -1;
event->crossing.y_root = -1;
event->crossing.mode = GDK_CROSSING_NORMAL;
event->crossing.detail = GDK_NOTIFY_UNKNOWN;
event->crossing.focus = TRUE;
event->crossing.state = modifiers;
gimp_display_shell_canvas_tool_events (shell->canvas,
event,
shell);
gdk_event_free (event);
}
static void
gimp_tools_synthesize_image_click_drag_release (GimpDisplayShell *shell,
gdouble start_image_x,
gdouble start_image_y,
gdouble end_image_x,
gdouble end_image_y,
gint button /*1..3*/,
GdkModifierType modifiers)
{
gdouble start_canvas_x = -1.0;
gdouble start_canvas_y = -1.0;
gdouble middle_canvas_x = -1.0;
gdouble middle_canvas_y = -1.0;
gdouble end_canvas_x = -1.0;
gdouble end_canvas_y = -1.0;
/* Transform coordinates */
gimp_display_shell_transform_xy_f (shell,
start_image_x,
start_image_y,
&start_canvas_x,
&start_canvas_y);
gimp_display_shell_transform_xy_f (shell,
end_image_x,
end_image_y,
&end_canvas_x,
&end_canvas_y);
middle_canvas_x = (start_canvas_x + end_canvas_x) / 2;
middle_canvas_y = (start_canvas_y + end_canvas_y) / 2;
/* Enter notify */
gimp_test_synthesize_tool_crossing_event (shell,
(int)start_canvas_x,
(int)start_canvas_y,
modifiers,
GDK_ENTER_NOTIFY);
/* Button press */
gimp_test_synthesize_tool_button_event (shell,
(int)start_canvas_x,
(int)start_canvas_y,
button,
modifiers,
GDK_BUTTON_PRESS);
/* Move events */
gimp_test_synthesize_tool_motion_event (shell,
(int)start_canvas_x,
(int)start_canvas_y,
modifiers);
gimp_test_synthesize_tool_motion_event (shell,
(int)middle_canvas_x,
(int)middle_canvas_y,
modifiers);
gimp_test_synthesize_tool_motion_event (shell,
(int)end_canvas_x,
(int)end_canvas_y,
modifiers);
/* Button release */
gimp_test_synthesize_tool_button_event (shell,
(int)end_canvas_x,
(int)end_canvas_y,
button,
modifiers,
GDK_BUTTON_RELEASE);
/* Leave notify */
gimp_test_synthesize_tool_crossing_event (shell,
(int)start_canvas_x,
(int)start_canvas_y,
modifiers,
GDK_LEAVE_NOTIFY);
/* Process them */
gimp_test_run_mainloop_until_idle ();
}
/**
* crop_tool_can_crop:
* @fixture:
* @data:
*
* Make sure it's possible to crop at all. Regression test for
* "Bug 315255 - SIGSEGV, while doing a crop".
**/
static void
crop_tool_can_crop (GimpTestFixture *fixture,
gconstpointer data)
{
Gimp *gimp = GIMP (data);
GimpImage *image = gimp_test_get_only_image (gimp);
GimpDisplayShell *shell = gimp_test_get_only_display_shell (gimp);
gint cropped_x = 10;
gint cropped_y = 10;
gint cropped_w = 20;
gint cropped_h = 30;
/* Fit display and pause and let it stabalize (two idlings seems to
* always be enough)
*/
gimp_ui_manager_activate_action (gimp_test_utils_get_ui_manager (gimp),
"view",
"view-shrink-wrap");
gimp_test_run_mainloop_until_idle ();
gimp_test_run_mainloop_until_idle ();
/* Activate crop tool */
gimp_tools_set_tool (gimp, "gimp-crop-tool", shell->display);
/* Do the crop rect */
gimp_tools_synthesize_image_click_drag_release (shell,
cropped_x,
cropped_y,
cropped_x + cropped_w,
cropped_y + cropped_h,
1 /*button*/,
0 /*modifiers*/);
/* Crop */
gimp_test_utils_synthesize_key_event (GTK_WIDGET (shell), GDK_KEY_Return);
gimp_test_run_mainloop_until_idle ();
/* Make sure the new image has the expected size */
g_assert_cmpint (cropped_w, ==, gimp_image_get_width (image));
g_assert_cmpint (cropped_h, ==, gimp_image_get_height (image));
}
/**
* crop_tool_can_crop:
* @fixture:
* @data:
*
* Make sure it's possible to change width of crop rect in tool
* options without there being a pending rectangle. Regression test
* for "Bug 322396 - Crop dimension entering causes crash".
**/
static void
crop_set_width_without_pending_rect (GimpTestFixture *fixture,
gconstpointer data)
{
Gimp *gimp = GIMP (data);
GimpDisplay *display = gimp_test_get_only_display (gimp);
GimpToolInfo *tool_info;
GimpRectangleOptions *rectangle_options;
GtkWidget *tool_options_gui;
GtkWidget *size_entry;
/* Activate crop tool */
gimp_tools_set_tool (gimp, "gimp-crop-tool", display);
/* Get tool options */
tool_info = gimp_get_tool_info (gimp, "gimp-crop-tool");
tool_options_gui = gimp_tools_get_tool_options_gui (tool_info->tool_options);
rectangle_options = GIMP_RECTANGLE_OPTIONS (tool_info->tool_options);
/* Find 'Width' or 'Height' GtkTextEntry in tool options */
size_entry = gimp_rectangle_options_get_width_entry (rectangle_options);
/* Set arbitrary non-0 value */
gimp_size_entry_set_value (GIMP_SIZE_ENTRY (size_entry),
0 /*field*/,
42.0 /*lower*/);
/* If we don't crash, everything s fine */
}
int main(int argc, char **argv)
{
Gimp *gimp = NULL;
gint result = -1;
gimp_test_bail_if_no_display ();
gtk_test_init (&argc, &argv, NULL);
gimp_test_utils_set_gimp2_directory ("GIMP_TESTING_ABS_TOP_SRCDIR",
"app/tests/gimpdir");
gimp_test_utils_setup_menus_dir ();
/* Start up GIMP */
gimp = gimp_init_for_gui_testing (TRUE /*show_gui*/);
gimp_test_run_mainloop_until_idle ();
/* Add tests */
ADD_TEST (crop_tool_can_crop);
ADD_TEST (crop_set_width_without_pending_rect);
/* Run the tests and return status */
result = g_test_run ();
/* Don't write files to the source dir */
gimp_test_utils_set_gimp2_directory ("GIMP_TESTING_ABS_TOP_BUILDDIR",
"app/tests/gimpdir-output");
/* Exit properly so we don't break script-fu plug-in wire */
gimp_exit (gimp, TRUE);
return result;
}
|