From 5532c34613753044b834a1e8cfb8c20d76405af7 Mon Sep 17 00:00:00 2001 From: Seigo Nonaka Date: Tue, 1 Sep 2015 15:58:01 +0900 Subject: [PATCH] Introduce getTextWidths test cases. This CL includes HintedAdvanceWidthTest-Regular.{ttx|ttf} for fixing test expectation, which has hinting information. Bug: 11256006 Change-Id: Ia79a1d093e39cd47c914395e034cb90eda51c5c0 --- .../fonts/HintedAdvanceWidthTest-Regular.ttf | Bin 0 -> 1136 bytes .../fonts/HintedAdvanceWidthTest-Regular.ttx | 328 ++++++++++++++++++ .../src/android/graphics/PaintTest.java | 97 ++++++ 3 files changed, 425 insertions(+) create mode 100644 core/tests/coretests/assets/fonts/HintedAdvanceWidthTest-Regular.ttf create mode 100644 core/tests/coretests/assets/fonts/HintedAdvanceWidthTest-Regular.ttx create mode 100644 core/tests/coretests/src/android/graphics/PaintTest.java diff --git a/core/tests/coretests/assets/fonts/HintedAdvanceWidthTest-Regular.ttf b/core/tests/coretests/assets/fonts/HintedAdvanceWidthTest-Regular.ttf new file mode 100644 index 0000000000000000000000000000000000000000..1924c35a9c0a0335a5e0e7cb559e13364f5cbf7c GIT binary patch literal 1136 zcmZuw&rcIk5T3WYT`NU_(3X$@ZY^o3N+d0iP52QpkdTl_S}|y1DQjE0n6|96MZ6fV z=mlc}h8ur?30O$faJLsPCPqCHFB}ZaJ!x_ExJSW3b+bfpPNf*v_^N3uK?HN zwMB#8dp?051y%}LJ~MD{aF<9#KTI*^iY4<0C9r=#_)ErohWCSI^@|XgcpB-!MzXH| zCoyr4vmbq{A8ft7!L?8pcA(CXz!4U5#6435^8%<%C|i{*$`O*~kw|fJ1gfPLdI)yF zvueeNLYys=i`>Ff;Wj*Dwq{clVf=JJoh;tx$+|T*d4sN@^0=s=hbnsJr(?yi1FKU7 z@PHD$j!>isx@qtECZvsrJbjxO-JI23g= zP*o$LP)~PnZ#3ff`@FIo929!GL!sbBS@wDT{^+lzSZpaC<1*GU5)O}agt-h)ttXS~ zQ?^{`OLTQ5`fO>9@1_}N4o1!J*fR@3(6I&lVTNN%nEu!= zQ$IZ(oh_6yX=NzApcPUXWpXxM(v^u!u@ui_%Q0LVAEiA*c0#+VaK7fRHhu_=Ksq7JNq>EIJ5JgT<8BVxA`0W&ywI1>9WN# JV)zc~egow2%O?N; literal 0 HcmV?d00001 diff --git a/core/tests/coretests/assets/fonts/HintedAdvanceWidthTest-Regular.ttx b/core/tests/coretests/assets/fonts/HintedAdvanceWidthTest-Regular.ttx new file mode 100644 index 0000000000000..2b8478d522d23 --- /dev/null +++ b/core/tests/coretests/assets/fonts/HintedAdvanceWidthTest-Regular.ttx @@ -0,0 +1,328 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + PUSHB[] 1 2 4 3 + SLOOP[] + PUSH[] -400 + SHPIX[] + + PUSHB[] 0 3 5 3 + SLOOP[] + PUSH[] 400 + SHPIX[] + + + + + + + + + + + + + + + + + + + + PUSHB[] 0 11 12 3 + SLOOP[] + PUSH[] -200 + SHPIX[] + PUSHB[] 5 6 13 3 + SLOOP[] + PUSH[] 200 + SHPIX[] + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + PUSH[] 32 -200 + SHPIX[] + PUSHB[] 33 200 + SHPIX[] + + + + + + + PUSH[] 0 -200 + SHPIX[] + PUSHB[] 1 200 + SHPIX[] + + + + + + + + Hinted Advance Width Test + + + Regular + + + Hinted Advance Width Test + + + HintedAdvanceWidthTest-Regular + + + Hinted Advance Width Test + + + Regular + + + Hinted Advance Width Test + + + HintedAdvanceWidthTest-Regular + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/core/tests/coretests/src/android/graphics/PaintTest.java b/core/tests/coretests/src/android/graphics/PaintTest.java new file mode 100644 index 0000000000000..e97bb33487c47 --- /dev/null +++ b/core/tests/coretests/src/android/graphics/PaintTest.java @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2015 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 android.graphics; + +import android.graphics.Paint; +import android.test.InstrumentationTestCase; +import android.test.suitebuilder.annotation.SmallTest; + +/** + * PaintTest tests {@link Paint}. + */ +public class PaintTest extends InstrumentationTestCase { + private static final String FONT_PATH = "fonts/HintedAdvanceWidthTest-Regular.ttf"; + + static void assertEquals(String message, float[] expected, float[] actual) { + if (expected.length != actual.length) { + fail(message + " expected array length:<" + expected.length + "> but was:<" + + actual.length + ">"); + } + for (int i = 0; i < expected.length; ++i) { + if (expected[i] != actual[i]) { + fail(message + " expected array element[" +i + "]:<" + expected[i] + ">but was:<" + + actual[i] + ">"); + } + } + } + + static class HintingTestCase { + public final String mText; + public final float mTextSize; + public final float[] mWidthWithoutHinting; + public final float[] mWidthWithHinting; + + public HintingTestCase(String text, float textSize, float[] widthWithoutHinting, + float[] widthWithHinting) { + mText = text; + mTextSize = textSize; + mWidthWithoutHinting = widthWithoutHinting; + mWidthWithHinting = widthWithHinting; + } + } + + // Following test cases are only valid for HintedAdvanceWidthTest-Regular.ttf in assets/fonts. + HintingTestCase[] HINTING_TESTCASES = { + new HintingTestCase("H", 11f, new float[] { 7f }, new float[] { 13f }), + new HintingTestCase("O", 11f, new float[] { 7f }, new float[] { 13f }), + + new HintingTestCase("H", 13f, new float[] { 8f }, new float[] { 14f }), + new HintingTestCase("O", 13f, new float[] { 9f }, new float[] { 15f }), + + new HintingTestCase("HO", 11f, new float[] { 7f, 7f }, new float[] { 13f, 13f }), + new HintingTestCase("OH", 11f, new float[] { 7f, 7f }, new float[] { 13f, 13f }), + + new HintingTestCase("HO", 13f, new float[] { 8f, 9f }, new float[] { 14f, 15f }), + new HintingTestCase("OH", 13f, new float[] { 9f, 8f }, new float[] { 15f, 14f }), + }; + + @SmallTest + public void testHintingWidth() { + final Typeface fontTypeface = Typeface.createFromAsset( + getInstrumentation().getContext().getAssets(), FONT_PATH); + Paint paint = new Paint(); + paint.setTypeface(fontTypeface); + + for (int i = 0; i < HINTING_TESTCASES.length; ++i) { + HintingTestCase testCase = HINTING_TESTCASES[i]; + + paint.setTextSize(testCase.mTextSize); + + float[] widths = new float[testCase.mText.length()]; + + paint.setHinting(Paint.HINTING_OFF); + paint.getTextWidths(String.valueOf(testCase.mText), widths); + assertEquals("Text width of '" + testCase.mText + "' without hinting is not expected.", + testCase.mWidthWithoutHinting, widths); + + paint.setHinting(Paint.HINTING_ON); + paint.getTextWidths(String.valueOf(testCase.mText), widths); + assertEquals("Text width of '" + testCase.mText + "' with hinting is not expected.", + testCase.mWidthWithHinting, widths); + } + } +}