mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 01:08:50 -06:00
Add tests for HabitCardView
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 5.0 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 5.4 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 5.1 KiB |
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* 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.activities.habits.list.views;
|
||||
|
||||
import android.support.test.runner.*;
|
||||
import android.test.suitebuilder.annotation.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.isoron.uhabits.utils.*;
|
||||
import org.junit.*;
|
||||
import org.junit.runner.*;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@MediumTest
|
||||
public class HabitCardViewTest extends BaseViewTest
|
||||
{
|
||||
private HabitCardView view;
|
||||
|
||||
public static final String PATH = "habits/list/HabitCardView/";
|
||||
|
||||
private HabitCardView.Controller controller;
|
||||
|
||||
private Habit habit;
|
||||
|
||||
@Override
|
||||
public void setUp()
|
||||
{
|
||||
super.setUp();
|
||||
setTheme(R.style.AppBaseTheme);
|
||||
|
||||
habit = fixtures.createLongHabit();
|
||||
CheckmarkList checkmarks = habit.getCheckmarks();
|
||||
|
||||
long today = DateUtils.getStartOfToday();
|
||||
long day = DateUtils.millisecondsInOneDay;
|
||||
int[] values = checkmarks.getValues(today - 5 * day, today);
|
||||
|
||||
controller = mock(HabitCardView.Controller.class);
|
||||
|
||||
view = new HabitCardView(targetContext);
|
||||
view.setHabit(habit);
|
||||
view.setCheckmarkValues(values);
|
||||
view.setSelected(false);
|
||||
view.setScore(habit.getScores().getTodayValue());
|
||||
view.setController(controller);
|
||||
measureView(view, dpToPixels(400), dpToPixels(50));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRender() throws Exception
|
||||
{
|
||||
assertRenders(view, PATH + "render.png");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRender_selected() throws Exception
|
||||
{
|
||||
view.setSelected(true);
|
||||
measureView(view, dpToPixels(400), dpToPixels(50));
|
||||
assertRenders(view, PATH + "render_selected.png");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChangeModel() throws Exception
|
||||
{
|
||||
habit.setName("Wake up early");
|
||||
habit.setColor(2);
|
||||
habit.getObservable().notifyListeners();
|
||||
assertRenders(view, PATH + "render_changed.png");
|
||||
}
|
||||
}
|
||||
@@ -154,6 +154,7 @@ public class CheckmarkPanelView extends LinearLayout
|
||||
CheckmarkButtonView buttonView)
|
||||
{
|
||||
if (controller == null) return;
|
||||
if(!(getContext() instanceof ListHabitsActivity)) return;
|
||||
|
||||
ListHabitsActivity activity = (ListHabitsActivity) getContext();
|
||||
CheckmarkButtonControllerFactory buttonControllerFactory = activity
|
||||
|
||||
@@ -43,6 +43,16 @@ public class HabitCardView extends FrameLayout
|
||||
implements ModelObservable.Listener
|
||||
{
|
||||
|
||||
private static final String EDIT_MODE_HABITS[] = {
|
||||
"Wake up early",
|
||||
"Wash dishes",
|
||||
"Exercise",
|
||||
"Meditate",
|
||||
"Play guitar",
|
||||
"Wash clothes",
|
||||
"Get a haircut"
|
||||
};
|
||||
|
||||
@BindView(R.id.checkmarkPanel)
|
||||
CheckmarkPanelView checkmarkPanel;
|
||||
|
||||
@@ -105,15 +115,6 @@ public class HabitCardView extends FrameLayout
|
||||
postInvalidate();
|
||||
}
|
||||
|
||||
private void refresh()
|
||||
{
|
||||
int color = getActiveColor(habit);
|
||||
label.setText(habit.getName());
|
||||
label.setTextColor(color);
|
||||
scoreRing.setColor(color);
|
||||
checkmarkPanel.setColor(color);
|
||||
}
|
||||
|
||||
public void setScore(int score)
|
||||
{
|
||||
float percentage = (float) score / Score.MAX_VALUE;
|
||||
@@ -141,11 +142,6 @@ public class HabitCardView extends FrameLayout
|
||||
triggerRipple(x, y);
|
||||
}
|
||||
|
||||
protected void detachFromHabit()
|
||||
{
|
||||
if (habit != null) habit.getObservable().removeListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDetachedFromWindow()
|
||||
{
|
||||
@@ -158,6 +154,11 @@ public class HabitCardView extends FrameLayout
|
||||
if (habit != null) habit.getObservable().addListener(this);
|
||||
}
|
||||
|
||||
private void detachFromHabit()
|
||||
{
|
||||
if (habit != null) habit.getObservable().removeListener(this);
|
||||
}
|
||||
|
||||
private int getActiveColor(Habit habit)
|
||||
{
|
||||
int mediumContrastColor = res.getColor(R.attr.mediumContrastTextColor);
|
||||
@@ -189,27 +190,12 @@ public class HabitCardView extends FrameLayout
|
||||
@SuppressLint("SetTextI18n")
|
||||
private void initEditMode()
|
||||
{
|
||||
String habits[] = {
|
||||
"Wake up early",
|
||||
"Wash dishes",
|
||||
"Exercise",
|
||||
"Meditate",
|
||||
"Play guitar",
|
||||
"Wash clothes",
|
||||
"Get a haircut"
|
||||
};
|
||||
|
||||
Random rand = new Random();
|
||||
int color = ColorUtils.getAndroidTestColor(rand.nextInt(10));
|
||||
int[] values = {
|
||||
rand.nextInt(3),
|
||||
rand.nextInt(3),
|
||||
rand.nextInt(3),
|
||||
rand.nextInt(3),
|
||||
rand.nextInt(3)
|
||||
};
|
||||
int[] values = new int[5];
|
||||
for (int i = 0; i < 5; i++) values[i] = rand.nextInt(3);
|
||||
|
||||
label.setText(habits[rand.nextInt(habits.length)]);
|
||||
label.setText(EDIT_MODE_HABITS[rand.nextInt(EDIT_MODE_HABITS.length)]);
|
||||
label.setTextColor(color);
|
||||
scoreRing.setColor(color);
|
||||
scoreRing.setPercentage(rand.nextFloat());
|
||||
@@ -217,6 +203,16 @@ public class HabitCardView extends FrameLayout
|
||||
checkmarkPanel.setCheckmarkValues(values);
|
||||
}
|
||||
|
||||
private void refresh()
|
||||
{
|
||||
int color = getActiveColor(habit);
|
||||
label.setText(habit.getName());
|
||||
label.setTextColor(color);
|
||||
scoreRing.setColor(color);
|
||||
checkmarkPanel.setColor(color);
|
||||
postInvalidate();
|
||||
}
|
||||
|
||||
private void triggerRipple(final float x, final float y)
|
||||
{
|
||||
final Drawable background = innerFrame.getBackground();
|
||||
|
||||
Reference in New Issue
Block a user