View tests: adjust similarity cutoff

pull/286/head
Alinson S. Xavier 9 years ago
parent 6e0393f611
commit 704854fdf1

@ -21,6 +21,7 @@ package org.isoron.uhabits;
import android.appwidget.*; import android.appwidget.*;
import android.content.*; import android.content.*;
import android.content.res.*;
import android.os.*; import android.os.*;
import android.support.annotation.*; import android.support.annotation.*;
import android.support.test.*; import android.support.test.*;
@ -79,6 +80,7 @@ public class BaseAndroidTest
DateUtils.setFixedLocalTime(FIXED_LOCAL_TIME); DateUtils.setFixedLocalTime(FIXED_LOCAL_TIME);
setTheme(R.style.AppBaseTheme); setTheme(R.style.AppBaseTheme);
setLocale("en", "US");
component = DaggerAndroidTestComponent component = DaggerAndroidTestComponent
.builder() .builder()
@ -115,6 +117,15 @@ public class BaseAndroidTest
assertTrue(latch.await(60, TimeUnit.SECONDS)); assertTrue(latch.await(60, TimeUnit.SECONDS));
} }
protected void setLocale(@NonNull String language, @NonNull String country)
{
Locale locale = new Locale(language, country);
Locale.setDefault(locale);
Resources res = targetContext.getResources();
Configuration config = res.getConfiguration();
config.setLocale(locale);
}
protected void setTheme(@StyleRes int themeId) protected void setTheme(@StyleRes int themeId)
{ {
targetContext.setTheme(themeId); targetContext.setTheme(themeId);

@ -20,7 +20,6 @@
package org.isoron.uhabits; package org.isoron.uhabits;
import android.graphics.*; import android.graphics.*;
import android.os.*;
import android.support.annotation.*; import android.support.annotation.*;
import android.view.*; import android.view.*;
import android.widget.*; import android.widget.*;
@ -31,33 +30,32 @@ import org.isoron.uhabits.widgets.*;
import java.io.*; import java.io.*;
import java.util.*; import java.util.*;
import static android.os.Build.VERSION.*;
import static android.os.Build.VERSION_CODES.*;
import static android.view.View.MeasureSpec.*; import static android.view.View.MeasureSpec.*;
import static junit.framework.Assert.*; import static junit.framework.Assert.*;
public class BaseViewTest extends BaseAndroidTest public class BaseViewTest extends BaseAndroidTest
{ {
protected static final double DEFAULT_SIMILARITY_CUTOFF = 0.001; double similarityCutoff = 0.00150;
private double similarityCutoff;
@Override @Override
public void setUp() public void setUp()
{ {
super.setUp(); super.setUp();
similarityCutoff = DEFAULT_SIMILARITY_CUTOFF; if (SDK_INT < LOLLIPOP) similarityCutoff = 0.00175;
} }
protected void assertRenders(View view, String expectedImagePath) protected void assertRenders(View view, String expectedImagePath)
throws IOException throws IOException
{ {
StringBuilder errorMessage = new StringBuilder(); expectedImagePath = "views/" + expectedImagePath;
expectedImagePath = getVersionedViewAssetPath(expectedImagePath);
if (view.isLayoutRequested()) measureView(view, view.getMeasuredWidth(), if (view.isLayoutRequested()) measureView(view, view.getMeasuredWidth(),
view.getMeasuredHeight()); view.getMeasuredHeight());
view.setDrawingCacheEnabled(true); view.setDrawingCacheEnabled(true);
view.buildDrawingCache(); view.buildDrawingCache();
Bitmap actual = view.getDrawingCache(); Bitmap actual = view.getDrawingCache();
Bitmap expected = getBitmapFromAssets(expectedImagePath); Bitmap expected = getBitmapFromAssets(expectedImagePath);
@ -66,24 +64,14 @@ public class BaseViewTest extends BaseAndroidTest
Bitmap scaledExpected = Bitmap scaledExpected =
Bitmap.createScaledBitmap(expected, width, height, true); Bitmap.createScaledBitmap(expected, width, height, true);
boolean similarEnough = true;
double distance = distance(actual, scaledExpected); double distance = distance(actual, scaledExpected);
if (distance > similarityCutoff) if (distance > similarityCutoff)
{
similarEnough = false;
errorMessage.append(String.format(
"Rendered image has wrong histogram (distance=%f). ",
distance));
}
if (!similarEnough)
{ {
saveBitmap(expectedImagePath, ".expected", scaledExpected); saveBitmap(expectedImagePath, ".expected", scaledExpected);
String path = saveBitmap(expectedImagePath, "", actual); String path = saveBitmap(expectedImagePath, "", actual);
errorMessage.append( fail(String.format("Image differs from expected " +
String.format("Actual rendered image saved to %s", path)); "(distance=%f). Actual rendered " +
fail(errorMessage.toString()); "image saved to %s", distance, path));
} }
expected.recycle(); expected.recycle();
@ -119,11 +107,6 @@ public class BaseViewTest extends BaseAndroidTest
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
} }
protected void setSimilarityCutoff(double similarityCutoff)
{
this.similarityCutoff = similarityCutoff;
}
protected void skipAnimation(View view) protected void skipAnimation(View view)
{ {
ViewPropertyAnimator animator = view.animate(); ViewPropertyAnimator animator = view.animate();
@ -131,27 +114,20 @@ public class BaseViewTest extends BaseAndroidTest
animator.start(); animator.start();
} }
protected void tap(GestureDetector.OnGestureListener view, int x, int y) private int[] colorToArgb(int c1)
throws InterruptedException
{
long now = SystemClock.uptimeMillis();
MotionEvent e =
MotionEvent.obtain(now, now, MotionEvent.ACTION_UP, dpToPixels(x),
dpToPixels(y), 0);
view.onSingleTapUp(e);
e.recycle();
}
private Bitmap getBitmapFromAssets(String path) throws IOException
{ {
InputStream stream = testContext.getAssets().open(path); return new int[]{
return BitmapFactory.decodeStream(stream); (c1 >> 24) & 0xff, //alpha
(c1 >> 16) & 0xff, //red
(c1 >> 8) & 0xff, //green
(c1) & 0xff //blue
};
} }
private double distance(Bitmap b1, Bitmap b2) private double distance(Bitmap b1, Bitmap b2)
{ {
if(b1.getWidth() != b2.getWidth()) return 1.0; if (b1.getWidth() != b2.getWidth()) return 1.0;
if(b1.getHeight() != b2.getHeight()) return 1.0; if (b1.getHeight() != b2.getHeight()) return 1.0;
Random random = new Random(); Random random = new Random();
@ -160,7 +136,7 @@ public class BaseViewTest extends BaseAndroidTest
{ {
for (int y = 0; y < b1.getHeight(); y++) for (int y = 0; y < b1.getHeight(); y++)
{ {
if(random.nextInt(4) != 0) continue; if (random.nextInt(4) != 0) continue;
int[] argb1 = colorToArgb(b1.getPixel(x, y)); int[] argb1 = colorToArgb(b1.getPixel(x, y));
int[] argb2 = colorToArgb(b2.getPixel(x, y)); int[] argb2 = colorToArgb(b2.getPixel(x, y));
@ -175,44 +151,18 @@ public class BaseViewTest extends BaseAndroidTest
return distance; return distance;
} }
private int[] colorToArgb(int c1) private Bitmap getBitmapFromAssets(String path) throws IOException
{
return new int[]{
(c1 >> 24) & 0xff, //alpha
(c1 >> 16) & 0xff, //red
(c1 >> 8) & 0xff, //green
(c1) & 0xff //blue
};
}
private String getVersionedViewAssetPath(String path)
{ {
String result = null; InputStream stream = testContext.getAssets().open(path);
return BitmapFactory.decodeStream(stream);
if (android.os.Build.VERSION.SDK_INT >= 21)
{
try
{
String vpath = "views-v21/" + path;
testContext.getAssets().open(vpath);
result = vpath;
}
catch (IOException e)
{
// ignored
}
}
if (result == null) result = "views/" + path;
return result;
} }
private String saveBitmap(String filename, String suffix, Bitmap bitmap) private String saveBitmap(String filename, String suffix, Bitmap bitmap)
throws IOException throws IOException
{ {
File dir = FileUtils.getSDCardDir("test-screenshots"); File dir = FileUtils.getSDCardDir("test-screenshots");
if (dir == null) dir = FileUtils.getFilesDir(targetContext,"test-screenshots"); if (dir == null)
dir = FileUtils.getFilesDir(targetContext, "test-screenshots");
if (dir == null) throw new RuntimeException( if (dir == null) throw new RuntimeException(
"Could not find suitable dir for screenshots"); "Could not find suitable dir for screenshots");

@ -43,12 +43,9 @@ public class CheckmarkButtonViewTest extends BaseViewTest
public void setUp() public void setUp()
{ {
super.setUp(); super.setUp();
setSimilarityCutoff(0.015f);
view = new CheckmarkButtonView(targetContext); view = new CheckmarkButtonView(targetContext);
view.setValue(Checkmark.UNCHECKED); view.setValue(Checkmark.UNCHECKED);
view.setColor(ColorUtils.getAndroidTestColor(5)); view.setColor(ColorUtils.getAndroidTestColor(5));
measureView(view, dpToPixels(48), dpToPixels(48)); measureView(view, dpToPixels(48), dpToPixels(48));
} }

@ -47,7 +47,6 @@ public class CheckmarkPanelViewTest extends BaseViewTest
public void setUp() public void setUp()
{ {
super.setUp(); super.setUp();
setSimilarityCutoff(0.03f);
prefs.setShouldReverseCheckmarks(false); prefs.setShouldReverseCheckmarks(false);
Habit habit = fixtures.createEmptyHabit(); Habit habit = fixtures.createEmptyHabit();

@ -50,8 +50,6 @@ public class HeaderViewTest extends BaseViewTest
public void setUp() public void setUp()
{ {
super.setUp(); super.setUp();
setSimilarityCutoff(0);
prefs = mock(Preferences.class); prefs = mock(Preferences.class);
midnightTimer = mock(MidnightTimer.class); midnightTimer = mock(MidnightTimer.class);
view = new HeaderView(targetContext, prefs, midnightTimer); view = new HeaderView(targetContext, prefs, midnightTimer);

@ -47,10 +47,8 @@ public class NumberButtonViewTest extends BaseViewTest
public void setUp() public void setUp()
{ {
super.setUp(); super.setUp();
setSimilarityCutoff(0.015f);
view = new NumberButtonView(targetContext); view = new NumberButtonView(targetContext);
view.setUnit("steps"); view.setUnit("steps");
view.setThreshold(100.0); view.setThreshold(100.0);
view.setColor(ColorUtils.getAndroidTestColor(5)); view.setColor(ColorUtils.getAndroidTestColor(5));

Loading…
Cancel
Save