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.content.*;
import android.content.res.*;
import android.os.*;
import android.support.annotation.*;
import android.support.test.*;
@ -79,6 +80,7 @@ public class BaseAndroidTest
DateUtils.setFixedLocalTime(FIXED_LOCAL_TIME);
setTheme(R.style.AppBaseTheme);
setLocale("en", "US");
component = DaggerAndroidTestComponent
.builder()
@ -115,6 +117,15 @@ public class BaseAndroidTest
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)
{
targetContext.setTheme(themeId);

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

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

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

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

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

Loading…
Cancel
Save