1#include <gtk/gtk.h>
2#include <locale.h>
3
4#include "../gtk/gtktextprivate.h"
5#include "../gtk/gtktextviewprivate.h"
6
7static void
8test_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), text: "abcd");
22 gtk_editable_set_position (GTK_EDITABLE (widget), position: 2);
23
24 ret = gtk_im_context_get_surrounding_with_selection (context,
25 text: &text,
26 cursor_index: &cursor_pos,
27 anchor_index: &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 (mem: text);
35
36 ret = gtk_im_context_delete_surrounding (context, offset: -1, n_chars: 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, offset: 1, n_chars: 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), text: "abcd");
49 gtk_editable_select_region (GTK_EDITABLE (widget), start_pos: 4, end_pos: 2);
50
51 ret = gtk_im_context_get_surrounding_with_selection (context,
52 text: &text,
53 cursor_index: &cursor_pos,
54 anchor_index: &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 (mem: text);
62
63 g_object_ref_sink (widget);
64 g_object_unref (object: widget);
65}
66
67static void
68test_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, text: "abcd\nefgh\nijkl", len: -1);
86 gtk_text_buffer_get_iter_at_line_offset (buffer, iter: &iter, line_number: 1, char_offset: 2);
87 gtk_text_buffer_place_cursor (buffer, where: &iter);
88
89 ret = gtk_im_context_get_surrounding_with_selection (context,
90 text: &text,
91 cursor_index: &cursor_pos,
92 anchor_index: &selection_bound);
93
94 g_assert_true (ret);
95 g_assert_cmpstr (text, ==, "abcd\nefgh\nijkl");
96 g_assert_cmpint (cursor_pos, ==, 7);
97 g_assert_cmpint (selection_bound, ==, 7);
98
99 g_free (mem: text);
100
101 ret = gtk_im_context_delete_surrounding (context, offset: -1, n_chars: 1);
102 g_assert_true (ret);
103
104 gtk_text_buffer_get_bounds (buffer, start: &start, end: &end);
105 text = gtk_text_buffer_get_text (buffer, start: &start, end: &end, FALSE);
106 g_assert_cmpstr (text, ==, "abcd\negh\nijkl");
107 g_free (mem: text);
108 gtk_text_buffer_get_selection_bounds (buffer, start: &start, end: &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, offset: 1, n_chars: 1);
113 g_assert_true (ret);
114
115 gtk_text_buffer_get_bounds (buffer, start: &start, end: &end);
116 text = gtk_text_buffer_get_text (buffer, start: &start, end: &end, FALSE);
117 g_assert_cmpstr (text, ==, "abcd\neg\nijkl");
118 g_free (mem: text);
119 gtk_text_buffer_get_selection_bounds (buffer, start: &start, end: &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, text: "ab cd\nef gh\nijkl", len: -1);
125 gtk_text_buffer_get_iter_at_line_offset (buffer, iter: &start, line_number: 1, char_offset: 4);
126 gtk_text_buffer_get_iter_at_line_offset (buffer, iter: &end, line_number: 2, char_offset: 2);
127 gtk_text_buffer_select_range (buffer, ins: &start, bound: &end);
128
129 ret = gtk_im_context_get_surrounding_with_selection (context,
130 text: &text,
131 cursor_index: &cursor_pos,
132 anchor_index: &selection_bound);
133
134 g_assert_true (ret);
135 g_assert_cmpstr (text, ==, "cd\nef gh\nijkl");
136 g_assert_cmpint (cursor_pos, ==, 11);
137 g_assert_cmpint (selection_bound, ==, 7);
138
139 g_free (mem: text);
140
141 g_object_ref_sink (widget);
142 g_object_unref (object: widget);
143}
144
145int
146main (int argc, char *argv[])
147{
148 gtk_test_init (argcp: &argc, argvp: &argv, NULL);
149
150 g_object_set (object: gtk_settings_get_default (), first_property_name: "gtk-im-module", "gtk-im-context-simple", NULL);
151
152 g_test_add_func (testpath: "/im-context/text-surrounding", test_func: test_text_surrounding);
153 g_test_add_func (testpath: "/im-context/textview-surrounding", test_func: test_textview_surrounding);
154
155 return g_test_run ();
156}
157

source code of gtk/testsuite/gtk/imcontext.c