Skip to content

Commit

Permalink
Have special test runner and app to have initial settings for tests
Browse files Browse the repository at this point in the history
Disable Season Greetings on tests. Tests should not fail on christmas :)
  • Loading branch information
tasomaniac committed Dec 25, 2016
1 parent de748b7 commit b045ad8
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ android {

resConfigs "en", "ar", "cs", "de", "es", "fr", "hi", "hu", "hy-rAM", "it", "ja", "ko", "lt", "nl", "pl", "pt-rBR", "pt-rPT", "ru", "sr", "sk", "th", "tr", "uk", "zh-rCN", "zh-rHK", "zh-rTW"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner "org.gdg.frisbee.android.FrisbeeTestRunner"

//KEYS
buildConfigField "long", "DOORBELL_ID", local_properties.doorbell_id ?: "0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.gdg.frisbee.android;

import android.app.Application;
import android.content.Context;
import android.support.test.runner.AndroidJUnitRunner;

import org.gdg.frisbee.android.app.TestApp;

public class FrisbeeTestRunner extends AndroidJUnitRunner {

@Override
public Application newApplication(ClassLoader cl, String className, Context context)
throws InstantiationException, IllegalAccessException, ClassNotFoundException {
return super.newApplication(cl, TestApp.class.getName(), context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

import android.content.Intent;
import android.net.Uri;
import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.ViewInteraction;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.support.v7.widget.Toolbar;

import org.gdg.frisbee.android.chapter.MainActivity;
import org.gdg.frisbee.android.utils.PrefUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -29,12 +27,7 @@ public class MainActivityDeepLinkTest {
private static final Uri URI_GDG_BRUSSELS = Uri.parse("https://developers.google.com/groups/chapter/105068877693379070381/");

@Rule
public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule<MainActivity>(MainActivity.class, true, false) {
@Override
protected void beforeActivityLaunched() {
PrefUtils.setInitialSettings(InstrumentationRegistry.getTargetContext(), false);
}
};
public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule<>(MainActivity.class, true, false);

@Test
public void canHandleDevelopersGoogleChapterUri() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public class MainActivityTest {
public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule<MainActivity>(MainActivity.class) {
@Override
protected void beforeActivityLaunched() {
PrefUtils.setInitialSettings(InstrumentationRegistry.getTargetContext(), false);
PrefUtils.setHomeChapter(InstrumentationRegistry.getTargetContext(), CHAPTER_BRUSSELS);
}
};
Expand Down
15 changes: 15 additions & 0 deletions app/src/androidTest/java/org/gdg/frisbee/android/app/TestApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.gdg.frisbee.android.app;


import org.gdg.frisbee.android.utils.PrefUtils;

public class TestApp extends App {

@Override
public void onCreate() {
super.onCreate();

PrefUtils.setInitialSettings(this, false);
PrefUtils.skipSeasonsGreetings(this);
}
}
19 changes: 16 additions & 3 deletions app/src/main/java/org/gdg/frisbee/android/utils/PrefUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.Nullable;
import android.support.annotation.RestrictTo;

import com.crashlytics.android.Crashlytics;

Expand Down Expand Up @@ -113,7 +114,7 @@ public static int getAppStarts(final Context context) {
return prefs(context).getInt(PREFS_APP_STARTS, 0);
}

public static boolean shouldShowSeasonsGreetings(final Context context) {
public static boolean shouldShowSeasonsGreetings(Context context) {
DateTime now = DateTime.now();
if (prefs(context).getInt(PREFS_SEASONS_GREETINGS, now.getYear() - 1) < now.getYear()
&& (now.getDayOfYear() >= 354 && now.getDayOfYear() <= 366)) {
Expand All @@ -123,11 +124,23 @@ public static boolean shouldShowSeasonsGreetings(final Context context) {
return false;
}

public static void setVersionCode(final Context context, final int newVersion) {
@RestrictTo(RestrictTo.Scope.TESTS)
public static void skipSeasonsGreetings(Context context) {
prefs(context)
.edit()
.putInt(PREFS_SEASONS_GREETINGS, DateTime.now().getYear())
.apply();
}

public static void setVersionCode(Context context, int newVersion) {
prefs(context).edit().putInt(PREFS_VERSION_CODE, newVersion).apply();
}

public static void resetInitialSettings(final Context context) {
/**
* Used in alpha source set
*/
@SuppressWarnings("unused")
public static void resetInitialSettings(Context context) {
prefs(context).edit()
.clear()
.apply();
Expand Down

0 comments on commit b045ad8

Please sign in to comment.