Skip to content

Commit ca3e233

Browse files
author
Federico Iosue
committed
Fixed #377 and added both unit test and Espresso test
1 parent 6e97710 commit ca3e233

5 files changed

Lines changed: 130 additions & 18 deletions

File tree

omniNotes/build.gradle

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,15 @@ android {
9191
sourceCompatibility JavaVersion.VERSION_1_8
9292
targetCompatibility JavaVersion.VERSION_1_8
9393
}
94-
9594
}
9695

9796
dependencies {
9897
testCompile 'junit:junit:4.12'
98+
testCompile 'org.mockito:mockito-all:1.9.5'
99+
testCompile 'org.powermock:powermock:1.6.5'
100+
testCompile 'org.powermock:powermock-module-junit4:1.6.5'
101+
testCompile 'org.powermock:powermock-api-mockito:1.6.5'
102+
99103
androidTestCompile 'com.android.support:support-annotations:23.4.0'
100104
androidTestCompile 'com.android.support.test:runner:0.5'
101105
androidTestCompile 'com.android.support.test:rules:0.5'
@@ -171,6 +175,7 @@ dependencies {
171175
}
172176
fossCompile 'com.jakewharton.timber:timber:4.5.1'
173177

178+
testCompile 'junit:junit:4.12'
174179
}
175180

176181
configurations {
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package it.feio.android.omninotes;
2+
3+
4+
import android.support.test.espresso.ViewInteraction;
5+
import android.support.test.rule.ActivityTestRule;
6+
import android.support.test.runner.AndroidJUnit4;
7+
import android.test.suitebuilder.annotation.LargeTest;
8+
import android.view.View;
9+
import android.view.ViewGroup;
10+
import android.view.ViewParent;
11+
12+
import org.hamcrest.Description;
13+
import org.hamcrest.Matcher;
14+
import org.hamcrest.TypeSafeMatcher;
15+
import org.hamcrest.core.IsInstanceOf;
16+
import org.junit.Rule;
17+
import org.junit.Test;
18+
import org.junit.runner.RunWith;
19+
20+
import static android.support.test.espresso.Espresso.onView;
21+
import static android.support.test.espresso.action.ViewActions.click;
22+
import static android.support.test.espresso.action.ViewActions.scrollTo;
23+
import static android.support.test.espresso.assertion.ViewAssertions.matches;
24+
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
25+
import static android.support.test.espresso.matcher.ViewMatchers.withId;
26+
import static android.support.test.espresso.matcher.ViewMatchers.withParent;
27+
import static android.support.test.espresso.matcher.ViewMatchers.withText;
28+
import static org.hamcrest.Matchers.allOf;
29+
import static org.hamcrest.Matchers.startsWith;
30+
31+
@LargeTest
32+
@RunWith(AndroidJUnit4.class)
33+
public class RemindersLifecycleTest {
34+
35+
@Rule
36+
public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);
37+
38+
@Test
39+
public void remindersLifecycle() {
40+
ViewInteraction viewInteraction = onView(
41+
allOf(withId(R.id.fab_expand_menu_button),
42+
withParent(withId(R.id.fab)),
43+
isDisplayed()));
44+
viewInteraction.perform(click());
45+
46+
ViewInteraction floatingActionButton = onView(
47+
allOf(withId(R.id.fab_note),
48+
withParent(withId(R.id.fab)),
49+
isDisplayed()));
50+
floatingActionButton.perform(click());
51+
52+
ViewInteraction linearLayout = onView(
53+
withId(R.id.reminder_layout));
54+
linearLayout.perform(scrollTo(), click());
55+
56+
ViewInteraction appCompatButton = onView(
57+
allOf(withId(R.id.done), withText("Done"), isDisplayed()));
58+
appCompatButton.perform(click());
59+
60+
ViewInteraction appCompatButton2 = onView(
61+
allOf(withId(R.id.done_button), withText("Done"), isDisplayed()));
62+
appCompatButton2.perform(click());
63+
64+
ViewInteraction appCompatButton3 = onView(
65+
allOf(withId(R.id.done), withText("Done"), isDisplayed()));
66+
appCompatButton3.perform(click());
67+
68+
ViewInteraction textView = onView(withId(R.id.datetime));
69+
textView.check(matches(withText(startsWith("Reminder set for"))));
70+
}
71+
72+
private static Matcher<View> childAtPosition(
73+
final Matcher<View> parentMatcher, final int position) {
74+
75+
return new TypeSafeMatcher<View>() {
76+
@Override
77+
public void describeTo(Description description) {
78+
description.appendText("Child at position " + position + " in parent ");
79+
parentMatcher.describeTo(description);
80+
}
81+
82+
@Override
83+
public boolean matchesSafely(View view) {
84+
ViewParent parent = view.getParent();
85+
return parent instanceof ViewGroup && parentMatcher.matches(parent)
86+
&& view.equals(((ViewGroup) parent).getChildAt(position));
87+
}
88+
};
89+
}
90+
}

omniNotes/src/main/java/it/feio/android/omninotes/DetailFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ private void initViewReminder() {
599599
int pickerType = prefs.getBoolean("settings_simple_calendar", false) ? ReminderPickers.TYPE_AOSP :
600600
ReminderPickers.TYPE_GOOGLE;
601601
ReminderPickers reminderPicker = new ReminderPickers(mainActivity, mFragment, pickerType);
602-
reminderPicker.pick(DateUtils.getPresetReminder(Long.parseLong(noteTmp.getAlarm())), noteTmp
602+
reminderPicker.pick(DateUtils.getPresetReminder(noteTmp.getAlarm()), noteTmp
603603
.getRecurrenceRule());
604604
onDateSetListener = reminderPicker;
605605
onTimeSetListener = reminderPicker;

omniNotes/src/main/java/it/feio/android/omninotes/utils/date/DateUtils.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ public static long getPresetReminder(long currentReminder) {
163163
return now > currentReminder ? getNextMinute() : currentReminder;
164164
}
165165

166+
public static Long getPresetReminder(String alarm) {
167+
long alarmChecked = alarm == null ? 0 : Long.parseLong(alarm);
168+
return getPresetReminder(alarmChecked);
169+
}
170+
166171

167172
public static boolean isFuture(String timestamp) {
168173
try {

omniNotes/src/test/java/it/feio/android/omninotes/utils/date/DateUtilsTest.java

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,44 @@
1919

2020
import org.junit.Assert;
2121
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.powermock.api.mockito.PowerMockito;
24+
import org.powermock.core.classloader.annotations.PrepareForTest;
25+
import org.powermock.modules.junit4.PowerMockRunner;
2226

2327
import java.util.Calendar;
2428
import java.util.Locale;
2529

26-
30+
@RunWith(PowerMockRunner.class)
31+
@PrepareForTest({DateUtils.class})
2732
public class DateUtilsTest {
2833

29-
@Test
30-
public void prettyTime() {
31-
long now = Calendar.getInstance().getTimeInMillis();
34+
@Test
35+
public void prettyTime() {
36+
long now = Calendar.getInstance().getTimeInMillis();
37+
38+
String prettyTime = DateUtils.prettyTime(now, Locale.ENGLISH);
39+
Assert.assertEquals(prettyTime.toLowerCase(), "moments ago");
3240

33-
String prettyTime = DateUtils.prettyTime(now, Locale.ENGLISH);
34-
Assert.assertEquals(prettyTime.toLowerCase(), "moments ago");
41+
prettyTime = DateUtils.prettyTime(now + 10 * 60 * 1000, Locale.ENGLISH);
42+
Assert.assertEquals(prettyTime.toLowerCase(), "10 minutes from now");
3543

36-
prettyTime = DateUtils.prettyTime(now + 10 * 60 * 1000, Locale.ENGLISH);
37-
Assert.assertEquals(prettyTime.toLowerCase(), "10 minutes from now");
44+
prettyTime = DateUtils.prettyTime(now + 24 * 60 * 60 * 1000, Locale.ITALIAN);
45+
Assert.assertEquals(prettyTime.toLowerCase(), "fra 24 ore");
3846

39-
prettyTime = DateUtils.prettyTime(now + 24 * 60 * 60 * 1000, Locale.ITALIAN);
40-
Assert.assertEquals(prettyTime.toLowerCase(), "fra 24 ore");
47+
prettyTime = DateUtils.prettyTime(now + 25 * 60 * 60 * 1000, Locale.ITALIAN);
48+
Assert.assertEquals(prettyTime.toLowerCase(), "fra 1 giorno");
4149

42-
prettyTime = DateUtils.prettyTime(now + 25 * 60 * 60 * 1000, Locale.ITALIAN);
43-
Assert.assertEquals(prettyTime.toLowerCase(), "fra 1 giorno");
50+
prettyTime = DateUtils.prettyTime(null, Locale.JAPANESE);
51+
Assert.assertNotNull(prettyTime.toLowerCase());
52+
Assert.assertEquals(prettyTime.toLowerCase().length(), 0);
53+
}
4454

45-
prettyTime = DateUtils.prettyTime(null, Locale.JAPANESE);
46-
Assert.assertNotNull(prettyTime.toLowerCase());
47-
Assert.assertEquals(prettyTime.toLowerCase().length(), 0);
48-
}
55+
@Test
56+
public void getPresetReminder() {
57+
long mockedNextMinute = 1497315847L;
58+
PowerMockito.stub(PowerMockito.method(DateUtils.class, "getNextMinute")).toReturn(mockedNextMinute);
59+
Assert.assertTrue(mockedNextMinute == DateUtils.getPresetReminder(null));
60+
}
4961

5062
}

0 commit comments

Comments
 (0)