Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 4
0
 public ProgramPluginContext(CommandRunner commandRunner, IFeatureRegistry featureRegistry, ICommandProvider commandProvider)
 {
     this.commandRunner = commandRunner;
     this.featureRegistry = featureRegistry;
     this.commandProvider = commandProvider;
 }
Exemplo n.º 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);
        }
Exemplo n.º 6
0
        public void RunThrowsOnNull()
        {
            var runner = new CommandRunner();

            Assert.Throws<ArgumentNullException>(() => runner.Run(null));
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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());
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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());
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 15
0
        public static ProgramSettings Create(ProgramSettingsData settingsData, CommandRunner commandRunner, ExtensionManager extensionManager)
        {
            var settings = new ProgramSettings(settingsData);
            settings.Initialize(commandRunner, extensionManager);

            return settings;
        }