am f0268081: Merge change 20019 into donut
Merge commit 'f0268081f04a8d7c931e3481b433576f2b54685e'
* commit 'f0268081f04a8d7c931e3481b433576f2b54685e':
Integrate unsubmitted cupcake change 131132:
diff --git a/tests/assets/samplefont.ttf b/tests/assets/samplefont.ttf
new file mode 100644
index 0000000..49f1c62
--- /dev/null
+++ b/tests/assets/samplefont.ttf
Binary files differ
diff --git a/tests/res/drawable/typeface_test.png b/tests/res/drawable/typeface_test.png
new file mode 100644
index 0000000..2e69cfe
--- /dev/null
+++ b/tests/res/drawable/typeface_test.png
Binary files differ
diff --git a/tests/tests/graphics/src/android/graphics/cts/PorterDuffXfermodeTest.java b/tests/tests/graphics/src/android/graphics/cts/PorterDuffXfermodeTest.java
new file mode 100644
index 0000000..b22758b
--- /dev/null
+++ b/tests/tests/graphics/src/android/graphics/cts/PorterDuffXfermodeTest.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2009 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.cts;
+
+import dalvik.annotation.TestLevel;
+import dalvik.annotation.TestTargetClass;
+import dalvik.annotation.TestTargetNew;
+
+import android.graphics.Bitmap;
+import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.Paint;
+import android.graphics.PorterDuff;
+import android.graphics.PorterDuffXfermode;
+import android.graphics.Bitmap.Config;
+
+import junit.framework.TestCase;
+
+@TestTargetClass(PorterDuffXfermode.class)
+public class PorterDuffXfermodeTest extends TestCase {
+
+ private static final int WIDTH = 100;
+ private static final int HEIGHT = 100;
+
+ @TestTargetNew(
+ level = TestLevel.COMPLETE,
+ method = "PorterDuffXfermode",
+ args = {android.graphics.PorterDuff.Mode.class}
+ )
+ public void testPorterDuffXfermode() {
+ Bitmap target = Bitmap.createBitmap(WIDTH, HEIGHT, Config.ARGB_8888);
+ target.eraseColor(Color.TRANSPARENT);
+ Bitmap b1 = Bitmap.createBitmap(WIDTH / 2, HEIGHT, Config.ARGB_8888);
+ b1.eraseColor(Color.RED);
+ Bitmap b2 = Bitmap.createBitmap(WIDTH, HEIGHT / 2, Config.ARGB_8888);
+ b2.eraseColor(Color.BLUE);
+
+ Canvas canvas = new Canvas(target);
+ Paint p = new Paint();
+ canvas.drawBitmap(b1, 0, 0, p);
+ p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
+ canvas.drawBitmap(b2, 0, HEIGHT / 2, p);
+ assertEquals(Color.RED, target.getPixel(WIDTH / 4, HEIGHT / 4));
+ assertEquals(Color.BLUE, target.getPixel(WIDTH / 4, HEIGHT * 3 / 4));
+ assertEquals(Color.BLUE, target.getPixel(WIDTH * 3 / 4, HEIGHT * 3 / 4));
+
+ target.eraseColor(Color.TRANSPARENT);
+ p.setXfermode(null);
+ canvas.drawBitmap(b1, 0, 0, p);
+ p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST));
+ canvas.drawBitmap(b2, 0, HEIGHT / 2, p);
+ assertEquals(Color.RED, target.getPixel(WIDTH / 4, HEIGHT / 4));
+ assertEquals(Color.RED, target.getPixel(WIDTH / 4, HEIGHT * 3 / 4));
+ assertEquals(Color.TRANSPARENT, target.getPixel(WIDTH * 3 / 4, HEIGHT * 3 / 4));
+
+ target.eraseColor(Color.TRANSPARENT);
+ p.setXfermode(null);
+ canvas.drawBitmap(b1, 0, 0, p);
+ p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SCREEN));
+ canvas.drawBitmap(b2, 0, HEIGHT / 2, p);
+ assertEquals(Color.RED, target.getPixel(WIDTH / 4, HEIGHT / 4));
+ assertEquals(Color.MAGENTA, target.getPixel(WIDTH / 4, HEIGHT * 3 / 4));
+ assertEquals(Color.BLUE, target.getPixel(WIDTH * 3 / 4, HEIGHT * 3 / 4));
+ }
+}
diff --git a/tests/tests/graphics/src/android/graphics/cts/RadialGradientTest.java b/tests/tests/graphics/src/android/graphics/cts/RadialGradientTest.java
new file mode 100644
index 0000000..3db864b
--- /dev/null
+++ b/tests/tests/graphics/src/android/graphics/cts/RadialGradientTest.java
@@ -0,0 +1,128 @@
+/*
+ * Copyright (C) 2009 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.cts;
+
+import dalvik.annotation.TestLevel;
+import dalvik.annotation.TestTargetClass;
+import dalvik.annotation.TestTargetNew;
+
+import android.graphics.Bitmap;
+import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.Paint;
+import android.graphics.RadialGradient;
+import android.graphics.Shader;
+import android.graphics.Shader.TileMode;
+
+import junit.framework.TestCase;
+
+@TestTargetClass(RadialGradient.class)
+public class RadialGradientTest extends TestCase {
+ private static final int SIZE = 200;
+ private static final int RADIUS = 80;
+ private static final int CENTER = SIZE / 2;
+
+ @TestTargetNew(
+ level = TestLevel.COMPLETE,
+ method = "RadialGradient",
+ args = {float.class, float.class, float.class, int[].class, float[].class,
+ TileMode.class}
+ )
+ public void testRadialGradient() {
+ final int[] colors = { Color.BLUE, Color.GREEN, Color.RED };
+ final float[] positions = { 0f, 0.3f, 1f };
+ int tolerance = (int)(0xFF / (0.3f * RADIUS) * 2);
+ RadialGradient rg = new RadialGradient(CENTER, CENTER, RADIUS, colors, positions,
+ Shader.TileMode.CLAMP);
+ Bitmap b = Bitmap.createBitmap(SIZE, SIZE, Bitmap.Config.ARGB_8888);
+ Canvas canvas = new Canvas(b);
+ Paint p = new Paint();
+ p.setShader(rg);
+ canvas.drawRect(0, 0, SIZE, SIZE, p);
+ checkPixels(b, colors, positions, tolerance);
+ }
+
+ @TestTargetNew(
+ level = TestLevel.COMPLETE,
+ method = "RadialGradient",
+ args = {float.class, float.class, float.class, int.class, int.class, TileMode.class}
+ )
+ public void testRadialGradientWithColor() {
+ final int[] colors = { Color.BLUE, Color.GREEN };
+ final float[] positions = { 0f, 1f };
+ int tolerance = (int)(0xFF / RADIUS * 2);
+ RadialGradient rg = new RadialGradient(CENTER, CENTER, RADIUS, colors[0], colors[1],
+ Shader.TileMode.CLAMP);
+ Bitmap b = Bitmap.createBitmap(SIZE, SIZE, Bitmap.Config.ARGB_8888);
+ Canvas canvas = new Canvas(b);
+ Paint p = new Paint();
+ p.setShader(rg);
+ canvas.drawRect(0, 0, SIZE, SIZE, p);
+ checkPixels(b, colors, positions, tolerance);
+ }
+
+ private void checkPixels(Bitmap bitmap, int[] colors, float[] positions, int tolerance) {
+ for (int y = 0; y < SIZE; y++) {
+ for (int x = 0; x < SIZE; x++) {
+ double dist = dist(x, y, CENTER, CENTER) / RADIUS;
+ int idx;
+ int color;
+ for (idx = 0; idx < positions.length; idx++) {
+ if (positions[idx] > dist) {
+ break;
+ }
+ }
+ if (idx == 0) {
+ // use start color
+ color = colors[0];
+ } else if (idx == positions.length) {
+ // clamp to end color
+ color = colors[positions.length - 1];
+ } else {
+ // linear interpolation
+ int i1 = idx - 1; // index of next lower color and position
+ int i2 = idx; // index of next higher color and position
+ double delta = (dist - positions[i1]) / (positions[i2] - positions[i1]);
+ int alpha = (int) ((1d - delta) * Color.alpha(colors[i1]) +
+ delta * Color.alpha(colors[i2]));
+ int red = (int) ((1d - delta) * Color.red(colors[i1]) +
+ delta * Color.red(colors[i2]));
+ int green = (int) ((1d - delta) * Color.green(colors[i1]) +
+ delta * Color.green(colors[i2]));
+ int blue = (int) ((1d - delta) * Color.blue(colors[i1]) +
+ delta * Color.blue(colors[i2]));
+ color = Color.argb(alpha, red, green, blue);
+ }
+ int pixel = bitmap.getPixel(x, y);
+
+ assertEquals(Color.alpha(color), Color.alpha(pixel), tolerance);
+ assertEquals(Color.red(color), Color.red(pixel), tolerance);
+ assertEquals(Color.green(color), Color.green(pixel), tolerance);
+ assertEquals(Color.blue(color), Color.blue(pixel), tolerance);
+ }
+ }
+ }
+
+ /**
+ * Calculate distance between two points.
+ */
+ private double dist(int x1, int y1, int x2, int y2) {
+ int distX = x1 - x2;
+ int distY = y1 - y2;
+ return Math.sqrt(distX * distX + distY * distY);
+ }
+
+}
diff --git a/tests/tests/graphics/src/android/graphics/cts/Shader_TileModeTest.java b/tests/tests/graphics/src/android/graphics/cts/Shader_TileModeTest.java
new file mode 100644
index 0000000..135bf36
--- /dev/null
+++ b/tests/tests/graphics/src/android/graphics/cts/Shader_TileModeTest.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2009 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.cts;
+
+import junit.framework.TestCase;
+import android.graphics.Shader;
+import dalvik.annotation.TestLevel;
+import dalvik.annotation.TestTargetClass;
+import dalvik.annotation.TestTargetNew;
+import dalvik.annotation.TestTargets;
+
+@TestTargetClass(Shader.TileMode.class)
+public class Shader_TileModeTest extends TestCase {
+
+ @TestTargets({
+ @TestTargetNew(
+ level = TestLevel.NOT_NECESSARY,
+ method = "values",
+ args = {}
+ ),
+ @TestTargetNew(
+ level = TestLevel.NOT_NECESSARY,
+ method = "valueOf",
+ args = {java.lang.String.class}
+ )
+ })
+ public void testTileMode() {
+ }
+}
diff --git a/tests/tests/graphics/src/android/graphics/cts/SumPathEffectTest.java b/tests/tests/graphics/src/android/graphics/cts/SumPathEffectTest.java
new file mode 100644
index 0000000..c9a936a
--- /dev/null
+++ b/tests/tests/graphics/src/android/graphics/cts/SumPathEffectTest.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2009 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.cts;
+
+import dalvik.annotation.TestLevel;
+import dalvik.annotation.TestTargetClass;
+import dalvik.annotation.TestTargetNew;
+
+import android.graphics.Bitmap;
+import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.CornerPathEffect;
+import android.graphics.DashPathEffect;
+import android.graphics.Paint;
+import android.graphics.Path;
+import android.graphics.PathEffect;
+import android.graphics.SumPathEffect;
+import android.graphics.Bitmap.Config;
+import android.graphics.Path.Direction;
+
+import junit.framework.TestCase;
+
+@TestTargetClass(SumPathEffect.class)
+public class SumPathEffectTest extends TestCase {
+
+ private static final int WIDTH = 100;
+ private static final int HEIGHT = 100;
+
+ @TestTargetNew(
+ level = TestLevel.COMPLETE,
+ method = "SumPathEffect",
+ args = {android.graphics.PathEffect.class, android.graphics.PathEffect.class}
+ )
+ public void testSumPathEffect() {
+ Bitmap bitmap = Bitmap.createBitmap(WIDTH, HEIGHT, Config.ARGB_8888);
+ bitmap.eraseColor(Color.BLACK);
+ Bitmap expected = Bitmap.createBitmap(WIDTH, HEIGHT, Config.ARGB_8888);
+ expected.eraseColor(Color.BLACK);
+
+ Path path = new Path();
+ path.addRect(10, 10, WIDTH - 10, HEIGHT - 10, Direction.CW);
+
+ PathEffect first = new CornerPathEffect(40);
+ Canvas canvas = new Canvas(expected);
+ Paint paint = new Paint();
+ paint.setColor(Color.GREEN);
+ paint.setPathEffect(first);
+ paint.setStyle(Paint.Style.STROKE);
+ paint.setStrokeWidth(0); // 1-pixel hairline
+ canvas.drawPath(path, paint);
+
+ PathEffect second = new DashPathEffect(new float[] { 10, 5 }, 5);
+ paint.setPathEffect(second);
+ canvas.drawPath(path, paint);
+
+ SumPathEffect sumPathEffect = new SumPathEffect(second, first);
+ paint.setPathEffect(sumPathEffect);
+ canvas = new Canvas(bitmap);
+ canvas.drawPath(path, paint);
+
+ for (int i = 0; i < WIDTH; i++) {
+ for (int j = 0; j < HEIGHT; j++) {
+ assertEquals(expected.getPixel(i, j), bitmap.getPixel(i, j));
+ }
+ }
+ }
+}
diff --git a/tests/tests/graphics/src/android/graphics/cts/SweepGradientTest.java b/tests/tests/graphics/src/android/graphics/cts/SweepGradientTest.java
new file mode 100644
index 0000000..c46b89e
--- /dev/null
+++ b/tests/tests/graphics/src/android/graphics/cts/SweepGradientTest.java
@@ -0,0 +1,158 @@
+/*
+ * Copyright (C) 2009 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.cts;
+
+import dalvik.annotation.TestLevel;
+import dalvik.annotation.TestTargetClass;
+import dalvik.annotation.TestTargetNew;
+
+import android.graphics.Bitmap;
+import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.Paint;
+import android.graphics.Rect;
+import android.graphics.Shader;
+import android.graphics.SweepGradient;
+import android.graphics.Bitmap.Config;
+
+import junit.framework.TestCase;
+
+@TestTargetClass(SweepGradient.class)
+public class SweepGradientTest extends TestCase {
+
+ private Paint mPaint;
+ private Canvas mCanvas;
+ private Bitmap mBitmap;
+
+ private static final int SIZE = 200;
+ private static final int CENTER = SIZE / 2;
+ private static final int RADIUS = 80;
+ private static final int NUM_STEPS = 100;
+ private static final int TOLERANCE = 5;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ mPaint = new Paint();
+ mBitmap = Bitmap.createBitmap(SIZE, SIZE, Config.ARGB_8888);
+ mBitmap.eraseColor(Color.TRANSPARENT);
+ mCanvas = new Canvas(mBitmap);
+ }
+
+ @TestTargetNew(
+ level = TestLevel.COMPLETE,
+ method = "SweepGradient",
+ args = {float.class, float.class, int.class, int.class}
+ )
+ public void test2Colors() {
+ final int[] colors = new int[] { Color.GREEN, Color.RED };
+ final float[] positions = new float[] { 0f, 1f };
+ Shader shader = new SweepGradient(CENTER, CENTER, colors[0], colors[1]);
+ mPaint.setShader(shader);
+ mCanvas.drawRect(new Rect(0, 0, SIZE, SIZE), mPaint);
+ checkColors(colors, positions, TOLERANCE);
+ }
+
+ @TestTargetNew(
+ level = TestLevel.COMPLETE,
+ method = "SweepGradient",
+ args = {float.class, float.class, int[].class, float[].class}
+ )
+ public void testColorArray() {
+ final int[] colors = new int[] { Color.GREEN, Color.RED, Color.BLUE };
+ final float[] positions = new float[] { 0f, 0.3f, 1f };
+ Shader shader = new SweepGradient(CENTER, CENTER, colors, positions);
+ mPaint.setShader(shader);
+ mCanvas.drawRect(new Rect(0, 0, SIZE, SIZE), mPaint);
+
+ checkColors(colors, positions, TOLERANCE);
+ }
+
+ @TestTargetNew(
+ level = TestLevel.COMPLETE,
+ method = "SweepGradient",
+ args = {float.class, float.class, int[].class, float[].class}
+ )
+ public void testMultiColor() {
+ final int[] colors = new int[] { Color.GREEN, Color.RED, Color.BLUE, Color.GREEN };
+ final float[] positions = new float[] { 0f, 0.25f, 0.5f, 1f };
+
+ Shader shader = new SweepGradient(CENTER, CENTER, colors, positions);
+ mPaint.setShader(shader);
+ mCanvas.drawRect(new Rect(0, 0, SIZE, SIZE), mPaint);
+
+ checkColors(colors, positions, TOLERANCE);
+ }
+
+ private void checkColors(int[] colors, float[] positions, int tolerance) {
+ final double twoPi = Math.PI * 2;
+ final double step = twoPi / NUM_STEPS;
+
+ // exclude angle 0, which is not defined
+ for (double rad = step; rad <= twoPi - step; rad += step) {
+ int x = CENTER + (int)(Math.cos(rad) * RADIUS);
+ int y = CENTER + (int)(Math.sin(rad) * RADIUS);
+
+ float relPos = (float)(rad / twoPi);
+ int idx;
+ int color;
+ for (idx = 0; idx < positions.length; idx++) {
+ if (positions[idx] > relPos) {
+ break;
+ }
+ }
+ if (idx == 0) {
+ // use start color
+ color = colors[0];
+ } else if (idx == positions.length) {
+ // clamp to end color
+ color = colors[positions.length - 1];
+ } else {
+ // linear interpolation
+ int i1 = idx - 1; // index of next lower color and position
+ int i2 = idx; // index of next higher color and position
+ double delta = (relPos - positions[i1]) / (positions[i2] - positions[i1]);
+ int alpha = (int) ((1d - delta) * Color.alpha(colors[i1]) +
+ delta * Color.alpha(colors[i2]));
+ int red = (int) ((1d - delta) * Color.red(colors[i1]) +
+ delta * Color.red(colors[i2]));
+ int green = (int) ((1d - delta) * Color.green(colors[i1]) +
+ delta * Color.green(colors[i2]));
+ int blue = (int) ((1d - delta) * Color.blue(colors[i1]) +
+ delta * Color.blue(colors[i2]));
+ color = Color.argb(alpha, red, green, blue);
+ }
+
+ int pixel = mBitmap.getPixel(x, y);
+
+ try {
+ assertEquals(Color.alpha(color), Color.alpha(pixel), tolerance);
+ assertEquals(Color.red(color), Color.red(pixel), tolerance);
+ assertEquals(Color.green(color), Color.green(pixel), tolerance);
+ assertEquals(Color.blue(color), Color.blue(pixel), tolerance);
+ } catch (Error e) {
+ System.out.println("***************");
+ System.out.println(rad);
+ System.out.println(x);
+ System.out.println(y);
+ System.out.println(Integer.toHexString(pixel));
+ System.out.println(Integer.toHexString(color));
+ throw e;
+ }
+ }
+ }
+}
diff --git a/tests/tests/graphics/src/android/graphics/cts/TypefaceTest.java b/tests/tests/graphics/src/android/graphics/cts/TypefaceTest.java
new file mode 100644
index 0000000..2f44136
--- /dev/null
+++ b/tests/tests/graphics/src/android/graphics/cts/TypefaceTest.java
@@ -0,0 +1,198 @@
+/*
+ * Copyright (C) 2009 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.cts;
+
+import com.android.cts.stub.R;
+
+import dalvik.annotation.TestLevel;
+import dalvik.annotation.TestTargetClass;
+import dalvik.annotation.TestTargetNew;
+import dalvik.annotation.TestTargets;
+
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.Paint;
+import android.graphics.Typeface;
+import android.graphics.Bitmap.Config;
+import android.test.AndroidTestCase;
+
+@TestTargetClass(android.graphics.Typeface.class)
+public class TypefaceTest extends AndroidTestCase {
+
+ // generic family name for monospaced fonts
+ private static final String MONO = "monospace";
+ private static final String DEFAULT = (String)null;
+ private static final String INVALID = "invalid-family-name";
+
+ // list of family names to try when attempting to find a typeface with a given style
+ private static final String[] FAMILIES =
+ { (String) null, "monospace", "serif", "sans-serif", "cursive", "arial", "times" };
+
+ /**
+ * Create a typeface of the given style. If the default font does not support the style,
+ * a number of generic families are tried.
+ * @return The typeface or null, if no typeface with the given style can be found.
+ */
+ private Typeface createTypeface(int style) {
+ for (String family : FAMILIES) {
+ Typeface tf = Typeface.create(family, style);
+ if (tf.getStyle() == style) {
+ return tf;
+ }
+ }
+ return null;
+ }
+
+
+ @TestTargets ({
+ @TestTargetNew(
+ level = TestLevel.COMPLETE,
+ method = "isBold",
+ args = {}
+ ),
+ @TestTargetNew(
+ level = TestLevel.COMPLETE,
+ method = "isItalic",
+ args = {}
+ ),
+ @TestTargetNew(
+ level = TestLevel.COMPLETE,
+ method = "getStyle",
+ args = {}
+ )
+ })
+ public void testIsBold() {
+ Typeface typeface = createTypeface(Typeface.BOLD);
+ if (typeface != null) {
+ assertEquals(Typeface.BOLD, typeface.getStyle());
+ assertTrue(typeface.isBold());
+ assertFalse(typeface.isItalic());
+ }
+
+ typeface = createTypeface(Typeface.ITALIC);
+ if (typeface != null) {
+ assertEquals(Typeface.ITALIC, typeface.getStyle());
+ assertFalse(typeface.isBold());
+ assertTrue(typeface.isItalic());
+ }
+
+ typeface = createTypeface(Typeface.BOLD_ITALIC);
+ if (typeface != null) {
+ assertEquals(Typeface.BOLD_ITALIC, typeface.getStyle());
+ assertTrue(typeface.isBold());
+ assertTrue(typeface.isItalic());
+ }
+
+ typeface = createTypeface(Typeface.NORMAL);
+ if (typeface != null) {
+ assertEquals(Typeface.NORMAL, typeface.getStyle());
+ assertFalse(typeface.isBold());
+ assertFalse(typeface.isItalic());
+ }
+ }
+
+ @TestTargets({
+ @TestTargetNew(
+ level = TestLevel.COMPLETE,
+ method = "create",
+ args = {java.lang.String.class, int.class}
+ ),
+ @TestTargetNew(
+ level = TestLevel.COMPLETE,
+ method = "create",
+ args = {android.graphics.Typeface.class, int.class}
+ )
+ })
+ public void testCreate() {
+ Typeface typeface = Typeface.create(DEFAULT, Typeface.NORMAL);
+ assertNotNull(typeface);
+ typeface = Typeface.create(MONO, Typeface.BOLD);
+ assertNotNull(typeface);
+ typeface = Typeface.create(INVALID, Typeface.ITALIC);
+ assertNotNull(typeface);
+
+ typeface = Typeface.create(typeface, Typeface.NORMAL);
+ assertNotNull(typeface);
+ typeface = Typeface.create(typeface, Typeface.BOLD);
+ assertNotNull(typeface);
+ }
+
+ @TestTargetNew(
+ level = TestLevel.COMPLETE,
+ method = "defaultFromStyle",
+ args = {int.class}
+ )
+ public void testDefaultFromStyle() {
+ Typeface typeface = Typeface.defaultFromStyle(Typeface.NORMAL);
+ assertNotNull(typeface);
+ typeface = Typeface.defaultFromStyle(Typeface.BOLD);
+ assertNotNull(typeface);
+ typeface = Typeface.defaultFromStyle(Typeface.ITALIC);
+ assertNotNull(typeface);
+ typeface = Typeface.defaultFromStyle(Typeface.BOLD_ITALIC);
+ assertNotNull(typeface);
+ }
+
+ public void testConstants() {
+ assertNotNull(Typeface.DEFAULT);
+ assertNotNull(Typeface.DEFAULT_BOLD);
+ assertNotNull(Typeface.MONOSPACE);
+ assertNotNull(Typeface.SANS_SERIF);
+ assertNotNull(Typeface.SERIF);
+ }
+
+ @TestTargetNew(
+ level = TestLevel.COMPLETE,
+ method = "createFromAsset",
+ args = {android.content.res.AssetManager.class, java.lang.String.class}
+ )
+ public void testCreateFromAsset() {
+ // input abnormal params.
+ try {
+ Typeface.createFromAsset(null, null);
+ fail("Should throw a NullPointerException.");
+ } catch (NullPointerException e) {
+ // except here
+ }
+
+ Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "samplefont.ttf");
+ assertNotNull(typeface);
+
+ Bitmap bitmap = Bitmap.createBitmap(100, 100, Config.ARGB_8888);
+ bitmap.eraseColor(Color.BLACK);
+ Canvas canvas = new Canvas(bitmap);
+ Paint p = new Paint();
+ p.setTypeface(typeface);
+ p.setColor(Color.WHITE);
+ p.setTextAlign(Paint.Align.CENTER);
+ p.setTextSize(50);
+ p.setFlags(0); // clear all flags (not sure what defaults flags are set)
+ canvas.drawText("test", bitmap.getWidth() / 2, 3 * bitmap.getHeight() / 4 , p);
+
+ Bitmap expected =
+ BitmapFactory.decodeResource(getContext().getResources(), R.drawable.typeface_test);
+ assertEquals(expected.getWidth(), bitmap.getWidth());
+ assertEquals(expected.getHeight(), bitmap.getHeight());
+ for (int y = 0; y < bitmap.getHeight(); y++) {
+ for (int x = 0; x < bitmap.getWidth(); x++) {
+ assertEquals(expected.getPixel(x, y), bitmap.getPixel(x, y));
+ }
+ }
+ }
+}