public void Run_SupplySettingToDeleteFromTargetFolder_AllFilesAreDeleted()
        {
            // arrange
            _settings = new RandomizerWorkerSettings
            {
                DeleteFromTargetFolder = true,
                PathTo = "path to"
            };

            List<AppFile> files = new List<AppFile>
            {
                new AppFile { },
                new AppFile { },
                new AppFile { }
            };

            _fileServiceMock.Setup(x => x.GetFiles(_settings.PathTo)).Returns(files);

            _fileServiceMock
                .Setup(x => x.DeleteFile(It.IsAny<AppFile>()))
                .Callback((AppFile file) =>
                {
                    Assert.That(file, Is.EqualTo(files.SingleOrDefault(x => x == file)));
                });

            // act, assert
            _worker.Run(_settings, (x) => { }, (y) => { }, () => { });

            _backgroundWorkerMock.Raise(x => { x.OnDoWork += null; }, null, new System.ComponentModel.DoWorkEventArgs(null));
        }
        public void Run_SupplySettingOnDuplicateDontCopyAndCancellationPending_WorkerStopsAfterThirdFileAndDoesntContinueIteratingFiles()
        {
            // arrange
            _settings = new RandomizerWorkerSettings
            {
                OnDuplicateDoNotCopy = true,
                PathTo = "path to"
            };

            FileEnumerationStub stub = new FileEnumerationStub(_backgroundWorkerMock);

            _fileServiceMock
                .Setup(x => x.GetFiles(_settings.PathTo))
                .Returns(stub.GetFilesWithCancelCallback);

            // act
            _worker.Run(_settings, (x) => { }, (y) => { }, () => { });

            _backgroundWorkerMock.Raise(x => { x.OnDoWork += null; }, null, new System.ComponentModel.DoWorkEventArgs(null));

            // assert
            Assert.That(stub.filesCalledIndex, Is.EqualTo(3));
        }
        public void Run_SupplySettingOnDuplicateDontCopy_ReturnsAllExistingFilesInTargetFolder()
        {
            // arrange
            _settings = new RandomizerWorkerSettings
            {
                OnDuplicateDoNotCopy = true,
                PathTo = "path to"
            };

            List<AppFile> files = new List<AppFile>
            {
                new AppFile { },
                new AppFile { },
                new AppFile { }
            };

            _fileServiceMock.Setup(x => x.GetFiles(_settings.PathTo)).Returns(files);

            // act
            _worker.Run(_settings, (x) => { }, (y) => { }, () => { });

            _backgroundWorkerMock.Raise(x => { x.OnDoWork += null; }, null, new System.ComponentModel.DoWorkEventArgs(null));

            // assert
            _fileServiceMock.Verify(x => x.GetFiles(_settings.PathTo), Times.Once());
        }
        public void Run_SupplyFilesToDeleteFromTargetFolderAndCancellationPending_WorkerStopsAfterThirdFileAndDoesntContinueDeletingFiles()
        {
            // arrange
            _settings = new RandomizerWorkerSettings
            {
                DeleteFromTargetFolder = true,
                PathTo = "path to"
            };

            List<AppFile> files = new List<AppFile>
            {
                new AppFile { },
                new AppFile { },
                new AppFile { },
                new AppFile { },
                new AppFile { },
                new AppFile { }
            };

            _fileServiceMock.Setup(x => x.GetFiles(_settings.PathTo)).Returns(files);

            int fileIndex = 0;

            _fileServiceMock
                .Setup(x => x.DeleteFile(It.IsAny<AppFile>()))
                .Callback((AppFile file) =>
                {
                    ++fileIndex;

                    if (fileIndex == 3)
                    {
                        _backgroundWorkerMock.SetupGet(x => x.CancellationPending).Returns(true);
                    }
                });

            // act
            _worker.Run(_settings, (x) => { }, (y) => { }, () => { });

            _backgroundWorkerMock.Raise(x => { x.OnDoWork += null; }, null, new System.ComponentModel.DoWorkEventArgs(null));

            // assert
            _fileServiceMock.Verify(x => x.DeleteFile(It.IsAny<AppFile>()), Times.Exactly(3));
        }
        public void Run_SupplyNullOnFinishedCallback_ThrowsOnFinishedArgumentNullException()
        {
            // arrange
            _settings = new RandomizerWorkerSettings();

            TestDelegate testDelegate = () => _worker.Run(_settings, null, null, null);

            // act, assert
            Assert.That(testDelegate, Throws.Exception.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("onFinished"));
        }
        public void Run_SettingsContainsFilesNumberLimit_ReturnsRandomNonUniqueFiles()
        {
            // arrange
            _settings = new RandomizerWorkerSettings
            {
                FilesNumberLimit = 1,
                SelectedLimit = LimitType.FilesNumber
            };

            FileEnumerationStub stub = new FileEnumerationStub(_backgroundWorkerMock);

            _fileServiceMock.Setup(x => x.GetFilesByFilesNumber(It.IsAny<bool>(), It.IsAny<IEnumerable<AppFile>>(), It.IsAny<IEnumerable<AppFile>>(), _settings.FilesNumberLimit)).Returns(stub.GetFiles);

            // act
            _worker.Run(_settings, (x) => { }, (y) => { }, () => { });

            _backgroundWorkerMock.Raise(x => { x.OnDoWork += null; }, null, new System.ComponentModel.DoWorkEventArgs(null));

            // assert
            Assert.That(stub.filesCalledIndex, Is.EqualTo(5));
        }
        public void Run_SettingsContainsFoldersNumberLimitAndFilesInFolderNumberLimit_WorkerStopsAfterThirdFileAndDoesntContinueIteratingFiles()
        {
            // arrange
            _settings = new RandomizerWorkerSettings
            {
                FoldersNumberLimit = 1,
                FilesNumberPerFolderLimit = 1,
                SelectedLimit = LimitType.FilesNumberPerFolder
            };

            FileEnumerationStub stub = new FileEnumerationStub(_backgroundWorkerMock);

            _fileServiceMock.Setup(x => x.GetFilesByFilesNumber(It.IsAny<bool>(), It.IsAny<IEnumerable<AppFile>>(), It.IsAny<IEnumerable<AppFile>>(), _settings.FoldersNumberLimit)).Returns(stub.GetFilesWithCancelCallback);

            // act
            _worker.Run(_settings, (x) => { }, (y) => { }, () => { });
            _backgroundWorkerMock.Raise(x => { x.OnDoWork += null; }, null, new System.ComponentModel.DoWorkEventArgs(null));

            // assert
            Assert.That(stub.filesCalledIndex, Is.EqualTo(3));
        }
        public void Run_ExecutesTraverseFunctionAndReturnsFilesFromSourceFolder()
        {
            // arrange
            _settings = new RandomizerWorkerSettings
            {
                PathsFrom = new string[] { "path from" }
            };

            FileEnumerationStub stub = new FileEnumerationStub(_backgroundWorkerMock);

            _settings.PathsFrom.ToList().ForEach(x => _traverseServiceMock.Setup(y => y.TraverseFolder(x)).Returns(stub.GetFiles));

            // act
            _worker.Run(_settings, (x) => { }, (y) => { }, () => { });

            _backgroundWorkerMock.Raise(x => { x.OnDoWork += null; }, null, new System.ComponentModel.DoWorkEventArgs(null));

            // assert
            Assert.That(stub.filesCalledIndex, Is.EqualTo(5));
        }
        public void Run_CancellationPending_WorkerStopsAfterThirdFileAndDoesntContinueTraverseFiles()
        {
            // arrange
            _settings = new RandomizerWorkerSettings
            {
                PathsFrom = new string[] { "path from" }
            };

            FileEnumerationStub stub = new FileEnumerationStub(_backgroundWorkerMock);

            _settings.PathsFrom.ToList().ForEach(x => _traverseServiceMock.Setup(y => y.TraverseFolder(x)).Returns(stub.GetFilesWithCancelCallback));

            // act
            _worker.Run(_settings, (x) => { }, (y) => { }, () => { });

            _backgroundWorkerMock.Raise(x => { x.OnDoWork += null; }, null, new System.ComponentModel.DoWorkEventArgs(null));

            // assert
            Assert.That(stub.filesCalledIndex, Is.EqualTo(3));
        }
        public void Run_CancellationIsPending_WorkerStopsAndCallsOnFinishedCallback()
        {
            // arrange
            _backgroundWorkerMock.SetupGet(x => x.CancellationPending).Returns(true);

            _settings = new RandomizerWorkerSettings();

            Action<List<AppFile>> onFinished = (files) => Assert.That(files.Count, Is.EqualTo(0));

            // act, assert
            _worker.Run(_settings, onFinished, (str) => { }, () => { });

            _backgroundWorkerMock.Raise(x => { x.OnDoWork += null; }, null, new System.ComponentModel.DoWorkEventArgs(null));
        }
        public void Run(RandomizerWorkerSettings settings, Action<List<AppFile>> onFinished, Action<string> onFailed, Action onCancelled)
        {
            if (settings == null) throw new ArgumentNullException("settings");
            if (onFinished == null) throw new ArgumentNullException("onFinished");
            if (onFailed == null) throw new ArgumentNullException("onFailed");
            if (onCancelled == null) throw new ArgumentNullException("onCancelled");

            _backgroundWorker.WorkerSupportsCancellation = true;

            _backgroundWorker.OnDoWork += (sender, e) =>
            {
                List<AppFile> filesFound = new List<AppFile>();
                List<AppFile> filesToCopy = new List<AppFile>();
                List<AppFile> filesExisting = new List<AppFile>();

                if (!IsCancellationPending(e))
                {
                    if (settings.DeleteFromTargetFolder)
                    {
                        foreach (AppFile info in _fileService.GetFiles(settings.PathTo))
                        {
                            if (IsCancellationPending(e)) return;
                            _fileService.DeleteFile(info);
                        }
                    }
                }

                if (!IsCancellationPending(e))
                {
                    if (settings.OnDuplicateDoNotCopy)
                    {
                        foreach (AppFile file in _fileService.GetFiles(settings.PathTo))
                        {
                            if (IsCancellationPending(e)) return;
                            filesExisting.Add(file);
                        }
                    }
                }

                if (!IsCancellationPending(e) && settings.PathsFrom != null)
                {
                    foreach (string path in settings.PathsFrom)
                    {
                        foreach (AppFile file in _traverseService.TraverseFolder(path))
                        {
                            if (IsCancellationPending(e)) return;
                            filesFound.Add(file);
                        }
                    }
                }

                if (!IsCancellationPending(e))
                {
                    if (settings.SelectedLimit == LimitType.FilesTotalSize)
                    {
                        foreach (AppFile file in _fileService.GetFilesByMaxFileSize(settings.FindOnlyUniqueFiles, filesFound, filesExisting, settings.SizeLimitBytes))
                        {
                            if (IsCancellationPending(e)) return;
                            filesToCopy.Add(file);
                        }
                    }
                    else if (settings.SelectedLimit == LimitType.FilesNumber || settings.SelectedLimit == LimitType.FilesNumberPerFolder)
                    {
                        int filesNumber = 0;

                        if (settings.SelectedLimit == LimitType.FilesNumber)
                        {
                            filesNumber = settings.FilesNumberLimit;
                        }
                        else if (settings.SelectedLimit == LimitType.FilesNumberPerFolder)
                        {
                            filesNumber = settings.FoldersNumberLimit * settings.FilesNumberPerFolderLimit;
                        }

                        foreach (AppFile file in _fileService.GetFilesByFilesNumber(settings.FindOnlyUniqueFiles, filesFound, filesExisting, filesNumber))
                        {
                            if (IsCancellationPending(e)) return;
                            filesToCopy.Add(file);
                        }
                    }
                }

                e.Result = filesToCopy;
            };

            _backgroundWorker.OnRunWorkerCompleted += (sender, e) =>
            {
                if (e.Error != null)
                {
                    _uiContext.Post(x => onFailed(e.Error.Message), null);
                }
                else if (e.Cancelled)
                {
                    _uiContext.Post(x => onCancelled(), null);
                }
                else
                {
                    _uiContext.Post(x => onFinished((List<AppFile>)e.Result), null);
                }
            };

            if (_backgroundWorker.IsBusy)
            {
                _backgroundWorker.CancelAsync();
            }

            _backgroundWorker.RunWorkerAsync();
        }
 public GlobalWizardViewModel(string titleSuffix)
 {
     _titleSuffix = titleSuffix;
     RandomizerWorkerSettings = new RandomizerWorkerSettings();
     CopyWorkerSettings = new CopyWorkerSettings();
 }