Schematic editor
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

diagram.c 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * diagram.c
  3. *
  4. * This file is a part of logdiag.
  5. * Copyright Přemysl Janouch 2011
  6. *
  7. * See the file LICENSE for licensing information.
  8. *
  9. */
  10. #include <liblogdiag/liblogdiag.h>
  11. typedef struct
  12. {
  13. LdDiagram *diagram;
  14. }
  15. Diagram;
  16. static void
  17. diagram_setup (Diagram *fixture, gconstpointer test_data)
  18. {
  19. fixture->diagram = ld_diagram_new ();
  20. }
  21. static void
  22. diagram_teardown (Diagram *fixture, gconstpointer test_data)
  23. {
  24. g_object_unref (fixture->diagram);
  25. }
  26. static void
  27. diagram_test_history (Diagram *fixture, gconstpointer user_data)
  28. {
  29. const gdouble start_x = 1;
  30. const guint move_length = 3;
  31. LdDiagramObject *object;
  32. guint i;
  33. gdouble x;
  34. object = ld_diagram_object_new (NULL);
  35. g_object_set (object, "x", start_x, NULL);
  36. /* Link an object with the diagram. */
  37. ld_diagram_insert_object (fixture->diagram, object, 0);
  38. g_assert (ld_diagram_can_undo (fixture->diagram) != FALSE);
  39. /* Create some object actions to undo. */
  40. for (i = 0; i++ < move_length;)
  41. g_object_set (object, "x", start_x + i, NULL);
  42. /* Undo them and check the state. */
  43. for (i = move_length; i--;)
  44. {
  45. g_assert (ld_diagram_can_undo (fixture->diagram) != FALSE);
  46. ld_diagram_undo (fixture->diagram);
  47. g_object_get (object, "x", &x, NULL);
  48. g_assert_cmpfloat (x, ==, start_x + i);
  49. }
  50. /* Redo them and check the state. */
  51. for (i = 0; i++ < move_length;)
  52. {
  53. g_assert (ld_diagram_can_redo (fixture->diagram) != FALSE);
  54. ld_diagram_redo (fixture->diagram);
  55. g_object_get (object, "x", &x, NULL);
  56. g_assert_cmpfloat (x, ==, start_x + i);
  57. }
  58. g_object_unref (object);
  59. }
  60. static void
  61. diagram_test_history_grouping (Diagram *fixture, gconstpointer user_data)
  62. {
  63. const LdPoint start_position = {1, 3};
  64. const guint move_length = 3;
  65. LdDiagramObject *object;
  66. guint i;
  67. gdouble x, y;
  68. object = ld_diagram_object_new (NULL);
  69. g_object_set (object,
  70. "x", start_position.x,
  71. "y", start_position.y,
  72. NULL);
  73. /* Create a single user action. */
  74. ld_diagram_begin_user_action (fixture->diagram);
  75. ld_diagram_insert_object (fixture->diagram, object, 0);
  76. for (i = 0; i++ < move_length;)
  77. {
  78. ld_diagram_begin_user_action (fixture->diagram);
  79. g_object_set (object,
  80. "x", start_position.x + i,
  81. "y", start_position.y + i,
  82. NULL);
  83. ld_diagram_end_user_action (fixture->diagram);
  84. }
  85. ld_diagram_end_user_action (fixture->diagram);
  86. /* Undo the action. */
  87. g_assert (ld_diagram_get_objects (fixture->diagram) != NULL);
  88. g_assert (ld_diagram_can_undo (fixture->diagram) != FALSE);
  89. ld_diagram_undo (fixture->diagram);
  90. /* Check that it has been undone correctly. */
  91. g_assert (ld_diagram_get_objects (fixture->diagram) == NULL);
  92. g_assert (ld_diagram_can_undo (fixture->diagram) == FALSE);
  93. g_object_get (object, "x", &x, "y", &y, NULL);
  94. g_assert_cmpfloat (x, ==, start_position.x);
  95. g_assert_cmpfloat (y, ==, start_position.y);
  96. /* Redo the action. */
  97. g_assert (ld_diagram_can_redo (fixture->diagram) != FALSE);
  98. ld_diagram_redo (fixture->diagram);
  99. /* Check that it has been redone correctly. */
  100. g_object_get (object, "x", &x, "y", &y, NULL);
  101. g_assert_cmpfloat (x, ==, start_position.x + move_length);
  102. g_assert_cmpfloat (y, ==, start_position.y + move_length);
  103. g_object_unref (object);
  104. }
  105. int
  106. main (int argc, char *argv[])
  107. {
  108. gtk_test_init (&argc, &argv, NULL);
  109. /* History. */
  110. g_test_add ("/diagram/history", Diagram, NULL,
  111. diagram_setup, diagram_test_history,
  112. diagram_teardown);
  113. g_test_add ("/diagram/history-grouping", Diagram, NULL,
  114. diagram_setup, diagram_test_history_grouping,
  115. diagram_teardown);
  116. return g_test_run ();
  117. }