diff options
author | Danesh M <danesh@cyngn.com> | 2016-06-15 14:13:55 -0700 |
---|---|---|
committer | Steve Kondik <shade@chemlab.org> | 2016-08-14 21:57:37 -0700 |
commit | e1ba9407f75a08f696dcdf1adf8bc8dac1d3d598 (patch) | |
tree | 53f49dc217a6bb68b9fd03ecb1371d8b5edd03e0 | |
parent | 4ceac56022f4e452aec5fb9e31917850ab1dde1f (diff) | |
download | vendor_cmsdk-e1ba9407f75a08f696dcdf1adf8bc8dac1d3d598.zip vendor_cmsdk-e1ba9407f75a08f696dcdf1adf8bc8dac1d3d598.tar.gz vendor_cmsdk-e1ba9407f75a08f696dcdf1adf8bc8dac1d3d598.tar.bz2 |
CMSDK : Fix generateAlertColorFromDrawable for non BitmapDrawable
In the case that the bitmap being passed in was not a bitmap drawable,
we were not retaining any of the attributes from the original drawable.
This patch ensures we ask that drawable to draw on the canvas/bitmap so we
can use that information. Also add tests around it.
Change-Id: I3eefba6e6624fe0bed4965ddf9029320c40f7420
-rw-r--r-- | sdk/src/java/cyanogenmod/util/ColorUtils.java | 9 | ||||
-rw-r--r-- | tests/src/org/cyanogenmod/tests/util/ColorUtilTest.java | 33 |
2 files changed, 38 insertions, 4 deletions
diff --git a/sdk/src/java/cyanogenmod/util/ColorUtils.java b/sdk/src/java/cyanogenmod/util/ColorUtils.java index a5633ff..0bcb7fd 100644 --- a/sdk/src/java/cyanogenmod/util/ColorUtils.java +++ b/sdk/src/java/cyanogenmod/util/ColorUtils.java @@ -16,6 +16,7 @@ package cyanogenmod.util; import android.graphics.Bitmap; +import android.graphics.Canvas; import android.graphics.Color; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; @@ -270,9 +271,13 @@ public class ColorUtils { if (drawable instanceof BitmapDrawable) { bitmap = ((BitmapDrawable) drawable).getBitmap(); } else { - bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), - drawable.getIntrinsicHeight(), + int width = drawable.getIntrinsicWidth(); + int height = drawable.getIntrinsicHeight(); + bitmap = Bitmap.createBitmap(Math.max(1, width), + Math.max(1, height), Bitmap.Config.ARGB_8888); + Canvas canvas = new Canvas(bitmap); + drawable.draw(canvas); } if (bitmap != null) { diff --git a/tests/src/org/cyanogenmod/tests/util/ColorUtilTest.java b/tests/src/org/cyanogenmod/tests/util/ColorUtilTest.java index 769ce66..56e5a2a 100644 --- a/tests/src/org/cyanogenmod/tests/util/ColorUtilTest.java +++ b/tests/src/org/cyanogenmod/tests/util/ColorUtilTest.java @@ -21,9 +21,12 @@ import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.ColorDrawable; import android.test.AndroidTestCase; -import android.util.MathUtils; import cyanogenmod.util.ColorUtils; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; public class ColorUtilTest extends AndroidTestCase { private ColorUtils mColorUtils; @@ -59,13 +62,17 @@ public class ColorUtilTest extends AndroidTestCase { assertEquals(color, Color.BLACK); Bitmap bitmap = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888); + BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap); + bitmapDrawable.setBounds(0, 0, 10, 10); Canvas canvas = new Canvas(bitmap); canvas.drawColor(Color.RED); // Test fully red bitmap - BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap); color = mColorUtils.generateAlertColorFromDrawable(bitmapDrawable); assertEquals(color, Color.RED); + color = mColorUtils.generateAlertColorFromDrawable( + getColorDrawableFromBitmapDrawable(bitmapDrawable)); + assertEquals(color, Color.RED); // Test blue/red bitmap with blue dominating Paint p = new Paint(); @@ -74,12 +81,18 @@ public class ColorUtilTest extends AndroidTestCase { canvas.drawRect(0, 0, 8, 8, p); color = mColorUtils.generateAlertColorFromDrawable(bitmapDrawable); assertEquals(color, Color.BLUE); + color = mColorUtils.generateAlertColorFromDrawable( + getColorDrawableFromBitmapDrawable(bitmapDrawable)); + assertEquals(color, Color.BLUE); // Test large white + small blue scenario canvas.drawColor(Color.WHITE); canvas.drawRect(0, 0, 2, 2, p); color = mColorUtils.generateAlertColorFromDrawable(bitmapDrawable); assertEquals(color, Color.BLUE); + color = mColorUtils.generateAlertColorFromDrawable( + getColorDrawableFromBitmapDrawable(bitmapDrawable)); + assertEquals(color, Color.BLUE); // Test large white + small black scenario canvas.drawColor(Color.WHITE); @@ -87,8 +100,24 @@ public class ColorUtilTest extends AndroidTestCase { canvas.drawRect(0, 0, 2, 2, p); color = mColorUtils.generateAlertColorFromDrawable(bitmapDrawable); assertEquals(color, Color.WHITE); + color = mColorUtils.generateAlertColorFromDrawable( + getColorDrawableFromBitmapDrawable(bitmapDrawable)); + assertEquals(color, Color.WHITE); assertEquals(bitmap.isRecycled(), false); bitmap.recycle(); } + + private ColorDrawable getColorDrawableFromBitmapDrawable(final BitmapDrawable bitmapDrawable) { + ColorDrawable colorDrawable = Mockito.mock(ColorDrawable.class); + Mockito.doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + Canvas canvas = (Canvas) invocation.getArguments()[0]; + bitmapDrawable.draw(canvas); + return null; + } + }).when(colorDrawable).draw(Mockito.any(Canvas.class)); + return colorDrawable; + } } |