1 #include <gtk/gtk.h>
2 #include <locale.h>
3 
4 #include "../gtk/gtktextprivate.h"
5 #include "../gtk/gtktextviewprivate.h"
6 
7 static void
test_text_surrounding(void)8 test_text_surrounding (void)
9 {
10   GtkWidget *widget;
11   GtkEventController *controller;
12   GtkIMContext *context;
13   gboolean ret;
14   char *text;
15   int cursor_pos, selection_bound;
16 
17   widget = gtk_text_new ();
18   controller = gtk_text_get_key_controller (GTK_TEXT (widget));
19   context = gtk_event_controller_key_get_im_context (GTK_EVENT_CONTROLLER_KEY (controller));
20 
21   gtk_editable_set_text (GTK_EDITABLE (widget), "abcd");
22   gtk_editable_set_position (GTK_EDITABLE (widget), 2);
23 
24   ret = gtk_im_context_get_surrounding_with_selection (context,
25                                                        &text,
26                                                        &cursor_pos,
27                                                        &selection_bound);
28 
29   g_assert_true (ret);
30   g_assert_cmpstr (text, ==, "abcd");
31   g_assert_cmpint (cursor_pos, ==, 2);
32   g_assert_cmpint (selection_bound, ==, 2);
33 
34   g_free (text);
35 
36   ret = gtk_im_context_delete_surrounding (context, -1, 1);
37   g_assert_true (ret);
38 
39   g_assert_cmpstr (gtk_editable_get_text (GTK_EDITABLE (widget)), ==, "acd");
40   g_assert_cmpint (gtk_editable_get_position (GTK_EDITABLE (widget)), ==, 1);
41 
42   ret = gtk_im_context_delete_surrounding (context, 1, 1);
43   g_assert_true (ret);
44 
45   g_assert_cmpstr (gtk_editable_get_text (GTK_EDITABLE (widget)), ==, "ac");
46   g_assert_cmpint (gtk_editable_get_position (GTK_EDITABLE (widget)), ==, 1);
47 
48   gtk_editable_set_text (GTK_EDITABLE (widget), "abcd");
49   gtk_editable_select_region (GTK_EDITABLE (widget), 4, 2);
50 
51   ret = gtk_im_context_get_surrounding_with_selection (context,
52                                                        &text,
53                                                        &cursor_pos,
54                                                        &selection_bound);
55 
56   g_assert_true (ret);
57   g_assert_cmpstr (text, ==, "abcd");
58   g_assert_cmpint (cursor_pos, ==, 2);
59   g_assert_cmpint (selection_bound, ==, 4);
60 
61   g_free (text);
62 
63   g_object_ref_sink (widget);
64   g_object_unref (widget);
65 }
66 
67 static void
test_textview_surrounding(void)68 test_textview_surrounding (void)
69 {
70   GtkWidget *widget;
71   GtkEventController *controller;
72   GtkIMContext *context;
73   GtkTextBuffer *buffer;
74   GtkTextIter iter;
75   GtkTextIter start, end;
76   gboolean ret;
77   char *text;
78   int cursor_pos, selection_bound;
79 
80   widget = gtk_text_view_new ();
81   controller = gtk_text_view_get_key_controller (GTK_TEXT_VIEW (widget));
82   context = gtk_event_controller_key_get_im_context (GTK_EVENT_CONTROLLER_KEY (controller));
83 
84   buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (widget));
85   gtk_text_buffer_set_text (buffer, "abcd\nefgh\nijkl", -1);
86   gtk_text_buffer_get_iter_at_line_offset (buffer, &iter, 1, 2);
87   gtk_text_buffer_place_cursor (buffer, &iter);
88 
89   ret = gtk_im_context_get_surrounding_with_selection (context,
90                                                        &text,
91                                                        &cursor_pos,
92                                                        &selection_bound);
93 
94   g_assert_true (ret);
95   g_assert_cmpstr (text, ==, "efgh");
96   g_assert_cmpint (cursor_pos, ==, 2);
97   g_assert_cmpint (selection_bound, ==, 2);
98 
99   g_free (text);
100 
101   ret = gtk_im_context_delete_surrounding (context, -1, 1);
102   g_assert_true (ret);
103 
104   gtk_text_buffer_get_bounds (buffer, &start, &end);
105   text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
106   g_assert_cmpstr (text, ==, "abcd\negh\nijkl");
107   g_free (text);
108   gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
109   g_assert_cmpint (gtk_text_iter_get_line (&start), ==, 1);
110   g_assert_cmpint (gtk_text_iter_get_line_offset (&start), ==, 1);
111 
112   ret = gtk_im_context_delete_surrounding (context, 1, 1);
113   g_assert_true (ret);
114 
115   gtk_text_buffer_get_bounds (buffer, &start, &end);
116   text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
117   g_assert_cmpstr (text, ==, "abcd\neg\nijkl");
118   g_free (text);
119   gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
120   g_assert_cmpint (gtk_text_iter_get_line (&start), ==, 1);
121   g_assert_cmpint (gtk_text_iter_get_line_offset (&start), ==, 1);
122 
123   buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (widget));
124   gtk_text_buffer_set_text (buffer, "abcd\nefgh\nijkl", -1);
125   gtk_text_buffer_get_iter_at_line_offset (buffer, &start, 1, 2);
126   gtk_text_buffer_get_iter_at_line_offset (buffer, &end, 2, 2);
127   gtk_text_buffer_select_range (buffer, &start, &end);
128 
129   ret = gtk_im_context_get_surrounding_with_selection (context,
130                                                        &text,
131                                                        &cursor_pos,
132                                                        &selection_bound);
133 
134   g_assert_true (ret);
135   g_assert_cmpstr (text, ==, "efgh\nijkl");
136   g_assert_cmpint (cursor_pos, ==, 7);
137   g_assert_cmpint (selection_bound, ==, 2);
138 
139   g_free (text);
140 
141   g_object_ref_sink (widget);
142   g_object_unref (widget);
143 }
144 
145 int
main(int argc,char * argv[])146 main (int argc, char *argv[])
147 {
148   gtk_test_init (&argc, &argv, NULL);
149 
150   g_object_set (gtk_settings_get_default (), "gtk-im-module", "gtk-im-context-simple", NULL);
151 
152   g_test_add_func ("/im-context/text-surrounding", test_text_surrounding);
153   g_test_add_func ("/im-context/textview-surrounding", test_textview_surrounding);
154 
155   return g_test_run ();
156 }
157