Skip to content

Instantly share code, notes, and snippets.

@acontal
acontal / assertBitmapsAreSimilar.java
Created May 10, 2014 10:05
[android] Assert that two bitmaps are similar
public static void assertBitmapsAreSimilar(Bitmap expectedBitmap, Bitmap actualBitmap, double tolerance) {
Bitmap reducedExpectedBitmap = Bitmap.createScaledBitmap(expectedBitmap, 32, 32, true);
Bitmap reducedActualBitmap = Bitmap.createScaledBitmap(actualBitmap, 32, 32, true);
int cumulatedColorDistance = 0;
for (int x = 0 ; x < reducedExpectedBitmap.getWidth() ; x++) {
for (int y = 0 ; y<reducedExpectedBitmap.getHeight() ; y++) {
int expectedColor = reducedExpectedBitmap.getPixel(x, y);
int actualColor = reducedActualBitmap.getPixel(x, y);
@acontal
acontal / EspressoViewCountAssertion.java
Created February 16, 2014 18:14
[android][espresso] Assert that a View contains a given number of matching views.
/**
* Sugar for has(int, isAssignableFrom(Class)).
*
* Example: onView(rootView).check(has(3, EditText.class);
*/
public static ViewAssertion has(final int expectedCount, Class<? extends View> clazz) {
return has(expectedCount, isAssignableFrom(clazz));
}
/**
@acontal
acontal / SaveAndroidCameraPreviewImageAsJpeg.java
Created January 19, 2014 15:35
[android] Save the raw preview image provided by the Camera's preview as a JPEG file.
// Input: byte[] data, int width, int height, File jpegFile
YuvImage yuvImage = new YuvImage(data, ImageFormat.NV21, width, height, null);
FileOutputStream jepgFileOS = new FileOutputStream(jpegFile);
yuvimage.compressToJpeg(new Rect(0, 0, width, height), 95, jpegFileOS);
jpegFileOS.close();
@acontal
acontal / CopyAndroidAsset.java
Last active January 3, 2016 12:29
[android] Copy asset to file system
// Dependency: Guava
// From an Activity, assets = getAssets();
// From a test, assets = getInstrumentation().getContext().getAssets();
ByteStreams.copy(assets.open(assetFileName), new FileOutputStream(targetPath));
@acontal
acontal / JettySilentLogger.java
Created November 23, 2013 12:09
Mute Jetty 8 logging.
/**
* Usage: <code>org.eclipse.jetty.util.log.Log.setLog(new JettySilentLogger());</code>
*/
public class JettySilentLogger implements org.eclipse.jetty.util.log.Logger {
@Override public void debug(Throwable arg0) {}
@Override public void debug(String arg0, Object... arg1) {}
@Override public void debug(String arg0, Throwable arg1) {}
@Override public Logger getLogger(String arg0) { return this; }
@Override public String getName() { return this.getName(); }
import java.lang.reflect.Type;
import android.util.Base64;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;