@Test
  public void test_getNextOffset2() {
    if (SwtTestUtil.isGTK || SwtTestUtil.isCocoa) {
      // TODO Fix GTK and Cocoa failure.
      if (SwtTestUtil.verbose) {
        System.out.println(
            "Excluded test_getNextOffset2(org.eclipse.swt.tests.junit.Test_org_eclipse_swt_graphics_TextLayout)");
      }
      return;
    }
    // Text thai cluster separate so it can be excluded
    // for gtk, testing machine (rhel4) is too old to
    // support thai.

    TextLayout layout = new TextLayout(display);
    layout.setText("A\u0E19\u0E49\u0E33B");
    String[] messages = {
      "no segments",
      "segments",
      "segments (duplicate at 0)",
      "segments (duplicate at 1)",
      "segments (duplicate at 2)",
      "segments (duplicate at 3)",
      "segments (duplicate at 4)",
      "segments (duplicate at 5)"
    };
    //    int[][] segments = {null, {0, 1, 2, 3, 4, 5}, {0, 0, 1, 2, 3, 4, 5}, {0, 1, 1, 2, 3, 4,
    // 5}, {0, 1, 2, 2, 3, 4, 5}, {0, 1, 2, 3, 3, 4, 5},
    //            			{0, 1, 2, 3, 4, 4, 5}, {0, 1, 2, 3, 4, 5, 5}};

    int[][] segments = {null};

    for (int i = 0; i < segments.length; i++) {
      String m = messages[i];
      layout.setSegments(segments[i]);
      assertEquals(m, 1, layout.getNextOffset(0, SWT.MOVEMENT_CLUSTER));
      assertEquals(m, 4, layout.getNextOffset(1, SWT.MOVEMENT_CLUSTER));
      assertEquals(m, 5, layout.getNextOffset(4, SWT.MOVEMENT_CLUSTER));
      assertEquals(m, 4, layout.getPreviousOffset(5, SWT.MOVEMENT_CLUSTER));
      assertEquals(m, 1, layout.getPreviousOffset(4, SWT.MOVEMENT_CLUSTER));
      assertEquals(m, 0, layout.getPreviousOffset(1, SWT.MOVEMENT_CLUSTER));
    }
    layout.dispose();
  }
예제 #2
0
 int validateOffset(TextLayout layout, int offset) {
   int nextOffset = layout.getNextOffset(offset, SWT.MOVEMENT_CLUSTER);
   if (nextOffset != offset) return layout.getPreviousOffset(nextOffset, SWT.MOVEMENT_CLUSTER);
   return offset;
 }
  @Test
  public void test_getNextOffset() {
    if (SwtTestUtil.isCocoa) {
      // TODO Fix Cocoa failure.
      if (SwtTestUtil.verbose) {
        System.out.println(
            "Excluded test_getNextOffset(org.eclipse.swt.tests.junit.Test_org_eclipse_swt_graphics_TextLayout).");
      }
      return;
    }
    TextLayout layout = new TextLayout(display);
    String text;
    int offset;

    text = "word word word";
    layout.setText(text);
    offset = 0;
    offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_END);
    assertEquals(4, offset);
    offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_END);
    assertEquals(9, offset);
    offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_END);
    assertEquals(14, offset);
    offset = layout.getPreviousOffset(offset, SWT.MOVEMENT_WORD_END);
    assertEquals(9, offset);
    offset = layout.getPreviousOffset(offset, SWT.MOVEMENT_WORD_END);
    assertEquals(4, offset);
    offset = layout.getPreviousOffset(offset, SWT.MOVEMENT_WORD_END);
    assertEquals(0, offset);
    assertEquals(4, layout.getNextOffset(2, SWT.MOVEMENT_WORD_END));
    assertEquals(4, layout.getNextOffset(3, SWT.MOVEMENT_WORD_END));
    assertEquals(9, layout.getNextOffset(8, SWT.MOVEMENT_WORD_END));
    assertEquals(14, layout.getNextOffset(10, SWT.MOVEMENT_WORD_END));
    assertEquals(14, layout.getNextOffset(13, SWT.MOVEMENT_WORD_END));
    assertEquals(14, layout.getNextOffset(14, SWT.MOVEMENT_WORD_END));
    assertEquals(0, layout.getPreviousOffset(0, SWT.MOVEMENT_WORD_END));
    assertEquals(0, layout.getPreviousOffset(4, SWT.MOVEMENT_WORD_END));
    assertEquals(4, layout.getPreviousOffset(5, SWT.MOVEMENT_WORD_END));
    assertEquals(9, layout.getPreviousOffset(10, SWT.MOVEMENT_WORD_END));
    assertEquals(9, layout.getPreviousOffset(11, SWT.MOVEMENT_WORD_END));

    offset = 0;
    offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START);
    assertEquals(5, offset);
    offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START);
    assertEquals(10, offset);
    offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START);
    assertEquals(14, offset);
    offset = layout.getPreviousOffset(offset, SWT.MOVEMENT_WORD_START);
    assertEquals(10, offset);
    offset = layout.getPreviousOffset(offset, SWT.MOVEMENT_WORD_START);
    assertEquals(5, offset);
    offset = layout.getPreviousOffset(offset, SWT.MOVEMENT_WORD_START);
    assertEquals(0, offset);
    assertEquals(5, layout.getNextOffset(2, SWT.MOVEMENT_WORD_START));
    assertEquals(5, layout.getNextOffset(4, SWT.MOVEMENT_WORD_START));
    assertEquals(10, layout.getNextOffset(9, SWT.MOVEMENT_WORD_START));
    assertEquals(14, layout.getNextOffset(10, SWT.MOVEMENT_WORD_START));
    assertEquals(14, layout.getNextOffset(13, SWT.MOVEMENT_WORD_START));
    assertEquals(14, layout.getNextOffset(14, SWT.MOVEMENT_WORD_START));
    assertEquals(0, layout.getPreviousOffset(0, SWT.MOVEMENT_WORD_START));
    assertEquals(0, layout.getPreviousOffset(3, SWT.MOVEMENT_WORD_START));
    assertEquals(0, layout.getPreviousOffset(4, SWT.MOVEMENT_WORD_START));
    assertEquals(0, layout.getPreviousOffset(5, SWT.MOVEMENT_WORD_START));
    assertEquals(5, layout.getPreviousOffset(6, SWT.MOVEMENT_WORD_START));

    text = "AB \u05E9\u05E0 CD\nHello";
    layout.setText(text);
    offset = 0;
    offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START);
    assertEquals(3, offset);
    offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START);
    assertEquals(6, offset);
    offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START);
    assertEquals(8, offset);
    offset = layout.getPreviousOffset(offset, SWT.MOVEMENT_WORD_START);
    assertEquals(6, offset);
    offset = layout.getPreviousOffset(offset, SWT.MOVEMENT_WORD_START);
    assertEquals(3, offset);
    offset = layout.getPreviousOffset(offset, SWT.MOVEMENT_WORD_START);
    assertEquals(0, offset);
    offset = 7;
    offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START);
    assertEquals(8, offset);
    offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START);
    assertEquals(9, offset);
    offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START);
    assertEquals(text.length(), offset);
    offset = layout.getPreviousOffset(offset, SWT.MOVEMENT_WORD_START);
    assertEquals(9, offset);
    offset = layout.getPreviousOffset(offset, SWT.MOVEMENT_WORD_START);
    assertEquals(8, offset);
    offset = layout.getNextOffset(offset, SWT.MOVEMENT_CLUSTER);
    assertEquals(9, offset);
    offset = layout.getNextOffset(offset, SWT.MOVEMENT_CLUSTER);
    assertEquals(10, offset);
    offset = layout.getPreviousOffset(offset, SWT.MOVEMENT_CLUSTER);
    assertEquals(9, offset);
    offset = layout.getPreviousOffset(offset, SWT.MOVEMENT_CLUSTER);
    assertEquals(8, offset);
    offset = layout.getPreviousOffset(offset, SWT.MOVEMENT_CLUSTER);
    assertEquals(7, offset);
    for (int i = 0; i < text.length(); i++) {
      assertEquals(i + 1, layout.getNextOffset(i, SWT.MOVEMENT_CLUSTER));
    }
    for (int i = text.length(); i > 0; i--) {
      assertEquals(i - 1, layout.getPreviousOffset(i, SWT.MOVEMENT_CLUSTER));
    }
    layout.dispose();
  }
  @Test
  public void test_getSegments() {
    TextLayout layout = new TextLayout(display);
    layout.setText("AB");
    String[] messages = {
      "no segments",
      "segments",
      "segments (duplicate at 0)",
      "segments (duplicate at 1)",
      "segments (duplicate at 2)"
    };
    int[][] segments = {null, {0, 1, 2}, {0, 0, 1, 2}, {0, 1, 1, 2}, {0, 1, 2, 2}};
    for (int i = 0; i < segments.length; i++) {
      String m = messages[i];
      layout.setSegments(segments[i]);
      assertEquals(m, 1, layout.getNextOffset(0, SWT.MOVEMENT_CLUSTER));
      assertEquals(m, 2, layout.getNextOffset(1, SWT.MOVEMENT_CLUSTER));
      assertEquals(m, 2, layout.getNextOffset(2, SWT.MOVEMENT_CLUSTER));
      assertEquals(m, 1, layout.getPreviousOffset(2, SWT.MOVEMENT_CLUSTER));
      assertEquals(m, 0, layout.getPreviousOffset(1, SWT.MOVEMENT_CLUSTER));
      assertEquals(m, 0, layout.getPreviousOffset(0, SWT.MOVEMENT_CLUSTER));
    }

    // Bug 295513
    layout.setText("Line");
    layout.setAscent(20);
    layout.setDescent(6);
    layout.setSegments(new int[] {0, layout.getText().length()});
    layout.getBounds();
    layout.dispose();
    layout = new TextLayout(display);

    // Bug 241482 comment 74
    layout.setText("word word word");
    layout.setSegments(new int[] {0, 5, 10});
    int offset = 0;
    offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START);
    assertEquals(5, offset);
    offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START);
    assertEquals(10, offset);
    offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START);
    assertEquals(14, offset);

    layout.setWidth(layout.getBounds().width / 2);
    layout.setAscent(20);
    layout.setDescent(6);
    offset = 0;
    offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START);
    assertEquals(5, offset);
    offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START);
    assertEquals(10, offset);
    offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START);
    assertEquals(14, offset);
    layout.dispose();
    layout = new TextLayout(display);

    // Bug 241482 comment 64
    layout.setText("\nAB");
    layout.setSegments(new int[] {0, 1, 3});
    int[] expected = new int[] {0, 1, 3};
    int[] offsets = layout.getLineOffsets();
    for (int i = 0; i < offsets.length; i++) {
      assertEquals(" i = " + i, expected[i], offsets[i]);
    }
    layout.dispose();
    layout = new TextLayout(display);

    // Bug 241482 comment  80
    layout.setText("MNMNMN");
    layout.setSegments(new int[] {0, 1, 6});
    layout.getBounds();
    assertEquals(
        layout.getText().length() - 1, layout.getOffset(layout.getBounds().width, 0, null));
    layout.setAscent(9);
    assertEquals(
        layout.getText().length() - 1, layout.getOffset(layout.getBounds().width, 0, null));
    layout.dispose();
    layout = new TextLayout(display);

    // Bug 486600 comment 2
    layout.setText("Abcdef\nGhij\nKl");
    Rectangle bounds1 = layout.getBounds(0, 6 - 1);
    int[] trailing = new int[1];
    offset = layout.getOffset(bounds1.width, bounds1.height + 2, trailing);
    assertEquals(10, offset);
    assertEquals(1, trailing[0]);
    layout.dispose();
    layout = new TextLayout(display);

    layout.setText("AbcdefGhijKl");
    layout.setSegments(new int[] {6, 10});
    layout.setSegmentsChars(new char[] {'\n', '\n'});
    bounds1 = layout.getBounds(0, 6 - 1);
    trailing = new int[1];
    offset = layout.getOffset(bounds1.width, bounds1.height + 2, trailing);
    assertEquals(9, offset);
    assertEquals(1, trailing[0]);
    layout.dispose();
    layout = new TextLayout(display);

    // Lina's bug (bug 241482, comment 37)
    boolean doit = false; // known to be broken
    if (doit) {
      int length = layout.getText().length();
      layout.setSegments(new int[] {length});
      trailing = new int[1];
      int width = layout.getBounds().width + 20;
      assertEquals("hit test to the left", 0, layout.getOffset(0, 0, trailing));
      assertEquals("hit test to the left (trailing)", 0, trailing[0]);
      assertEquals("hit test to the right", length - 1, layout.getOffset(width, 0, trailing));
      assertEquals("hit test to the right (trailing)", 1, trailing[0]);
      layout.setSegmentsChars(new char[] {'*'});
      assertEquals("hit test to the left", 0, layout.getOffset(0, 0, trailing));
      assertEquals("hit test to the left (trailing)", 0, trailing[0]);
      assertEquals("hit test to the right", length - 1, layout.getOffset(width, 0, trailing));
      assertEquals("hit test to the right (trailing)", 1, trailing[0]);
    }

    /* wrong: internal testing */
    //	String text = "AB";
    //	int textLength = text.length();
    //	layout.setText(text);
    //	messages = new String [] {
    //			"no segments",
    //			"segments",
    //			"segments (duplicate at 0)",
    //			"segments (duplicate at 1)",
    //			"segments (duplicate at 2)" };
    //	segments = new int [][] {
    //			null,
    //			{ 0, 1, 2 },
    //			{ 0, 0, 1, 2 },
    //			{ 0, 1, 1, 2 },
    //			{ 0, 1, 2, 2 } };
    //	int[][] translatedOffsets = {
    //			{ 0, 1, 2 },
    //			{ 1, 3, 5 },
    //			{ 2, 4, 6 },
    //			{ 1, 4, 6 },
    //			{ 1, 3, 6 } };
    //	int[][] untranslatedOffsets = {
    //			{ 0, 1, 2 },
    //			{ 0, 0, 1, 1, 2, 2 },
    //			{ 0, 0, 0, 1, 1, 2, 2 },
    //			{ 0, 0, 1, 1, 1, 2, 2 },
    //			{ 0, 0, 1, 1, 2, 2, 2 } };
    //	for (int i = 0; i < segments.length; i++) {
    //		layout.setSegments(segments[i]);
    //		layout.getBounds();
    //		for (int j = 0; j <= textLength; j++) {
    //			assertEquals(messages[i] + " j = " + j,	translatedOffsets[i][j],
    // layout.translateOffset(j));
    //		}
    //		for (int j = 0, n = layout.getSegments() == null ? 0 : textLength +
    // layout.getSegments().length; j < n; j++) {
    //			assertEquals(messages[i] + " j = " + j,	untranslatedOffsets[i][j],
    // layout.untranslateOffset(j));
    //		}
    //	}
    layout.dispose();
  }