mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Fix view tests
This commit is contained in:
@@ -27,7 +27,6 @@ import org.isoron.uhabits.helpers.DateHelper;
|
|||||||
import org.isoron.uhabits.tasks.BaseTask;
|
import org.isoron.uhabits.tasks.BaseTask;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
public class BaseTest
|
public class BaseTest
|
||||||
@@ -55,6 +54,6 @@ public class BaseTest
|
|||||||
|
|
||||||
protected void waitForAsyncTasks() throws InterruptedException, TimeoutException
|
protected void waitForAsyncTasks() throws InterruptedException, TimeoutException
|
||||||
{
|
{
|
||||||
BaseTask.waitForTasks(30, TimeUnit.SECONDS);
|
BaseTask.waitForTasks(30000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ public class CheckmarkViewTest extends ViewTest
|
|||||||
habit = HabitFixtures.createShortHabit();
|
habit = HabitFixtures.createShortHabit();
|
||||||
view = new CheckmarkView(targetContext);
|
view = new CheckmarkView(targetContext);
|
||||||
view.setHabit(habit);
|
view.setHabit(habit);
|
||||||
|
refreshData(view);
|
||||||
measureView(dpToPixels(100), dpToPixels(200), view);
|
measureView(dpToPixels(100), dpToPixels(200), view);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ public class HabitFrequencyViewTest extends ViewTest
|
|||||||
|
|
||||||
view = new HabitFrequencyView(targetContext);
|
view = new HabitFrequencyView(targetContext);
|
||||||
view.setHabit(habit);
|
view.setHabit(habit);
|
||||||
|
refreshData(view);
|
||||||
measureView(dpToPixels(300), dpToPixels(100), view);
|
measureView(dpToPixels(300), dpToPixels(100), view);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -53,6 +53,7 @@ public class HabitHistoryViewTest extends ViewTest
|
|||||||
|
|
||||||
view = new HabitHistoryView(targetContext);
|
view = new HabitHistoryView(targetContext);
|
||||||
view.setHabit(habit);
|
view.setHabit(habit);
|
||||||
|
refreshData(view);
|
||||||
measureView(dpToPixels(300), dpToPixels(100), view);
|
measureView(dpToPixels(300), dpToPixels(100), view);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ public class HabitScoreViewTest extends ViewTest
|
|||||||
view = new HabitScoreView(targetContext);
|
view = new HabitScoreView(targetContext);
|
||||||
view.setHabit(habit);
|
view.setHabit(habit);
|
||||||
view.setBucketSize(7);
|
view.setBucketSize(7);
|
||||||
|
refreshData(view);
|
||||||
measureView(dpToPixels(300), dpToPixels(100), view);
|
measureView(dpToPixels(300), dpToPixels(100), view);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ package org.isoron.uhabits.unit.views;
|
|||||||
|
|
||||||
import android.support.test.runner.AndroidJUnit4;
|
import android.support.test.runner.AndroidJUnit4;
|
||||||
import android.test.suitebuilder.annotation.SmallTest;
|
import android.test.suitebuilder.annotation.SmallTest;
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
import org.isoron.uhabits.models.Habit;
|
import org.isoron.uhabits.models.Habit;
|
||||||
import org.isoron.uhabits.unit.HabitFixtures;
|
import org.isoron.uhabits.unit.HabitFixtures;
|
||||||
@@ -45,8 +44,10 @@ public class HabitStreakViewTest extends ViewTest
|
|||||||
Habit habit = HabitFixtures.createLongHabit();
|
Habit habit = HabitFixtures.createLongHabit();
|
||||||
|
|
||||||
view = new HabitStreakView(targetContext);
|
view = new HabitStreakView(targetContext);
|
||||||
view.setHabit(habit);
|
|
||||||
measureView(dpToPixels(300), dpToPixels(100), view);
|
measureView(dpToPixels(300), dpToPixels(100), view);
|
||||||
|
|
||||||
|
view.setHabit(habit);
|
||||||
|
refreshData(view);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -66,6 +67,8 @@ public class HabitStreakViewTest extends ViewTest
|
|||||||
public void render_withSmallSize() throws Throwable
|
public void render_withSmallSize() throws Throwable
|
||||||
{
|
{
|
||||||
measureView(dpToPixels(100), dpToPixels(100), view);
|
measureView(dpToPixels(100), dpToPixels(100), view);
|
||||||
|
refreshData(view);
|
||||||
|
|
||||||
assertRenders(view, "HabitStreakView/renderSmallSize.png");
|
assertRenders(view, "HabitStreakView/renderSmallSize.png");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,8 @@ import android.view.View;
|
|||||||
|
|
||||||
import org.isoron.uhabits.BaseTest;
|
import org.isoron.uhabits.BaseTest;
|
||||||
import org.isoron.uhabits.helpers.UIHelper;
|
import org.isoron.uhabits.helpers.UIHelper;
|
||||||
|
import org.isoron.uhabits.tasks.BaseTask;
|
||||||
|
import org.isoron.uhabits.views.HabitDataView;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
@@ -190,4 +192,26 @@ public class ViewTest extends BaseTest
|
|||||||
view.onSingleTapUp(e);
|
view.onSingleTapUp(e);
|
||||||
e.recycle();
|
e.recycle();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void refreshData(final HabitDataView view)
|
||||||
|
{
|
||||||
|
new BaseTask()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
protected Void doInBackground(Void... params)
|
||||||
|
{
|
||||||
|
view.refreshData();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}.execute();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
waitForAsyncTasks();
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
throw new RuntimeException("Time out");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,13 +21,10 @@ package org.isoron.uhabits.tasks;
|
|||||||
|
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
|
|
||||||
import java.util.concurrent.CountDownLatch;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
public class BaseTask extends AsyncTask<Void, Void, Void>
|
public class BaseTask extends AsyncTask<Void, Void, Void>
|
||||||
{
|
{
|
||||||
private static CountDownLatch latch;
|
|
||||||
private static int activeTaskCount;
|
private static int activeTaskCount;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -40,22 +37,27 @@ public class BaseTask extends AsyncTask<Void, Void, Void>
|
|||||||
protected void onPreExecute()
|
protected void onPreExecute()
|
||||||
{
|
{
|
||||||
activeTaskCount++;
|
activeTaskCount++;
|
||||||
latch = new CountDownLatch(activeTaskCount);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPostExecute(Void aVoid)
|
protected void onPostExecute(Void aVoid)
|
||||||
{
|
{
|
||||||
activeTaskCount--;
|
activeTaskCount--;
|
||||||
latch.countDown();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void waitForTasks(long timeout, TimeUnit unit)
|
public static void waitForTasks(long timeout)
|
||||||
throws TimeoutException, InterruptedException
|
throws TimeoutException, InterruptedException
|
||||||
{
|
{
|
||||||
if(activeTaskCount == 0) return;
|
int poolInterval = 100;
|
||||||
|
|
||||||
boolean successful = latch.await(timeout, unit);
|
while(timeout > 0)
|
||||||
if(!successful) throw new TimeoutException();
|
{
|
||||||
|
if(activeTaskCount == 0) return;
|
||||||
|
|
||||||
|
timeout -= poolInterval;
|
||||||
|
Thread.sleep(poolInterval);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new TimeoutException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ import org.isoron.uhabits.R;
|
|||||||
import org.isoron.uhabits.helpers.ColorHelper;
|
import org.isoron.uhabits.helpers.ColorHelper;
|
||||||
import org.isoron.uhabits.models.Habit;
|
import org.isoron.uhabits.models.Habit;
|
||||||
|
|
||||||
public class CheckmarkView extends View
|
public class CheckmarkView extends View implements HabitDataView
|
||||||
{
|
{
|
||||||
private Paint pCard;
|
private Paint pCard;
|
||||||
private Paint pIcon;
|
private Paint pIcon;
|
||||||
|
|||||||
@@ -401,6 +401,7 @@ public class HabitHistoryView extends ScrollableDataView implements HabitDataVie
|
|||||||
protected void onPostExecute(Void aVoid)
|
protected void onPostExecute(Void aVoid)
|
||||||
{
|
{
|
||||||
invalidate();
|
invalidate();
|
||||||
|
super.onPostExecute(null);
|
||||||
}
|
}
|
||||||
}.execute();
|
}.execute();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user