summaryrefslogtreecommitdiff
path: root/testsuite/gtk/imcontext.c
blob: f6f3acf2e44fa1ae1f3751c0223a961c5143f719 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <gtk/gtk.h>
#include <locale.h>

#include "../gtk/gtktextprivate.h"
#include "../gtk/gtktextviewprivate.h"

static void
test_text_surrounding (void)
{
  GtkWidget *widget;
  GtkEventController *controller;
  GtkIMContext *context;
  gboolean ret;
  char *text;
  int cursor_pos, selection_bound;

  widget = gtk_text_new ();
  controller = gtk_text_get_key_controller (GTK_TEXT (widget));
  context = gtk_event_controller_key_get_im_context (GTK_EVENT_CONTROLLER_KEY (controller));

  gtk_editable_set_text (GTK_EDITABLE (widget), "abcd");
  gtk_editable_set_position (GTK_EDITABLE (widget), 2);

  ret = gtk_im_context_get_surrounding_with_selection (context,
                                                       &text,
                                                       &cursor_pos,
                                                       &selection_bound);

  g_assert_true (ret);
  g_assert_cmpstr (text, ==, "abcd");
  g_assert_cmpint (cursor_pos, ==, 2);
  g_assert_cmpint (selection_bound, ==, 2);

  g_free (text);

  ret = gtk_im_context_delete_surrounding (context, -1, 1);
  g_assert_true (ret);

  g_assert_cmpstr (gtk_editable_get_text (GTK_EDITABLE (widget)), ==, "acd");
  g_assert_cmpint (gtk_editable_get_position (GTK_EDITABLE (widget)), ==, 1);

  ret = gtk_im_context_delete_surrounding (context, 1, 1);
  g_assert_true (ret);

  g_assert_cmpstr (gtk_editable_get_text (GTK_EDITABLE (widget)), ==, "ac");
  g_assert_cmpint (gtk_editable_get_position (GTK_EDITABLE (widget)), ==, 1);

  gtk_editable_set_text (GTK_EDITABLE (widget), "abcd");
  gtk_editable_select_region (GTK_EDITABLE (widget), 4, 2);

  ret = gtk_im_context_get_surrounding_with_selection (context,
                                                       &text,
                                                       &cursor_pos,
                                                       &selection_bound);

  g_assert_true (ret);
  g_assert_cmpstr (text, ==, "abcd");
  g_assert_cmpint (cursor_pos, ==, 2);
  g_assert_cmpint (selection_bound, ==, 4);

  g_free (text);

  g_object_ref_sink (widget);
  g_object_unref (widget);
}

static void
test_textview_surrounding (void)
{
  GtkWidget *widget;
  GtkEventController *controller;
  GtkIMContext *context;
  GtkTextBuffer *buffer;
  GtkTextIter iter;
  GtkTextIter start, end;
  gboolean ret;
  char *text;
  int cursor_pos, selection_bound;

  widget = gtk_text_view_new ();
  controller = gtk_text_view_get_key_controller (GTK_TEXT_VIEW (widget));
  context = gtk_event_controller_key_get_im_context (GTK_EVENT_CONTROLLER_KEY (controller));

  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (widget));
  gtk_text_buffer_set_text (buffer, "abcd\nefgh\nijkl", -1);
  gtk_text_buffer_get_iter_at_line_offset (buffer, &iter, 1, 2);
  gtk_text_buffer_place_cursor (buffer, &iter);

  ret = gtk_im_context_get_surrounding_with_selection (context,
                                                       &text,
                                                       &cursor_pos,
                                                       &selection_bound);

  g_assert_true (ret);
  g_assert_cmpstr (text, ==, "efgh");
  g_assert_cmpint (cursor_pos, ==, 2);
  g_assert_cmpint (selection_bound, ==, 2);

  g_free (text);

  ret = gtk_im_context_delete_surrounding (context, -1, 1);
  g_assert_true (ret);

  gtk_text_buffer_get_bounds (buffer, &start, &end);
  text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
  g_assert_cmpstr (text, ==, "abcd\negh\nijkl");
  g_free (text);
  gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
  g_assert_cmpint (gtk_text_iter_get_line (&start), ==, 1);
  g_assert_cmpint (gtk_text_iter_get_line_offset (&start), ==, 1);

  ret = gtk_im_context_delete_surrounding (context, 1, 1);
  g_assert_true (ret);

  gtk_text_buffer_get_bounds (buffer, &start, &end);
  text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
  g_assert_cmpstr (text, ==, "abcd\neg\nijkl");
  g_free (text);
  gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
  g_assert_cmpint (gtk_text_iter_get_line (&start), ==, 1);
  g_assert_cmpint (gtk_text_iter_get_line_offset (&start), ==, 1);

  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (widget));
  gtk_text_buffer_set_text (buffer, "abcd\nefgh\nijkl", -1);
  gtk_text_buffer_get_iter_at_line_offset (buffer, &start, 1, 2);
  gtk_text_buffer_get_iter_at_line_offset (buffer, &end, 2, 2);
  gtk_text_buffer_select_range (buffer, &start, &end);

  ret = gtk_im_context_get_surrounding_with_selection (context,
                                                       &text,
                                                       &cursor_pos,
                                                       &selection_bound);

  g_assert_true (ret);
  g_assert_cmpstr (text, ==, "efgh\nijkl");
  g_assert_cmpint (cursor_pos, ==, 7);
  g_assert_cmpint (selection_bound, ==, 2);

  g_free (text);

  g_object_ref_sink (widget);
  g_object_unref (widget);
}

int
main (int argc, char *argv[])
{
  gtk_test_init (&argc, &argv, NULL);

  g_object_set (gtk_settings_get_default (), "gtk-im-module", "gtk-im-context-simple", NULL);

  g_test_add_func ("/im-context/text-surrounding", test_text_surrounding);
  g_test_add_func ("/im-context/textview-surrounding", test_textview_surrounding);

  return g_test_run ();
}