testtextbuffer.c 32.1 KB
Newer Older
1 2
/* Simplistic test suite */

Owen Taylor's avatar
Owen Taylor committed
3

4
#include <stdio.h>
5
#include <string.h>
6 7

#include <gtk/gtk.h>
Owen Taylor's avatar
Owen Taylor committed
8
#include "../gtk/gtktexttypes.h" /* Private header, for UNKNOWN_CHAR */
9

10 11 12 13 14 15 16 17 18 19 20
static void
gtk_text_iter_spew (const GtkTextIter *iter, const gchar *desc)
{
  g_print (" %20s: line %d / char %d / line char %d / line byte %d\n",
           desc,
           gtk_text_iter_get_line (iter),
           gtk_text_iter_get_offset (iter),
           gtk_text_iter_get_line_offset (iter),
           gtk_text_iter_get_line_index (iter));
}

21
static void fill_buffer (GtkTextBuffer *buffer);
22

23
static void run_tests (GtkTextBuffer *buffer);
24

25 26 27 28 29 30
static void check_get_set_text (GtkTextBuffer *buffer,
                                const char    *str);


static void line_separator_tests (void);

31 32
static void logical_motion_tests (void);

33
int
34
main (int argc, char** argv)
35 36 37
{
  GtkTextBuffer *buffer;
  int n;
38
  gunichar ch;
39
  GtkTextIter start, end;
40
  
41
  gtk_init (&argc, &argv);
42 43

  /* Check UTF8 unknown char thing */
44
  g_assert (g_utf8_strlen (gtk_text_unknown_char_utf8, 3) == 1);
45
  ch = g_utf8_get_char (gtk_text_unknown_char_utf8);
Havoc Pennington's avatar
Havoc Pennington committed
46
  g_assert (ch == GTK_TEXT_UNKNOWN_CHAR);
47

48 49 50
  /* First, we turn on btree debugging. */
  gtk_debug_flags |= GTK_DEBUG_TEXT;

51 52
  /* Check some line separator stuff */
  line_separator_tests ();
53 54 55

  /* Check log attr motion */
  logical_motion_tests ();
56
  
57
  /* Create a buffer */
58
  buffer = gtk_text_buffer_new (NULL);
59 60

  /* Check that buffer starts with one empty line and zero chars */
61

62
  n = gtk_text_buffer_get_line_count (buffer);
63
  if (n != 1)
64
    g_error ("%d lines, expected 1", n);
65

66
  n = gtk_text_buffer_get_char_count (buffer);
67 68
  if (n != 0)
    g_error ("%d chars, expected 0", n);
69

70 71 72 73 74 75 76 77 78
  /* empty first line contains 0 chars */
  gtk_text_buffer_get_start_iter (buffer, &start);
  n = gtk_text_iter_get_chars_in_line (&start);
  if (n != 0)
    g_error ("%d chars in first line, expected 0", n);
  n = gtk_text_iter_get_bytes_in_line (&start);
  if (n != 0)
    g_error ("%d bytes in first line, expected 0", n);
  
79
  /* Run gruesome alien test suite on buffer */
80
  run_tests (buffer);
81

82
  /* Check set/get text */
83 84 85 86 87 88
  check_get_set_text (buffer, "Hello");
  check_get_set_text (buffer, "Hello\n");
  check_get_set_text (buffer, "Hello\r\n");
  check_get_set_text (buffer, "Hello\r");
  check_get_set_text (buffer, "Hello\nBar\nFoo");
  check_get_set_text (buffer, "Hello\nBar\nFoo\n");
89 90

  g_print ("get/set tests passed.\n");
91
  
92 93
  /* Put stuff in the buffer */

94
  fill_buffer (buffer);
95

96
  /* Subject stuff-bloated buffer to further torment */
97
  run_tests (buffer);
98

99
  /* Delete all stuff from the buffer */
100 101
  gtk_text_buffer_get_bounds (buffer, &start, &end);
  gtk_text_buffer_delete (buffer, &start, &end);
102 103 104

  /* Check buffer for emptiness (note that a single
     empty line always remains in the buffer) */
105
  n = gtk_text_buffer_get_line_count (buffer);
106
  if (n != 1)
107
    g_error ("%d lines, expected 1", n);
108

109
  n = gtk_text_buffer_get_char_count (buffer);
110 111
  if (n != 0)
    g_error ("%d chars, expected 0", n);
112

113
  run_tests (buffer);
114

115 116 117 118 119 120 121
  gtk_text_buffer_set_text (buffer, "adcdef", -1);
  gtk_text_buffer_get_iter_at_offset (buffer, &start, 1);
  gtk_text_buffer_get_iter_at_offset (buffer, &end, 3);
  gtk_text_buffer_apply_tag_by_name (buffer, "fg_blue", &start, &end);
  
  run_tests (buffer);
  
Manish Singh's avatar
Manish Singh committed
122
  g_object_unref (buffer);
123
  
124
  g_print ("All tests passed.\n");
125

126 127 128
  return 0;
}

129 130 131 132
static void
check_get_set_text (GtkTextBuffer *buffer,
                    const char    *str)
{
133
  GtkTextIter start, end, iter;
134 135 136 137 138 139 140 141 142 143 144 145 146 147
  char *text;
  int n;
  
  gtk_text_buffer_set_text (buffer, str, -1);
  if (gtk_text_buffer_get_char_count (buffer) != g_utf8_strlen (str, -1))
    g_error ("Wrong number of chars (%d not %d)",
             gtk_text_buffer_get_char_count (buffer),
             (int) g_utf8_strlen (str, -1));
  gtk_text_buffer_get_bounds (buffer, &start, &end);
  text = gtk_text_buffer_get_text (buffer, &start, &end, TRUE);
  if (strcmp (text, str) != 0)
    g_error ("Got '%s' as buffer contents", text);
  g_free (text);

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
  /* line char counts */
  iter = start;
  n = 0;
  do
    {
      n += gtk_text_iter_get_chars_in_line (&iter);
    }
  while (gtk_text_iter_forward_line (&iter));

  if (n != gtk_text_buffer_get_char_count (buffer))
    g_error ("Sum of chars in lines is %d but buffer char count is %d",
             n, gtk_text_buffer_get_char_count (buffer));

  /* line byte counts */
  iter = start;
  n = 0;
  do
    {
      n += gtk_text_iter_get_bytes_in_line (&iter);
    }
  while (gtk_text_iter_forward_line (&iter));

  if (n != strlen (str))
    g_error ("Sum of chars in lines is %d but buffer byte count is %d",
             n, strlen (str));
  
174 175 176 177 178 179 180 181 182 183 184
  gtk_text_buffer_set_text (buffer, "", -1);

  n = gtk_text_buffer_get_line_count (buffer);
  if (n != 1)
    g_error ("%d lines, expected 1", n);

  n = gtk_text_buffer_get_char_count (buffer);
  if (n != 0)
    g_error ("%d chars, expected 0", n);
}

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
static gint
count_toggles_at_iter (GtkTextIter *iter,
                       GtkTextTag  *of_tag)
{
  GSList *tags;
  GSList *tmp;
  gint count = 0;
  
  /* get toggle-ons and toggle-offs */
  tags = gtk_text_iter_get_toggled_tags (iter, TRUE);
  tags = g_slist_concat (tags,
                         gtk_text_iter_get_toggled_tags (iter, FALSE));
  
  tmp = tags;
  while (tmp != NULL)
    {
      if (of_tag == NULL)
        ++count;
      else if (of_tag == tmp->data)
        ++count;
      
      tmp = g_slist_next (tmp);
    }
  
  g_slist_free (tags);

  return count;
}
213

214
static gint
215 216 217 218
count_toggles_in_range_by_char (GtkTextBuffer     *buffer,
                                GtkTextTag        *of_tag,
                                const GtkTextIter *start,
                                const GtkTextIter *end)
219 220 221 222
{
  GtkTextIter iter;
  gint count = 0;
  
223
  iter = *start;
224 225 226
  do
    {
      count += count_toggles_at_iter (&iter, of_tag);
227 228 229 230 231 232
      if (!gtk_text_iter_forward_char (&iter))
        {
          /* end iterator */
          count += count_toggles_at_iter (&iter, of_tag);
          break;
        }
233
    }
234
  while (gtk_text_iter_compare (&iter, end) <= 0);
235 236 237 238
  
  return count;
}

239 240 241 242 243 244 245 246 247 248 249
static gint
count_toggles_in_buffer (GtkTextBuffer *buffer,
                         GtkTextTag    *of_tag)
{
  GtkTextIter start, end;

  gtk_text_buffer_get_bounds (buffer, &start, &end);

  return count_toggles_in_range_by_char (buffer, of_tag, &start, &end);
}

250
static void
251 252 253 254
check_specific_tag_in_range (GtkTextBuffer     *buffer,
                             const gchar       *tag_name,
                             const GtkTextIter *start,
                             const GtkTextIter *end)
255 256 257 258 259 260 261
{
  GtkTextIter iter;
  GtkTextTag *tag;
  gboolean state;
  gint count;
  gint buffer_count;
  gint last_offset;
262 263 264 265 266 267

  if (gtk_text_iter_compare (start, end) > 0)
    {
      g_print ("  (inverted range for checking tags, skipping)\n");
      return;
    }
268 269 270 271
  
  tag = gtk_text_tag_table_lookup (gtk_text_buffer_get_tag_table (buffer),
                                   tag_name);

272
  buffer_count = count_toggles_in_range_by_char (buffer, tag, start, end);
273 274 275 276 277
  
  state = FALSE;
  count = 0;

  last_offset = -1;
278
  iter = *start;
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
  if (gtk_text_iter_toggles_tag (&iter, tag) ||
      gtk_text_iter_forward_to_tag_toggle (&iter, tag))
    {
      do
        {
          gint this_offset;
          
          ++count;

          this_offset = gtk_text_iter_get_offset (&iter);

          if (this_offset <= last_offset)
            g_error ("forward_to_tag_toggle moved in wrong direction");

          last_offset = this_offset;
          
          if (gtk_text_iter_begins_tag (&iter, tag))
            {
              if (state)
                g_error ("Tag %p is already on, and was toggled on?", tag);
              state = TRUE;
            }          
          else if (gtk_text_iter_ends_tag (&iter, tag))
            {
              if (!state)
                g_error ("Tag %p toggled off, but wasn't toggled on?", tag);
              state = FALSE;
            }
          else
            g_error ("forward_to_tag_toggle went to a location without a toggle");
        }
310 311
      while (gtk_text_iter_forward_to_tag_toggle (&iter, tag) &&
             gtk_text_iter_compare (&iter, end) <= 0);
312 313 314
    }

  if (count != buffer_count)
315
    g_error ("Counted %d tags iterating by char, %d iterating forward by tag toggle\n",
316 317 318 319 320
             buffer_count, count);
  
  state = FALSE;
  count = 0;
  
321
  iter = *end;
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
  last_offset = gtk_text_iter_get_offset (&iter);
  if (gtk_text_iter_toggles_tag (&iter, tag) ||
      gtk_text_iter_backward_to_tag_toggle (&iter, tag))
    {
      do
        {
          gint this_offset;
          
          ++count;

          this_offset = gtk_text_iter_get_offset (&iter);
          
          if (this_offset >= last_offset)
            g_error ("backward_to_tag_toggle moved in wrong direction");
          
          last_offset = this_offset;

          if (gtk_text_iter_begins_tag (&iter, tag))
            {
              if (!state)
                g_error ("Tag %p wasn't on when we got to the on toggle going backward?", tag);
              state = FALSE;
            }
          else if (gtk_text_iter_ends_tag (&iter, tag))
            {
              if (state)
                g_error ("Tag %p off toggle, but we were already inside a tag?", tag);
              state = TRUE;
            }
          else
            g_error ("backward_to_tag_toggle went to a location without a toggle");
        }
354 355
      while (gtk_text_iter_backward_to_tag_toggle (&iter, tag) &&
             gtk_text_iter_compare (&iter, start) >= 0);
356 357 358
    }

  if (count != buffer_count)
359
    g_error ("Counted %d tags iterating by char, %d iterating backward by tag toggle\n",
360
             buffer_count, count);
361 362 363 364 365 366 367
}

static void
check_specific_tag (GtkTextBuffer *buffer,
                    const gchar   *tag_name)
{
  GtkTextIter start, end;
368

369 370 371 372 373 374
  gtk_text_buffer_get_bounds (buffer, &start, &end);
  check_specific_tag_in_range (buffer, tag_name, &start, &end);
  gtk_text_iter_forward_chars (&start, 2);
  gtk_text_iter_backward_chars (&end, 2);
  if (gtk_text_iter_compare (&start, &end) < 0)
    check_specific_tag_in_range (buffer, tag_name, &start, &end);
375 376
}

377
static void
378
run_tests (GtkTextBuffer *buffer)
379 380 381 382 383 384 385 386
{
  GtkTextIter iter;
  GtkTextIter start;
  GtkTextIter end;
  GtkTextIter mark;
  gint i, j;
  gint num_chars;
  GtkTextMark *bar_mark;
387 388 389 390
  GtkTextTag *tag;
  GHashTable *tag_states;
  gint count;
  gint buffer_count;
391
  
392
  gtk_text_buffer_get_bounds (buffer, &start, &end);
393

394
  /* Check that walking the tree via chars and via iterators produces
395 396
   * the same number of indexable locations.
   */
397
  num_chars = gtk_text_buffer_get_char_count (buffer);
398
  iter = start;
399
  bar_mark = gtk_text_buffer_create_mark (buffer, "bar", &iter, FALSE);
400 401 402 403 404
  i = 0;
  while (i < num_chars)
    {
      GtkTextIter current;
      GtkTextMark *foo_mark;
405

406
      gtk_text_buffer_get_iter_at_offset (buffer, &current, i);
407

408
      if (!gtk_text_iter_equal (&iter, &current))
409
        {
410
          g_error ("get_char_index didn't return current iter");
411 412
        }

413
      j = gtk_text_iter_get_offset (&iter);
414 415 416

      if (i != j)
        {
417
          g_error ("iter converted to %d not %d", j, i);
418 419 420
        }

      /* get/set mark */
421
      gtk_text_buffer_get_iter_at_mark (buffer, &mark, bar_mark);
422

423
      if (!gtk_text_iter_equal (&iter, &mark))
424
        {
425 426 427
          gtk_text_iter_spew (&iter, "iter");
          gtk_text_iter_spew (&mark, "mark");
          g_error ("Mark not moved to the right place.");
428
        }
429

430 431 432
      foo_mark = gtk_text_buffer_create_mark (buffer, "foo", &iter, FALSE);
      gtk_text_buffer_get_iter_at_mark (buffer, &mark, foo_mark);
      gtk_text_buffer_delete_mark (buffer, foo_mark);
433

434
      if (!gtk_text_iter_equal (&iter, &mark))
435
        {
436 437 438
          gtk_text_iter_spew (&iter, "iter");
          gtk_text_iter_spew (&mark, "mark");
          g_error ("Mark not created in the right place.");
439
        }
440

441
      if (gtk_text_iter_is_end (&iter))
442 443
        g_error ("iterators ran out before chars (offset %d of %d)",
                 i, num_chars);
444

445
      gtk_text_iter_forward_char (&iter);
446

447
      gtk_text_buffer_move_mark (buffer, bar_mark, &iter);
448

449 450 451
      ++i;
    }

452 453
  if (!gtk_text_iter_equal (&iter, &end))
    g_error ("Iterating over all chars didn't end with the end iter");
454

455
  /* Do the tree-walk backward
456
   */
457 458
  num_chars = gtk_text_buffer_get_char_count (buffer);
  gtk_text_buffer_get_iter_at_offset (buffer, &iter, -1);
459

460
  gtk_text_buffer_move_mark (buffer, bar_mark, &iter);
461

462 463
  i = num_chars;

464 465
  if (!gtk_text_iter_equal (&iter, &end))
    g_error ("iter at char -1 is not equal to the end iterator");
466

467 468 469 470
  while (i >= 0)
    {
      GtkTextIter current;
      GtkTextMark *foo_mark;
471

472
      gtk_text_buffer_get_iter_at_offset (buffer, &current, i);
473

474
      if (!gtk_text_iter_equal (&iter, &current))
475
        {
476
          g_error ("get_char_index didn't return current iter while going backward");
477
        }
478
      j = gtk_text_iter_get_offset (&iter);
479 480 481

      if (i != j)
        {
482
          g_error ("going backward, iter converted to %d not %d", j, i);
483 484 485
        }

      /* get/set mark */
486
      gtk_text_buffer_get_iter_at_mark (buffer, &mark, bar_mark);
487

488
      if (!gtk_text_iter_equal (&iter, &mark))
489
        {
490 491 492
          gtk_text_iter_spew (&iter, "iter");
          gtk_text_iter_spew (&mark, "mark");
          g_error ("Mark not moved to the right place.");
493
        }
494

495 496 497
      foo_mark = gtk_text_buffer_create_mark (buffer, "foo", &iter, FALSE);
      gtk_text_buffer_get_iter_at_mark (buffer, &mark, foo_mark);
      gtk_text_buffer_delete_mark (buffer, foo_mark);
498

499
      if (!gtk_text_iter_equal (&iter, &mark))
500
        {
501 502 503
          gtk_text_iter_spew (&iter, "iter");
          gtk_text_iter_spew (&mark, "mark");
          g_error ("Mark not created in the right place.");
504
        }
505

506 507
      if (i > 0)
        {
508
          if (!gtk_text_iter_backward_char (&iter))
509
            g_error ("iterators ran out before char indexes");
510

511
          gtk_text_buffer_move_mark (buffer, bar_mark, &iter);
512 513 514
        }
      else
        {
515
          if (gtk_text_iter_backward_char (&iter))
516
            g_error ("went backward from 0?");
517
        }
518

519 520
      --i;
    }
521

522 523
  if (!gtk_text_iter_equal (&iter, &start))
    g_error ("Iterating backward over all chars didn't end with the start iter");
524 525 526 527 528 529

  /*
   * Check that get_line_count returns the same number of lines
   * as walking the tree by line
   */
  i = 1; /* include current (first) line */
530 531
  gtk_text_buffer_get_iter_at_line (buffer, &iter, 0);
  while (gtk_text_iter_forward_line (&iter))
532
    ++i;
533

534 535
  if (i != gtk_text_buffer_get_line_count (buffer))
    g_error ("Counted %d lines, buffer has %d", i,
536
             gtk_text_buffer_get_line_count (buffer));
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622

  /*
   * Check that moving over tag toggles thinks about working.
   */

  buffer_count = count_toggles_in_buffer (buffer, NULL);
  
  tag_states = g_hash_table_new (NULL, NULL);
  count = 0;
  
  gtk_text_buffer_get_iter_at_offset (buffer, &iter, 0);
  if (gtk_text_iter_toggles_tag (&iter, NULL) ||
      gtk_text_iter_forward_to_tag_toggle (&iter, NULL))
    {
      do
        {
          GSList *tags;
          GSList *tmp;
          gboolean found_some = FALSE;
          
          /* get toggled-on tags */
          tags = gtk_text_iter_get_toggled_tags (&iter, TRUE);

          if (tags)
            found_some = TRUE;
          
          tmp = tags;
          while (tmp != NULL)
            {
              ++count;
              
              tag = tmp->data;
              
              if (g_hash_table_lookup (tag_states, tag))
                g_error ("Tag %p is already on, and was toggled on?", tag);

              g_hash_table_insert (tag_states, tag, GINT_TO_POINTER (TRUE));
          
              tmp = g_slist_next (tmp);
            }

          g_slist_free (tags);
      
          /* get toggled-off tags */
          tags = gtk_text_iter_get_toggled_tags (&iter, FALSE);

          if (tags)
            found_some = TRUE;
          
          tmp = tags;
          while (tmp != NULL)
            {
              ++count;
              
              tag = tmp->data;

              if (!g_hash_table_lookup (tag_states, tag))
                g_error ("Tag %p is already off, and was toggled off?", tag);

              g_hash_table_remove (tag_states, tag);
          
              tmp = g_slist_next (tmp);
            }

          g_slist_free (tags);

          if (!found_some)
            g_error ("No tags found going forward to tag toggle.");

        }
      while (gtk_text_iter_forward_to_tag_toggle (&iter, NULL));
    }
  
  g_hash_table_destroy (tag_states);

  if (count != buffer_count)
    g_error ("Counted %d tags iterating by char, %d iterating by tag toggle\n",
             buffer_count, count);
  
  /* Go backward; here TRUE in the hash means we saw
   * an off toggle last.
   */
  
  tag_states = g_hash_table_new (NULL, NULL);
  count = 0;
  
623
  gtk_text_buffer_get_end_iter (buffer, &iter);
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
  if (gtk_text_iter_toggles_tag (&iter, NULL) ||
      gtk_text_iter_backward_to_tag_toggle (&iter, NULL))
    {
      do
        {
          GSList *tags;
          GSList *tmp;
          gboolean found_some = FALSE;
          
          /* get toggled-off tags */
          tags = gtk_text_iter_get_toggled_tags (&iter, FALSE);

          if (tags)
            found_some = TRUE;
          
          tmp = tags;
          while (tmp != NULL)
            {
              ++count;
              
              tag = tmp->data;

              if (g_hash_table_lookup (tag_states, tag))
                g_error ("Tag %p has two off-toggles in a row?", tag);
          
              g_hash_table_insert (tag_states, tag, GINT_TO_POINTER (TRUE));
          
              tmp = g_slist_next (tmp);
            }

          g_slist_free (tags);
      
          /* get toggled-on tags */
          tags = gtk_text_iter_get_toggled_tags (&iter, TRUE);

          if (tags)
            found_some = TRUE;
          
          tmp = tags;
          while (tmp != NULL)
            {
              ++count;
              
              tag = tmp->data;

              if (!g_hash_table_lookup (tag_states, tag))
                g_error ("Tag %p was toggled on, but saw no off-toggle?", tag);

              g_hash_table_remove (tag_states, tag);
          
              tmp = g_slist_next (tmp);
            }

          g_slist_free (tags);

          if (!found_some)
            g_error ("No tags found going backward to tag toggle.");
        }
      while (gtk_text_iter_backward_to_tag_toggle (&iter, NULL));
    }
  
  g_hash_table_destroy (tag_states);

  if (count != buffer_count)
    g_error ("Counted %d tags iterating by char, %d iterating by tag toggle\n",
             buffer_count, count);

  check_specific_tag (buffer, "fg_red");
  check_specific_tag (buffer, "bg_green");
  check_specific_tag (buffer, "front_tag");
  check_specific_tag (buffer, "center_tag");
  check_specific_tag (buffer, "end_tag");
696 697 698
}


699
static const char  *book_closed_xpm[] = {
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
"16 16 6 1",
"       c None s None",
".      c black",
"X      c red",
"o      c yellow",
"O      c #808080",
"#      c white",
"                ",
"       ..       ",
"     ..XX.      ",
"   ..XXXXX.     ",
" ..XXXXXXXX.    ",
".ooXXXXXXXXX.   ",
"..ooXXXXXXXXX.  ",
".X.ooXXXXXXXXX. ",
".XX.ooXXXXXX..  ",
" .XX.ooXXX..#O  ",
"  .XX.oo..##OO. ",
"   .XX..##OO..  ",
"    .X.#OO..    ",
"     ..O..      ",
"      ..        ",
"                "};

static void
725
fill_buffer (GtkTextBuffer *buffer)
726 727 728 729 730
{
  GtkTextTag *tag;
  GdkColor color, color2;
  GtkTextIter iter;
  GtkTextIter iter2;
Havoc Pennington's avatar
Havoc Pennington committed
731
  GdkPixbuf *pixbuf;
732
  int i;
733

734 735 736 737 738
  color.red = color.green = 0;
  color.blue = 0xffff;
  color2.red = 0xfff;
  color2.blue = 0x0;
  color2.green = 0;
739 740 741 742 743 744
  
  gtk_text_buffer_create_tag (buffer, "fg_blue",
                              "foreground_gdk", &color,
                              "background_gdk", &color2,
                              "font", "-*-courier-bold-r-*-*-30-*-*-*-*-*-*-*",
                              NULL);
745 746 747

  color.blue = color.green = 0;
  color.red = 0xffff;
748 749 750 751 752
  
  gtk_text_buffer_create_tag (buffer, "fg_red",
                              "rise", -4,
                              "foreground_gdk", &color,
                              NULL);
753 754 755

  color.blue = color.red = 0;
  color.green = 0xffff;
756 757 758 759 760
  
  gtk_text_buffer_create_tag (buffer, "bg_green",
                              "background_gdk", &color,
                              "font", "-*-courier-bold-r-*-*-10-*-*-*-*-*-*-*",
                              NULL);
761

Havoc Pennington's avatar
Havoc Pennington committed
762
  pixbuf = gdk_pixbuf_new_from_xpm_data (book_closed_xpm);
763

Havoc Pennington's avatar
Havoc Pennington committed
764
  g_assert (pixbuf != NULL);
765

766 767 768 769 770
  i = 0;
  while (i < 10)
    {
      gchar *str;

771
      gtk_text_buffer_get_iter_at_offset (buffer, &iter, 0);
772

Havoc Pennington's avatar
Havoc Pennington committed
773
      gtk_text_buffer_insert_pixbuf (buffer, &iter, pixbuf);
774

775
      gtk_text_buffer_get_iter_at_offset (buffer, &iter, 1);
776

Havoc Pennington's avatar
Havoc Pennington committed
777
      gtk_text_buffer_insert_pixbuf (buffer, &iter, pixbuf);
778

779
      str = g_strdup_printf ("%d Hello World!\nwoo woo woo woo woo woo woo woo\n",
780
                            i);
781

782
      gtk_text_buffer_insert (buffer, &iter, str, -1);
783

784
      g_free (str);
785

786
      gtk_text_buffer_insert (buffer, &iter,
787 788 789
                              "(Hello World!)\nfoo foo Hello this is some text we are using to text word wrap. It has punctuation! gee; blah - hmm, great.\nnew line\n\n"
                              /* This is UTF8 stuff, Emacs doesn't
                                 really know how to display it */
790
                              "Spanish (Espa\303\261ol) \302\241Hola! / French (Fran\303\247ais) Bonjour, Salut / German (Deutsch S\303\274d) Gr\303\274\303\237 Gott (testing Latin-1 chars encoded in UTF8)\nThai (we can't display this, just making sure we don't crash)  (\340\270\240\340\270\262\340\270\251\340\270\262\340\271\204\340\270\227\340\270\242)  \340\270\252\340\270\247\340\270\261\340\270\252\340\270\224\340\270\265\340\270\204\340\270\243\340\270\261\340\270\232, \340\270\252\340\270\247\340\270\261\340\270\252\340\270\224\340\270\265\340\270\204\340\271\210\340\270\260\n",
791 792
                              -1);

Havoc Pennington's avatar
Havoc Pennington committed
793 794
      gtk_text_buffer_insert_pixbuf (buffer, &iter, pixbuf);
      gtk_text_buffer_insert_pixbuf (buffer, &iter, pixbuf);
795

796
      gtk_text_buffer_get_iter_at_offset (buffer, &iter, 4);
797

Havoc Pennington's avatar
Havoc Pennington committed
798
      gtk_text_buffer_insert_pixbuf (buffer, &iter, pixbuf);
799

800
      gtk_text_buffer_get_iter_at_offset (buffer, &iter, 7);
801

Havoc Pennington's avatar
Havoc Pennington committed
802
      gtk_text_buffer_insert_pixbuf (buffer, &iter, pixbuf);
803

804
      gtk_text_buffer_get_iter_at_offset (buffer, &iter, 8);
805

Havoc Pennington's avatar
Havoc Pennington committed
806
      gtk_text_buffer_insert_pixbuf (buffer, &iter, pixbuf);
807

808
      gtk_text_buffer_get_iter_at_line_offset (buffer, &iter, 0, 8);
809
      iter2 = iter;
810
      gtk_text_iter_forward_chars (&iter2, 10);
811

812
      gtk_text_buffer_apply_tag_by_name (buffer, "fg_blue", &iter, &iter2);
813

814 815
      gtk_text_iter_forward_chars (&iter, 7);
      gtk_text_iter_forward_chars (&iter2, 10);
816

817
      gtk_text_buffer_apply_tag_by_name (buffer, "bg_green", &iter, &iter2);
818

819 820
      gtk_text_iter_forward_chars (&iter, 12);
      gtk_text_iter_forward_chars (&iter2, 10);
821

822
      gtk_text_buffer_apply_tag_by_name (buffer, "bg_green", &iter, &iter2);
823

824 825
      gtk_text_iter_forward_chars (&iter, 10);
      gtk_text_iter_forward_chars (&iter2, 15);
826

827
      gtk_text_buffer_apply_tag_by_name (buffer, "fg_red", &iter, &iter2);
828
      gtk_text_buffer_apply_tag_by_name (buffer, "fg_blue", &iter, &iter2);
829

830 831
      gtk_text_iter_forward_chars (&iter, 20);
      gtk_text_iter_forward_chars (&iter2, 20);
832

833
      gtk_text_buffer_apply_tag_by_name (buffer, "fg_red", &iter, &iter2);
834
      gtk_text_buffer_apply_tag_by_name (buffer, "fg_blue", &iter, &iter2);
835

836 837
      gtk_text_iter_backward_chars (&iter, 25);
      gtk_text_iter_forward_chars (&iter2, 5);
838

839
      gtk_text_buffer_apply_tag_by_name (buffer, "fg_red", &iter, &iter2);
840
      gtk_text_buffer_apply_tag_by_name (buffer, "fg_blue", &iter, &iter2);
841

842 843
      gtk_text_iter_forward_chars (&iter, 15);
      gtk_text_iter_backward_chars (&iter2, 10);
844

845
      gtk_text_buffer_remove_tag_by_name (buffer, "fg_red", &iter, &iter2);
846 847
      gtk_text_buffer_remove_tag_by_name (buffer, "fg_blue", &iter, &iter2);

848 849 850
      ++i;
    }

851 852 853
  /* Put in tags that are just at the beginning, and just near the end,
   * and just near the middle.
   */
854
  tag = gtk_text_buffer_create_tag (buffer, "front_tag", NULL);
855 856 857 858 859
  gtk_text_buffer_get_iter_at_offset (buffer, &iter, 3);
  gtk_text_buffer_get_iter_at_offset (buffer, &iter2, 300);

  gtk_text_buffer_apply_tag (buffer, tag, &iter, &iter2);  
  
860
  tag = gtk_text_buffer_create_tag (buffer, "end_tag", NULL);
861
  gtk_text_buffer_get_end_iter (buffer, &iter2);
862 863 864 865 866 867
  gtk_text_iter_backward_chars (&iter2, 12);
  iter = iter2;
  gtk_text_iter_backward_chars (&iter, 157);

  gtk_text_buffer_apply_tag (buffer, tag, &iter, &iter2);
  
868
  tag = gtk_text_buffer_create_tag (buffer, "center_tag", NULL);
869 870 871 872 873 874 875 876
  gtk_text_buffer_get_iter_at_offset (buffer, &iter,
                                      gtk_text_buffer_get_char_count (buffer)/2);
  gtk_text_iter_backward_chars (&iter, 37);
  iter2 = iter;
  gtk_text_iter_forward_chars (&iter2, 57);

  gtk_text_buffer_apply_tag (buffer, tag, &iter, &iter2);  

Manish Singh's avatar
Manish Singh committed
877
  g_object_unref (pixbuf);
878
}
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941


/*
 * Line separator tests (initially to avoid regression on bugzilla #57428)
 */

static void
test_line_separation (const char* str,
                      gboolean    expect_next_line,
                      gboolean    expect_end_iter,
                      int         expected_line_count,
                      int         expected_line_break,
                      int         expected_next_line_start)
{
  GtkTextIter iter;
  GtkTextBuffer* buffer;
  gboolean on_next_line;
  gboolean on_end_iter;
  gint new_pos;

  buffer = gtk_text_buffer_new (NULL);

  gtk_text_buffer_set_text (buffer, str, -1);
  gtk_text_buffer_get_iter_at_offset (buffer, &iter, expected_line_break);

  g_assert (gtk_text_iter_ends_line (&iter) || gtk_text_iter_is_end (&iter));

  g_assert (gtk_text_buffer_get_line_count (buffer) == expected_line_count);
  
  on_next_line = gtk_text_iter_forward_line (&iter);

  g_assert (expect_next_line == on_next_line);

  on_end_iter = gtk_text_iter_is_end (&iter);

  g_assert (on_end_iter == expect_end_iter);
  
  new_pos = gtk_text_iter_get_offset (&iter);
    
  if (on_next_line)
    g_assert (expected_next_line_start == new_pos);

  ++expected_line_break;
  while (expected_line_break < expected_next_line_start)
    {
      gtk_text_buffer_get_iter_at_offset (buffer, &iter, expected_line_break);

      g_assert (!gtk_text_iter_ends_line (&iter));

      on_next_line = gtk_text_iter_forward_line (&iter);
        
      g_assert (expect_next_line == on_next_line);
        
      new_pos = gtk_text_iter_get_offset (&iter);
        
      if (on_next_line)
        g_assert (expected_next_line_start == new_pos);
        
      ++expected_line_break;
    }

  /* FIXME tests for backward line */
  
Manish Singh's avatar
Manish Singh committed
942
  g_object_unref (buffer);
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
}


static void
line_separator_tests (void)
{
  char *str;
  char buf[7] = { '\0', };

  /* Only one character has type G_UNICODE_PARAGRAPH_SEPARATOR in
   * Unicode 3.0; update this if that changes.
   */
#define PARAGRAPH_SEPARATOR 0x2029
  
  test_line_separation ("line", FALSE, TRUE, 1, 4, 4);
  test_line_separation ("line\r\n", FALSE, TRUE, 2, 4, 6);
  test_line_separation ("line\r", FALSE, TRUE, 2, 4, 5);
  test_line_separation ("line\n", FALSE, TRUE, 2, 4, 5);
  test_line_separation ("line\rqw", TRUE, FALSE, 2, 4, 5);
  test_line_separation ("line\nqw", TRUE, FALSE, 2, 4, 5);
  test_line_separation ("line\r\nqw", TRUE, FALSE, 2, 4, 6);
  
  g_unichar_to_utf8 (PARAGRAPH_SEPARATOR, buf);
  
  str = g_strdup_printf ("line%s", buf);
  test_line_separation (str, FALSE, TRUE, 2, 4, 5);
  g_free (str);
  str = g_strdup_printf ("line%sqw", buf);
  test_line_separation (str, TRUE, FALSE, 2, 4, 5);
  g_free (str);
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017

  g_print ("Line separator tests passed\n");
}

static void
logical_motion_tests (void)
{
  char *str;
  char buf1[7] = { '\0', };
  char buf2[7] = { '\0', };
  char buf3[7] = { '\0', };
  int expected[30];
  int expected_steps;
  int i;
  GtkTextBuffer *buffer;
  GtkTextIter iter;
  
  buffer = gtk_text_buffer_new (NULL);
  
#define LEADING_JAMO 0x1111
#define VOWEL_JAMO 0x1167
#define TRAILING_JAMO 0x11B9
  
  g_unichar_to_utf8 (LEADING_JAMO, buf1);
  g_unichar_to_utf8 (VOWEL_JAMO, buf2);
  g_unichar_to_utf8 (TRAILING_JAMO, buf3);

  /* Build the string "abc<leading><vowel><trailing>def\r\nxyz" */
  str = g_strconcat ("abc", buf1, buf2, buf3, "def\r\nxyz", NULL);
  gtk_text_buffer_set_text (buffer, str, -1);
  g_free (str);
  
  /* Check cursor positions */
  memset (expected, 0, sizeof (expected));
  expected[0] = 0;    /* before 'a' */
  expected[1] = 1;    /* before 'b' */
  expected[2] = 2;    /* before 'c' */
  expected[3] = 3;    /* before jamo */
  expected[4] = 6;    /* before 'd' */
  expected[5] = 7;    /* before 'e' */
  expected[6] = 8;    /* before 'f' */
  expected[7] = 9;    /* before '\r' */
  expected[8] = 11;   /* before 'x' */
  expected[9] = 12;   /* before 'y' */
  expected[10] = 13;  /* before 'z' */
Havoc Pennington's avatar
docs  
Havoc Pennington committed
1018
  expected[11] = 14;  /* after 'z' (only matters going backward) */
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
  expected_steps = 11;
  
  gtk_text_buffer_get_start_iter (buffer, &iter);
  i = 0;
  do
    {
      int pos;

      pos = gtk_text_iter_get_offset (&iter);
      
      if (pos != expected[i])
        {
          g_error ("Cursor position %d, expected %d",
                   pos, expected[i]);
        }

      ++i;      
    }
  while (gtk_text_iter_forward_cursor_position (&iter));

  if (!gtk_text_iter_is_end (&iter))
    g_error ("Expected to stop at the end iterator\n");
1041 1042 1043

  if (!gtk_text_iter_is_cursor_position (&iter))
    g_error ("Should be a cursor position before the end iterator");
Havoc Pennington's avatar
docs  
Havoc Pennington committed
1044 1045 1046
  
  if (i != expected_steps)
    g_error ("Expected %d steps, there were actually %d\n", expected_steps, i);
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071

  i = expected_steps;
  do
    {
      int pos;

      pos = gtk_text_iter_get_offset (&iter);
      
      if (pos != expected[i])
        {
          g_error ("Moving backward, cursor position %d, expected %d",
                   pos, expected[i]);
        }

      /* g_print ("%d = %d\n", pos, expected[i]); */
      
      --i;
    }
  while (gtk_text_iter_backward_cursor_position (&iter));

  if (i != -1)
    g_error ("Expected %d steps, there were actually %d\n", expected_steps - i, i);

  if (!gtk_text_iter_is_start (&iter))
    g_error ("Expected to stop at the start iterator\n");
Havoc Pennington's avatar