Skip to content

Commit

Permalink
Fixed #377 and added both unit test and Espresso test
Browse files Browse the repository at this point in the history
  • Loading branch information
Federico Iosue committed Jun 13, 2017
1 parent 6e97710 commit ca3e233
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 18 deletions.
7 changes: 6 additions & 1 deletion omniNotes/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -171,6 +175,7 @@ dependencies {
}
fossCompile 'com.jakewharton.timber:timber:4.5.1'

testCompile 'junit:junit:4.12'
}

configurations {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<MainActivity> 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<View> childAtPosition(
final Matcher<View> parentMatcher, final int position) {

return new TypeSafeMatcher<View>() {
@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));
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

}

0 comments on commit ca3e233

Please sign in to comment.