Exemplo n.º 1
0
        public void Should_NotAllowEqualTransitions_When_AddToTransitionList()
        {
            StateTransitionCollection coll = new StateTransitionCollection();

            coll.Add <WuStateReady, WuStateDownloading>();
            coll.Add <WuStateReady, WuStateDownloading>((c) => { return(new ConditionEvalResult(true, null)); });
        }
        /// <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);
        }