/// <summary> /// Creates a <see cref="WuApiController"/> which uses the given Interfaces to search, download and install updates. /// </summary> /// <param name="session">Session to be used.</param> /// <param name="updateCollectionFactory">Factory to create <see cref="IUpdateCollection"/>s.</param> /// <param name="systemInfo">System informations about the OS enviroment.</param> public WuApiController(IUpdateSession session, UpdateCollectionFactory updateCollectionFactory, ISystemInfo systemInfo) { if (session == null) { throw new ArgumentNullException(nameof(session)); } if (updateCollectionFactory == null) { throw new ArgumentNullException(nameof(updateCollectionFactory)); } if (systemInfo == null) { throw new ArgumentNullException(nameof(systemInfo)); } UpdateSession = session; UpdateSession.ClientApplicationID = this.GetType().FullName; UpdateSearcher = session.CreateUpdateSearcher(); UpdateDownloader = session.CreateUpdateDownloader(); UpdateInstaller = session.CreateUpdateInstaller(); UpdateCollectionFactory = updateCollectionFactory; SystemInfo = systemInfo; StateTransitions = SetupStateTransitions(); EnterState((SystemInfo.IsRebootRequired()) ? (WuProcessState) new WuStateRebootRequired() : new WuStateReady()); Log.Info("Initial state: " + _currentState.GetType().Name); if (Log.IsDebugEnabled) { OnStateChanged += (s, e) => { Log.Debug($"Event {nameof(OnStateChanged)} fired: {e.ToString()}"); }; OnAsyncOperationCompleted += (s, e) => { Log.Debug($"Event {nameof(OnAsyncOperationCompleted)} fired: {e.ToString()}"); }; OnProgressChanged += (s, e) => { Log.Debug($"Event {nameof(OnProgressChanged)} fired: {e.ToString()}"); }; } }
public void Should_NotAllowEqualTransitions_When_AddToTransitionList() { StateTransitionCollection coll = new StateTransitionCollection(); coll.Add <WuStateReady, WuStateDownloading>(); coll.Add <WuStateReady, WuStateDownloading>((c) => { return(new ConditionEvalResult(true, null)); }); }
public void Should_ReturnFalse_When_CallIsReadOnly() { StateTransitionCollection coll = new StateTransitionCollection(); Assert.IsFalse(coll.IsReadOnly); }
/// <summary> /// Setups a list of all valid state transistions. /// </summary> private StateTransitionCollection SetupStateTransitions() { StateTransition.TransitionCondition downloadingPreCon = (c) => { using (ll.Lock(UpdateHolderLock)) { var applUpdates = UpdateHolder.GetSelectedUpdates((u) => u.EulaAccepted); if (applUpdates.Any()) { if (applUpdates.All(u => u.IsDownloaded || u.IsInstalled)) { return(new ConditionEvalResult(true, "All selected updates are already downloaded.")); // or installed } if (SystemInfo.GetFreeSpace() < (decimal)1.5 * applUpdates.Where(u => !u.IsDownloaded && !u.IsInstalled).Sum(u => (u.RecommendedHardDiskSpace > 0) ? u.RecommendedHardDiskSpace : u.MaxDownloadSize)) { return(new ConditionEvalResult(false, "Not enough free space on system drive.")); } return(ConditionEvalResult.ValidStateChange); } return(new ConditionEvalResult(false, "Please search for updates first, select some updates and accept the eulas.")); } }; StateTransition.TransitionCondition installingPreCon = (c) => { using (ll.Lock(UpdateHolderLock)) { if (UpdateInstaller.IsBusy) { return(new ConditionEvalResult(false, "The update installer is currently busy, an other update session installs or uninstalls updates.")); } if (UpdateInstaller.RebootRequiredBeforeInstallation) { return(new ConditionEvalResult(false, "A reboot is required before updates can be installed.")); } var applUpdates = UpdateHolder.GetSelectedUpdates((u) => u.EulaAccepted); if (applUpdates.Any()) { if (applUpdates.All(u => u.IsInstalled)) { return(new ConditionEvalResult(true, "All selected updates are already downloaded.")); // or installed } return(ConditionEvalResult.ValidStateChange); } return(new ConditionEvalResult(false, "Please search for updates first, select some updates and accept the eulas.")); } }; var stateTransitions = new StateTransitionCollection(); // fromState -goes-> toState stateTransitions.Add <WuStateReady, WuStateSearching>(); stateTransitions.Add <WuStateReady, WuStateRestartSentToOS>(); stateTransitions.Add <WuStateSearching, WuStateSearchCompleted>(); stateTransitions.Add <WuStateSearching, WuStateSearchFailed>(); stateTransitions.Add <WuStateSearchCompleted, WuStateSearching>(); stateTransitions.Add <WuStateSearchCompleted, WuStateDownloading>(downloadingPreCon); stateTransitions.Add <WuStateSearchCompleted, WuStateInstalling>(installingPreCon); stateTransitions.Add <WuStateSearchFailed, WuStateSearching>(); stateTransitions.Add <WuStateSearchFailed, WuStateDownloading>(downloadingPreCon); stateTransitions.Add <WuStateSearchFailed, WuStateInstalling>(installingPreCon); stateTransitions.Add <WuStateDownloading, WuStateDownloadFailed>(); stateTransitions.Add <WuStateDownloading, WuStateDownloadCompleted>(); stateTransitions.Add <WuStateDownloading, WuStateDownloadPartiallyFailed>(); stateTransitions.Add <WuStateDownloadFailed, WuStateSearching>(); stateTransitions.Add <WuStateDownloadFailed, WuStateDownloading>(downloadingPreCon); stateTransitions.Add <WuStateDownloadFailed, WuStateInstalling>(installingPreCon); stateTransitions.Add <WuStateDownloadCompleted, WuStateSearching>(); stateTransitions.Add <WuStateDownloadCompleted, WuStateDownloading>(downloadingPreCon); stateTransitions.Add <WuStateDownloadCompleted, WuStateInstalling>(installingPreCon); stateTransitions.Add <WuStateDownloadPartiallyFailed, WuStateSearching>(); stateTransitions.Add <WuStateDownloadPartiallyFailed, WuStateDownloading>(downloadingPreCon); stateTransitions.Add <WuStateDownloadPartiallyFailed, WuStateInstalling>(installingPreCon); stateTransitions.Add <WuStateInstalling, WuStateInstallCompleted>(); stateTransitions.Add <WuStateInstalling, WuStateInstallFailed>(); stateTransitions.Add <WuStateInstalling, WuStateInstallPartiallyFailed>(); stateTransitions.Add <WuStateInstalling, WuStateRebootRequired>(); stateTransitions.Add <WuStateInstalling, WuStateUserInputRequired>(); stateTransitions.Add <WuStateInstallCompleted, WuStateSearching>(); stateTransitions.Add <WuStateInstallCompleted, WuStateDownloading>(downloadingPreCon); stateTransitions.Add <WuStateInstallCompleted, WuStateInstalling>(installingPreCon); stateTransitions.Add <WuStateInstallFailed, WuStateSearching>(); stateTransitions.Add <WuStateInstallFailed, WuStateDownloading>(downloadingPreCon); stateTransitions.Add <WuStateInstallFailed, WuStateInstalling>(installingPreCon); stateTransitions.Add <WuStateInstallPartiallyFailed, WuStateSearching>(); stateTransitions.Add <WuStateInstallPartiallyFailed, WuStateDownloading>(downloadingPreCon); stateTransitions.Add <WuStateInstallPartiallyFailed, WuStateInstalling>(installingPreCon); stateTransitions.Add <WuStateInstallPartiallyFailed, WuStateRestartSentToOS>(); stateTransitions.Add <WuStateUserInputRequired, WuStateSearching>(); stateTransitions.Add <WuStateUserInputRequired, WuStateDownloading>(downloadingPreCon); stateTransitions.Add <WuStateUserInputRequired, WuStateInstalling>(installingPreCon); stateTransitions.Add <WuStateRebootRequired, WuStateRestartSentToOS>(); return(stateTransitions); }