Example #1
0
        public void CancellationFiresCanceled()
        {
            var runner = new CommandRunner();

            var cancelException = new CommandCanceledException();
            CommandCanceledException firedReason = null;

            int fireCount = 0;

            runner.TaskCanceled += (sender, e) => {
                ++fireCount;

                firedReason = e.CancelReason;
            };

            var mockCommand = GetCommandMock();
            mockCommand.Setup((command) => command.Process(null, It.Is<IMutableProgressTracker>((mpt) => mpt != null), It.IsAny<CancellationToken>()))
                .Returns((TypedData) null)
                .Callback(() => {
                    throw cancelException;
                });

            var task = runner.Run(mockCommand.Object);
            task.WaitHandle.WaitOne();

            Assert.AreEqual(1, fireCount);
            AssertCanceledExceptionsSame(cancelException, firedReason);
        }
Example #2
0
        public void CancellationSetsCancelled()
        {
            var runner = new CommandRunner();

            var mockCommand = GetCommandMock();
            mockCommand.Setup((command) => command.Process(null, It.Is<IMutableProgressTracker>((mpt) => mpt != null), It.IsAny<CancellationToken>()))
                .Returns((TypedData) null)
                .Callback(() => {
                    throw new CommandCanceledException();
                });

            var task = runner.Run(mockCommand.Object);
            task.WaitHandle.WaitOne();

            Assert.AreEqual(TaskState.Canceled, task.State);
        }
Example #3
0
        private void Load()
        {
            var commandRunner = new CommandRunner();

            Log.Info("Loading extension manager");
            this.extensionManager = new ExtensionManager(Directory.CreateDirectory(Path.Combine(Directory.GetCurrentDirectory(), "Extensions")));

            Log.Info("Loading application settings");
            this.applicationSettings = new ApplicationSettings(new ProgramSettingsDataSerializer(this.extensionManager));

            ProgramSettingsData settingsData = null;

            try {
                settingsData = this.applicationSettings.LoadSettingsData();
            } catch (SerializationException e) {
                Log.Warn("Loading settings failed", e);

                if (this.guiMode) {
                    MessageBox.Show(
                        "An error occurred loading your NoCap settings.  Settings will be restored to their defaults.",
                        "Error loading settings",
                        MessageBoxButton.OK,
                        MessageBoxImage.Exclamation
                    );
                }
            }

            bool loadCommandDefaults = false;

            if (settingsData == null) {
                Log.Debug("Application settings not found; using defaults");

                settingsData = new ProgramSettingsData();

                loadCommandDefaults = true;
            }

            this.settings = ProgramSettings.Create(settingsData, commandRunner, this.extensionManager);

            var featureRegistry = this.settings.FeatureRegistry;

            // TODO Move elsewhere
            featureRegistry.Register(CommandFeatures.ImageUploader, "Image uploader");
            featureRegistry.Register(CommandFeatures.UrlShortener,  "Url shortener" );
            featureRegistry.Register(CommandFeatures.FileUploader,  "File uploader" );
            featureRegistry.Register(CommandFeatures.TextUploader,  "Text uploader" );

            if (loadCommandDefaults) {
                this.settings.LoadCommandDefaults();
            }
        }
Example #4
0
 public ProgramPluginContext(CommandRunner commandRunner, IFeatureRegistry featureRegistry, ICommandProvider commandProvider)
 {
     this.commandRunner = commandRunner;
     this.featureRegistry = featureRegistry;
     this.commandProvider = commandProvider;
 }
Example #5
0
        public void TaskNameIsCommandName()
        {
            var runner = new CommandRunner();

            const string commandName = "Command name";

            var mockCommand = GetCommandMock();
            mockCommand.Setup((command) => command.Process(null, It.Is<IMutableProgressTracker>((mpt) => mpt != null), It.IsAny<CancellationToken>()))
                .Returns((TypedData) null);
            mockCommand.Setup((command) => command.Name)
                .Returns(commandName);

            var task = runner.Run(mockCommand.Object);
            task.WaitHandle.WaitOne();

            Assert.AreEqual(commandName, task.Name);
        }
Example #6
0
        public void RunThrowsOnNull()
        {
            var runner = new CommandRunner();

            Assert.Throws<ArgumentNullException>(() => runner.Run(null));
        }
Example #7
0
        public void RunSetsIsRunning()
        {
            var runner = new CommandRunner();

            ICommandTask task = null;
            TaskState? taskStateInCommand = null;
            var taskReadySync = new AutoResetEvent(false);

            var mockCommand = GetCommandMock();
            mockCommand.Setup((command) => command.Process(null, It.Is<IMutableProgressTracker>((mpt) => mpt != null), It.IsAny<CancellationToken>()))
                .Returns((TypedData) null)
                .Callback(() => {
                    taskReadySync.WaitOne();
                    taskStateInCommand = task.State;
                });

            task = runner.Run(mockCommand.Object);
            taskReadySync.Set();
            task.WaitHandle.WaitOne();

            Assert.IsNotNull(taskStateInCommand);
            Assert.AreEqual(TaskState.Running, (TaskState) taskStateInCommand);
        }
Example #8
0
        public void RunFiresStartOnce()
        {
            var runner = new CommandRunner();

            int fireCount = 0;

            runner.TaskStarted += (sender, e) => {
                ++fireCount;
            };

            var task = runner.Run(new CommandChain());
            task.WaitHandle.WaitOne();

            Assert.AreEqual(1, fireCount);
        }
Example #9
0
        public void RunExecutesCommand()
        {
            var runner = new CommandRunner();

            var mockCommand = GetCommandMock();
            mockCommand.Setup((command) => command.Process(null, It.Is<IMutableProgressTracker>((mpt) => mpt != null), It.IsAny<CancellationToken>()))
                .Returns((TypedData) null);

            var task = runner.Run(mockCommand.Object);
            task.WaitHandle.WaitOne();

            mockCommand.Verify((command) => command.Process(null, It.IsAny<IMutableProgressTracker>(), It.IsAny<CancellationToken>()), Times.Once());
        }
Example #10
0
        public void RunExecutesCommandInSeparateThread()
        {
            var runner = new CommandRunner();
            Thread commandThread = null;

            var mockCommand = GetCommandMock();
            mockCommand.Setup((command) => command.Process(null, It.Is<IMutableProgressTracker>((mpt) => mpt != null), It.IsAny<CancellationToken>()))
                .Returns((TypedData) null)
                .Callback(() => {
                    commandThread = Thread.CurrentThread;
                });

            var task = runner.Run(mockCommand.Object);
            task.WaitHandle.WaitOne();

            Assert.AreNotEqual(Thread.CurrentThread, commandThread);
        }
Example #11
0
        public void RunEndResetsIsRunning()
        {
            var runner = new CommandRunner();

            var mockCommand = GetCommandMock();
            mockCommand.Setup((command) => command.Process(null, It.Is<IMutableProgressTracker>((mpt) => mpt != null), It.IsAny<CancellationToken>()))
                .Returns((TypedData) null);

            var task = runner.Run(mockCommand.Object);
            task.WaitHandle.WaitOne();

            Assert.AreNotEqual(TaskState.Running, task.State);
        }
Example #12
0
        public void RunDisposesReturnedData()
        {
            var mockDisposable = new Mock<IDisposable>(MockBehavior.Strict);
            mockDisposable.Setup((typedData) => typedData.Dispose());

            var mockCommand = GetCommandMock();
            mockCommand.Setup((command) => command.Process(null, It.Is<IMutableProgressTracker>((mpt) => mpt != null), It.IsAny<CancellationToken>()))
                .Returns(new TypedData(TypedDataType.User, mockDisposable.Object, "my data"));

            var runner = new CommandRunner();
            var task = runner.Run(mockCommand.Object);
            task.WaitHandle.WaitOne();

            mockDisposable.Verify((typedData) => typedData.Dispose(), Times.Once());
        }
Example #13
0
        public void ProgressUpdatesFire()
        {
            var runner = new CommandRunner();

            var progressUpdates = new List<double>();

            runner.ProgressUpdated += (sender, e) => progressUpdates.Add(e.Progress);

            var mockCommand = GetCommandMock();
            mockCommand.Setup((command) => command.Process(null, It.Is<IMutableProgressTracker>((mpt) => mpt != null), It.IsAny<CancellationToken>()))
                .Returns((TypedData) null)
                .Callback((TypedData data, IMutableProgressTracker progress, CancellationToken cancelToken) => {
                    progress.Progress = 0.5;
                    progress.Progress = 0.6;
                    progress.Progress = 1;
                });

            var task = runner.Run(mockCommand.Object);
            task.WaitHandle.WaitOne();

            CollectionAssert.AreEqual(new[] { 0.5, 0.6, 1 }, progressUpdates);
        }
Example #14
0
        private void Initialize(CommandRunner commandRunner, ExtensionManager extensionManager)
        {
            var commandProvider = new ProgramSettingsCommandProvider(this, extensionManager.CommandCompositionContainer);
            var defaultRegistry = new ProgramFeatureRegistry(DefaultCommands, commandProvider);
            var pluginContext = new ProgramPluginContext(commandRunner, defaultRegistry, commandProvider);

            this.commandProvider = commandProvider;
            this.pluginContext = pluginContext;

            Plugins.Initialize(pluginContext, extensionManager.CommandCompositionContainer);
        }
Example #15
0
        public static ProgramSettings Create(ProgramSettingsData settingsData, CommandRunner commandRunner, ExtensionManager extensionManager)
        {
            var settings = new ProgramSettings(settingsData);
            settings.Initialize(commandRunner, extensionManager);

            return settings;
        }