~ubuntu-branches/ubuntu/trusty/gtkhtml3.14/trusty-proposed

« back to all changes in this revision

Viewing changes to src/test-stress.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2007-03-13 11:26:39 UTC
  • Revision ID: james.westby@ubuntu.com-20070313112639-5calgv5774i4f95v
Tags: upstream-3.14.0
ImportĀ upstreamĀ versionĀ 3.14.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <string.h>
 
2
#include <stdio.h>
 
3
#include <stdlib.h>
 
4
#include <glib/gstring.h>
 
5
#include <gtk/gtkmain.h>
 
6
#include <gtk/gtkscrolledwindow.h>
 
7
#include <gtk/gtkwindow.h>
 
8
#include "gtkhtml.h"
 
9
#include "htmlclue.h"
 
10
#include "htmlclueflow.h"
 
11
#include "htmlcluev.h"
 
12
#include "htmlcursor.h"
 
13
#include "htmlgdkpainter.h"
 
14
#include "htmlengine.h"
 
15
#include "htmlengine-edit.h"
 
16
#include "htmlengine-edit-cut-and-paste.h"
 
17
#include "htmlengine-edit-movement.h"
 
18
#include "htmlengine-edit-text.h"
 
19
#include "htmlengine-save.h"
 
20
#include "htmlplainpainter.h"
 
21
#include "htmlselection.h"
 
22
#include "htmltable.h"
 
23
#include "htmltablecell.h"
 
24
#include "htmltext.h"
 
25
 
 
26
typedef struct {
 
27
        char *name;
 
28
        int (*test_function) (GtkHTML *html);
 
29
} Test;
 
30
 
 
31
static int test_level_1 (GtkHTML *html);
 
32
 
 
33
static Test tests[] = {
 
34
        { "cursor movement", NULL },
 
35
        { "level 1 - cut/copy/paste", test_level_1 },
 
36
        { NULL, NULL }
 
37
};
 
38
 
 
39
static HTMLGdkPainter *gdk_painter = NULL;
 
40
static HTMLGdkPainter *plain_painter = NULL;
 
41
 
 
42
static void
 
43
set_format (GtkHTML *html, gboolean format_html)
 
44
{
 
45
        HTMLGdkPainter *p, *old_p;
 
46
 
 
47
        gtk_widget_ensure_style (GTK_WIDGET (html));
 
48
        
 
49
        if (!plain_painter) {
 
50
                gdk_painter = HTML_GDK_PAINTER (html->engine->painter);
 
51
                plain_painter = HTML_GDK_PAINTER (html_plain_painter_new (GTK_WIDGET (html), TRUE));
 
52
 
 
53
                g_object_ref (G_OBJECT (gdk_painter));
 
54
        }       
 
55
        
 
56
 
 
57
        if (format_html) {
 
58
                p = gdk_painter;
 
59
                old_p = plain_painter;
 
60
        } else {
 
61
                p = plain_painter;
 
62
                old_p = gdk_painter;
 
63
        }               
 
64
 
 
65
        if (html->engine->painter != (HTMLPainter *) p) {
 
66
                html_gdk_painter_unrealize (old_p);
 
67
                if (html->engine->window)
 
68
                        html_gdk_painter_realize (p, html->engine->window);
 
69
 
 
70
                html_font_manager_set_default (&HTML_PAINTER (p)->font_manager,
 
71
                                               HTML_PAINTER (old_p)->font_manager.variable.face,
 
72
                                               HTML_PAINTER (old_p)->font_manager.fixed.face,
 
73
                                               HTML_PAINTER (old_p)->font_manager.var_size,
 
74
                                               HTML_PAINTER (old_p)->font_manager.var_points,
 
75
                                               HTML_PAINTER (old_p)->font_manager.fix_size,
 
76
                                               HTML_PAINTER (old_p)->font_manager.fix_points);
 
77
 
 
78
                html_engine_set_painter (html->engine, HTML_PAINTER (p));
 
79
                html_engine_schedule_redraw (html->engine);
 
80
        }
 
81
                
 
82
}
 
83
 
 
84
static int test_level_1 (GtkHTML *html)
 
85
{
 
86
        int i, total_len = 0;
 
87
 
 
88
        set_format (html, FALSE);
 
89
 
 
90
        srand (2);
 
91
 
 
92
        for (i = 0; i < 200; i ++) {
 
93
                int j, len = 1 + (int) (10.0*rand()/(RAND_MAX+1.0));
 
94
                char word [12];
 
95
 
 
96
                for (j = 0; j < len; j ++)
 
97
                        word [j] = 'a' + (int) (26.0*rand()/(RAND_MAX+1.0));
 
98
                word [len] = ' ';
 
99
                word [len + 1] = 0;
 
100
                total_len += len + 1;
 
101
 
 
102
                html_engine_insert_text (html->engine, word, -1);
 
103
        }
 
104
 
 
105
        while (html_cursor_backward (html->engine->cursor, html->engine))
 
106
                ;
 
107
 
 
108
        if (html->engine->cursor->position != 0 || html->engine->cursor->offset != 0)
 
109
                return FALSE;
 
110
 
 
111
        for (i = 0; i < 1000; i ++) {
 
112
                int j, new_pos, pos, len;
 
113
 
 
114
                len = 1 + (int) (120.0*rand()/(RAND_MAX+1.0));
 
115
                pos = MAX (0, (int) (((double) (total_len - len))*rand()/(RAND_MAX+1.0)));
 
116
 
 
117
                printf ("step: %d pos: %d len: %d\n", i, pos, len);
 
118
 
 
119
                switch ((int) (10.0*rand()/(RAND_MAX+1.0))) {
 
120
                case 0:
 
121
                        /* cut'n'paste */
 
122
                        printf ("cut'n'paste\n");
 
123
                        html_cursor_jump_to_position (html->engine->cursor, html->engine, pos);
 
124
                        html_engine_set_mark (html->engine);
 
125
                        html_cursor_jump_to_position (html->engine->cursor, html->engine, pos + len);
 
126
                        html_engine_cut (html->engine);
 
127
 
 
128
                        new_pos = (int) (((double) (total_len - len))*rand()/(RAND_MAX+1.0));
 
129
 
 
130
                        html_cursor_jump_to_position (html->engine->cursor, html->engine, new_pos);
 
131
                        html_engine_paste (html->engine);
 
132
                        break;
 
133
                case 1:
 
134
                        /* insert text */
 
135
                        printf ("insert text\n");
 
136
                        html_cursor_jump_to_position (html->engine->cursor, html->engine, pos);
 
137
                        for (j = 0; j < len; j ++) {
 
138
                                int et = (int) (10.0*rand()/(RAND_MAX+1.0));
 
139
                                if (et == 0)
 
140
                                        gtk_html_command (html, "insert-tab");
 
141
                                else {
 
142
                                        char ch [2];
 
143
 
 
144
                                        if (et == 1)
 
145
                                                ch [0] = ' ';
 
146
                                        else
 
147
                                                ch [0] = 'a' + (int) (26.0*rand()/(RAND_MAX+1.0));
 
148
                                        ch [1] = 0;
 
149
                                        html_engine_insert_text (html->engine, ch, 1);
 
150
                                }
 
151
                        }
 
152
                        total_len += len;
 
153
                        break;
 
154
                case 2:
 
155
                        /* delete text */
 
156
                        printf ("delete text\n");
 
157
                        html_cursor_jump_to_position (html->engine->cursor, html->engine, pos + len);
 
158
                        for (j = 0; j < len; j++) {
 
159
                                gtk_html_command (html, "delete");
 
160
                        }
 
161
                        total_len -= len;
 
162
                        break;
 
163
                case 3:
 
164
                        /* undo */
 
165
                        printf ("undo\n");
 
166
                        for (j = 0; j < len; j ++) {
 
167
                                html_engine_undo (html->engine);
 
168
                        }
 
169
                        break;
 
170
                case 4:
 
171
                        /* redo */
 
172
                        printf ("redo\n");
 
173
                        for (j = 0; j < len; j ++) {
 
174
                                html_engine_redo (html->engine);
 
175
                        }
 
176
                        break;
 
177
                case 5:
 
178
                        /* cut'n'quoted paste */
 
179
                        printf ("cut'n'quoted paste\n");
 
180
                        html_cursor_jump_to_position (html->engine->cursor, html->engine, pos);
 
181
                        html_engine_set_mark (html->engine);
 
182
                        html_cursor_jump_to_position (html->engine->cursor, html->engine, pos + len);
 
183
                        gtk_html_cut (html);
 
184
 
 
185
                        new_pos = (int) (((double) (total_len - len))*rand()/(RAND_MAX+1.0));
 
186
 
 
187
                        html_cursor_jump_to_position (html->engine->cursor, html->engine, new_pos);
 
188
                        gtk_html_paste (html, TRUE);
 
189
                        break;
 
190
                case 6:
 
191
                        /* left */
 
192
                        printf ("left\n");
 
193
                        for (j = 0; j < 5*len; j ++) {
 
194
                                html_cursor_left (html->engine->cursor, html->engine);
 
195
                        }
 
196
                        break;
 
197
                case 7:
 
198
                        /* right */
 
199
                        printf ("right\n");
 
200
                        for (j = 0; j < 5*len; j ++) {
 
201
                                html_cursor_right (html->engine->cursor, html->engine);
 
202
                        }
 
203
                        break;
 
204
                case 8:
 
205
                        /* bol */
 
206
                        printf ("beginning of line\n");
 
207
                        html_cursor_beginning_of_line (html->engine->cursor, html->engine);
 
208
                        break;
 
209
                case 9:
 
210
                        /* eol */
 
211
                        printf ("end of line\n");
 
212
                        html_cursor_end_of_line (html->engine->cursor, html->engine);
 
213
                        break;
 
214
                }
 
215
 
 
216
                html_engine_thaw_idle_flush (html->engine);
 
217
 
 
218
                while (html_cursor_backward (html->engine->cursor, html->engine))
 
219
                        ;
 
220
                if (html->engine->cursor->position != 0 || html->engine->cursor->offset != 0)
 
221
                        return FALSE;
 
222
        }
 
223
 
 
224
        while (html_cursor_backward (html->engine->cursor, html->engine))
 
225
                ;
 
226
 
 
227
        if (html->engine->cursor->position != 0 || html->engine->cursor->offset != 0)
 
228
                return FALSE;
 
229
 
 
230
        return TRUE;
 
231
}
 
232
 
 
233
int main (int argc, char *argv[])
 
234
{
 
235
        GtkWidget *win, *html_widget, *sw;
 
236
        GtkHTML *html;
 
237
        int i = 0, n_all, n_successful;
 
238
 
 
239
        if (argc > 1)
 
240
                return 0;
 
241
 
 
242
        gtk_init (&argc, &argv);
 
243
 
 
244
        html_widget = gtk_html_new ();
 
245
        html = GTK_HTML (html_widget);
 
246
        gtk_html_load_empty (html);
 
247
        gtk_html_set_editable (html, TRUE);
 
248
        win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
 
249
        sw = gtk_scrolled_window_new (NULL, NULL);
 
250
        gtk_widget_set_size_request (win, 600, 400);
 
251
        gtk_container_add (GTK_CONTAINER (sw), html_widget);
 
252
        gtk_container_add (GTK_CONTAINER (win), sw);
 
253
 
 
254
        gtk_widget_show_all (win);
 
255
        gtk_widget_show_now (win);
 
256
 
 
257
        n_all = n_successful = 0;
 
258
 
 
259
        fprintf (stderr, "\nGtkHTML test suite\n");
 
260
        fprintf (stderr, "--------------------------------------------------------------------------------\n");
 
261
        for (i = 0; tests [i].name; i ++) {
 
262
                int j, result;
 
263
 
 
264
                if (tests [i].test_function) {
 
265
                        fprintf (stderr, "  %s ", tests [i].name);
 
266
                        for (j = strlen (tests [i].name); j < 69; j ++)
 
267
                                fputc ('.', stderr);
 
268
                        result = (*tests [i].test_function) (html);
 
269
                        fprintf (stderr, " %s\n", result ? "passed" : "failed");
 
270
 
 
271
                        n_all ++;
 
272
                        if (result)
 
273
                                n_successful ++;
 
274
                } else {
 
275
                        fprintf (stderr, "* %s\n", tests [i].name);
 
276
                }
 
277
        }
 
278
 
 
279
        fprintf (stderr, "--------------------------------------------------------------------------------\n");
 
280
        fprintf (stderr, "%d tests failed %d tests passed (of %d tests)\n\n", n_all - n_successful, n_successful, n_all);
 
281
 
 
282
        gtk_main ();
 
283
 
 
284
        return n_all != n_successful;
 
285
}