From 677a643e5b7b6ac596a119e6dd6511e95900638a Mon Sep 17 00:00:00 2001 From: Alinson Xavier Date: Sat, 30 Apr 2016 19:13:36 -0400 Subject: [PATCH] Allow theme to be fixed during tests --- .../java/org/isoron/uhabits/BaseTest.java | 4 +-- .../unit/views/CheckmarkWidgetViewTest.java | 3 ++ .../org/isoron/uhabits/helpers/UIHelper.java | 34 ++++++++++++------- run_tests | 4 +-- 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/app/src/androidTest/java/org/isoron/uhabits/BaseTest.java b/app/src/androidTest/java/org/isoron/uhabits/BaseTest.java index a9a15d9ed..5515b5121 100644 --- a/app/src/androidTest/java/org/isoron/uhabits/BaseTest.java +++ b/app/src/androidTest/java/org/isoron/uhabits/BaseTest.java @@ -25,6 +25,7 @@ import android.os.Looper; import android.support.test.InstrumentationRegistry; import org.isoron.uhabits.helpers.DateHelper; +import org.isoron.uhabits.helpers.UIHelper; import org.isoron.uhabits.tasks.BaseTask; import org.junit.Before; @@ -48,10 +49,9 @@ public class BaseTest } targetContext = InstrumentationRegistry.getTargetContext(); - targetContext.setTheme(R.style.AppBaseTheme); - testContext = InstrumentationRegistry.getContext(); + UIHelper.setFixedTheme(R.style.AppBaseTheme); DateHelper.setFixedLocalTime(FIXED_LOCAL_TIME); } diff --git a/app/src/androidTest/java/org/isoron/uhabits/unit/views/CheckmarkWidgetViewTest.java b/app/src/androidTest/java/org/isoron/uhabits/unit/views/CheckmarkWidgetViewTest.java index cc367b52f..874c8666f 100644 --- a/app/src/androidTest/java/org/isoron/uhabits/unit/views/CheckmarkWidgetViewTest.java +++ b/app/src/androidTest/java/org/isoron/uhabits/unit/views/CheckmarkWidgetViewTest.java @@ -22,7 +22,9 @@ 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.DateHelper; +import org.isoron.uhabits.helpers.UIHelper; import org.isoron.uhabits.models.Habit; import org.isoron.uhabits.unit.HabitFixtures; import org.isoron.uhabits.views.CheckmarkWidgetView; @@ -43,6 +45,7 @@ public class CheckmarkWidgetViewTest extends ViewTest public void setup() { super.setup(); + UIHelper.setFixedTheme(R.style.TransparentWidgetTheme); habit = HabitFixtures.createShortHabit(); view = new CheckmarkWidgetView(targetContext); diff --git a/app/src/main/java/org/isoron/uhabits/helpers/UIHelper.java b/app/src/main/java/org/isoron/uhabits/helpers/UIHelper.java index 04b03f4e7..2aee987a8 100644 --- a/app/src/main/java/org/isoron/uhabits/helpers/UIHelper.java +++ b/app/src/main/java/org/isoron/uhabits/helpers/UIHelper.java @@ -51,6 +51,12 @@ public abstract class UIHelper public static final int THEME_DARK = 1; private static Typeface fontAwesome; + private static Integer fixedTheme; + + public static void setFixedTheme(Integer fixedTheme) + { + UIHelper.fixedTheme = fixedTheme; + } public interface OnSavedListener { @@ -202,18 +208,25 @@ public abstract class UIHelper public static int getStyledColor(Context context, int attrId) { - int[] attrs = new int[]{ attrId }; - TypedArray ta = context.obtainStyledAttributes(attrs); + TypedArray ta = getTypedArray(context, attrId); int color = ta.getColor(0, 0); ta.recycle(); return color; } - public static Drawable getStyledDrawable(Context context, int attrId) + private static TypedArray getTypedArray(Context context, int attrId) { int[] attrs = new int[]{ attrId }; - TypedArray ta = context.obtainStyledAttributes(attrs); + if(fixedTheme != null) + return context.getTheme().obtainStyledAttributes(fixedTheme, attrs); + else + return context.obtainStyledAttributes(attrs); + } + + public static Drawable getStyledDrawable(Context context, int attrId) + { + TypedArray ta = getTypedArray(context, attrId); Drawable drawable = ta.getDrawable(0); ta.recycle(); @@ -222,8 +235,7 @@ public abstract class UIHelper public static boolean getStyledBoolean(Context context, int attrId) { - int[] attrs = new int[]{ attrId }; - TypedArray ta = context.obtainStyledAttributes(attrs); + TypedArray ta = getTypedArray(context, attrId); boolean bool = ta.getBoolean(0, false); ta.recycle(); @@ -232,8 +244,7 @@ public abstract class UIHelper public static float getStyledFloat(Context context, int attrId) { - int[] attrs = new int[]{ attrId }; - TypedArray ta = context.obtainStyledAttributes(attrs); + TypedArray ta = getTypedArray(context, attrId); float f = ta.getFloat(0, 0); ta.recycle(); @@ -242,10 +253,9 @@ public abstract class UIHelper static int getStyleResource(Context context, int attrId) { - int[] attr = new int[] { attrId }; - TypedArray array = context.obtainStyledAttributes(attr); - int resourceId = array.getResourceId(0, -1); - array.recycle(); + TypedArray ta = getTypedArray(context, attrId); + int resourceId = ta.getResourceId(0, -1); + ta.recycle(); return resourceId; } diff --git a/run_tests b/run_tests index b3c51fada..8ec15cd6d 100755 --- a/run_tests +++ b/run_tests @@ -41,8 +41,8 @@ grep -q "FAILURES\!\!\!" ${OUTPUT_DIR}/runner.txt && failed=1 info "Fetching failed generated files..." mkdir -p ${OUTPUT_DIR}/failed adb pull /mnt/sdcard/test-screenshots/ ${OUTPUT_DIR}/failed >> $LOG 2>> $LOG -adb pull /sdcard/Android/data/${PACKAGE_NAME}/files/test-screenshots/ \ - ${OUTPUT_DIR}/failed >> $LOG 2>> $LOG +adb pull /storage/sdcard/test-screenshots/ ${OUTPUT_DIR}/failed >> $LOG 2>> $LOG +adb pull /sdcard/Android/data/${PACKAGE_NAME}/files/test-screenshots/ ${OUTPUT_DIR}/failed >> $LOG 2>> $LOG adb shell rm -r /sdcard/Android/data/${PACKAGE_NAME}/files/test-screenshots/ >> $LOG 2>> $LOG info "Fetching logcat..."