Skip to content

Commit

Permalink
plusonelabs#356 Make custom Clock / Time zone specific to each widget…
Browse files Browse the repository at this point in the history
… (and not device global, as before)
  • Loading branch information
yvolk committed Dec 31, 2019
1 parent 2f01976 commit fcc81ec
Show file tree
Hide file tree
Showing 40 changed files with 302 additions and 326 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.andstatus.todoagenda;

import org.andstatus.todoagenda.prefs.InstanceSettings;
import org.andstatus.todoagenda.provider.MockCalendarContentProvider;
import org.andstatus.todoagenda.widget.LastEntry;
import org.joda.time.DateTime;
Expand Down Expand Up @@ -47,8 +48,12 @@ DateTime dateTime(
}

protected void playResults(String tag) {
provider.updateAppSettings();
provider.updateAppSettings(tag);
factory.onDataSetChanged();
factory.logWidgetEntries(tag);
}

protected InstanceSettings getSettings() {
return provider.getSettings();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package org.andstatus.todoagenda;

import androidx.test.platform.app.InstrumentationRegistry;

import org.andstatus.todoagenda.prefs.ApplicationPreferences;
import org.andstatus.todoagenda.provider.QueryResultsStorage;
import org.andstatus.todoagenda.util.DateUtil;
import org.andstatus.todoagenda.widget.CalendarEntry;
import org.joda.time.DateTime;
import org.json.JSONException;
Expand All @@ -22,14 +19,14 @@ public class BirthdayTest extends BaseWidgetTest {

@Test
public void testBirthdayOneDayOnly() throws IOException, JSONException {
QueryResultsStorage inputs = provider.loadResultsAndSettings(InstrumentationRegistry.getInstrumentation().getContext(),
QueryResultsStorage inputs = provider.loadResultsAndSettings(
org.andstatus.todoagenda.tests.R.raw.birthday);

provider.startEditing();
provider.startEditingPreferences();
ApplicationPreferences.setEventsEnded(provider.getContext(), EndedSomeTimeAgo.NONE);
ApplicationPreferences.setShowPastEventsWithDefaultColor(provider.getContext(), false);
ApplicationPreferences.setEventRange(provider.getContext(), 30);
provider.saveSettings();
provider.savePreferences();

playAtOneTime(inputs, dateTime(2015, 8, 1, 17, 0), 0);
playAtOneTime(inputs, dateTime(2015, 8, 9, 23, 59), 0);
Expand All @@ -46,22 +43,22 @@ public void testBirthdayOneDayOnly() throws IOException, JSONException {
playAtOneTime(inputs, dateTime(2015, 9, 10, 11, 0), 0);

ApplicationPreferences.setEventsEnded(provider.getContext(), EndedSomeTimeAgo.ONE_HOUR);
provider.saveSettings();
provider.savePreferences();
playAtOneTime(inputs, dateTime(2015, 9, 10, 0, 30), 2);
playAtOneTime(inputs, dateTime(2015, 9, 10, 1, 30), 0);

ApplicationPreferences.setEventsEnded(provider.getContext(), EndedSomeTimeAgo.TODAY);
provider.saveSettings();
provider.savePreferences();
playAtOneTime(inputs, dateTime(2015, 9, 10, 1, 30), 0);

ApplicationPreferences.setEventsEnded(provider.getContext(), EndedSomeTimeAgo.FOUR_HOURS);
provider.saveSettings();
provider.savePreferences();
playAtOneTime(inputs, dateTime(2015, 9, 10, 1, 30), 2);
playAtOneTime(inputs, dateTime(2015, 9, 10, 3, 59), 2);
playAtOneTime(inputs, dateTime(2015, 9, 10, 4, 0), 0);

ApplicationPreferences.setEventsEnded(provider.getContext(), EndedSomeTimeAgo.YESTERDAY);
provider.saveSettings();
provider.savePreferences();
playAtOneTime(inputs, dateTime(2015, 9, 10, 4, 0), 2);
playAtOneTime(inputs, dateTime(2015, 9, 10, 11, 0), 2);
playAtOneTime(inputs, dateTime(2015, 9, 10, 17, 0), 2);
Expand All @@ -70,14 +67,14 @@ public void testBirthdayOneDayOnly() throws IOException, JSONException {
playAtOneTime(inputs, dateTime(2015, 9, 11, 0, 30), 0);

ApplicationPreferences.setShowPastEventsWithDefaultColor(provider.getContext(), true);
provider.saveSettings();
provider.savePreferences();
playAtOneTime(inputs, dateTime(2015, 9, 11, 0, 30), 0);
}

private void playAtOneTime(QueryResultsStorage inputs, DateTime now, int entriesWithoutLastExpected) {
provider.clear();
provider.addResults(inputs.getResults());
DateUtil.setNow(now);
getSettings().clock().setNow(now);
EnvironmentChangedReceiver.sleep(MIN_MILLIS_BETWEEN_RELOADS);
playResults(TAG);
assertEquals(entriesWithoutLastExpected == 0 ? 0 : entriesWithoutLastExpected + 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package org.andstatus.todoagenda;

import android.util.Log;

import androidx.test.platform.app.InstrumentationRegistry;

import org.andstatus.todoagenda.provider.QueryResultsStorage;
import org.json.JSONException;
import org.junit.Test;
Expand All @@ -23,10 +19,9 @@ public class DuplicateEventsTest extends BaseWidgetTest {
@Test
public void testIssue354() throws IOException, JSONException {
final String method = "testIssue354";
QueryResultsStorage inputs = provider.loadResultsAndSettings(InstrumentationRegistry.getInstrumentation().getContext(),
QueryResultsStorage inputs = provider.loadResultsAndSettings(
org.andstatus.todoagenda.tests.R.raw.duplicates);
provider.addResults(inputs.getResults());
Log.d(method, "Results executed at " + inputs.getResults().get(0).getExecutedAt());

playResults(method);
assertEquals("Number of entries", 40, factory.getWidgetEntries().size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import org.andstatus.todoagenda.calendar.CalendarEvent;
import org.andstatus.todoagenda.provider.QueryRow;
import org.andstatus.todoagenda.util.DateUtil;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDateTime;
Expand Down Expand Up @@ -43,9 +42,9 @@ public void testIllegalInstantDueToTimeZoneOffsetTransition() {
oneTimeDst("2015-10-25T00:00:00+00:00");
oneTimeDst("2011-03-27T00:00:00+00:00");
oneTimeDst("1980-04-06T00:00:00+00:00");
provider.addRow(new CalendarEvent(provider.getContext(), provider.getWidgetId(),
provider.getSettings().getTimeZone(),false)
.setStartDate(DateUtil.startOfTomorrow(provider.getSettings().getTimeZone()))
provider.addRow(new CalendarEvent(getSettings(), provider.getContext(), provider.getWidgetId(),
getSettings().getTimeZone(),false)
.setStartDate(getSettings().clock().startOfTomorrow(getSettings().getTimeZone()))
.setEventSource(provider.getFirstActiveEventSource())
.setTitle("This will be the only event that will be shown"));
playResults(TAG);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import org.andstatus.todoagenda.prefs.ApplicationPreferences;
import org.andstatus.todoagenda.provider.QueryResultsStorage;
import org.andstatus.todoagenda.util.DateUtil;
import org.andstatus.todoagenda.widget.DayHeader;
import org.andstatus.todoagenda.widget.LastEntry;
import org.andstatus.todoagenda.widget.WidgetEntry;
Expand All @@ -14,8 +13,6 @@

import java.io.IOException;

import androidx.test.platform.app.InstrumentationRegistry;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

Expand All @@ -27,16 +24,16 @@ public class MultidayAllDayEventTest extends BaseWidgetTest {
@Test
public void testInsidePeriod() throws IOException, JSONException {
final String method = "testInsidePeriod";
QueryResultsStorage inputs = provider.loadResultsAndSettings(InstrumentationRegistry.getInstrumentation().getContext(),
QueryResultsStorage inputs = provider.loadResultsAndSettings(
org.andstatus.todoagenda.tests.R.raw.multi_day);
provider.addResults(inputs.getResults());

int dateRange = 30;
provider.startEditing();
provider.startEditingPreferences();
ApplicationPreferences.setEventRange(provider.getContext(), dateRange);
provider.saveSettings();
DateTime now = new DateTime(2015, 8, 30, 0, 0, 1, 0, provider.getSettings().getTimeZone());
DateUtil.setNow(now);
provider.savePreferences();
DateTime now = new DateTime(2015, 8, 30, 0, 0, 1, 0, getSettings().getTimeZone());
getSettings().clock().setNow(now);
playResults(method);

DateTime today = now.withTimeAtStartOfDay();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.util.Log;

import org.andstatus.todoagenda.calendar.CalendarEvent;
import org.andstatus.todoagenda.util.DateUtil;
import org.andstatus.todoagenda.widget.CalendarEntry;
import org.andstatus.todoagenda.widget.WidgetEntry;
import org.joda.time.DateTime;
Expand All @@ -30,17 +29,17 @@ public class MultidayEventTest extends BaseWidgetTest {
*/
@Test
public void testEventWhichCarryOverToTheNextDay() {
DateTimeZone timeZone = provider.getSettings().getTimeZone();
DateTime today = DateUtil.now(timeZone).withTimeAtStartOfDay();
CalendarEvent event = new CalendarEvent(provider.getContext(), provider.getWidgetId(),
DateTimeZone timeZone = getSettings().getTimeZone();
DateTime today = getSettings().clock().now(timeZone).withTimeAtStartOfDay();
CalendarEvent event = new CalendarEvent(getSettings(), provider.getContext(), provider.getWidgetId(),
timeZone, false);
event.setEventSource(provider.getFirstActiveEventSource());
event.setEventId(++eventId);
event.setTitle("Event that carry over to the next day, show as ending midnight");
event.setStartDate(today.plusHours(19));
event.setEndDate(today.plusDays(1).plusHours(7));

DateUtil.setNow(today.plusHours(10).plusMinutes(33));
getSettings().clock().setNow(today.plusHours(10).plusMinutes(33));
provider.addRow(event);
playResults(TAG);
CalendarEntry entry1 = null;
Expand Down Expand Up @@ -78,8 +77,8 @@ public void testEventWhichCarryOverToTheNextDay() {
public void testThreeDaysEvent() {
DateTime friday = dateTime(2015, 9, 18);
DateTime sunday = friday.plusDays(2);
CalendarEvent event = new CalendarEvent(provider.getContext(), provider.getWidgetId(),
provider.getSettings().getTimeZone(), false);
CalendarEvent event = new CalendarEvent(getSettings(), provider.getContext(), provider.getWidgetId(),
getSettings().getTimeZone(), false);
event.setEventSource(provider.getFirstActiveEventSource());
event.setEventId(++eventId);
event.setTitle("Leader's weekend");
Expand All @@ -102,7 +101,7 @@ private void assertSundayEntryAt(CalendarEvent event, DateTime sunday, DateTime
}

private CalendarEntry getSundayEntryAt(CalendarEvent event, DateTime currentDateTime) {
DateUtil.setNow(currentDateTime);
getSettings().clock().setNow(currentDateTime);
provider.clear();
provider.addRow(event);
Log.i(TAG, "getSundayEntryAt " + currentDateTime);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.andstatus.todoagenda;

import org.andstatus.todoagenda.calendar.CalendarEvent;
import org.andstatus.todoagenda.util.DateUtil;
import org.andstatus.todoagenda.widget.CalendarEntry;
import org.andstatus.todoagenda.widget.WidgetEntry;
import org.joda.time.DateTime;
Expand All @@ -24,16 +23,16 @@ public class OngoingEventTest extends BaseWidgetTest {
*/
@Test
public void testTodaysOngoingEvent() {
DateTime today = DateUtil.now(provider.getSettings().getTimeZone()).withTimeAtStartOfDay();
CalendarEvent event = new CalendarEvent(provider.getContext(), provider.getWidgetId(),
provider.getSettings().getTimeZone(), false);
DateTime today = getSettings().clock().now(getSettings().getTimeZone()).withTimeAtStartOfDay();
CalendarEvent event = new CalendarEvent(getSettings(), provider.getContext(), provider.getWidgetId(),
getSettings().getTimeZone(), false);
event.setEventSource(provider.getFirstActiveEventSource());
event.setEventId(++eventId);
event.setTitle("Ongoing event shows original start time");
event.setStartDate(today.plusHours(9));
event.setEndDate(today.plusHours(12));

DateUtil.setNow(today.plusHours(10).plusMinutes(33));
getSettings().clock().setNow(today.plusHours(10).plusMinutes(33));
provider.addRow(event);
playResults(TAG);
CalendarEntry entry = null;
Expand All @@ -54,16 +53,16 @@ public void testTodaysOngoingEvent() {
*/
@Test
public void testYesterdaysOngoingEvent() {
DateTime today = DateUtil.now(provider.getSettings().getTimeZone()).withTimeAtStartOfDay();
CalendarEvent event = new CalendarEvent(provider.getContext(), provider.getWidgetId(),
provider.getSettings().getTimeZone(), false);
DateTime today = getSettings().clock().now(getSettings().getTimeZone()).withTimeAtStartOfDay();
CalendarEvent event = new CalendarEvent(getSettings(), provider.getContext(), provider.getWidgetId(),
getSettings().getTimeZone(), false);
event.setEventSource(provider.getFirstActiveEventSource());
event.setEventId(++eventId);
event.setTitle("Ongoing event, which started yesterday, shows no start time");
event.setStartDate(today.minusDays(1).plusHours(9));
event.setEndDate(today.plusHours(12));

DateUtil.setNow(today.plusHours(10).plusMinutes(33));
getSettings().clock().setNow(today.plusHours(10).plusMinutes(33));
provider.addRow(event);
playResults(TAG);
CalendarEntry entry = null;
Expand All @@ -83,16 +82,16 @@ public void testYesterdaysOngoingEvent() {

@Test
public void testEventWhichCarryOverToTheNextDay() {
DateTime today = DateUtil.now(provider.getSettings().getTimeZone()).withTimeAtStartOfDay();
CalendarEvent event = new CalendarEvent(provider.getContext(), provider.getWidgetId(),
provider.getSettings().getTimeZone(), false);
DateTime today = getSettings().clock().now(getSettings().getTimeZone()).withTimeAtStartOfDay();
CalendarEvent event = new CalendarEvent(getSettings(), provider.getContext(), provider.getWidgetId(),
getSettings().getTimeZone(), false);
event.setEventSource(provider.getFirstActiveEventSource());
event.setEventId(++eventId);
event.setTitle("Event that carry over to the next day, show as ending midnight");
event.setStartDate(today.plusHours(19));
event.setEndDate(today.plusDays(1).plusHours(7));

DateUtil.setNow(today.plusHours(20).plusMinutes(33));
getSettings().clock().setNow(today.plusHours(20).plusMinutes(33));
provider.addRow(event);
playResults(TAG);
CalendarEntry entry = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package org.andstatus.todoagenda;

import android.util.Log;

import org.andstatus.todoagenda.provider.QueryResultsStorage;
import org.andstatus.todoagenda.util.DateUtil;
import org.andstatus.todoagenda.util.MyClock;
import org.andstatus.todoagenda.widget.CalendarEntry;
import org.andstatus.todoagenda.widget.LastEntry;
import org.andstatus.todoagenda.widget.TaskEntry;
Expand All @@ -12,8 +10,6 @@

import java.io.IOException;

import androidx.test.platform.app.InstrumentationRegistry;

import static org.junit.Assert.assertEquals;

/**
Expand All @@ -27,13 +23,12 @@ public class PastDueHeaderWithTasksTest extends BaseWidgetTest {
@Test
public void testPastDueHeaderWithTasks() throws IOException, JSONException {
final String method = "testPastDueHeaderWithTasks";
QueryResultsStorage inputs = provider.loadResultsAndSettings(InstrumentationRegistry.getInstrumentation().getContext(),
QueryResultsStorage inputs = provider.loadResultsAndSettings(
org.andstatus.todoagenda.tests.R.raw.past_due_header_with_tasks);
provider.addResults(inputs.getResults());
Log.d(method, "Results executed at " + inputs.getResults().get(0).getExecutedAt());

playResults(method);
assertEquals("Past and Due header", DateUtil.DATETIME_MIN, factory.getWidgetEntries().get(0).entryDate);
assertEquals("Past and Due header", MyClock.DATETIME_MIN, factory.getWidgetEntries().get(0).entryDate);
assertEquals("Past Calendar Entry", CalendarEntry.class, factory.getWidgetEntries().get(1).getClass());
assertEquals("Due task Entry", TaskEntry.class, factory.getWidgetEntries().get(2).getClass());
assertEquals("Due task Entry", dateTime(2019, 8, 1, 9, 0),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.andstatus.todoagenda.prefs.ApplicationPreferences;
import org.andstatus.todoagenda.provider.QueryRow;
import org.andstatus.todoagenda.util.DateUtil;
import org.andstatus.todoagenda.widget.CalendarEntry;
import org.andstatus.todoagenda.widget.WidgetEntry;
import org.joda.time.DateTime;
Expand All @@ -28,9 +27,9 @@ public class RecurringEventsTest extends BaseWidgetTest {
public void testShowRecurringEvents() {
generateEventInstances();
assertEquals("Entries: " + factory.getWidgetEntries().size(), 15, countCalendarEntries());
provider.startEditing();
provider.startEditingPreferences();
ApplicationPreferences.setShowOnlyClosestInstanceOfRecurringEvent(provider.getContext(), true);
provider.saveSettings();
provider.savePreferences();
generateEventInstances();
assertEquals("Entries: " + factory.getWidgetEntries().size(), 1, countCalendarEntries());
}
Expand All @@ -48,7 +47,7 @@ int countCalendarEntries() {
void generateEventInstances() {
EnvironmentChangedReceiver.sleep(MIN_MILLIS_BETWEEN_RELOADS);
provider.clear();
DateTime date = DateUtil.now(provider.getSettings().getTimeZone()).withTimeAtStartOfDay();
DateTime date = getSettings().clock().now(getSettings().getTimeZone()).withTimeAtStartOfDay();
long millis = date.getMillis() + TimeUnit.HOURS.toMillis(10);
eventId++;
for (int ind = 0; ind < 15; ind++) {
Expand Down
Loading

0 comments on commit fcc81ec

Please sign in to comment.