-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Restore notifications when app starts #1509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from 1 commit
33100df
0d1d8a9
794289b
09c46f5
857e21a
68f2639
4174359
f1c104a
015c0d1
1540427
e65357b
ee0663e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| package org.isoron.uhabits.core.ui | ||
|
|
||
| import org.hamcrest.CoreMatchers.equalTo | ||
| import org.hamcrest.MatcherAssert.assertThat | ||
| import org.isoron.uhabits.core.BaseUnitTest | ||
| import org.isoron.uhabits.core.models.Habit | ||
| import org.isoron.uhabits.core.models.HabitList | ||
| import org.isoron.uhabits.core.models.Reminder | ||
| import org.isoron.uhabits.core.models.Timestamp | ||
| import org.isoron.uhabits.core.models.WeekdayList | ||
| import org.isoron.uhabits.core.preferences.Preferences | ||
| import org.isoron.uhabits.core.preferences.Preferences.Storage | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
|
|
||
| class NotificationTrayTest : BaseUnitTest() { | ||
| private val systemTray = object : NotificationTray.SystemTray { | ||
| override fun removeNotification(notificationId: Int) {} | ||
|
|
||
| override fun showNotification( | ||
| habit: Habit, | ||
| notificationId: Int, | ||
| timestamp: Timestamp, | ||
| reminderTime: Long, | ||
| silent: Boolean | ||
| ) { | ||
| } | ||
|
|
||
| override fun log(msg: String) {} | ||
| } | ||
|
|
||
| private var preferences = MockPreferences() | ||
| private lateinit var notificationTray: NotificationTray | ||
|
|
||
| class DummyStorage : Storage { | ||
| override fun clear() { | ||
| throw NotImplementedError("Mock implementation missing") | ||
| } | ||
|
|
||
| override fun getBoolean(key: String, defValue: Boolean): Boolean { | ||
| throw NotImplementedError("Mock implementation missing") | ||
| } | ||
|
|
||
| override fun getInt(key: String, defValue: Int): Int { | ||
| throw NotImplementedError("Mock implementation missing") | ||
| } | ||
|
|
||
| override fun getLong(key: String, defValue: Long): Long { | ||
| throw NotImplementedError("Mock implementation missing") | ||
| } | ||
|
|
||
| override fun getString(key: String, defValue: String): String { | ||
| throw NotImplementedError("Mock implementation missing") | ||
| } | ||
|
|
||
| override fun onAttached(preferences: Preferences) { | ||
| } | ||
|
|
||
| override fun putBoolean(key: String, value: Boolean) { | ||
| throw NotImplementedError("Mock implementation missing") | ||
| } | ||
|
|
||
| override fun putInt(key: String, value: Int) { | ||
| throw NotImplementedError("Mock implementation missing") | ||
| } | ||
|
|
||
| override fun putLong(key: String, value: Long) { | ||
| throw NotImplementedError("Mock implementation missing") | ||
| } | ||
|
|
||
| override fun putString(key: String, value: String) { | ||
| throw NotImplementedError("Mock implementation missing") | ||
| } | ||
|
|
||
| override fun remove(key: String) { | ||
| throw NotImplementedError("Mock implementation missing") | ||
| } | ||
| } | ||
|
|
||
| class MockPreferences : Preferences(DummyStorage()) { | ||
| private var activeNotifications: HashMap<Habit, NotificationTray.NotificationData> = | ||
| HashMap() | ||
|
|
||
| override fun setActiveNotifications(activeNotifications: Map<Habit, NotificationTray.NotificationData>) { | ||
| this.activeNotifications = HashMap(activeNotifications) | ||
| } | ||
|
|
||
| override fun getActiveNotifications(habitList: HabitList): HashMap<Habit, NotificationTray.NotificationData> { | ||
| return activeNotifications | ||
| } | ||
| } | ||
|
|
||
| @Before | ||
| @Throws(Exception::class) | ||
| override fun setUp() { | ||
| super.setUp() | ||
| notificationTray = | ||
| NotificationTray(taskRunner, commandRunner, preferences, systemTray, habitList) | ||
| } | ||
|
|
||
| @Test | ||
| @Throws(Exception::class) | ||
| fun testShow() { | ||
| // Show a reminder for a habit | ||
| val habit = fixtures.createEmptyHabit() | ||
| habit.reminder = Reminder(8, 30, WeekdayList.EVERY_DAY) | ||
| val timestamp = Timestamp(System.currentTimeMillis()) | ||
| val reminderTime = System.currentTimeMillis() | ||
| notificationTray.show(habit, timestamp, reminderTime) | ||
|
|
||
| // Verify that the active notifications include exactly the one shown reminder | ||
| // TODO are we guaranteed that task has executed? | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a |
||
| assertThat(preferences.getActiveNotifications(habitList).size, equalTo(1)) | ||
| assertThat( | ||
| preferences.getActiveNotifications(habitList)[habit], | ||
| equalTo(NotificationTray.NotificationData(timestamp, reminderTime)) | ||
| ) | ||
|
|
||
| // Remove the reminder from the notification tray and verify that active notifications are empty | ||
| notificationTray.cancel(habit) | ||
| assertThat(preferences.getActiveNotifications(habitList).size, equalTo(0)) | ||
|
|
||
| // TODO test cases where reminders should be removed (e.g. reshowAll) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we could add tests whether the remaining methods which touch the active notifications map remove entries in the correct situations. It is a little work to setup all the special cases, I can't promise right now when I'll be able to do that. |
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a manual workaround for the missing matcher/library function I was looking for.