Skip to content
This repository has been archived by the owner on Apr 12, 2023. It is now read-only.

Commit

Permalink
Fixed app reset
Browse files Browse the repository at this point in the history
  • Loading branch information
cocoa-dev004 committed Jul 7, 2022
1 parent 9393bc3 commit a09ea73
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 3 deletions.
36 changes: 36 additions & 0 deletions Covid19Radar/Covid19Radar/Repository/EventLogRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public Task<List<EventLog>> GetLogsAsync(

public Task<bool> RemoveAsync(EventLog eventLog);

public Task RemoveAllAsync();

public Task AddEventNotifiedAsync(
long maxSize = AppConstants.EventLogMaxRequestSizeInBytes
);
Expand Down Expand Up @@ -269,6 +271,40 @@ private async Task AddEventNotifiedAsyncInternal(long maxSize)
};
await AddAsyncInternal(eventLog, maxSize);
}

public async Task RemoveAllAsync()
{
_loggerService.StartMethod();

await _semaphore.WaitAsync();

try
{
RemoveAllAsyncInternal();
}
finally
{
_semaphore.Release();

_loggerService.EndMethod();
}
}

private void RemoveAllAsyncInternal()
{
_loggerService.StartMethod();
try
{
if (Directory.Exists(_basePath))
{
Directory.Delete(_basePath, true);
}
}
finally
{
_loggerService.EndMethod();
}
}
}

public class EventContentExposureNotified
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public interface ISendEventLogStateRepository
SendEventLogState GetSendEventLogState(EventType eventType);

bool IsExistNotSetEventType();

void RemoveAll();
}

public class SendEventLogStateRepository : ISendEventLogStateRepository
Expand Down Expand Up @@ -156,5 +158,10 @@ public bool IsExistNotSetEventType()
.Select(eventType => GetSendEventLogState(eventType))
.Any(state => state == SendEventLogState.NotSet);
}

public void RemoveAll()
{
_preferencesService.RemoveValue(PreferenceKey.SendEventLogState);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public string AppVer
private readonly IUserDataRepository userDataRepository;
private readonly IExposureDataRepository exposureDataRepository;
private readonly IExposureConfigurationRepository exposureConfigurationRepository;
private readonly ISendEventLogStateRepository _sendEventLogStateRepository;
private readonly IEventLogRepository _eventLogRepository;
private readonly ILogFileService logFileService;
private readonly AbsExposureNotificationApiService exposureNotificationApiService;
private readonly ICloseApplicationService closeApplicationService;
Expand All @@ -46,6 +48,8 @@ public SettingsPageViewModel(
IUserDataRepository userDataRepository,
IExposureDataRepository exposureDataRepository,
IExposureConfigurationRepository exposureConfigurationRepository,
ISendEventLogStateRepository sendEventLogStateRepository,
IEventLogRepository eventLogRepository,
ILogFileService logFileService,
AbsExposureNotificationApiService exposureNotificationApiService,
ICloseApplicationService closeApplicationService,
Expand All @@ -58,6 +62,8 @@ IEssentialsService essentialsService
this.userDataRepository = userDataRepository;
this.exposureDataRepository = exposureDataRepository;
this.exposureConfigurationRepository = exposureConfigurationRepository;
_sendEventLogStateRepository = sendEventLogStateRepository;
_eventLogRepository = eventLogRepository;
this.logFileService = logFileService;
this.exposureNotificationApiService = exposureNotificationApiService;
this.closeApplicationService = closeApplicationService;
Expand All @@ -73,7 +79,7 @@ IEssentialsService essentialsService
loggerService.EndMethod();
});

public ICommand OnChangeResetData => new Command(async () =>
public IAsyncCommand OnChangeResetData => new AsyncCommand(async () =>
{
loggerService.StartMethod();
Expand All @@ -100,6 +106,9 @@ IEssentialsService essentialsService
userDataRepository.RemoveAllUpdateDate();
userDataRepository.RemoveAllExposureNotificationStatus();
_sendEventLogStateRepository.RemoveAll();
await _eventLogRepository.RemoveAllAsync();
_ = logFileService.DeleteLogsDir();
UserDialogs.Instance.HideLoading();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,5 +231,13 @@ public void IsExistNotSetEventTypeTest_Enabled()

Assert.False(sendEventLogStateRepository.IsExistNotSetEventType());
}

[Fact]
public void RemoveAllTest()
{
var sendEventLogStateRepository = CreateRepository();
sendEventLogStateRepository.RemoveAll();
mockPreferencesService.Verify(x => x.RemoveValue(PreferenceKey.SendEventLogState), Times.Once());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,38 @@
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

using System;
using System.Linq;
using System.Threading.Tasks;
using Acr.UserDialogs;
using Covid19Radar.Repository;
using Covid19Radar.Resources;
using Covid19Radar.Services;
using Covid19Radar.Services.Logs;
using Covid19Radar.ViewModels;
using Covid19Radar.Views;
using Moq;
using Prism.Navigation;
using Xamarin.Forms;
using Xunit;

namespace Covid19Radar.UnitTests.ViewModels
{
public class SettingsPageViewModelTests
public class SettingsPageViewModelTests : IDisposable
{
private readonly MockRepository _mockRepository;
private readonly Mock<INavigationService> _mockNavigationService;
private readonly Mock<ILoggerService> _mockLoggerService;
private readonly Mock<IUserDataRepository> _mockUserDataRepository;
private readonly Mock<IExposureDataRepository> _mockExposureDataRepository;
private readonly Mock<IExposureConfigurationRepository> _mockExposureConfigurationRepository;
private readonly Mock<ISendEventLogStateRepository> _mockSendEventLogStateRepository;
private readonly Mock<IEventLogRepository> _mockEventLogRepository;
private readonly Mock<ILogFileService> _mockLogFileService;
private readonly Mock<AbsExposureNotificationApiService> _mockAbsExposureNotificationApiService;
private readonly Mock<ICloseApplicationService> _mockCloseApplicationService;
private readonly Mock<IEssentialsService> _mockEssentialsService;

private readonly Mock<IUserDialogs> _mockUserDialogs;

public SettingsPageViewModelTests()
{
_mockRepository = new MockRepository(MockBehavior.Default);
Expand All @@ -37,10 +43,24 @@ public SettingsPageViewModelTests()
_mockUserDataRepository = _mockRepository.Create<IUserDataRepository>();
_mockExposureDataRepository = _mockRepository.Create<IExposureDataRepository>();
_mockExposureConfigurationRepository = _mockRepository.Create<IExposureConfigurationRepository>();
_mockSendEventLogStateRepository = _mockRepository.Create<ISendEventLogStateRepository>();
_mockEventLogRepository = _mockRepository.Create<IEventLogRepository>();
_mockLogFileService = _mockRepository.Create<ILogFileService>();
_mockAbsExposureNotificationApiService = new Mock<AbsExposureNotificationApiService>(_mockLoggerService.Object);
_mockCloseApplicationService = _mockRepository.Create<ICloseApplicationService>();
_mockEssentialsService = _mockRepository.Create<IEssentialsService>();

_mockUserDialogs = _mockRepository.Create<IUserDialogs>();
UserDialogs.Instance = _mockUserDialogs.Object;

Xamarin.Forms.Mocks.MockForms.Init();
Application.Current = new Application();
}

public void Dispose()
{
UserDialogs.Instance = null;
Application.Current = null;
}

private SettingsPageViewModel CreateViewModel()
Expand All @@ -51,6 +71,8 @@ private SettingsPageViewModel CreateViewModel()
_mockUserDataRepository.Object,
_mockExposureDataRepository.Object,
_mockExposureConfigurationRepository.Object,
_mockSendEventLogStateRepository.Object,
_mockEventLogRepository.Object,
_mockLogFileService.Object,
_mockAbsExposureNotificationApiService.Object,
_mockCloseApplicationService.Object,
Expand All @@ -66,6 +88,86 @@ public void AppVerTest()
Assert.Equal("1.2.3", unitUnderTest.AppVer);
}

[Fact]
public async Task OnChangeResetDataTest_Ok()
{
_mockUserDialogs.Setup(x =>
x.ConfirmAsync(
AppResources.SettingsPageDialogResetText,
AppResources.SettingsPageDialogResetTitle,
AppResources.ButtonOk,
AppResources.ButtonCancel,
null)
).ReturnsAsync(true);
//_mockAbsExposureNotificationApiService.Setup(x => x.StopExposureNotificationAsync()).ReturnsAsync(true);

SettingsPageViewModel unitUnderTest = CreateViewModel();
await unitUnderTest.OnChangeResetData.ExecuteAsync();

_mockUserDialogs.Verify(x => x.ShowLoading(It.IsAny<string>(), null), Times.Once());
_mockUserDialogs.Verify(x => x.HideLoading(), Times.Once());

//_mockAbsExposureNotificationApiService.Verify(x => x.StopExposureNotificationAsync(), Times.Once());
_mockExposureDataRepository.Verify(x => x.RemoveDailySummariesAsync(), Times.Once());
_mockExposureDataRepository.Verify(x => x.RemoveExposureWindowsAsync(), Times.Once());
_mockExposureDataRepository.Verify(x => x.RemoveExposureInformation(), Times.Once());
_mockUserDataRepository.Verify(x => x.RemoveLastProcessDiagnosisKeyTimestampAsync(), Times.Once());
_mockUserDataRepository.Verify(x => x.RemoveStartDate(), Times.Once());
_mockUserDataRepository.Verify(x => x.RemoveAllUpdateDate(), Times.Once());
_mockUserDataRepository.Verify(x => x.RemoveAllExposureNotificationStatus(), Times.Once());
_mockExposureConfigurationRepository.Verify(x => x.RemoveExposureConfigurationAsync(), Times.Once());
_mockSendEventLogStateRepository.Verify(x => x.RemoveAll(), Times.Once());
_mockEventLogRepository.Verify(x => x.RemoveAllAsync(), Times.Once());
_mockLogFileService.Verify(x => x.DeleteLogsDir(), Times.Once());
_mockUserDialogs.Verify(x =>
x.AlertAsync(
AppResources.SettingsPageDialogResetCompletedText,
AppResources.SettingsPageDialogResetCompletedTitle,
AppResources.ButtonOk, null),
Times.Once());
_mockCloseApplicationService.Verify(x => x.CloseApplication(), Times.Once());
}

[Fact]
public async Task OnChangeResetDataTest_Cancel()
{
_mockUserDialogs.Setup(x =>
x.ConfirmAsync(
AppResources.SettingsPageDialogResetText,
AppResources.SettingsPageDialogResetTitle,
AppResources.ButtonOk,
AppResources.ButtonCancel,
null)
).ReturnsAsync(false);
//_mockAbsExposureNotificationApiService.Setup(x => x.StopExposureNotificationAsync()).ReturnsAsync(true);

SettingsPageViewModel unitUnderTest = CreateViewModel();
await unitUnderTest.OnChangeResetData.ExecuteAsync();

_mockUserDialogs.Verify(x => x.ShowLoading(It.IsAny<string>(), null), Times.Never());
_mockUserDialogs.Verify(x => x.HideLoading(), Times.Never());

//_mockAbsExposureNotificationApiService.Verify(x => x.StopExposureNotificationAsync(), Times.Never());
_mockExposureDataRepository.Verify(x => x.RemoveDailySummariesAsync(), Times.Never());
_mockExposureDataRepository.Verify(x => x.RemoveExposureWindowsAsync(), Times.Never());
_mockExposureDataRepository.Verify(x => x.RemoveExposureInformation(), Times.Never());
_mockUserDataRepository.Verify(x => x.RemoveLastProcessDiagnosisKeyTimestampAsync(), Times.Never());
_mockUserDataRepository.Verify(x => x.RemoveStartDate(), Times.Never());
_mockUserDataRepository.Verify(x => x.RemoveAllUpdateDate(), Times.Never());
_mockUserDataRepository.Verify(x => x.RemoveAllExposureNotificationStatus(), Times.Never());
_mockExposureConfigurationRepository.Verify(x => x.RemoveExposureConfigurationAsync(), Times.Never());
_mockSendEventLogStateRepository.Verify(x => x.RemoveAll(), Times.Never());
_mockEventLogRepository.Verify(x => x.RemoveAllAsync(), Times.Never());
_mockLogFileService.Verify(x => x.DeleteLogsDir(), Times.Never());
_mockUserDialogs.Verify(x =>
x.AlertAsync(
AppResources.SettingsPageDialogResetCompletedText,
AppResources.SettingsPageDialogResetCompletedTitle,
AppResources.ButtonOk, null),
Times.Never());
_mockCloseApplicationService.Verify(x => x.CloseApplication(), Times.Never());
}

[Fact]
public async Task OnEventLogSendTest()
{
Expand Down

0 comments on commit a09ea73

Please sign in to comment.