/platform/webkit/WebKit/gtk/tests/testatk.c

https://github.com/lems111/Intercept-CM6-Kernel · C · 240 lines · 150 code · 61 blank · 29 comment · 2 complexity · c28c72f0f6bf6e7e0fae37dbc705dc9b MD5 · raw file

  1. /*
  2. * Copyright (C) 2009 Igalia S.L.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public License
  15. * along with this library; see the file COPYING.LIB. If not, write to
  16. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301, USA.
  18. */
  19. #include <errno.h>
  20. #include <unistd.h>
  21. #include <glib.h>
  22. #include <glib/gstdio.h>
  23. #include <gtk/gtk.h>
  24. #include <webkit/webkit.h>
  25. #if GLIB_CHECK_VERSION(2, 16, 0) && GTK_CHECK_VERSION(2, 14, 0)
  26. static const char* contents = "<html><body><p>This is a test. This is the second sentence. And this the third.</p></body></html>";
  27. static gboolean bail_out(GMainLoop* loop)
  28. {
  29. if (g_main_loop_is_running(loop))
  30. g_main_loop_quit(loop);
  31. return FALSE;
  32. }
  33. typedef gchar* (*AtkGetTextFunction) (AtkText*, gint, AtkTextBoundary, gint*, gint*);
  34. static void test_get_text_function(AtkText* text_obj, AtkGetTextFunction fn, AtkTextBoundary boundary, gint offset, const char* text_result, gint start_offset_result, gint end_offset_result)
  35. {
  36. gint start_offset, end_offset;
  37. char* text;
  38. text = fn(text_obj, offset, boundary, &start_offset, &end_offset);
  39. g_assert_cmpstr(text, ==, text_result);
  40. g_assert_cmpint(start_offset, ==, start_offset_result);
  41. g_assert_cmpint(end_offset, ==, end_offset_result);
  42. g_free(text);
  43. }
  44. static void test_webkit_atk_get_text_at_offset(void)
  45. {
  46. WebKitWebView* webView;
  47. AtkObject *obj;
  48. GMainLoop* loop;
  49. AtkText* text_obj;
  50. char* text;
  51. webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
  52. g_object_ref_sink(webView);
  53. webkit_web_view_load_string(webView, contents, NULL, NULL, NULL);
  54. loop = g_main_loop_new(NULL, TRUE);
  55. g_timeout_add(100, (GSourceFunc)bail_out, loop);
  56. g_main_loop_run(loop);
  57. /* Get to the inner AtkText object */
  58. obj = gtk_widget_get_accessible(GTK_WIDGET(webView));
  59. g_assert(obj);
  60. obj = atk_object_ref_accessible_child(obj, 0);
  61. g_assert(obj);
  62. obj = atk_object_ref_accessible_child(obj, 0);
  63. g_assert(obj);
  64. text_obj = ATK_TEXT(obj);
  65. g_assert(ATK_IS_TEXT(text_obj));
  66. text = atk_text_get_text(text_obj, 0, -1);
  67. g_assert_cmpstr(text, ==, "This is a test. This is the second sentence. And this the third.");
  68. g_free(text);
  69. /* ATK_TEXT_BOUNDARY_CHAR */
  70. test_get_text_function(text_obj, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_CHAR,
  71. 0, "T", 0, 1);
  72. test_get_text_function(text_obj, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_CHAR,
  73. 0, "h", 1, 2);
  74. test_get_text_function(text_obj, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_CHAR,
  75. 0, "", 0, 0);
  76. test_get_text_function(text_obj, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_CHAR,
  77. 1, "T", 0, 1);
  78. /* ATK_TEXT_BOUNDARY_WORD_START */
  79. test_get_text_function(text_obj, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_START,
  80. 0, "This ", 0, 5);
  81. test_get_text_function(text_obj, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_START,
  82. 4, "This ", 0, 5);
  83. test_get_text_function(text_obj, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_START,
  84. 10, "test. ", 10, 16);
  85. test_get_text_function(text_obj, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_START,
  86. 58, "third.", 58, 64);
  87. test_get_text_function(text_obj, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_WORD_START,
  88. 5, "This ", 0, 5);
  89. test_get_text_function(text_obj, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_WORD_START,
  90. 7, "This ", 0, 5);
  91. test_get_text_function(text_obj, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_WORD_START,
  92. 0, "is ", 5, 8);
  93. test_get_text_function(text_obj, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_WORD_START,
  94. 4, "is ", 5, 8);
  95. test_get_text_function(text_obj, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_WORD_START,
  96. 3, "is ", 5, 8);
  97. /* ATK_TEXT_BOUNDARY_WORD_END */
  98. test_get_text_function(text_obj, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_END,
  99. 0, "This", 0, 4);
  100. test_get_text_function(text_obj, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_END,
  101. 4, " is", 4, 7);
  102. test_get_text_function(text_obj, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_END,
  103. 5, " is", 4, 7);
  104. test_get_text_function(text_obj, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_END,
  105. 9, " test", 9, 14);
  106. test_get_text_function(text_obj, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_WORD_END,
  107. 5, "This", 0, 4);
  108. test_get_text_function(text_obj, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_WORD_END,
  109. 4, "This", 0, 4);
  110. test_get_text_function(text_obj, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_WORD_END,
  111. 7, " is", 4, 7);
  112. test_get_text_function(text_obj, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_WORD_END,
  113. 5, " a", 7, 9);
  114. test_get_text_function(text_obj, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_WORD_END,
  115. 4, " a", 7, 9);
  116. test_get_text_function(text_obj, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_WORD_END,
  117. 58, " third", 57, 63);
  118. /* ATK_TEXT_BOUNDARY_SENTENCE_START */
  119. test_get_text_function(text_obj, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_SENTENCE_START,
  120. 0, "This is a test. ", 0, 16);
  121. test_get_text_function(text_obj, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_SENTENCE_START,
  122. 15, "This is a test. ", 0, 16);
  123. test_get_text_function(text_obj, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_SENTENCE_START,
  124. 0, "This is the second sentence. ", 16, 45);
  125. test_get_text_function(text_obj, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_SENTENCE_START,
  126. 15, "This is the second sentence. ", 16, 45);
  127. test_get_text_function(text_obj, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_SENTENCE_START,
  128. 16, "This is a test. ", 0, 16);
  129. test_get_text_function(text_obj, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_SENTENCE_START,
  130. 44, "This is a test. ", 0, 16);
  131. test_get_text_function(text_obj, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_SENTENCE_START,
  132. 15, "", 0, 0);
  133. /* ATK_TEXT_BOUNDARY_SENTENCE_END */
  134. test_get_text_function(text_obj, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_SENTENCE_END,
  135. 0, "This is a test.", 0, 15);
  136. test_get_text_function(text_obj, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_SENTENCE_END,
  137. 15, " This is the second sentence.", 15, 44);
  138. test_get_text_function(text_obj, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_SENTENCE_END,
  139. 16, " This is the second sentence.", 15, 44);
  140. test_get_text_function(text_obj, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_SENTENCE_END,
  141. 17, " This is the second sentence.", 15, 44);
  142. test_get_text_function(text_obj, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_SENTENCE_END,
  143. 0, " This is the second sentence.", 15, 44);
  144. test_get_text_function(text_obj, atk_text_get_text_after_offset, ATK_TEXT_BOUNDARY_SENTENCE_END,
  145. 15, " And this the third.", 44, 64);
  146. test_get_text_function(text_obj, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_SENTENCE_END,
  147. 16, "This is a test.", 0, 15);
  148. test_get_text_function(text_obj, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_SENTENCE_END,
  149. 15, "This is a test.", 0, 15);
  150. test_get_text_function(text_obj, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_SENTENCE_END,
  151. 14, "", 0, 0);
  152. test_get_text_function(text_obj, atk_text_get_text_before_offset, ATK_TEXT_BOUNDARY_SENTENCE_END,
  153. 44, " This is the second sentence.", 15, 44);
  154. /* It's trick to test these properly right now, since our a11y
  155. implementation splits different lines in different a11y
  156. items */
  157. /* ATK_TEXT_BOUNDARY_LINE_START */
  158. test_get_text_function(text_obj, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_START,
  159. 0, "This is a test. This is the second sentence. And this the third.", 0, 64);
  160. /* ATK_TEXT_BOUNDARY_LINE_END */
  161. test_get_text_function(text_obj, atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_END,
  162. 0, "This is a test. This is the second sentence. And this the third.", 0, 64);
  163. g_object_unref(webView);
  164. }
  165. int main(int argc, char** argv)
  166. {
  167. g_thread_init(NULL);
  168. gtk_test_init(&argc, &argv, NULL);
  169. g_test_bug_base("https://bugs.webkit.org/");
  170. g_test_add_func("/webkit/atk/get_text_at_offset", test_webkit_atk_get_text_at_offset);
  171. return g_test_run ();
  172. }
  173. #else
  174. int main(int argc, char** argv)
  175. {
  176. g_critical("You will need at least glib-2.16.0 and gtk-2.14.0 to run the unit tests. Doing nothing now.");
  177. return 0;
  178. }
  179. #endif