For my Android application, I need several image processing procedures that I want to test with robolectric. These procedures work on sites Bitmap. To do this, I need to upload test images. To my great disappointment, I found that robolectric does not support the Android class angle BitmapFactory. They speak:
Robolectric is not here to run your entire JVM application, it's just to provide enough functionality so you can test your code.
So, I decided to write my own bootloader Bitmap:
public static Bitmap loadFromFile( File file ) throws IOException
{
final BufferedImage image = ImageIO.read( file );
final int[] pixels = new int[ image.getWidth() * image.getHeight() ];
image.getRGB
( 0
, 0
, image.getWidth()
, image.getHeight()
, pixels
, 0
, image.getWidth() );
final Bitmap result = Bitmap.createBitmap
( pixels
, image.getWidth()
, image.getHeight()
, Bitmap.Config.ARGB_8888 );
return result;
}
I also wanted to check if the bootloader was working, so I wrote a short test procedure shown below.
@Test
public void test() throws Exception
{
final Bitmap source = loadFromFile( new File( "in.png" ) );
saveToFile( source, "png", new File( "out.png" ) );
}
:
public static void saveToFile
( Bitmap bitmap
, String format, File file ) throws IOException
{
final BufferedImage image = new BufferedImage
( bitmap.getWidth()
, bitmap.getHeight()
, BufferedImage.TYPE_INT_ARGB );
final int[] pixels = new int[ image.getWidth() * image.getHeight() ];
bitmap.getPixels
( pixels
, 0
, image.getWidth()
, 0
, 0
, image.getWidth()
, image.getHeight() );
image.setRGB
( 0
, 0
, image.getWidth()
, image.getHeight()
, pixels
, 0
, image.getWidth() );
ImageIO.write( image, format, file );
}
out.png in.png. , , in.png , out.png . , -.
: ? robolectric, ?
. pixels 0xFFFFFFFF Bitmap, . . , - .
robolectric 2.2.