test_gio.mod 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. MODULE test_gio ;
  2. (*
  3. m2-GTK4 test - GIO extras: GListStore (with GtkStringObject items),
  4. GFile, and GSettings (guarded by a schema lookup so it is safe and
  5. skips cleanly when the test schema is not installed).
  6. Needs no display.
  7. *)
  8. FROM GListStore IMPORT g_list_store_new, g_list_store_append,
  9. g_list_store_remove, g_list_store_find;
  10. FROM GtkStringObject IMPORT gtk_string_object_new,
  11. gtk_string_object_get_string, gtk_string_object_get_type;
  12. FROM GListModel IMPORT g_list_model_get_n_items, g_list_model_get_item;
  13. FROM GFile IMPORT g_file_new_for_path, g_file_get_basename,
  14. g_file_get_path, g_file_query_exists;
  15. FROM GSettings IMPORT g_settings_new, g_settings_get_boolean,
  16. g_settings_set_boolean, g_settings_get_int, g_settings_set_int,
  17. g_settings_get_string, g_settings_set_string, g_settings_reset,
  18. g_settings_sync;
  19. FROM GSettingsSchema IMPORT g_settings_schema_source_get_default,
  20. g_settings_schema_source_lookup, g_settings_schema_unref,
  21. g_settings_schema_has_key;
  22. FROM GObject IMPORT g_object_unref;
  23. FROM GLib IMPORT g_free;
  24. FROM GtkUtils IMPORT CStrToM2;
  25. FROM SYSTEM IMPORT ADDRESS;
  26. FROM libc IMPORT printf;
  27. VAR
  28. failed: BOOLEAN;
  29. PROCEDURE Check (condition: BOOLEAN; VAR ok: BOOLEAN; what: ARRAY OF CHAR);
  30. BEGIN
  31. IF NOT condition THEN
  32. printf("FAIL: %s\n", what);
  33. ok := FALSE
  34. END
  35. END Check;
  36. PROCEDURE TestListStore (VAR ok: BOOLEAN);
  37. VAR
  38. store, o1, o2, o3, item: ADDRESS;
  39. pos: CARDINAL;
  40. buf: ARRAY [0..63] OF CHAR;
  41. BEGIN
  42. store := g_list_store_new(gtk_string_object_get_type());
  43. o1 := gtk_string_object_new("one");
  44. o2 := gtk_string_object_new("two");
  45. o3 := gtk_string_object_new("three");
  46. g_list_store_append(store, o1);
  47. g_list_store_append(store, o2);
  48. g_list_store_append(store, o3);
  49. Check(g_list_model_get_n_items(store) = 3, ok, "list store size");
  50. IF g_list_store_find(store, o3, pos) # 0 THEN
  51. Check(pos = 2, ok, "list store find position")
  52. ELSE
  53. Check(FALSE, ok, "list store find")
  54. END;
  55. (* remove the middle item; the rest shift down *)
  56. g_list_store_remove(store, 1);
  57. Check(g_list_model_get_n_items(store) = 2, ok, "list store after remove");
  58. item := g_list_model_get_item(store, 1);
  59. CStrToM2(gtk_string_object_get_string(item), buf);
  60. Check((buf[0] = 't') AND (buf[4] = 'e') AND (buf[5] = 0C),
  61. ok, "list store item text");
  62. g_object_unref(item);
  63. (* release our references; the store keeps the items alive *)
  64. g_object_unref(o1);
  65. g_object_unref(o2);
  66. g_object_unref(o3);
  67. g_object_unref(store)
  68. END TestListStore;
  69. PROCEDURE TestFile (VAR ok: BOOLEAN);
  70. VAR
  71. file, temp: ADDRESS;
  72. buf: ARRAY [0..255] OF CHAR;
  73. BEGIN
  74. file := g_file_new_for_path("/tmp");
  75. Check(g_file_query_exists(file, NIL) # 0, ok, "file query_exists /tmp");
  76. temp := g_file_get_basename(file);
  77. CStrToM2(temp, buf);
  78. g_free(temp);
  79. Check((buf[0] = 't') AND (buf[2] = 'p') AND (buf[3] = 0C),
  80. ok, "file basename");
  81. g_object_unref(file);
  82. file := g_file_new_for_path("/no/such/m2gtk4/path");
  83. Check(g_file_query_exists(file, NIL) = 0, ok, "file missing");
  84. g_object_unref(file)
  85. END TestFile;
  86. PROCEDURE TestSettings (VAR ok: BOOLEAN);
  87. VAR
  88. source, schema, settings: ADDRESS;
  89. text: ADDRESS;
  90. buf: ARRAY [0..63] OF CHAR;
  91. BEGIN
  92. source := g_settings_schema_source_get_default();
  93. schema := g_settings_schema_source_lookup(source, "org.example.m2gtk4", 1);
  94. IF schema = NIL THEN
  95. printf("(test schema not installed: GSettings part skipped)\n");
  96. RETURN
  97. END;
  98. Check(g_settings_schema_has_key(schema, "flag") # 0, ok, "schema has flag");
  99. g_settings_schema_unref(schema);
  100. settings := g_settings_new("org.example.m2gtk4");
  101. g_settings_set_boolean(settings, "flag", 1);
  102. Check(g_settings_get_boolean(settings, "flag") # 0, ok, "settings boolean");
  103. g_settings_reset(settings, "flag");
  104. g_settings_set_int(settings, "count", 7);
  105. Check(g_settings_get_int(settings, "count") = 7, ok, "settings int");
  106. g_settings_reset(settings, "count");
  107. g_settings_set_string(settings, "name", "modula");
  108. text := g_settings_get_string(settings, "name");
  109. CStrToM2(text, buf);
  110. g_free(text);
  111. Check((buf[0] = 'm') AND (buf[5] = 'a') AND (buf[6] = 0C),
  112. ok, "settings string");
  113. g_settings_reset(settings, "name");
  114. g_settings_sync();
  115. g_object_unref(settings)
  116. END TestSettings;
  117. VAR
  118. ok: BOOLEAN;
  119. BEGIN
  120. ok := TRUE;
  121. TestListStore(ok);
  122. TestFile(ok);
  123. TestSettings(ok);
  124. IF ok THEN
  125. printf("test_gio: PASS\n");
  126. HALT(0)
  127. ELSE
  128. printf("test_gio: FAIL\n");
  129. HALT(1)
  130. END
  131. END test_gio.