Beispiel #1
0
        public void StartExport()
        {
            // arrange
            var exportUtility = new ExportUtility();

            var exportViewModel = new ExportOptionsViewModel();
            exportUtility.TheExportOptionsViewModel = new Lazy<ExportOptionsViewModel>(() => exportViewModel);

            var shell = Mock.Create<IShell>(Behavior.Loose);
            exportUtility.TheWindowManager = new Lazy<IShell>(() => shell);

            var dynamicTypeManager = Mock.Create<IDynamicTypeManager>(Behavior.Loose);
            exportUtility.TheDynamicTypeManager = new Lazy<IDynamicTypeManager>(() => dynamicTypeManager);

            Mock.Arrange(() => Utils.CurrentUserAccountId).Returns(-1);

            var beginInsertEmailLogWasCalled = false;
            Mock.Arrange(() => MethodCaller.CallFactoryMethod(Arg.IsAny<Type>(), Arg.AnyString, Arg.IsAny<object[]>())).DoInstead(() => beginInsertEmailLogWasCalled = true);

            var popupFactory = new PopupFactory();

            var notifySuccessCalled = false;
            Mock.Arrange(() => popupFactory.NotifySuccess(LanguageService.Translate("Msg_ProcessExported"), "Popup_Success")).DoInstead(() => notifySuccessCalled = true);

            var notifyFailureCalled = false;
            Mock.Arrange(() => popupFactory.NotifyFailure(LanguageService.Translate("Msg_ExportError"), "Popup_Error", 3, false)).DoInstead(() => notifyFailureCalled = true);

            exportUtility.ThePopupFactory = new Lazy<PopupFactory>(() => popupFactory);

            var exportMethodWasCalled = false;
            Mock.NonPublic.Arrange<bool>(exportUtility, "Export", new object[] { ArgExpr.IsAny<GridViewDataControl>(), ArgExpr.IsAny<string>() }).DoInstead(() => exportMethodWasCalled = true).Returns(true);

            // act
            exportUtility.ExportStart("eCar_p", new GridViewDataControl(), "Id|CurrentState");

            // assert
            Assert.IsTrue(beginInsertEmailLogWasCalled);
            Assert.IsTrue(notifySuccessCalled);
            Assert.IsFalse(notifyFailureCalled);
            Assert.IsTrue(exportMethodWasCalled); // After exporting, we have to return it as it was, that is, back 25 info items

            // arrange
            notifySuccessCalled = notifyFailureCalled = false;
            Mock.NonPublic.Arrange<bool>(exportUtility, "Export", new object[] { ArgExpr.IsAny<GridViewDataControl>(), ArgExpr.IsAny<string>() }).Returns(false);

            // act
            exportUtility.ExportStart("eCar_p", new GridViewDataControl(), "Id|CurrentState");

            // assert
            Assert.IsFalse(notifySuccessCalled);
            Assert.IsTrue(notifyFailureCalled);
        }
        public void UploadImageCommand()
        {
            // arrange
            var vm = new ImageFieldViewModel();

            var openFileDialog = Mock.Create<OpenFileDialog>(Behavior.CallOriginal);
            Mock.Arrange(() => openFileDialog.ShowDialog()).Returns(false);

            var privateAccessor = new PrivateAccessor(vm);
            privateAccessor.SetField("_openFileDialog", openFileDialog);

            try
            {
                // act
                vm.UploadImageCommand.Execute(null);
            }
            catch (Exception ex)
            {
                // assert
                Assert.Fail("Expected no exception, but got: " + ex.Message);
            }

            // arrange
            Mock.Arrange(() => openFileDialog.ShowDialog()).Returns(true);

            var file = Mock.Create<FileInfo>(Behavior.CallOriginal);
            Mock.Arrange(() => file.Length).Returns(8 * 1024);
            Mock.Arrange(() => openFileDialog.File).Returns(file);

            var popupFactory = new PopupFactory();
            var notifyFailureCalled = false;
            Mock.Arrange(() => popupFactory.NotifyFailure(Arg.AnyString, "Popup_Error", 3, false)).DoInstead(() => notifyFailureCalled = true);

            vm.ThePopupFactory = popupFactory;

            // act
            vm.UploadImageCommand.Execute(null);

            // assert
            Assert.IsTrue(notifyFailureCalled);

            // arrange
            Mock.NonPublic.Arrange<ulong>(vm, "MaxFileSize").Returns((ulong)file.Length + 1);

            var fileStream = Mock.Create<FileStream>(Behavior.Loose);

            var exit = false;
            Mock.Arrange(() => fileStream.Read(Arg.IsAny<byte[]>(), 0, Arg.AnyInt)).Returns(() =>
                    {
                        if (!exit)
                        {
                            exit = true;
                            return 1;
                        }

                        return 0;
                    });

            Mock.Arrange(() => file.OpenRead()).Returns(fileStream);

            var imageVisibilityRaises = false;
            Mock.NonPublic.Arrange(vm, "RaisePropertyChanged", ArgExpr.IsAny<Expression<Func<bool>>>()).DoInstead(() => imageVisibilityRaises = true);

            var downloadImageCommandRaises = false;
            Mock.Arrange(() => vm.DownloadImageCommand.RaiseCanExecuteChanged()).DoInstead(() => downloadImageCommandRaises = true);

            var clearImageCommandRaises = false;
            Mock.Arrange(() => vm.ClearImageCommand.RaiseCanExecuteChanged()).DoInstead(() => clearImageCommandRaises = true);

            // act
            vm.UploadImageCommand.Execute(null);

            // assert
            Assert.AreEqual(8 * 1024, ((byte[])vm.Value).Length);
            Assert.IsTrue(imageVisibilityRaises);
            Assert.IsTrue(downloadImageCommandRaises);
            Assert.IsTrue(clearImageCommandRaises);
            Assert.IsNull(privateAccessor.GetField("_openFileDialog"));
        }
        public void OnImageFailed()
        {
            // arrange
            var vm = new ImageFieldViewModel();
            var exception = Mock.Create<ExceptionRoutedEventArgs>(Behavior.CallOriginal);

            var popupFactory = new PopupFactory();
            var notifyFailureCalled = false;
            Mock.Arrange(() => popupFactory.NotifyFailure(Arg.IsAny<Exception>(), "Popup_Error", false)).DoInstead(() => notifyFailureCalled = true);

            vm.ThePopupFactory = popupFactory;

            // act
            new PrivateAccessor(vm).CallMethod("OnImageFailed", new object[] { null, exception });

            // assert
            Assert.IsTrue(notifyFailureCalled);
        }
        public void ExportDataGrid()
        {
            // arrange
            var vm = new ProcessSearchGridGadgetGridTabViewModel
                {
                    ExportGridControl = new GridViewDataControl()
                };

            var exportUtility = new ExportUtility();
            Mock.Arrange(() => exportUtility.ExportRecordCount).Returns(int.MaxValue);

            Mock.Arrange(() => exportUtility.ExportSetup(null, Arg.IsAny<int>(), Arg.IsAny<Action>()))
                .DoInstead<object, int, Action>((orientation, totalRowCount, action) => action());

            vm.TheExportUtility = new Lazy<ExportUtility>(() => exportUtility);

            var privateAccessor = new PrivateAccessor(vm);
            privateAccessor.SetField("_currentPageNumber", 10);
            privateAccessor.SetField("_pageSize", 25);

            var called = false;
            Mock.Arrange(() => vm.RefreshView()).DoInstead(() => called = true);

            // act
            vm.ExportDataGrid();

            // assert
            Assert.IsTrue(called);

            Assert.AreEqual(10, privateAccessor.GetField("_tempCurrentPageNumber"));
            Assert.AreEqual(25, privateAccessor.GetField("_tempPageSize"));

            Assert.AreEqual(0, privateAccessor.GetField("_currentPageNumber"));
            Assert.AreEqual(int.MaxValue, privateAccessor.GetField("_pageSize"));

            // arrange
            called = false;
            vm.ExportGridControl = null;
            
            var popupFactory = new PopupFactory();

            var notifyFailureCalled = false;
            Mock.Arrange(() => popupFactory.NotifyFailure(LanguageService.Translate("Msg_OpenGridViewBeforeExport"), "Popup_Error", 3, false)).DoInstead(() => notifyFailureCalled = true);

            vm.ThePopupFactory = popupFactory;

            // act
            vm.ExportDataGrid();
            
            // assert
            Assert.IsTrue(notifyFailureCalled);
            Assert.IsFalse(called);
        }