Testing image processing routines with robolectric

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                    // start x
        , 0                    // start y
        , image.getWidth()     // width
        , image.getHeight()    // height
        , pixels               // pixel array
        , 0                    // offset
        , image.getWidth() );  // stride

    final Bitmap result = Bitmap.createBitmap
        ( pixels                      // pixel array
        , image.getWidth()            // width
        , image.getHeight()           // height
        , Bitmap.Config.ARGB_8888 );  // format

    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()               // width
        , bitmap.getHeight()              // height
        , BufferedImage.TYPE_INT_ARGB );  // channels

    final int[] pixels = new int[ image.getWidth() * image.getHeight() ];

    bitmap.getPixels
        ( pixels                // pixel array
        , 0                     // offset
        , image.getWidth()      // stride
        , 0                     // start x
        , 0                     // start y
        , image.getWidth()      // width
        , image.getHeight() );  // height

    image.setRGB
        ( 0                    // start x
        , 0                    // start y
        , image.getWidth()     // width
        , image.getHeight()    // height
        , pixels               // pixel array
        , 0                    // offset
        , image.getWidth() );  // stride

    ImageIO.write( image, format, file );
}

out.png in.png. , , in.png , out.png . , -.

: ? robolectric, ?

. pixels 0xFFFFFFFF Bitmap, . . , - .

robolectric 2.2.

+4
1

, , , Android. :

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.test.ActivityInstrumentationTestCase2;
import android.test.UiThreadTest;

public class InvadersFromSpace extends ActivityInstrumentationTestCase2<ColorPickerActivity> {

    public InvadersFromSpace() {
        super(ColorPickerActivity.class);
    }

    ColorPickerActivity mainActivity;
    Bitmap forpixels;

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        mainActivity = getActivity();
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inScaled = false;
        forpixels = BitmapFactory.decodeFile("/storage/sdcard0/photos/IMG_20130815_194838.jpg");
    }

    @UiThreadTest
    public void testImageNormallyLoaded() {
        assertTrue(forpixels.getWidth() == 1716);
        assertTrue(forpixels.getHeight() == 1709);
    }

    @UiThreadTest
    public void testFlagDarkBlue() {
        CompareFewColors(forpixels, 772, 690, new String[]{"DarkMidnightBlue","Charcoal","CoolBlack"}, true);
        CompareFewColors(forpixels, 991, 1269, new String[]{"Arsenic","DarkMidnightBlue","Charcoal"}, false);
    }
}

, 2 , : 1) ActivityInstrumentationTestCase2 2) @UiThreadTest. ( ui /mocks/ )

+2

Source: https://habr.com/ru/post/1524828/


All Articles