Skip to content

Commit

Permalink
Factored out TimeZoneUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek2010 committed Feb 27, 2023
1 parent 1613abc commit c337d92
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import android.content.Context;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.joda.time.DateTimeZone;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
Expand All @@ -28,6 +27,7 @@
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.logic.DatePickerDetails;
import org.odk.collect.android.widgets.utilities.DateTimeWidgetUtils;
import org.odk.collect.testshared.TimeZoneUtils;

import java.util.Calendar;
import java.util.Date;
Expand Down Expand Up @@ -69,8 +69,7 @@ public void setUp() {
@Test
public void getDateTimeLabelTest() {
Locale.setDefault(Locale.ENGLISH);
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
DateTimeZone.setDefault(DateTimeZone.forID("GMT"));
TimeZoneUtils.setTimezone(TimeZone.getTimeZone("GMT"));

// 20 Oct 1991 14:00 GMT
Calendar calendar = Calendar.getInstance();
Expand Down Expand Up @@ -102,7 +101,6 @@ public void getDateTimeLabelTest() {
@After
public void resetTimeZone() {
Locale.setDefault(defaultLocale);
TimeZone.setDefault(defaultTimezone);
DateTimeZone.setDefault(DateTimeZone.forID(defaultTimezone.getID()));
TimeZoneUtils.setTimezone(defaultTimezone);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

import org.javarosa.core.model.data.TimeData;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDateTime;
import org.joda.time.chrono.GregorianChronology;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.odk.collect.testshared.TimeZoneUtils;

import static org.junit.Assert.assertEquals;

Expand Down Expand Up @@ -83,16 +83,14 @@ public void getDateWithSkippedDaylightSavingGapIfExists_returnsCorrectDateAndTim

@Test
public void skipDaylightSavingGapIfExistsTest() {
DateTimeZone originalDefaultTimeZone = DateTimeZone.getDefault();
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Warsaw"));
DateTimeZone.setDefault(DateTimeZone.forID("Europe/Warsaw"));
TimeZone originalDefaultTimeZone = TimeZone.getDefault();
TimeZoneUtils.setTimezone(TimeZone.getTimeZone("Europe/Warsaw"));

// 29 March 2020 at 02:00:00 clocks were turned forward to 03:00:00
LocalDateTime ldtOriginal = new LocalDateTime().withYear(2020).withMonthOfYear(3).withDayOfMonth(29).withHourOfDay(2).withMinuteOfHour(30).withSecondOfMinute(0).withMillisOfSecond(0);
LocalDateTime ldtExpected = new LocalDateTime().withYear(2020).withMonthOfYear(3).withDayOfMonth(29).withHourOfDay(3).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);

assertEquals(ldtExpected, DateTimeUtils.skipDaylightSavingGapIfExists(ldtOriginal));
TimeZone.setDefault(TimeZone.getTimeZone(originalDefaultTimeZone.getID()));
DateTimeZone.setDefault(originalDefaultTimeZone);
TimeZoneUtils.setTimezone(originalDefaultTimeZone);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.javarosa.core.model.QuestionDef;
import org.javarosa.form.api.FormEntryPrompt;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDateTime;
import org.junit.After;
import org.junit.Before;
Expand All @@ -42,6 +41,7 @@
import org.odk.collect.android.widgets.DateTimeWidget;
import org.odk.collect.android.widgets.DateWidget;
import org.odk.collect.android.widgets.utilities.DateTimeWidgetUtils;
import org.odk.collect.testshared.TimeZoneUtils;

import java.util.TimeZone;

Expand Down Expand Up @@ -70,15 +70,13 @@ public void setUp() {

@After
public void tearDown() {
TimeZone.setDefault(currentTimeZone);
DateTimeZone.setDefault(DateTimeZone.forID(currentTimeZone.getID()));
TimeZoneUtils.setTimezone(currentTimeZone);
}

@Test
// 26 Mar 2017 at 02:00:00 clocks were turned forward to 03:00:00.
public void testESTTimeZoneWithDateTimeWidget() {
TimeZone.setDefault(TimeZone.getTimeZone(CET_TIME_ZONE));
DateTimeZone.setDefault(DateTimeZone.forID(CET_TIME_ZONE));
TimeZoneUtils.setTimezone(TimeZone.getTimeZone(CET_TIME_ZONE));

DateTimeWidget dateTimeWidget = prepareDateTimeWidget(2017, 3, 26, 2, 30);

Expand All @@ -92,8 +90,7 @@ public void testESTTimeZoneWithDateTimeWidget() {
@Test
// 1 Jan 1960 at 00:00:00 clocks were turned forward to 00:15:00
public void testEATTimezoneWithDateWidget() {
TimeZone.setDefault(TimeZone.getTimeZone(EAT_IME_ZONE));
DateTimeZone.setDefault(DateTimeZone.forID(EAT_IME_ZONE));
TimeZoneUtils.setTimezone(TimeZone.getTimeZone(EAT_IME_ZONE));

DateWidget dateWidget = prepareDateWidget(1960, 0, 1);

Expand Down
1 change: 1 addition & 0 deletions testshared/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ dependencies {
implementation Dependencies.junit
implementation Dependencies.androidx_test_espresso_intents
implementation Dependencies.android_material
implementation Dependencies.danlew_android_joda
implementation(Dependencies.androidx_fragment_testing) {
exclude group: 'androidx.test', module: 'monitor' //fixes issue https://github.com/android/android-test/issues/731
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.odk.collect.testshared

import org.joda.time.DateTimeZone
import java.util.TimeZone

object TimeZoneUtils {
/**
* Always update both java.util.TimeZone and org.joda.time.DateTimeZone to avoid weird bugs in
* tests that depend on time zones.
*/
@JvmStatic
fun setTimezone(timezone: TimeZone) {
TimeZone.setDefault(timezone)
DateTimeZone.setDefault(DateTimeZone.forID(timezone.id))
}
}

0 comments on commit c337d92

Please sign in to comment.