Skip to content

Commit

Permalink
More tests for reminders' RecurrenceRules workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
federicoiosue committed Dec 26, 2019
1 parent 0ff8e2a commit 6a64dd2
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 15 deletions.
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.1'
classpath 'me.tatarka.retrolambda.projectlombok:lombok.ast:0.2.3.a2'
classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.8"
classpath 'com.dicedmelon.gradle:jacoco-android:0.1.4'
}
Expand Down
8 changes: 8 additions & 0 deletions omniNotes/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ android {
multiDexEnabled true
}

testOptions {
unitTests.returnDefaultValues = true
}

packagingOptions {
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
Expand Down Expand Up @@ -107,6 +111,7 @@ android {
applicationId project.PACKAGE + ".alpha"
}
}

lintOptions {
abortOnError false
disable 'MissingTranslation'
Expand All @@ -132,6 +137,9 @@ dependencies {
exclude group: 'com.android.support', module: 'support-annotations'
}

compileOnly "org.projectlombok:lombok:1.18.10"
annotationProcessor 'org.projectlombok:lombok:1.18.10'

implementation 'com.larswerkman:HoloColorPicker:1.4'
implementation 'de.keyboardsurfer.android.widget:crouton:1.8.4@aar'
implementation 'com.google.android.apps.dashclock:dashclock-api:2.0.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package it.feio.android.omninotes;


import static androidx.test.espresso.Espresso.onData;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
Expand All @@ -29,12 +30,14 @@
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withParent;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anything;
import static org.hamcrest.Matchers.is;

import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import androidx.core.view.GravityCompat;
import androidx.test.espresso.DataInteraction;
import androidx.test.espresso.ViewInteraction;
import androidx.test.rule.ActivityTestRule;
import org.hamcrest.Description;
Expand Down Expand Up @@ -108,6 +111,15 @@ void createNote (String title, String content) {
navigateUp();
}

void selectNoteInList(int number) {
onData(anything())
.inAdapterView(allOf(withId(R.id.list),
childAtPosition(
withClassName(is("android.widget.FrameLayout")),
0)))
.atPosition(number).perform(click());
}

void navigateUp () {
onView(allOf(childAtPosition(allOf(withId(R.id.toolbar),
childAtPosition(withClassName(is("android.widget.RelativeLayout")), 0)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (C) 2013-2019 Federico Iosue ([email protected])
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package it.feio.android.omninotes;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withContentDescription;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import it.feio.android.omninotes.helpers.date.RecurrenceHelper;
import it.feio.android.omninotes.models.Note;
import java.util.Calendar;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
public class RecurrenceRuleTest extends BaseEspressoTest {

private Note testNote;

@Before
@Override
public void setUp () throws Exception {
super.setUp();
testNote = new Note();
testNote.setTitle("Title 1");
Calendar c = Calendar.getInstance();
c.add(Calendar.MINUTE, 2);
testNote.setAlarm(c.getTimeInMillis());
testNote.setRecurrenceRule("FREQ=WEEKLY;WKST=MO;BYDAY=MO,TU,TH");
dbHelper.updateNote(testNote, false);
}

@Test
public void recurrenceRuleTextHasValue () throws InterruptedException {

Thread.sleep(1500);

onView(allOf(withId(R.id.menu_sort), withContentDescription("Sort"),
childAtPosition(
childAtPosition(
withId(R.id.toolbar),
2),
1),
isDisplayed())).perform(click());

Thread.sleep(1500);

onView(allOf(withId(R.id.title), withText("Creation date"),
childAtPosition(
childAtPosition(
withId(R.id.content),
0),
0),
isDisplayed())).perform(click());

selectNoteInList(0);

String expectedText = RecurrenceHelper.getNoteRecurrentReminderText(Long.parseLong(testNote.getAlarm()),
testNote.getRecurrenceRule());
onView(withId(R.id.datetime)).check(matches(withText(expectedText)));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2013-2019 Federico Iosue ([email protected])
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package it.feio.android.omninotes.utils.date;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import it.feio.android.omninotes.helpers.date.RecurrenceHelper;
import java.util.Calendar;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
public class RecurrenceHelperTest {

@Test
public void testGetNoteRecurrentReminderText () {
long reminder = 1577369824425L;
String rrule = "FREQ=WEEKLY;WKST=MO;BYDAY=MO,TU,TH";
String alarmText = RecurrenceHelper.getNoteRecurrentReminderText(reminder, rrule);

Assert.assertEquals("Weekly on Mon, Tue, Thu starting from Thu, Dec 26 3:17 PM", alarmText);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import static com.appeaser.sublimepickerlibrary.recurrencepicker.SublimeRecurrencePicker.RecurrenceOption.DOES_NOT_REPEAT;

import android.content.Context;
import android.text.TextUtils;
import org.apache.commons.lang3.StringUtils;
import android.text.format.Time;
import com.appeaser.sublimepickerlibrary.recurrencepicker.EventRecurrence;
import com.appeaser.sublimepickerlibrary.recurrencepicker.EventRecurrenceFormatter;
Expand All @@ -43,24 +43,23 @@ private RecurrenceHelper() {
}

public static String formatRecurrence (Context mContext, String recurrenceRule) {
if (!TextUtils.isEmpty(recurrenceRule)) {
EventRecurrence recurrenceEvent = new EventRecurrence();
recurrenceEvent.setStartDate(new Time("" + new Date().getTime()));
recurrenceEvent.parse(recurrenceRule);
return EventRecurrenceFormatter.getRepeatString(mContext.getApplicationContext(),
mContext.getResources(), recurrenceEvent, true);
} else {
if (StringUtils.isEmpty(recurrenceRule)) {
return "";
}
EventRecurrence recurrenceEvent = new EventRecurrence();
recurrenceEvent.setStartDate(new Time("" + new Date().getTime()));
recurrenceEvent.parse(recurrenceRule);
return EventRecurrenceFormatter.getRepeatString(mContext.getApplicationContext(),
mContext.getResources(), recurrenceEvent, true);
}

public static Long nextReminderFromRecurrenceRule (long reminder, String recurrenceRule) {
return nextReminderFromRecurrenceRule(reminder, Calendar.getInstance().getTimeInMillis(), recurrenceRule);
}

public static Long nextReminderFromRecurrenceRule (long reminder, long currentTime, String recurrenceRule) {
RRule rule = new RRule();
try {
RRule rule= new RRule();
rule.setValue(recurrenceRule);
long startTimestamp = reminder + 60 * 1000;
if (startTimestamp < currentTime) {
Expand All @@ -70,8 +69,8 @@ public static Long nextReminderFromRecurrenceRule (long reminder, long currentTi
return nextDate == null ? 0L : nextDate.getTime();
} catch (ParseException e) {
LogDelegate.e("Error parsing rrule");
return 0L;
}
return 0L;
}

public static String getNoteReminderText (long reminder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,28 @@ public class RecurrenceHelperTest {
private int MILLISEC_TO_HOURS_RATIO = 60 * 60 * 1000;

@Test
public void nextReminderFromRecurrenceRule () {
public void nextReminderFromRecurrenceRule_daily () {
long currentTime = Calendar.getInstance().getTimeInMillis();
long reminder = Calendar.getInstance().getTimeInMillis() + TEN_MINUTES;

// Daily test
String rruleDaily = "FREQ=DAILY;COUNT=30;WKST=MO";
long nextReminder = RecurrenceHelper.nextReminderFromRecurrenceRule(reminder, currentTime, rruleDaily);

Assert.assertNotEquals(0, nextReminder);
Assert.assertEquals(24 - 1, (nextReminder - reminder) / MILLISEC_TO_HOURS_RATIO);
}

@Test
public void nextReminderFromRecurrenceRule_3days () {
long currentTime = Calendar.getInstance().getTimeInMillis();
long reminder = Calendar.getInstance().getTimeInMillis() + TEN_MINUTES;

// 3-Daily test
String rruleDaily2 = "FREQ=DAILY;COUNT=30;INTERVAL=3";
long nextReminder2 = RecurrenceHelper.nextReminderFromRecurrenceRule(reminder, currentTime, rruleDaily2);
Assert.assertNotEquals(0, nextReminder2);
long delta = (nextReminder2 - reminder) / MILLISEC_TO_HOURS_RATIO;
Assert.assertTrue(delta == 3 * 24 - 2 || delta == 3 * 24 - 1); // The results depends on the day of week

Assert.assertTrue(delta == 3 * 24 - 2 || delta == 3 * 24 - 1); // The results depends on the day of week
}

}

0 comments on commit 6a64dd2

Please sign in to comment.