private async void ResendMessageIfNeeded() { DateTime nowLocal = TimeZoneInfo.ConvertTimeFromUtc(SystemTime.Now(), TimeZoneInfo.Local); DateTime todayUtc = SystemTime.Now().Date; DateTime lastReceivedMessageDateTimeUtc = MessageUtils.GetDateTimeFromSecureStorageForKey(SecureStorageKeys.LAST_SENT_NOTIFICATION_UTC_KEY, nameof(ResendMessageIfNeeded)); DateTime lastReceivedMessageDateTimeLocal = lastReceivedMessageDateTimeUtc.ToLocalTime(); if (lastReceivedMessageDateTimeUtc < todayUtc && nowLocal.Date.Subtract(lastReceivedMessageDateTimeLocal.Date).TotalHours >= Conf.HOURS_UNTIL_RESEND_MESSAGES) { if (nowLocal.Hour >= Conf.HOUR_WHEN_MESSAGE_SHOULD_BE_RESEND) { List <MessageSQLiteModel> unreadMessages = await MessageUtils.GetAllUnreadMessages(); List <MessageSQLiteModel> unreadMessagesNotOlderThanMsgRetentionTime = unreadMessages.FindAll(message => { double totalMinutes = SystemTime.Now().Subtract(message.TimeStamp).TotalMinutes; return(totalMinutes < Conf.MAX_MESSAGE_RETENTION_TIME_IN_MINUTES); }) .ToList(); if (unreadMessagesNotOlderThanMsgRetentionTime.Count > 0) { NotificationsHelper.CreateNotification(NotificationsEnum.NewMessageReceived, 0); MessageUtils.SaveDateTimeToSecureStorageForKey( SecureStorageKeys.LAST_SENT_NOTIFICATION_UTC_KEY, SystemTime.Now(), nameof(ResendMessageIfNeeded)); } } } }
[InlineData(14, 0, false)] // Equal 9PM public async void ShouldUpdateLastMessageDate(int daysOfTimeShift, int minutesOfTimeShift, bool shouldBeCalled) { await ServiceLocator.Current.GetInstance <IMessagesManager>().DeleteAll(); foreach (string key in SecureStorageKeys.GetAllKeysForCleaningDevice()) { _secureStorageService.Delete(key); } SystemTime.SetDateTime(DateTime.Now.Date.AddHours(12).ToUniversalTime()); await ServiceLocator.Current.GetInstance <IMessagesManager>().SaveNewMessage(new MessageSQLiteModel() { TimeStamp = SystemTime.Now(), ID = 1, MessageLink = "", Title = "" }); LocalNotificationsManager.GenerateLocalNotification(new NotificationViewModel(), 0); LocalNotificationsManager.HasBeenCalled = false; DateTime preFetchDateTime = MessageUtils.GetDateTimeFromSecureStorageForKey(SecureStorageKeys.LAST_SENT_NOTIFICATION_UTC_KEY, ""); SystemTime.SetDateTime(DateTime.Now.Date.AddDays(daysOfTimeShift).AddHours(21).AddMinutes(minutesOfTimeShift).ToUniversalTime()); try { await new FetchExposureKeysHelper().FetchExposureKeyBatchFilesFromServerAsync(null, CancellationToken.None); } catch { // ignore as ZipDownloader is not mocked in this test } Assert.Equal(shouldBeCalled, preFetchDateTime < MessageUtils.GetDateTimeFromSecureStorageForKey(SecureStorageKeys.LAST_SENT_NOTIFICATION_UTC_KEY, "")); Assert.Equal(shouldBeCalled, LocalNotificationsManager.HasBeenCalled); SystemTime.ResetDateTime(); }
[InlineData(14, 1, false, true)] // After upper bound public async void ShouldUpdateLastMessageDate( int daysOfTimeShift, int minutesOfTimeShift, bool shouldBeCalled = false, bool isUpperBound = false) { ResetData(); SystemTime.SetDateTime( DateTime.Now.Date.AddHours(Conf.HOUR_WHEN_MESSAGE_SHOULD_BE_RESEND_BEGIN - 1).ToUniversalTime()); await ServiceLocator.Current.GetInstance <IMessagesManager>().SaveNewMessage(new MessageSQLiteModel() { TimeStamp = SystemTime.Now(), ID = 1, MessageLink = "", Title = "" }); LocalNotificationsManager.GenerateLocalNotification(new NotificationViewModel(), 0); LocalNotificationsManager.HasBeenCalled[NotificationsEnum.NewMessageReceived] = false; DateTime preFetchDateTime = DateTime.MaxValue; int initialDay = daysOfTimeShift > 1 ? daysOfTimeShift - 2 : 0; // To fasten up the execution time // Simulates pulling on each day for (int i = initialDay; i <= daysOfTimeShift; i++) { if (i == daysOfTimeShift) { LocalNotificationsManager.HasBeenCalled[NotificationsEnum.NewMessageReceived] = false; } preFetchDateTime = MessageUtils.GetDateTimeFromSecureStorageForKey( SecureStorageKeys.LAST_SENT_NOTIFICATION_UTC_KEY, ""); SystemTime.SetDateTime( DateTime.Now.Date.AddDays(i) .AddHours(isUpperBound ? Conf.HOUR_WHEN_MESSAGE_SHOULD_BE_RESEND_END : Conf.HOUR_WHEN_MESSAGE_SHOULD_BE_RESEND_BEGIN).AddMinutes(minutesOfTimeShift) .ToUniversalTime()); try { await new FetchExposureKeysHelper().FetchExposureKeyBatchFilesFromServerAsync(null, CancellationToken.None); } catch { // ignore as ZipDownloader is not mocked in this test } } Assert.Equal(shouldBeCalled, preFetchDateTime < MessageUtils.GetDateTimeFromSecureStorageForKey(SecureStorageKeys.LAST_SENT_NOTIFICATION_UTC_KEY, "")); Assert.Equal(shouldBeCalled, LocalNotificationsManager.HasBeenCalled[NotificationsEnum.NewMessageReceived]); SystemTime.ResetDateTime(); }