mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Add tests for NumberView
This commit is contained in:
BIN
app/src/androidTest/assets/views/NumberView/render.png
Normal file
BIN
app/src/androidTest/assets/views/NumberView/render.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 5.2 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 3.9 KiB |
BIN
app/src/androidTest/assets/views/NumberView/renderLongLabel.png
Normal file
BIN
app/src/androidTest/assets/views/NumberView/renderLongLabel.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 13 KiB |
@@ -0,0 +1,77 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.unit.views;
|
||||||
|
|
||||||
|
import android.support.test.runner.AndroidJUnit4;
|
||||||
|
import android.test.suitebuilder.annotation.SmallTest;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.R;
|
||||||
|
import org.isoron.uhabits.helpers.ColorHelper;
|
||||||
|
import org.isoron.uhabits.views.NumberView;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@RunWith(AndroidJUnit4.class)
|
||||||
|
@SmallTest
|
||||||
|
public class NumberViewTest extends ViewTest
|
||||||
|
{
|
||||||
|
private NumberView view;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup()
|
||||||
|
{
|
||||||
|
super.setup();
|
||||||
|
|
||||||
|
view = new NumberView(targetContext);
|
||||||
|
view.setLabel("Hello world");
|
||||||
|
view.setNumber(31);
|
||||||
|
view.setColor(ColorHelper.palette[0]);
|
||||||
|
measureView(dpToPixels(100), dpToPixels(100), view);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void render_base() throws IOException
|
||||||
|
{
|
||||||
|
assertRenders(view, "NumberView/render.png");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void render_withLongLabel() throws IOException
|
||||||
|
{
|
||||||
|
view.setLabel("The quick brown fox jumps over the lazy fox");
|
||||||
|
|
||||||
|
measureView(dpToPixels(100), dpToPixels(100), view);
|
||||||
|
assertRenders(view, "NumberView/renderLongLabel.png");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void render_withDifferentParams() throws IOException
|
||||||
|
{
|
||||||
|
view.setNumber(500);
|
||||||
|
view.setColor(ColorHelper.palette[5]);
|
||||||
|
view.setTextSize(targetContext.getResources().getDimension(R.dimen.tinyTextSize));
|
||||||
|
|
||||||
|
measureView(dpToPixels(200), dpToPixels(200), view);
|
||||||
|
assertRenders(view, "NumberView/renderDifferentParams.png");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -23,7 +23,6 @@ import android.content.Context;
|
|||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
import android.graphics.Typeface;
|
import android.graphics.Typeface;
|
||||||
import android.os.Vibrator;
|
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
import android.util.DisplayMetrics;
|
import android.util.DisplayMetrics;
|
||||||
@@ -71,26 +70,31 @@ public abstract class DialogHelper
|
|||||||
return prefs.getInt("launch_count", 0);
|
return prefs.getInt("launch_count", 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getAttribute(Context context, AttributeSet attrs, String name)
|
public static String getAttribute(Context context, AttributeSet attrs, String name,
|
||||||
|
String defaultValue)
|
||||||
{
|
{
|
||||||
int resId = attrs.getAttributeResourceValue(ISORON_NAMESPACE, name, 0);
|
int resId = attrs.getAttributeResourceValue(ISORON_NAMESPACE, name, 0);
|
||||||
|
|
||||||
if (resId != 0) return context.getResources().getString(resId);
|
if (resId != 0) return context.getResources().getString(resId);
|
||||||
else return attrs.getAttributeValue(ISORON_NAMESPACE, name);
|
|
||||||
|
String value = attrs.getAttributeValue(ISORON_NAMESPACE, name);
|
||||||
|
if(value != null) return value;
|
||||||
|
else return defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int getIntAttribute(Context context, AttributeSet attrs, String name)
|
public static int getIntAttribute(Context context, AttributeSet attrs, String name,
|
||||||
|
int defaultValue)
|
||||||
{
|
{
|
||||||
String number = getAttribute(context, attrs, name);
|
String number = getAttribute(context, attrs, name, null);
|
||||||
if(number != null) return Integer.parseInt(number);
|
if(number != null) return Integer.parseInt(number);
|
||||||
else return 0;
|
else return defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static float getFloatAttribute(Context context, AttributeSet attrs, String name)
|
public static float getFloatAttribute(Context context, AttributeSet attrs, String name,
|
||||||
|
float defaultValue)
|
||||||
{
|
{
|
||||||
String number = getAttribute(context, attrs, name);
|
String number = getAttribute(context, attrs, name, null);
|
||||||
if(number != null) return Float.parseFloat(number);
|
if(number != null) return Float.parseFloat(number);
|
||||||
else return 0;
|
else return defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static float dpToPixels(Context context, float dp)
|
public static float dpToPixels(Context context, float dp)
|
||||||
|
|||||||
@@ -31,13 +31,12 @@ import android.text.TextPaint;
|
|||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.R;
|
||||||
import org.isoron.uhabits.helpers.ColorHelper;
|
import org.isoron.uhabits.helpers.ColorHelper;
|
||||||
import org.isoron.uhabits.helpers.DialogHelper;
|
import org.isoron.uhabits.helpers.DialogHelper;
|
||||||
|
|
||||||
public class NumberView extends View
|
public class NumberView extends View
|
||||||
{
|
{
|
||||||
|
|
||||||
private int size;
|
|
||||||
private int color;
|
private int color;
|
||||||
private int number;
|
private int number;
|
||||||
private float labelMarginTop;
|
private float labelMarginTop;
|
||||||
@@ -48,19 +47,30 @@ public class NumberView extends View
|
|||||||
|
|
||||||
private int width;
|
private int width;
|
||||||
private int height;
|
private int height;
|
||||||
|
|
||||||
private float textSize;
|
private float textSize;
|
||||||
private float labelTextSize;
|
private float labelTextSize;
|
||||||
private float numberTextSize;
|
private float numberTextSize;
|
||||||
private StaticLayout numberLayout;
|
private StaticLayout numberLayout;
|
||||||
|
|
||||||
|
public NumberView(Context context)
|
||||||
|
{
|
||||||
|
super(context);
|
||||||
|
this.textSize = getResources().getDimension(R.dimen.regularTextSize);
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
|
||||||
public NumberView(Context context, AttributeSet attrs)
|
public NumberView(Context context, AttributeSet attrs)
|
||||||
{
|
{
|
||||||
super(context, attrs);
|
super(context, attrs);
|
||||||
|
|
||||||
this.label = DialogHelper.getAttribute(context, attrs, "label");
|
this.textSize = getResources().getDimension(R.dimen.regularTextSize);
|
||||||
this.number = DialogHelper.getIntAttribute(context, attrs, "number");
|
|
||||||
this.textSize = DialogHelper.getFloatAttribute(context, attrs, "textSize");
|
this.label = DialogHelper.getAttribute(context, attrs, "label", "Number");
|
||||||
this.textSize = DialogHelper.spToPixels(getContext(), textSize);
|
this.number = DialogHelper.getIntAttribute(context, attrs, "number", 0);
|
||||||
|
this.textSize = DialogHelper.getFloatAttribute(context, attrs, "textSize",
|
||||||
|
getResources().getDimension(R.dimen.regularTextSize));
|
||||||
|
|
||||||
this.color = ColorHelper.palette[7];
|
this.color = ColorHelper.palette[7];
|
||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
@@ -75,12 +85,21 @@ public class NumberView extends View
|
|||||||
public void setLabel(String label)
|
public void setLabel(String label)
|
||||||
{
|
{
|
||||||
this.label = label;
|
this.label = label;
|
||||||
|
requestLayout();
|
||||||
|
postInvalidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setNumber(int number)
|
public void setNumber(int number)
|
||||||
{
|
{
|
||||||
this.number = number;
|
this.number = number;
|
||||||
createNumberLayout();
|
requestLayout();
|
||||||
|
postInvalidate();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTextSize(float textSize)
|
||||||
|
{
|
||||||
|
this.textSize = textSize;
|
||||||
|
requestLayout();
|
||||||
postInvalidate();
|
postInvalidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,7 +125,10 @@ public class NumberView extends View
|
|||||||
labelMarginTop = textSize * 0.35f;
|
labelMarginTop = textSize * 0.35f;
|
||||||
numberTextSize = textSize * 2.85f;
|
numberTextSize = textSize * 2.85f;
|
||||||
|
|
||||||
createNumberLayout();
|
pText.setTextSize(numberTextSize);
|
||||||
|
numberLayout = new StaticLayout(Integer.toString(number), pText, width,
|
||||||
|
Layout.Alignment.ALIGN_NORMAL, 1.0f, 0f, false);
|
||||||
|
|
||||||
int numberWidth = numberLayout.getWidth();
|
int numberWidth = numberLayout.getWidth();
|
||||||
int numberHeight = numberLayout.getHeight();
|
int numberHeight = numberLayout.getHeight();
|
||||||
|
|
||||||
@@ -122,20 +144,10 @@ public class NumberView extends View
|
|||||||
setMeasuredDimension(width, height);
|
setMeasuredDimension(width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createNumberLayout()
|
|
||||||
{
|
|
||||||
pText.setTextSize(numberTextSize);
|
|
||||||
numberLayout = new StaticLayout(Integer.toString(number), pText, width,
|
|
||||||
Layout.Alignment.ALIGN_NORMAL, 1.0f, 0f, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onDraw(Canvas canvas)
|
protected void onDraw(Canvas canvas)
|
||||||
{
|
{
|
||||||
super.onDraw(canvas);
|
super.onDraw(canvas);
|
||||||
|
|
||||||
pText.setColor(color);
|
|
||||||
pText.setTextSize(size * 0.4f);
|
|
||||||
rect.set(0, 0, width, height);
|
rect.set(0, 0, width, height);
|
||||||
|
|
||||||
canvas.save();
|
canvas.save();
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ package org.isoron.uhabits.views;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.R;
|
||||||
import org.isoron.uhabits.helpers.DateHelper;
|
import org.isoron.uhabits.helpers.DateHelper;
|
||||||
import org.isoron.uhabits.helpers.DialogHelper;
|
import org.isoron.uhabits.helpers.DialogHelper;
|
||||||
import org.isoron.uhabits.models.Habit;
|
import org.isoron.uhabits.models.Habit;
|
||||||
@@ -37,13 +38,12 @@ public class RepetitionCountView extends NumberView implements HabitDataView
|
|||||||
public RepetitionCountView(Context context, AttributeSet attrs)
|
public RepetitionCountView(Context context, AttributeSet attrs)
|
||||||
{
|
{
|
||||||
super(context, attrs);
|
super(context, attrs);
|
||||||
this.interval = DialogHelper.getIntAttribute(context, attrs, "interval");
|
this.interval = DialogHelper.getIntAttribute(context, attrs, "interval", 7);
|
||||||
|
int labelValue = DialogHelper.getIntAttribute(context, attrs, "labelValue", 7);
|
||||||
|
String labelFormat = DialogHelper.getAttribute(context, attrs, "labelFormat",
|
||||||
|
getResources().getString(R.string.last_x_days));
|
||||||
|
|
||||||
int labelValue = DialogHelper.getIntAttribute(context, attrs, "labelValue");
|
setLabel(String.format(labelFormat, labelValue));
|
||||||
String labelFormat = DialogHelper.getAttribute(context, attrs, "labelFormat");
|
|
||||||
|
|
||||||
if(labelValue > 0)
|
|
||||||
setLabel(String.format(labelFormat, labelValue));
|
|
||||||
|
|
||||||
refreshData();
|
refreshData();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,8 +63,8 @@ public class RingView extends View
|
|||||||
{
|
{
|
||||||
super(context, attrs);
|
super(context, attrs);
|
||||||
|
|
||||||
this.label = DialogHelper.getAttribute(context, attrs, "label");
|
this.label = DialogHelper.getAttribute(context, attrs, "label", "Label");
|
||||||
this.maxDiameter = DialogHelper.getFloatAttribute(context, attrs, "maxDiameter");
|
this.maxDiameter = DialogHelper.getFloatAttribute(context, attrs, "maxDiameter", 100);
|
||||||
this.maxDiameter = DialogHelper.dpToPixels(context, maxDiameter);
|
this.maxDiameter = DialogHelper.dpToPixels(context, maxDiameter);
|
||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user