Update test injection, fix broken tests

pull/820/head
Alex Baker 6 years ago
parent b37e9870f6
commit c77f49de80

@ -24,7 +24,7 @@ import static org.tasks.makers.TaskMaker.REMINDER_LAST;
import static org.tasks.makers.TaskMaker.SNOOZE_TIME;
import static org.tasks.makers.TaskMaker.newTask;
import androidx.test.runner.AndroidJUnit4;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.data.Task;
import java.util.concurrent.TimeUnit;
@ -62,7 +62,7 @@ public class ReminderServiceTest extends InjectingTestCase {
jobs = mock(NotificationQueue.class);
random = mock(Random.class);
when(random.nextFloat()).thenReturn(1.0f);
preferences.reset();
preferences.clear();
service = new ReminderService(preferences, jobs, random, taskDao);
}

@ -163,7 +163,7 @@ public class TitleParserTest extends InjectingTestCase {
}
private Task insertTitleAddTask(String title) {
return taskCreator.createWithValues(null, title);
return taskCreator.createWithValues(title);
}
// ----------------Days begin----------------//
@ -172,13 +172,13 @@ public class TitleParserTest extends InjectingTestCase {
Calendar today = Calendar.getInstance();
String title = "Jog today";
Task task = taskCreator.createWithValues(null, title);
Task task = taskCreator.createWithValues(title);
DateTime date = newDateTime(task.getDueDate());
assertEquals(date.getDayOfWeek(), today.get(Calendar.DAY_OF_WEEK));
// Calendar starts 1-6, date.getDay() starts at 0
title = "Jog tomorrow";
task = taskCreator.createWithValues(null, title);
task = taskCreator.createWithValues(title);
date = newDateTime(task.getDueDate());
assertEquals((date.getDayOfWeek()) % 7, (today.get(Calendar.DAY_OF_WEEK) + 1) % 7);
@ -189,12 +189,12 @@ public class TitleParserTest extends InjectingTestCase {
for (int i = 1; i <= 6; i++) {
title = "Jog " + days[i];
task = taskCreator.createWithValues(null, title);
task = taskCreator.createWithValues(title);
date = newDateTime(task.getDueDate());
assertEquals(date.getDayOfWeek(), i + 1);
title = "Jog " + abrevDays[i];
task = taskCreator.createWithValues(null, title);
task = taskCreator.createWithValues(title);
date = newDateTime(task.getDueDate());
assertEquals(date.getDayOfWeek(), i + 1);
}
@ -210,12 +210,12 @@ public class TitleParserTest extends InjectingTestCase {
String[] acceptedStrings = {"priority 0", "least priority", "lowest priority", "bang 0"};
for (String acceptedString : acceptedStrings) {
String title = "Jog " + acceptedString;
Task task = taskCreator.createWithValues(null, title);
Task task = taskCreator.createWithValues(title);
assertEquals((int) task.getPriority(), Priority.NONE);
}
for (String acceptedString : acceptedStrings) {
String title = acceptedString + " jog";
Task task = taskCreator.createWithValues(null, title);
Task task = taskCreator.createWithValues(title);
assertNotSame(task.getPriority(), Priority.NONE);
}
}
@ -257,20 +257,20 @@ public class TitleParserTest extends InjectingTestCase {
String[] acceptedStringsAnywhere = {"!2", "!!"};
for (String acceptedStringAtEnd : acceptedStringsAtEnd) {
String title = "Jog " + acceptedStringAtEnd;
Task task = taskCreator.createWithValues(null, title);
Task task = taskCreator.createWithValues(title);
assertEquals((int) task.getPriority(), Priority.MEDIUM);
title = acceptedStringAtEnd + " jog";
task = taskCreator.createWithValues(null, title);
task = taskCreator.createWithValues(title);
assertNotSame(task.getPriority(), Priority.MEDIUM);
}
for (String acceptedStringAnywhere : acceptedStringsAnywhere) {
String title = "Jog " + acceptedStringAnywhere;
Task task = taskCreator.createWithValues(null, title);
Task task = taskCreator.createWithValues(title);
assertEquals((int) task.getPriority(), Priority.MEDIUM);
title = acceptedStringAnywhere + " jog";
task = taskCreator.createWithValues(null, title);
task = taskCreator.createWithValues(title);
assertEquals((int) task.getPriority(), Priority.MEDIUM);
}
}
@ -287,20 +287,20 @@ public class TitleParserTest extends InjectingTestCase {
String[] acceptedStringsAnywhere = {"!3", "!!!", "!6", "!!!!!!!!!!!!!"};
for (String acceptedStringAtEnd : acceptedStringsAtEnd) {
String title = "Jog " + acceptedStringAtEnd;
Task task = taskCreator.createWithValues(null, title);
Task task = taskCreator.createWithValues(title);
assertEquals((int) task.getPriority(), Priority.HIGH);
title = acceptedStringAtEnd + " jog";
task = taskCreator.createWithValues(null, title);
task = taskCreator.createWithValues(title);
assertNotSame(task.getPriority(), Priority.HIGH);
}
for (String acceptedStringAnywhere : acceptedStringsAnywhere) {
String title = "Jog " + acceptedStringAnywhere;
Task task = taskCreator.createWithValues(null, title);
Task task = taskCreator.createWithValues(title);
assertEquals((int) task.getPriority(), Priority.HIGH);
title = acceptedStringAnywhere + " jog";
task = taskCreator.createWithValues(null, title);
task = taskCreator.createWithValues(title);
assertEquals((int) task.getPriority(), Priority.HIGH);
}
}
@ -313,7 +313,7 @@ public class TitleParserTest extends InjectingTestCase {
@Test
public void testDailyWithNoDueDate() {
String title = "Jog daily";
Task task = taskCreator.createWithValues(null, title);
Task task = taskCreator.createWithValues(title);
RRule rrule = new RRule();
rrule.setFreq(Frequency.DAILY);
rrule.setInterval(1);
@ -322,7 +322,7 @@ public class TitleParserTest extends InjectingTestCase {
assertFalse(task.hasDueDate());
title = "Jog every day";
task = taskCreator.createWithValues(null, title);
task = taskCreator.createWithValues(title);
assertEquals(task.getRecurrence(), rrule.toIcal());
assertFalse(task.hasDueTime());
assertFalse(task.hasDueDate());
@ -330,7 +330,7 @@ public class TitleParserTest extends InjectingTestCase {
for (int i = 1; i <= 12; i++) {
title = "Jog every " + i + " days.";
rrule.setInterval(i);
task = taskCreator.createWithValues(null, title);
task = taskCreator.createWithValues(title);
assertEquals(task.getRecurrence(), rrule.toIcal());
assertFalse(task.hasDueTime());
assertFalse(task.hasDueDate());
@ -341,7 +341,7 @@ public class TitleParserTest extends InjectingTestCase {
@Test
public void testWeeklyWithNoDueDate() {
String title = "Jog weekly";
Task task = taskCreator.createWithValues(null, title);
Task task = taskCreator.createWithValues(title);
RRule rrule = new RRule();
rrule.setFreq(Frequency.WEEKLY);
rrule.setInterval(1);
@ -350,7 +350,7 @@ public class TitleParserTest extends InjectingTestCase {
assertFalse(task.hasDueDate());
title = "Jog every week";
task = taskCreator.createWithValues(null, title);
task = taskCreator.createWithValues(title);
assertEquals(task.getRecurrence(), rrule.toIcal());
assertFalse(task.hasDueTime());
assertFalse(task.hasDueDate());
@ -358,7 +358,7 @@ public class TitleParserTest extends InjectingTestCase {
for (int i = 1; i <= 12; i++) {
title = "Jog every " + i + " weeks";
rrule.setInterval(i);
task = taskCreator.createWithValues(null, title);
task = taskCreator.createWithValues(title);
assertEquals(task.getRecurrence(), rrule.toIcal());
assertFalse(task.hasDueTime());
assertFalse(task.hasDueDate());
@ -369,7 +369,7 @@ public class TitleParserTest extends InjectingTestCase {
@Test
public void testMonthlyFromNoDueDate() {
String title = "Jog monthly";
Task task = taskCreator.createWithValues(null, title);
Task task = taskCreator.createWithValues(title);
RRule rrule = new RRule();
rrule.setFreq(Frequency.MONTHLY);
rrule.setInterval(1);
@ -378,7 +378,7 @@ public class TitleParserTest extends InjectingTestCase {
assertFalse(task.hasDueDate());
title = "Jog every month";
task = taskCreator.createWithValues(null, title);
task = taskCreator.createWithValues(title);
assertEquals(task.getRecurrence(), rrule.toIcal());
assertFalse(task.hasDueTime());
assertFalse(task.hasDueDate());
@ -386,7 +386,7 @@ public class TitleParserTest extends InjectingTestCase {
for (int i = 1; i <= 12; i++) {
title = "Jog every " + i + " months";
rrule.setInterval(i);
task = taskCreator.createWithValues(null, title);
task = taskCreator.createWithValues(title);
assertEquals(task.getRecurrence(), rrule.toIcal());
assertFalse(task.hasDueTime());
assertFalse(task.hasDueDate());
@ -396,7 +396,7 @@ public class TitleParserTest extends InjectingTestCase {
@Test
public void testDailyFromDueDate() {
String title = "Jog daily starting from today";
Task task = taskCreator.createWithValues(null, title);
Task task = taskCreator.createWithValues(title);
RRule rrule = new RRule();
rrule.setFreq(Frequency.DAILY);
rrule.setInterval(1);
@ -404,14 +404,14 @@ public class TitleParserTest extends InjectingTestCase {
assertTrue(task.hasDueDate());
title = "Jog every day starting from today";
task = taskCreator.createWithValues(null, title);
task = taskCreator.createWithValues(title);
assertEquals(task.getRecurrence(), rrule.toIcal());
assertTrue(task.hasDueDate());
for (int i = 1; i <= 12; i++) {
title = "Jog every " + i + " days starting from today";
rrule.setInterval(i);
task = taskCreator.createWithValues(null, title);
task = taskCreator.createWithValues(title);
assertEquals(task.getRecurrence(), rrule.toIcal());
assertTrue(task.hasDueDate());
}
@ -420,7 +420,7 @@ public class TitleParserTest extends InjectingTestCase {
@Test
public void testWeeklyFromDueDate() {
String title = "Jog weekly starting from today";
Task task = taskCreator.createWithValues(null, title);
Task task = taskCreator.createWithValues(title);
RRule rrule = new RRule();
rrule.setFreq(Frequency.WEEKLY);
rrule.setInterval(1);
@ -428,14 +428,14 @@ public class TitleParserTest extends InjectingTestCase {
assertTrue(task.hasDueDate());
title = "Jog every week starting from today";
task = taskCreator.createWithValues(null, title);
task = taskCreator.createWithValues(title);
assertEquals(task.getRecurrence(), rrule.toIcal());
assertTrue(task.hasDueDate());
for (int i = 1; i <= 12; i++) {
title = "Jog every " + i + " weeks starting from today";
rrule.setInterval(i);
task = taskCreator.createWithValues(null, title);
task = taskCreator.createWithValues(title);
assertEquals(task.getRecurrence(), rrule.toIcal());
assertTrue(task.hasDueDate());
}

@ -1,8 +1,9 @@
package org.tasks.injection;
import static androidx.test.InstrumentationRegistry.getTargetContext;
import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import static org.tasks.TestUtilities.initializeMockito;
import android.content.Context;
import org.junit.Before;
import timber.log.Timber;
@ -12,10 +13,14 @@ public abstract class InjectingTestCase {
public void setUp() {
Thread.setDefaultUncaughtExceptionHandler((t, e) -> Timber.e(e));
initializeMockito(getTargetContext());
Context context = getApplicationContext();
initializeMockito(context);
TestComponent component =
DaggerTestComponent.builder().testModule(new TestModule(getTargetContext())).build();
DaggerTestComponent.builder()
.applicationModule(new ApplicationModule(context))
.testModule(new TestModule()).build();
inject(component);
}

@ -3,124 +3,34 @@ package org.tasks.injection;
import android.content.Context;
import androidx.room.Room;
import com.todoroo.astrid.dao.Database;
import com.todoroo.astrid.dao.TaskDao;
import dagger.Module;
import dagger.Provides;
import org.tasks.data.AlarmDao;
import org.tasks.data.CaldavDao;
import org.tasks.data.DeletionDao;
import org.tasks.data.FilterDao;
import org.tasks.data.GoogleTaskDao;
import org.tasks.data.GoogleTaskListDao;
import org.tasks.data.LocationDao;
import org.tasks.data.TagDao;
import org.tasks.data.TagDataDao;
import org.tasks.data.TaskAttachmentDao;
import org.tasks.data.TaskListMetadataDao;
import org.tasks.data.UserActivityDao;
import org.tasks.jobs.WorkManager;
import org.tasks.notifications.NotificationDao;
import org.tasks.preferences.PermissionChecker;
import org.tasks.preferences.PermissivePermissionChecker;
import org.tasks.preferences.Preferences;
@Module
@Module(includes = ApplicationModule.class)
public class TestModule {
private final Context context;
TestModule(Context context) {
this.context = context;
public static Preferences newPreferences(Context context) {
return new Preferences(context, "test_preferences");
}
@Provides
@ApplicationScope
public Database getDatabase() {
public Database getDatabase(@ForApplication Context context) {
return Room.inMemoryDatabaseBuilder(context, Database.class)
.fallbackToDestructiveMigration()
.build();
}
@Provides
public NotificationDao getNotificationDao(Database appDatabase) {
return appDatabase.notificationDao();
}
@Provides
public TagDataDao getTagDataDao(Database database) {
return database.getTagDataDao();
}
@Provides
public UserActivityDao getUserActivityDao(Database database) {
return database.getUserActivityDao();
}
@Provides
public TaskListMetadataDao getTaskListMetadataDao(Database database) {
return database.getTaskListMetadataDao();
}
@Provides
public GoogleTaskListDao getGoogleTaskListDao(Database database) {
return database.getGoogleTaskListDao();
}
@Provides
public AlarmDao getAlarmDao(Database database) {
return database.getAlarmDao();
}
@Provides
public GoogleTaskDao getGoogleTaskDao(Database database) {
return database.getGoogleTaskDao();
}
@Provides
public TagDao getTagDao(Database database) {
return database.getTagDao();
}
@Provides
public LocationDao getLocationDao(Database database) {
return database.getLocationDao();
}
@Provides
public TaskDao getTaskDao(WorkManager workManager, Database database) {
TaskDao taskDao = database.getTaskDao();
taskDao.initialize(workManager);
return taskDao;
}
@Provides
public CaldavDao getCaldavDao(Database database) {
return database.getCaldavDao();
}
@Provides
public FilterDao getFilterDao(Database database) {
return database.getFilterDao();
}
@Provides
public DeletionDao getDeletionDao(Database database) {
return database.getDeletionDao();
}
@Provides
public TaskAttachmentDao getTaskAttachmentDao(Database database) {
return database.getTaskAttachmentDao();
}
@ApplicationScope
@Provides
@ForApplication
public Context getContext() {
return context;
public PermissionChecker getPermissionChecker(@ForApplication Context context) {
return new PermissivePermissionChecker(context);
}
@Provides
public PermissionChecker getPermissionChecker() {
return new PermissivePermissionChecker(context);
public Preferences getPreferences(@ForApplication Context context) {
return newPreferences(context);
}
}

@ -1,7 +1,8 @@
package org.tasks.preferences;
import static androidx.test.InstrumentationRegistry.getTargetContext;
import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import static junit.framework.Assert.assertEquals;
import static org.tasks.injection.TestModule.newPreferences;
import android.annotation.SuppressLint;
import androidx.test.runner.AndroidJUnit4;
@ -22,7 +23,7 @@ public class PreferenceTests {
@Before
public void setUp() {
preferences = new Preferences(getTargetContext());
preferences = newPreferences(getApplicationContext());
preferences.clear();
preferences.setBoolean(R.string.p_rmd_enable_quiet, true);
}

@ -21,7 +21,7 @@ import org.tasks.jobs.BackupServiceTests;
@ApplicationScope
@Component(modules = TestModule.class)
public interface TestComponent {
public interface TestComponent extends ApplicationComponent {
void inject(GtasksIndentActionTest gtasksIndentActionTest);

@ -6,7 +6,7 @@ import org.tasks.dashclock.DashClockExtension;
import org.tasks.widget.ScrollableWidgetUpdateService;
@ApplicationScope
@Component(modules = ApplicationModule.class)
@Component(modules = {ApplicationModule.class, ProductionModule.class})
public interface ApplicationComponent {
void inject(DashClockExtension dashClockExtension);

@ -3,7 +3,6 @@ package org.tasks.injection;
import static com.todoroo.andlib.utility.AndroidUtilities.atLeastMarshmallow;
import android.content.Context;
import androidx.room.Room;
import com.todoroo.astrid.dao.Database;
import com.todoroo.astrid.dao.TaskDao;
import dagger.Module;
@ -24,7 +23,6 @@ import org.tasks.data.TagDataDao;
import org.tasks.data.TaskAttachmentDao;
import org.tasks.data.TaskListMetadataDao;
import org.tasks.data.UserActivityDao;
import org.tasks.db.Migrations;
import org.tasks.jobs.WorkManager;
import org.tasks.locale.Locale;
import org.tasks.notifications.NotificationDao;
@ -52,15 +50,6 @@ public class ApplicationModule {
return context;
}
@Provides
@ApplicationScope
public Database getAppDatabase() {
return Room.databaseBuilder(context, Database.class, Database.NAME)
.allowMainThreadQueries() // TODO: remove me
.addMigrations(Migrations.MIGRATIONS)
.build();
}
@Provides
@ApplicationScope
public NotificationDao getNotificationDao(Database database) {

@ -4,7 +4,7 @@ import com.todoroo.astrid.provider.Astrid2TaskProvider;
import dagger.Component;
@ApplicationScope
@Component(modules = {ApplicationModule.class, ContentProviderModule.class})
@Component(modules = ContentProviderModule.class)
public interface ContentProviderComponent {
void inject(Astrid2TaskProvider astrid2TaskProvider);

@ -2,5 +2,5 @@ package org.tasks.injection;
import dagger.Module;
@Module
@Module(includes = ProductionModule.class)
public class ContentProviderModule {}

@ -22,6 +22,7 @@ class Dagger {
applicationComponent =
DaggerApplicationComponent.builder()
.applicationModule(new ApplicationModule(localeContext))
.productionModule(new ProductionModule())
.build();
}

@ -11,6 +11,7 @@ public abstract class InjectingContentProvider extends ContentProvider {
inject(
DaggerContentProviderComponent.builder()
.applicationModule(new ApplicationModule(context.getApplicationContext()))
.productionModule(new ProductionModule())
.contentProviderModule(new ContentProviderModule())
.build());

@ -0,0 +1,26 @@
package org.tasks.injection;
import android.content.Context;
import androidx.room.Room;
import com.todoroo.astrid.dao.Database;
import dagger.Module;
import dagger.Provides;
import org.tasks.db.Migrations;
import org.tasks.preferences.Preferences;
@Module(includes = ApplicationModule.class)
public class ProductionModule {
@Provides
@ApplicationScope
public Database getAppDatabase(@ForApplication Context context) {
return Room.databaseBuilder(context, Database.class, Database.NAME)
.allowMainThreadQueries() // TODO: remove me
.addMigrations(Migrations.MIGRATIONS)
.build();
}
@Provides
public Preferences getPreferences(@ForApplication Context context) {
return new Preferences(context);
}
}

@ -28,11 +28,9 @@ import com.todoroo.astrid.data.Task;
import java.io.File;
import java.util.Collection;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import org.tasks.R;
import org.tasks.billing.Purchase;
import org.tasks.data.TaskAttachment;
import org.tasks.injection.ForApplication;
import org.tasks.time.DateTime;
import timber.log.Timber;
@ -46,14 +44,21 @@ public class Preferences {
private final SharedPreferences prefs;
private final SharedPreferences publicPrefs;
@Inject
public Preferences(@ForApplication Context context) {
public Preferences(Context context) {
this(context, getSharedPreferencesName(context));
}
public Preferences(Context context, String name) {
this.context = context;
prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs = context.getSharedPreferences(name, Context.MODE_PRIVATE);
publicPrefs =
context.getSharedPreferences(AstridApiConstants.PUBLIC_PREFS, Context.MODE_PRIVATE);
}
private static String getSharedPreferencesName(Context context) {
return context.getPackageName() + "_preferences";
}
public boolean backButtonSavesTask() {
return getBoolean(R.string.p_back_button_saves_task, false);
}

Loading…
Cancel
Save