From ca3e2332bf8a585e3fc46498b6a3c39c01f6f258 Mon Sep 17 00:00:00 2001 From: Federico Iosue Date: Tue, 13 Jun 2017 04:11:01 +0200 Subject: [PATCH] Fixed #377 and added both unit test and Espresso test --- omniNotes/build.gradle | 7 +- .../omninotes/RemindersLifecycleTest.java | 90 +++++++++++++++++++ .../android/omninotes/DetailFragment.java | 2 +- .../omninotes/utils/date/DateUtils.java | 5 ++ .../omninotes/utils/date/DateUtilsTest.java | 44 +++++---- 5 files changed, 130 insertions(+), 18 deletions(-) create mode 100644 omniNotes/src/androidTest/java/it/feio/android/omninotes/RemindersLifecycleTest.java diff --git a/omniNotes/build.gradle b/omniNotes/build.gradle index 7ad7367b68..10acd4a457 100644 --- a/omniNotes/build.gradle +++ b/omniNotes/build.gradle @@ -91,11 +91,15 @@ android { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } - } dependencies { testCompile 'junit:junit:4.12' + testCompile 'org.mockito:mockito-all:1.9.5' + testCompile 'org.powermock:powermock:1.6.5' + testCompile 'org.powermock:powermock-module-junit4:1.6.5' + testCompile 'org.powermock:powermock-api-mockito:1.6.5' + androidTestCompile 'com.android.support:support-annotations:23.4.0' androidTestCompile 'com.android.support.test:runner:0.5' androidTestCompile 'com.android.support.test:rules:0.5' @@ -171,6 +175,7 @@ dependencies { } fossCompile 'com.jakewharton.timber:timber:4.5.1' + testCompile 'junit:junit:4.12' } configurations { diff --git a/omniNotes/src/androidTest/java/it/feio/android/omninotes/RemindersLifecycleTest.java b/omniNotes/src/androidTest/java/it/feio/android/omninotes/RemindersLifecycleTest.java new file mode 100644 index 0000000000..4fb8966467 --- /dev/null +++ b/omniNotes/src/androidTest/java/it/feio/android/omninotes/RemindersLifecycleTest.java @@ -0,0 +1,90 @@ +package it.feio.android.omninotes; + + +import android.support.test.espresso.ViewInteraction; +import android.support.test.rule.ActivityTestRule; +import android.support.test.runner.AndroidJUnit4; +import android.test.suitebuilder.annotation.LargeTest; +import android.view.View; +import android.view.ViewGroup; +import android.view.ViewParent; + +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeMatcher; +import org.hamcrest.core.IsInstanceOf; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static android.support.test.espresso.Espresso.onView; +import static android.support.test.espresso.action.ViewActions.click; +import static android.support.test.espresso.action.ViewActions.scrollTo; +import static android.support.test.espresso.assertion.ViewAssertions.matches; +import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; +import static android.support.test.espresso.matcher.ViewMatchers.withId; +import static android.support.test.espresso.matcher.ViewMatchers.withParent; +import static android.support.test.espresso.matcher.ViewMatchers.withText; +import static org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.startsWith; + +@LargeTest +@RunWith(AndroidJUnit4.class) +public class RemindersLifecycleTest { + + @Rule + public ActivityTestRule mActivityTestRule = new ActivityTestRule<>(MainActivity.class); + + @Test + public void remindersLifecycle() { + ViewInteraction viewInteraction = onView( + allOf(withId(R.id.fab_expand_menu_button), + withParent(withId(R.id.fab)), + isDisplayed())); + viewInteraction.perform(click()); + + ViewInteraction floatingActionButton = onView( + allOf(withId(R.id.fab_note), + withParent(withId(R.id.fab)), + isDisplayed())); + floatingActionButton.perform(click()); + + ViewInteraction linearLayout = onView( + withId(R.id.reminder_layout)); + linearLayout.perform(scrollTo(), click()); + + ViewInteraction appCompatButton = onView( + allOf(withId(R.id.done), withText("Done"), isDisplayed())); + appCompatButton.perform(click()); + + ViewInteraction appCompatButton2 = onView( + allOf(withId(R.id.done_button), withText("Done"), isDisplayed())); + appCompatButton2.perform(click()); + + ViewInteraction appCompatButton3 = onView( + allOf(withId(R.id.done), withText("Done"), isDisplayed())); + appCompatButton3.perform(click()); + + ViewInteraction textView = onView(withId(R.id.datetime)); + textView.check(matches(withText(startsWith("Reminder set for")))); + } + + private static Matcher childAtPosition( + final Matcher parentMatcher, final int position) { + + return new TypeSafeMatcher() { + @Override + public void describeTo(Description description) { + description.appendText("Child at position " + position + " in parent "); + parentMatcher.describeTo(description); + } + + @Override + public boolean matchesSafely(View view) { + ViewParent parent = view.getParent(); + return parent instanceof ViewGroup && parentMatcher.matches(parent) + && view.equals(((ViewGroup) parent).getChildAt(position)); + } + }; + } +} diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/DetailFragment.java b/omniNotes/src/main/java/it/feio/android/omninotes/DetailFragment.java index bfa3c169d5..b921df3384 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/DetailFragment.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/DetailFragment.java @@ -599,7 +599,7 @@ private void initViewReminder() { int pickerType = prefs.getBoolean("settings_simple_calendar", false) ? ReminderPickers.TYPE_AOSP : ReminderPickers.TYPE_GOOGLE; ReminderPickers reminderPicker = new ReminderPickers(mainActivity, mFragment, pickerType); - reminderPicker.pick(DateUtils.getPresetReminder(Long.parseLong(noteTmp.getAlarm())), noteTmp + reminderPicker.pick(DateUtils.getPresetReminder(noteTmp.getAlarm()), noteTmp .getRecurrenceRule()); onDateSetListener = reminderPicker; onTimeSetListener = reminderPicker; diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/utils/date/DateUtils.java b/omniNotes/src/main/java/it/feio/android/omninotes/utils/date/DateUtils.java index f82cfbdb5b..457e87df7a 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/utils/date/DateUtils.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/utils/date/DateUtils.java @@ -163,6 +163,11 @@ public static long getPresetReminder(long currentReminder) { return now > currentReminder ? getNextMinute() : currentReminder; } + public static Long getPresetReminder(String alarm) { + long alarmChecked = alarm == null ? 0 : Long.parseLong(alarm); + return getPresetReminder(alarmChecked); + } + public static boolean isFuture(String timestamp) { try { diff --git a/omniNotes/src/test/java/it/feio/android/omninotes/utils/date/DateUtilsTest.java b/omniNotes/src/test/java/it/feio/android/omninotes/utils/date/DateUtilsTest.java index b2831b3582..f42a8d93f6 100644 --- a/omniNotes/src/test/java/it/feio/android/omninotes/utils/date/DateUtilsTest.java +++ b/omniNotes/src/test/java/it/feio/android/omninotes/utils/date/DateUtilsTest.java @@ -19,32 +19,44 @@ import org.junit.Assert; import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; import java.util.Calendar; import java.util.Locale; - +@RunWith(PowerMockRunner.class) +@PrepareForTest({DateUtils.class}) public class DateUtilsTest { - @Test - public void prettyTime() { - long now = Calendar.getInstance().getTimeInMillis(); + @Test + public void prettyTime() { + long now = Calendar.getInstance().getTimeInMillis(); + + String prettyTime = DateUtils.prettyTime(now, Locale.ENGLISH); + Assert.assertEquals(prettyTime.toLowerCase(), "moments ago"); - String prettyTime = DateUtils.prettyTime(now, Locale.ENGLISH); - Assert.assertEquals(prettyTime.toLowerCase(), "moments ago"); + prettyTime = DateUtils.prettyTime(now + 10 * 60 * 1000, Locale.ENGLISH); + Assert.assertEquals(prettyTime.toLowerCase(), "10 minutes from now"); - prettyTime = DateUtils.prettyTime(now + 10 * 60 * 1000, Locale.ENGLISH); - Assert.assertEquals(prettyTime.toLowerCase(), "10 minutes from now"); + prettyTime = DateUtils.prettyTime(now + 24 * 60 * 60 * 1000, Locale.ITALIAN); + Assert.assertEquals(prettyTime.toLowerCase(), "fra 24 ore"); - prettyTime = DateUtils.prettyTime(now + 24 * 60 * 60 * 1000, Locale.ITALIAN); - Assert.assertEquals(prettyTime.toLowerCase(), "fra 24 ore"); + prettyTime = DateUtils.prettyTime(now + 25 * 60 * 60 * 1000, Locale.ITALIAN); + Assert.assertEquals(prettyTime.toLowerCase(), "fra 1 giorno"); - prettyTime = DateUtils.prettyTime(now + 25 * 60 * 60 * 1000, Locale.ITALIAN); - Assert.assertEquals(prettyTime.toLowerCase(), "fra 1 giorno"); + prettyTime = DateUtils.prettyTime(null, Locale.JAPANESE); + Assert.assertNotNull(prettyTime.toLowerCase()); + Assert.assertEquals(prettyTime.toLowerCase().length(), 0); + } - prettyTime = DateUtils.prettyTime(null, Locale.JAPANESE); - Assert.assertNotNull(prettyTime.toLowerCase()); - Assert.assertEquals(prettyTime.toLowerCase().length(), 0); - } + @Test + public void getPresetReminder() { + long mockedNextMinute = 1497315847L; + PowerMockito.stub(PowerMockito.method(DateUtils.class, "getNextMinute")).toReturn(mockedNextMinute); + Assert.assertTrue(mockedNextMinute == DateUtils.getPresetReminder(null)); + } }