public void TracksEventWhenAppResumed()
            {
                var backgroundService = new BackgroundService(TimeService, AnalyticsService, UpdateRemoteConfigCacheService);

                backgroundService.EnterBackground();
                backgroundService.EnterForeground();
                AnalyticsService.Received().AppDidEnterForeground.Track();
            }
            public void DoesNotEmitAnythingWhenTheEnterForegroundIsCalledMultipleTimes()
            {
                bool emitted           = false;
                var  backgroundService = new BackgroundService(TimeService, AnalyticsService, UpdateRemoteConfigCacheService);

                TimeService.CurrentDateTime.Returns(now);
                backgroundService.EnterBackground();
                TimeService.CurrentDateTime.Returns(now.AddMinutes(1));
                backgroundService.EnterForeground();
                TimeService.CurrentDateTime.Returns(now.AddMinutes(2));
                backgroundService
                .AppResumedFromBackground
                .Subscribe(_ => emitted = true);

                backgroundService.EnterForeground();

                emitted.Should().BeFalse();
            }
            public void DoesNotEmitAnythingWhenItHasNotEnterBackgroundFirst()
            {
                bool emitted           = false;
                var  backgroundService = new BackgroundService(TimeService, AnalyticsService, UpdateRemoteConfigCacheService);

                backgroundService
                .AppResumedFromBackground
                .Subscribe(_ => emitted = true);

                backgroundService.EnterForeground();

                emitted.Should().BeFalse();
            }
            public void EmitsValueWhenEnteringForegroundAfterBeingInBackground()
            {
                bool emitted           = false;
                var  backgroundService = new BackgroundService(TimeService, AnalyticsService, UpdateRemoteConfigCacheService);

                TimeService.CurrentDateTime.Returns(now);
                backgroundService
                .AppResumedFromBackground
                .Subscribe(_ => emitted = true);

                backgroundService.EnterBackground();
                backgroundService.EnterForeground();

                emitted.Should().BeTrue();
            }
            public async Task DoesNotTriggerRemoteConfigUpdateWhenRemoteConfigDataDoesNotNeedToBeUpdated()
            {
                var updateRemoteConfigCacheService = Substitute.For <IUpdateRemoteConfigCacheService>();

                updateRemoteConfigCacheService.NeedsToUpdateStoredRemoteConfigData().Returns(false);
                var backgroundService = new BackgroundService(TimeService, AnalyticsService, updateRemoteConfigCacheService);

                backgroundService.EnterForeground();

                // This delay is make sure FetchAndStoreRemoteConfigData has time to execute, since it's called inside a
                // fire and forget TaskTask.Run(() => {}).ConfigureAwait(false))
                await Task.Delay(1);

                updateRemoteConfigCacheService.DidNotReceive().FetchAndStoreRemoteConfigData();
            }
            public void EmitsAValueWhenEnteringForegroundAfterBeingInBackgroundForMoreThanTheLimit(NonNegativeInt waitingTime)
            {
                TimeSpan?resumedAfter      = null;
                var      backgroundService = new BackgroundService(TimeService, AnalyticsService, UpdateRemoteConfigCacheService);

                backgroundService
                .AppResumedFromBackground
                .Subscribe(timeInBackground => resumedAfter = timeInBackground);
                TimeService.CurrentDateTime.Returns(now);

                backgroundService.EnterBackground();
                TimeService.CurrentDateTime.Returns(now.AddMinutes(waitingTime.Get).AddSeconds(1));
                backgroundService.EnterForeground();

                resumedAfter.Should().NotBeNull();
                resumedAfter.Should().BeGreaterThan(TimeSpan.FromMinutes(waitingTime.Get));
            }