From c5254e5fe6bc71e316b7bfc9a3c1e2bd82c51735 Mon Sep 17 00:00:00 2001 From: cocoa-dev004 <66989461+cocoa-dev004@users.noreply.github.com> Date: Thu, 7 Jul 2022 16:26:23 +0900 Subject: [PATCH 1/3] Fixed app reset --- .../Repository/EventLogRepository.cs | 36 ++++++ .../Repository/SendEventLogStateRepository.cs | 7 ++ .../Settings/SettingsPageViewModel.cs | 11 +- .../SendEventLogStateRepositoryTests.cs | 8 ++ .../Settings/SettingsPageViewModelTests.cs | 106 +++++++++++++++++- 5 files changed, 165 insertions(+), 3 deletions(-) diff --git a/Covid19Radar/Covid19Radar/Repository/EventLogRepository.cs b/Covid19Radar/Covid19Radar/Repository/EventLogRepository.cs index 509248a38..2872f99f4 100644 --- a/Covid19Radar/Covid19Radar/Repository/EventLogRepository.cs +++ b/Covid19Radar/Covid19Radar/Repository/EventLogRepository.cs @@ -26,6 +26,8 @@ public Task> GetLogsAsync( public Task RemoveAsync(EventLog eventLog); + public Task RemoveAllAsync(); + public Task AddEventNotifiedAsync( long maxSize = AppConstants.EventLogMaxRequestSizeInBytes ); @@ -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 diff --git a/Covid19Radar/Covid19Radar/Repository/SendEventLogStateRepository.cs b/Covid19Radar/Covid19Radar/Repository/SendEventLogStateRepository.cs index e4d200848..247193d3e 100644 --- a/Covid19Radar/Covid19Radar/Repository/SendEventLogStateRepository.cs +++ b/Covid19Radar/Covid19Radar/Repository/SendEventLogStateRepository.cs @@ -49,6 +49,8 @@ public interface ISendEventLogStateRepository SendEventLogState GetSendEventLogState(EventType eventType); bool IsExistNotSetEventType(); + + void RemoveAll(); } public class SendEventLogStateRepository : ISendEventLogStateRepository @@ -156,5 +158,10 @@ public bool IsExistNotSetEventType() .Select(eventType => GetSendEventLogState(eventType)) .Any(state => state == SendEventLogState.NotSet); } + + public void RemoveAll() + { + _preferencesService.RemoveValue(PreferenceKey.SendEventLogState); + } } } diff --git a/Covid19Radar/Covid19Radar/ViewModels/Settings/SettingsPageViewModel.cs b/Covid19Radar/Covid19Radar/ViewModels/Settings/SettingsPageViewModel.cs index f4d4c3590..483aa2255 100644 --- a/Covid19Radar/Covid19Radar/ViewModels/Settings/SettingsPageViewModel.cs +++ b/Covid19Radar/Covid19Radar/ViewModels/Settings/SettingsPageViewModel.cs @@ -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; @@ -46,6 +48,8 @@ public SettingsPageViewModel( IUserDataRepository userDataRepository, IExposureDataRepository exposureDataRepository, IExposureConfigurationRepository exposureConfigurationRepository, + ISendEventLogStateRepository sendEventLogStateRepository, + IEventLogRepository eventLogRepository, ILogFileService logFileService, AbsExposureNotificationApiService exposureNotificationApiService, ICloseApplicationService closeApplicationService, @@ -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; @@ -73,7 +79,7 @@ IEssentialsService essentialsService loggerService.EndMethod(); }); - public ICommand OnChangeResetData => new Command(async () => + public IAsyncCommand OnChangeResetData => new AsyncCommand(async () => { loggerService.StartMethod(); @@ -100,6 +106,9 @@ IEssentialsService essentialsService userDataRepository.RemoveAllUpdateDate(); userDataRepository.RemoveAllExposureNotificationStatus(); + _sendEventLogStateRepository.RemoveAll(); + await _eventLogRepository.RemoveAllAsync(); + _ = logFileService.DeleteLogsDir(); UserDialogs.Instance.HideLoading(); diff --git a/Covid19Radar/Tests/Covid19Radar.UnitTests/Repository/SendEventLogStateRepositoryTests.cs b/Covid19Radar/Tests/Covid19Radar.UnitTests/Repository/SendEventLogStateRepositoryTests.cs index 78278a130..d4bce248c 100644 --- a/Covid19Radar/Tests/Covid19Radar.UnitTests/Repository/SendEventLogStateRepositoryTests.cs +++ b/Covid19Radar/Tests/Covid19Radar.UnitTests/Repository/SendEventLogStateRepositoryTests.cs @@ -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()); + } } } diff --git a/Covid19Radar/Tests/Covid19Radar.UnitTests/ViewModels/Settings/SettingsPageViewModelTests.cs b/Covid19Radar/Tests/Covid19Radar.UnitTests/ViewModels/Settings/SettingsPageViewModelTests.cs index 90e658603..e041d2cdc 100644 --- a/Covid19Radar/Tests/Covid19Radar.UnitTests/ViewModels/Settings/SettingsPageViewModelTests.cs +++ b/Covid19Radar/Tests/Covid19Radar.UnitTests/ViewModels/Settings/SettingsPageViewModelTests.cs @@ -3,20 +3,22 @@ // 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 _mockNavigationService; @@ -24,11 +26,15 @@ public class SettingsPageViewModelTests private readonly Mock _mockUserDataRepository; private readonly Mock _mockExposureDataRepository; private readonly Mock _mockExposureConfigurationRepository; + private readonly Mock _mockSendEventLogStateRepository; + private readonly Mock _mockEventLogRepository; private readonly Mock _mockLogFileService; private readonly Mock _mockAbsExposureNotificationApiService; private readonly Mock _mockCloseApplicationService; private readonly Mock _mockEssentialsService; + private readonly Mock _mockUserDialogs; + public SettingsPageViewModelTests() { _mockRepository = new MockRepository(MockBehavior.Default); @@ -37,10 +43,24 @@ public SettingsPageViewModelTests() _mockUserDataRepository = _mockRepository.Create(); _mockExposureDataRepository = _mockRepository.Create(); _mockExposureConfigurationRepository = _mockRepository.Create(); + _mockSendEventLogStateRepository = _mockRepository.Create(); + _mockEventLogRepository = _mockRepository.Create(); _mockLogFileService = _mockRepository.Create(); _mockAbsExposureNotificationApiService = new Mock(_mockLoggerService.Object); _mockCloseApplicationService = _mockRepository.Create(); _mockEssentialsService = _mockRepository.Create(); + + _mockUserDialogs = _mockRepository.Create(); + 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() @@ -51,6 +71,8 @@ private SettingsPageViewModel CreateViewModel() _mockUserDataRepository.Object, _mockExposureDataRepository.Object, _mockExposureConfigurationRepository.Object, + _mockSendEventLogStateRepository.Object, + _mockEventLogRepository.Object, _mockLogFileService.Object, _mockAbsExposureNotificationApiService.Object, _mockCloseApplicationService.Object, @@ -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(), 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(), 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() { From 2e65f956906c99ed470bebca0d4785cc2dc4c41b Mon Sep 17 00:00:00 2001 From: cocoa-dev004 <66989461+cocoa-dev004@users.noreply.github.com> Date: Fri, 8 Jul 2022 08:47:07 +0900 Subject: [PATCH 2/3] Add log --- .../Repository/EventLogRepository.cs | 44 +++++++++++++++++++ .../Settings/SettingsPageViewModel.cs | 16 +++++++ 2 files changed, 60 insertions(+) diff --git a/Covid19Radar/Covid19Radar/Repository/EventLogRepository.cs b/Covid19Radar/Covid19Radar/Repository/EventLogRepository.cs index 2872f99f4..77aec6faa 100644 --- a/Covid19Radar/Covid19Radar/Repository/EventLogRepository.cs +++ b/Covid19Radar/Covid19Radar/Repository/EventLogRepository.cs @@ -31,6 +31,8 @@ public Task> GetLogsAsync( public Task AddEventNotifiedAsync( long maxSize = AppConstants.EventLogMaxRequestSizeInBytes ); + + public Task IsExist(); } public class EventLogRepository : IEventLogRepository @@ -305,6 +307,48 @@ private void RemoveAllAsyncInternal() _loggerService.EndMethod(); } } + + public async Task IsExist() + { + _loggerService.StartMethod(); + + await _semaphore.WaitAsync(); + + try + { + return IsExistInternal(); + } + finally + { + _semaphore.Release(); + + _loggerService.EndMethod(); + } + } + + private bool IsExistInternal() + { + _loggerService.StartMethod(); + try + { + if (!Directory.Exists(_basePath)) + { + return false; + } + + string[] filesInDirectory = Directory.GetFiles(_basePath); + if (filesInDirectory.Length == 0) + { + return false; + } + + return true; + } + finally + { + _loggerService.EndMethod(); + } + } } public class EventContentExposureNotified diff --git a/Covid19Radar/Covid19Radar/ViewModels/Settings/SettingsPageViewModel.cs b/Covid19Radar/Covid19Radar/ViewModels/Settings/SettingsPageViewModel.cs index 483aa2255..c6ba95179 100644 --- a/Covid19Radar/Covid19Radar/ViewModels/Settings/SettingsPageViewModel.cs +++ b/Covid19Radar/Covid19Radar/ViewModels/Settings/SettingsPageViewModel.cs @@ -69,6 +69,22 @@ IEssentialsService essentialsService this.closeApplicationService = closeApplicationService; } + public override async void Initialize(INavigationParameters parameters) + { + loggerService.StartMethod(); + base.Initialize(parameters); + + try + { + bool isExistEventLogs = await _eventLogRepository.IsExist(); + loggerService.Info($"isExistEventLogs: {isExistEventLogs}"); + } + finally + { + loggerService.EndMethod(); + } + } + public IAsyncCommand OnEventLogSend => new AsyncCommand(async () => { loggerService.StartMethod(); From 6c6d00e4f19de6946adce74437970c85d063883a Mon Sep 17 00:00:00 2001 From: cocoa-dev004 <66989461+cocoa-dev004@users.noreply.github.com> Date: Fri, 8 Jul 2022 09:56:14 +0900 Subject: [PATCH 3/3] Add debug --- .../ViewModels/Settings/DebugPageViewModel.cs | 22 ++++++++++++++++++- .../Views/Settings/DebugPage.xaml | 10 ++++++++- .../Settings/SettingsPageViewModelTests.cs | 11 ++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/Covid19Radar/Covid19Radar/ViewModels/Settings/DebugPageViewModel.cs b/Covid19Radar/Covid19Radar/ViewModels/Settings/DebugPageViewModel.cs index 1f5923086..1c0b85529 100644 --- a/Covid19Radar/Covid19Radar/ViewModels/Settings/DebugPageViewModel.cs +++ b/Covid19Radar/Covid19Radar/ViewModels/Settings/DebugPageViewModel.cs @@ -18,6 +18,7 @@ using Covid19Radar.Services; using Covid19Radar.Views; using Prism.Navigation; +using Xamarin.CommunityToolkit.ObjectModel; using Xamarin.Essentials; using Xamarin.Forms; @@ -34,6 +35,8 @@ public class DebugPageViewModel : ViewModelBase private readonly ICloseApplicationService _closeApplicationService; private readonly IServerConfigurationRepository _serverConfigurationRepository; private readonly ILocalNotificationService _localNotificationService; + private readonly ISendEventLogStateRepository _sendEventLogStateRepository; + private readonly IEventLogRepository _eventLogRepository; private string _debugInfo; public string DebugInfo @@ -154,7 +157,9 @@ public DebugPageViewModel( AbsExposureDetectionBackgroundService exposureDetectionBackgroundService, ICloseApplicationService closeApplicationService, IServerConfigurationRepository serverConfigurationRepository, - ILocalNotificationService localNotificationService + ILocalNotificationService localNotificationService, + ISendEventLogStateRepository sendEventLogStateRepository, + IEventLogRepository eventLogRepository ) : base(navigationService) { Title = "Title:Debug"; @@ -167,6 +172,8 @@ ILocalNotificationService localNotificationService _closeApplicationService = closeApplicationService; _serverConfigurationRepository = serverConfigurationRepository; _localNotificationService = localNotificationService; + _sendEventLogStateRepository = sendEventLogStateRepository; + _eventLogRepository = eventLogRepository; } public override async void Initialize(INavigationParameters parameters) @@ -309,6 +316,19 @@ private string ConvertSha256(string text) _ = await NavigationService.NavigateAsync("/" + nameof(ReAgreePrivacyPolicyPage), navigationParams); }); + public IAsyncCommand OnClickAddEventNotifiedIfNeeded => new AsyncCommand(async () => + { + if (_sendEventLogStateRepository.GetSendEventLogState(EventType.ExposureNotified) == SendEventLogState.Enable) + { + await _eventLogRepository.AddEventNotifiedAsync(); + } + }); + + public IAsyncCommand OnClickAddEventNotifiedForce => new AsyncCommand(async () => + { + await _eventLogRepository.AddEventNotifiedAsync(); + }); + public Command OnClickQuit => new Command(() => { Application.Current.Quit(); diff --git a/Covid19Radar/Covid19Radar/Views/Settings/DebugPage.xaml b/Covid19Radar/Covid19Radar/Views/Settings/DebugPage.xaml index 11b383c48..a85b7bfd5 100644 --- a/Covid19Radar/Covid19Radar/Views/Settings/DebugPage.xaml +++ b/Covid19Radar/Covid19Radar/Views/Settings/DebugPage.xaml @@ -1,4 +1,4 @@ - + @@ -108,6 +108,14 @@ Command="{Binding Path=OnClickRemoveAllUpdateDate}" Style="{StaticResource DefaultButton}" Text="RemoveAllUpdateDate" /> +