Example #1
0
        public void PDFComponent_Attach_NewComponent_Success()
        {
            // Prepare 'IPDFDocumentObserver'
            var childComponent        = new Mock <IPDFChildComponent>();
            var documentObserver      = childComponent.As <IPDFDocumentObserver>();
            var documentClosedCounter = 0;

            documentObserver.Setup(a => a.DocumentClosed()).Callback(() => documentClosedCounter++);
            var documentClosingCounter = 0;

            documentObserver.Setup(a => a.DocumentClosing()).Callback(() => documentClosingCounter++);
            var documentOpenedCounter = 0;
            var openedFile            = string.Empty;

            documentObserver.Setup(a => a.DocumentOpened(It.IsAny <string>())).Callback <string>((file) =>
            {
                documentOpenedCounter++;
                openedFile = file;
            });
            var documentFailedCounter = 0;
            var failedFile            = string.Empty;

            documentObserver.Setup(a => a.DocumentOpenFailed(It.IsAny <string>())).Callback <string>((file) =>
            {
                documentFailedCounter++;
                failedFile = file;
            });
            var documentOpeningCounter = 0;
            var openingFile            = string.Empty;

            documentObserver.Setup(a => a.DocumentOpening(It.IsAny <string>())).Callback <string>((file) =>
            {
                documentOpeningCounter++;
                openingFile = file;
            });

            var pdfFileExists       = Path.Combine(_pdfFilesFolder, "Precalculus.pdf");
            var pdfFileDoesNotExist = Path.Combine(_pdfFilesFolder, "FileDoesNotExist.pdf");

            var component = new PDFComponent();

            component.Attach(childComponent.Object);

            // Check before any operation processed
            Assert.AreEqual(0, documentClosedCounter);
            Assert.AreEqual(0, documentClosingCounter);
            Assert.AreEqual(0, documentOpenedCounter);
            Assert.AreEqual(0, documentFailedCounter);
            Assert.AreEqual(0, documentOpeningCounter);
            Assert.AreEqual(string.Empty, openedFile);
            Assert.AreEqual(string.Empty, failedFile);
            Assert.AreEqual(string.Empty, openingFile);

            // Open pdf document
            component.OpenDocument(pdfFileExists);
            Assert.IsTrue(component.IsDocumentOpened);

            // Check values
            Assert.AreEqual(0, documentClosedCounter);
            Assert.AreEqual(0, documentClosingCounter);
            Assert.AreEqual(1, documentOpenedCounter);
            Assert.AreEqual(0, documentFailedCounter);
            Assert.AreEqual(1, documentOpeningCounter);
            Assert.AreEqual(pdfFileExists, openedFile);
            Assert.AreEqual(string.Empty, failedFile);
            Assert.AreEqual(pdfFileExists, openingFile);

            // Reset values
            openedFile  = string.Empty;
            openingFile = string.Empty;

            // Close pdf document
            component.CloseDocument();
            Assert.IsFalse(component.IsDocumentOpened);

            // Check values
            Assert.AreEqual(1, documentClosedCounter);
            Assert.AreEqual(1, documentClosingCounter);
            Assert.AreEqual(1, documentOpenedCounter);
            Assert.AreEqual(0, documentFailedCounter);
            Assert.AreEqual(1, documentOpeningCounter);
            Assert.AreEqual(string.Empty, openedFile);
            Assert.AreEqual(string.Empty, failedFile);
            Assert.AreEqual(string.Empty, openingFile);

            // Reset values
            documentClosedCounter  = 0;
            documentClosingCounter = 0;
            documentOpenedCounter  = 0;
            documentFailedCounter  = 0;
            documentOpeningCounter = 0;

            // Open pdf document that does not exist
            component.OpenDocument(pdfFileDoesNotExist);
            Assert.IsFalse(component.IsDocumentOpened);

            // Check values
            Assert.AreEqual(0, documentClosedCounter);
            Assert.AreEqual(0, documentClosingCounter);
            Assert.AreEqual(0, documentOpenedCounter);
            Assert.AreEqual(1, documentFailedCounter);
            Assert.AreEqual(1, documentOpeningCounter);
            Assert.AreEqual(string.Empty, openedFile);
            Assert.AreEqual(pdfFileDoesNotExist, failedFile);
            Assert.AreEqual(pdfFileDoesNotExist, openingFile);

            // Reset values
            failedFile  = string.Empty;
            openingFile = string.Empty;

            // Close pdf document
            component.CloseDocument();
            Assert.IsFalse(component.IsDocumentOpened);

            // Check values
            Assert.AreEqual(0, documentClosedCounter);
            Assert.AreEqual(0, documentClosingCounter);
            Assert.AreEqual(0, documentOpenedCounter);
            Assert.AreEqual(1, documentFailedCounter);
            Assert.AreEqual(1, documentOpeningCounter);
            Assert.AreEqual(string.Empty, openedFile);
            Assert.AreEqual(string.Empty, failedFile);
            Assert.AreEqual(string.Empty, openingFile);

            // Reset values
            documentClosedCounter  = 0;
            documentClosingCounter = 0;
            documentOpenedCounter  = 0;
            documentFailedCounter  = 0;
            documentOpeningCounter = 0;

            // Close not opened pdf document
            component.CloseDocument();
            Assert.IsFalse(component.IsDocumentOpened);

            // Check values
            Assert.AreEqual(0, documentClosedCounter);
            Assert.AreEqual(0, documentClosingCounter);
            Assert.AreEqual(0, documentOpenedCounter);
            Assert.AreEqual(0, documentFailedCounter);
            Assert.AreEqual(0, documentOpeningCounter);
            Assert.AreEqual(string.Empty, openedFile);
            Assert.AreEqual(string.Empty, failedFile);
            Assert.AreEqual(string.Empty, openingFile);

            component.Dispose();
        }