From c6f247ca31e5d4a9cc9d130cfb0abd5657176303 Mon Sep 17 00:00:00 2001 From: Fabrice Di Meglio Date: Fri, 15 Jul 2011 19:18:59 -0700 Subject: Add unit tests for bug #5037425 Canvas.drawText can't handle Right-to-Left text and text composing Change-Id: Ic71a563ad0f4ba6de5b7f030f04a3a9615b7617c --- tests/BiDiTests/res/layout/canvas2.xml | 63 +++++++++++++++++++++ tests/BiDiTests/res/values/attrs.xml | 8 +++ tests/BiDiTests/res/values/strings.xml | 3 + .../src/com/android/bidi/BiDiTestActivity.java | 1 + .../src/com/android/bidi/BiDiTestCanvas2.java | 36 ++++++++++++ .../src/com/android/bidi/BiDiTestView.java | 10 ++-- .../src/com/android/bidi/DrawTextTestView.java | 66 ++++++++++++++++++++++ 7 files changed, 183 insertions(+), 4 deletions(-) create mode 100644 tests/BiDiTests/res/layout/canvas2.xml create mode 100644 tests/BiDiTests/res/values/attrs.xml create mode 100644 tests/BiDiTests/src/com/android/bidi/BiDiTestCanvas2.java create mode 100644 tests/BiDiTests/src/com/android/bidi/DrawTextTestView.java (limited to 'tests/BiDiTests') diff --git a/tests/BiDiTests/res/layout/canvas2.xml b/tests/BiDiTests/res/layout/canvas2.xml new file mode 100644 index 0000000..2513c94 --- /dev/null +++ b/tests/BiDiTests/res/layout/canvas2.xml @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/BiDiTests/res/values/attrs.xml b/tests/BiDiTests/res/values/attrs.xml new file mode 100644 index 0000000..7f8a1d8 --- /dev/null +++ b/tests/BiDiTests/res/values/attrs.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/tests/BiDiTests/res/values/strings.xml b/tests/BiDiTests/res/values/strings.xml index bc99e79..1f6be7f 100644 --- a/tests/BiDiTests/res/values/strings.xml +++ b/tests/BiDiTests/res/values/strings.xml @@ -42,5 +42,8 @@ םמab?! abםמ?! םמ?!\nab?!\n?! + Left to right text" + "والحق أن تترك ونص" + "\u0644\u0627" diff --git a/tests/BiDiTests/src/com/android/bidi/BiDiTestActivity.java b/tests/BiDiTests/src/com/android/bidi/BiDiTestActivity.java index 7002c41..6b38cc1 100644 --- a/tests/BiDiTests/src/com/android/bidi/BiDiTestActivity.java +++ b/tests/BiDiTests/src/com/android/bidi/BiDiTestActivity.java @@ -102,6 +102,7 @@ public class BiDiTestActivity extends Activity { addItem(result, "Basic", BiDiTestBasic.class, R.id.basic); addItem(result, "Canvas", BiDiTestCanvas.class, R.id.canvas); + addItem(result, "Canvas2", BiDiTestCanvas2.class, R.id.canvas2); addItem(result, "Linear LTR", BiDiTestLinearLayoutLtr.class, R.id.linear_layout_ltr); addItem(result, "Linear RTL", BiDiTestLinearLayoutRtl.class, R.id.linear_layout_rtl); diff --git a/tests/BiDiTests/src/com/android/bidi/BiDiTestCanvas2.java b/tests/BiDiTests/src/com/android/bidi/BiDiTestCanvas2.java new file mode 100644 index 0000000..b801f0e --- /dev/null +++ b/tests/BiDiTests/src/com/android/bidi/BiDiTestCanvas2.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.bidi; + +import android.app.Fragment; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.SeekBar; + +import static com.android.bidi.BiDiTestConstants.FONT_MAX_SIZE; +import static com.android.bidi.BiDiTestConstants.FONT_MIN_SIZE; + +public class BiDiTestCanvas2 extends Fragment { + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + return inflater.inflate(R.layout.canvas2, container, false); + } +} diff --git a/tests/BiDiTests/src/com/android/bidi/BiDiTestView.java b/tests/BiDiTests/src/com/android/bidi/BiDiTestView.java index 27e1887..78b6495 100644 --- a/tests/BiDiTests/src/com/android/bidi/BiDiTestView.java +++ b/tests/BiDiTests/src/com/android/bidi/BiDiTestView.java @@ -22,6 +22,7 @@ import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.Typeface; +import android.text.TextPaint; import android.util.AttributeSet; import android.util.Log; import android.view.View; @@ -37,7 +38,7 @@ public class BiDiTestView extends View { private static final float DEFAULT_ITALIC_SKEW_X = -0.25f; - private Paint paint = new Paint(); + private TextPaint paint = new TextPaint(); private Rect rect = new Rect(); private String NORMAL_TEXT; @@ -51,6 +52,7 @@ public class BiDiTestView extends View { private String CHINESE_TEXT; private String MIXED_TEXT_1; private String HEBREW_TEXT; + private String RTL_TEXT; private Typeface typeface; @@ -83,9 +85,10 @@ public class BiDiTestView extends View { CHINESE_TEXT = context.getString(R.string.chinese_text); MIXED_TEXT_1 = context.getString(R.string.mixed_text_1); HEBREW_TEXT = context.getString(R.string.hebrew_text); + RTL_TEXT = context.getString(R.string.rtl); typeface = paint.getTypeface(); - paint.setAntiAlias(true); +// paint.setAntiAlias(true); } public void setCurrentTextSize(int size) { @@ -136,7 +139,7 @@ public class BiDiTestView extends View { paint, typeface, false, false, Paint.DIRECTION_LTR, currentTextSize); // Test Hebrew - deltaX = testString(canvas, HEBREW_TEXT, ORIGIN, ORIGIN + 14 * currentTextSize, + deltaX = testString(canvas, RTL_TEXT, ORIGIN, ORIGIN + 14 * currentTextSize, paint, typeface, false, false, Paint.DIRECTION_RTL, currentTextSize); } @@ -170,7 +173,6 @@ public class BiDiTestView extends View { paint.setColor(Color.WHITE); Log.v(TAG, "START -- drawText"); - setPaintDir(paint, dir); canvas.drawText(text, x, y + currentTextSize, this.paint); Log.v(TAG, "END -- drawText"); diff --git a/tests/BiDiTests/src/com/android/bidi/DrawTextTestView.java b/tests/BiDiTests/src/com/android/bidi/DrawTextTestView.java new file mode 100644 index 0000000..4a15100 --- /dev/null +++ b/tests/BiDiTests/src/com/android/bidi/DrawTextTestView.java @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.bidi; + +import android.content.Context; +import android.content.res.TypedArray; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint.Align; +import android.text.TextPaint; +import android.util.AttributeSet; +import android.view.View; + +public class DrawTextTestView extends View { + private float mSize; + private int mColor; + private String mText; + + public DrawTextTestView(Context context) { + this(context, null); + } + + public DrawTextTestView(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public DrawTextTestView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + + final TypedArray a = context.obtainStyledAttributes(attrs, + R.styleable.DrawTextTestView, defStyle, 0); + mSize = a.getDimension(R.styleable.DrawTextTestView_size, 21.0f); + mColor = a.getColor(R.styleable.DrawTextTestView_color, Color.YELLOW); + final CharSequence text = a.getText(R.styleable.DrawTextTestView_text); + mText = (text != null) ? text.toString() : "(empty)"; + a.recycle(); + } + + @Override + protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + final int width = getWidth(); + final int height = getHeight(); + + final TextPaint paint = new TextPaint(); + paint.setTextSize(mSize); + paint.setColor(mColor); + paint.setTextAlign(Align.CENTER); + + canvas.drawText(mText, width / 2, height * 2 / 3, paint); + } +} \ No newline at end of file -- cgit v1.1