Skip to content

Commit

Permalink
Fix date time tests (getodk#2846)
Browse files Browse the repository at this point in the history
* Remove @Suppress annotation from getDateTimeLabelTest

* Reuse date object and set locale as "en" only once

* Set default timezone as GMT

* Generate Date using Calendar instead of in millis

* Fix expected date for islamic date picker

* Reset default locale and timezone after test

This is to prevent polluting this test case from polluting other ones
  • Loading branch information
shobhitagarwal1612 authored and grzesiek2010 committed Mar 4, 2019
1 parent 98a0bde commit 35198a0
Showing 1 changed file with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@
package org.odk.collect.android.utilities;

import android.content.Context;
import android.support.test.filters.Suppress;
import android.support.test.runner.AndroidJUnit4;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.logic.DatePickerDetails;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import static org.junit.Assert.assertEquals;

Expand All @@ -41,6 +43,8 @@ public class DateTimeUtilsTest {
private DatePickerDetails bikramSambatDatePickerDetails;

private Context context;
private Locale defaultLocale;
private TimeZone defaultTimezone;

@Before
public void setUp() {
Expand All @@ -51,31 +55,39 @@ public void setUp() {
bikramSambatDatePickerDetails = new DatePickerDetails(DatePickerDetails.DatePickerType.BIKRAM_SAMBAT, DatePickerDetails.DatePickerMode.SPINNERS);

context = Collect.getInstance();
defaultLocale = Locale.getDefault();
defaultTimezone = TimeZone.getDefault();
}

@Test
@Suppress
public void getDateTimeLabelTest() {
long dateInMilliseconds = 687967200000L; // 20 Oct 1991 14:00

Locale.setDefault(Locale.ENGLISH);
assertEquals("Oct 20, 1991", DateTimeUtils.getDateTimeLabel(new Date(dateInMilliseconds), gregorianDatePickerDetails, false, context));
assertEquals("Oct 20, 1991, 14:00", DateTimeUtils.getDateTimeLabel(new Date(dateInMilliseconds), gregorianDatePickerDetails, true, context));
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));

Locale.setDefault(Locale.ENGLISH);
assertEquals("9 Tikimt 1984 (Oct 20, 1991)", DateTimeUtils.getDateTimeLabel(new Date(dateInMilliseconds), ethiopianDatePickerDetails, false, context));
assertEquals("9 Tikimt 1984, 14:00 (Oct 20, 1991, 14:00)", DateTimeUtils.getDateTimeLabel(new Date(dateInMilliseconds), ethiopianDatePickerDetails, true, context));
// 20 Oct 1991 14:00 GMT
Calendar calendar = Calendar.getInstance();
calendar.set(1991, 9, 20, 14, 0, 0);
Date date = calendar.getTime();

Locale.setDefault(Locale.ENGLISH);
assertEquals("9 Paopi 1708 (Oct 20, 1991)", DateTimeUtils.getDateTimeLabel(new Date(dateInMilliseconds), copticDatePickerDetails, false, context));
assertEquals("9 Paopi 1708, 14:00 (Oct 20, 1991, 14:00)", DateTimeUtils.getDateTimeLabel(new Date(dateInMilliseconds), copticDatePickerDetails, true, context));
assertEquals("Oct 20, 1991", DateTimeUtils.getDateTimeLabel(date, gregorianDatePickerDetails, false, context));
assertEquals("Oct 20, 1991, 14:00", DateTimeUtils.getDateTimeLabel(date, gregorianDatePickerDetails, true, context));

Locale.setDefault(Locale.ENGLISH);
assertEquals("11 Rabī‘ ath-thānī 1412 (Oct 20, 1991)", DateTimeUtils.getDateTimeLabel(new Date(dateInMilliseconds), islamicDatePickerDetails, false, context));
assertEquals("11 Rabī‘ ath-thānī 1412, 14:00 (Oct 20, 1991, 14:00)", DateTimeUtils.getDateTimeLabel(new Date(dateInMilliseconds), islamicDatePickerDetails, true, context));
assertEquals("9 Tikimt 1984 (Oct 20, 1991)", DateTimeUtils.getDateTimeLabel(date, ethiopianDatePickerDetails, false, context));
assertEquals("9 Tikimt 1984, 14:00 (Oct 20, 1991, 14:00)", DateTimeUtils.getDateTimeLabel(date, ethiopianDatePickerDetails, true, context));

Locale.setDefault(Locale.ENGLISH);
assertEquals("3 कार्तिक 2048 (Oct 20, 1991)", DateTimeUtils.getDateTimeLabel(new Date(dateInMilliseconds), bikramSambatDatePickerDetails, false, context));
assertEquals("3 कार्तिक 2048, 14:00 (Oct 20, 1991, 14:00)", DateTimeUtils.getDateTimeLabel(new Date(dateInMilliseconds), bikramSambatDatePickerDetails, true, context));
assertEquals("9 Paopi 1708 (Oct 20, 1991)", DateTimeUtils.getDateTimeLabel(date, copticDatePickerDetails, false, context));
assertEquals("9 Paopi 1708, 14:00 (Oct 20, 1991, 14:00)", DateTimeUtils.getDateTimeLabel(date, copticDatePickerDetails, true, context));

assertEquals("11 Rabi' al-thani 1412 (Oct 20, 1991)", DateTimeUtils.getDateTimeLabel(date, islamicDatePickerDetails, false, context));
assertEquals("11 Rabi' al-thani 1412, 14:00 (Oct 20, 1991, 14:00)", DateTimeUtils.getDateTimeLabel(date, islamicDatePickerDetails, true, context));

assertEquals("3 कार्तिक 2048 (Oct 20, 1991)", DateTimeUtils.getDateTimeLabel(date, bikramSambatDatePickerDetails, false, context));
assertEquals("3 कार्तिक 2048, 14:00 (Oct 20, 1991, 14:00)", DateTimeUtils.getDateTimeLabel(date, bikramSambatDatePickerDetails, true, context));
}

@After
public void resetTimeZone() {
Locale.setDefault(defaultLocale);
TimeZone.setDefault(defaultTimezone);
}
}

0 comments on commit 35198a0

Please sign in to comment.