public void TestOpenRtfDocument()
        {
            using (WordApplicationController applicationController = new WordApplicationController(true))
            {
                applicationController.CreateHostApplication(2000);
                Word._Application application = (Word._Application)applicationController.HostApplication;

                object officeDocument = applicationController.OpenDocument(m_testPath + "test.rtf", false);
                Assert.IsNotNull(officeDocument);
                try
                {
                    Assert.AreEqual(1, application.Documents.Count);
                    Assert.IsInstanceOf(typeof(Word.Document), officeDocument);
                }
                finally
                {
                    if (null != officeDocument)
                        Marshal.ReleaseComObject(officeDocument);

                    officeDocument = null;
                }
            }
        }
 public void TestOpenInvalidDocument()
 {
     using (WordApplicationController applicationController = new WordApplicationController(true))
     {
         applicationController.CreateHostApplication(2000);
         applicationController.OpenDocument(null, false);
     }
 }
        public void TestRecoverInvalidHostApplication()
        {
            using (WordApplicationController applicationController = new WordApplicationController(true))
            {
                applicationController.CreateHostApplication(2000);
                Word._Application application = (Word._Application)applicationController.HostApplication;
                Assert.IsNotNull(application);
                Assert.IsTrue(applicationController.IsHostApplicationValid);

                // User kills all word processes
                foreach (Process process in Process.GetProcessesByName("winword"))
                {
                    process.Kill();
                }

				System.Threading.Thread.Sleep(50);
				Process[] p = Process.GetProcessesByName("winword");
				int timeout = 100;
				while ((timeout-- > 0) && (p != null) && (p.Length != 0))
				{
					System.Threading.Thread.Sleep(50);
					p = Process.GetProcessesByName("winword");
				}

				Assert.IsFalse(applicationController.IsHostApplicationValid);

                object officeDocument = applicationController.OpenDocument(m_testPath + "test.doc", false);
                Assert.IsNotNull(officeDocument);
                Assert.IsTrue(applicationController.IsHostApplicationValid);

                application = (Word._Application)applicationController.HostApplication;
                try
                {
                    Assert.AreEqual(1, application.Documents.Count);
                    Assert.IsInstanceOf(typeof(Word.Document), officeDocument);
                }
                finally
                {
                    if (null != officeDocument)
                        Marshal.ReleaseComObject(officeDocument);

                    officeDocument = null;
                }
            }
        }
        public void TestQuit()
        {
            using (WordApplicationController applicationController = new WordApplicationController(true))
            {
                applicationController.CreateHostApplication(2000);
                Assert.IsTrue(applicationController.IsHostApplicationValid, "Expected a valid host application");

                applicationController.Quit();

                Assert.IsFalse(applicationController.IsHostApplicationValid, "Expected the host application to be invalid after a quit");
            }
        }
        public void TestScreenUpdate()
        {
            using (WordApplicationController applicationController = new WordApplicationController(true))
            {
                applicationController.CreateHostApplication(2000);
                object officeDocument = null;
                try
                {
                    Word._Application application = (Word._Application)applicationController.HostApplication;
                    Assert.IsTrue(application.ScreenUpdating);

                    officeDocument = applicationController.OpenDocument(m_testPath + "test.doc", false);
                    Assert.IsNotNull(officeDocument);

                    Assert.IsFalse(application.ScreenUpdating);

                    applicationController.CloseDocument(officeDocument, false);

                    Assert.IsTrue(application.ScreenUpdating);
                }
                finally
                {
                    if (null != officeDocument)
                        Marshal.ReleaseComObject(officeDocument);

                    officeDocument = null;
                }
            }
        }
 public void TestCloseInvalidDocument()
 {
     using (WordApplicationController applicationController = new WordApplicationController(true))
     {
         applicationController.CreateHostApplication(2000);
         applicationController.CloseDocument(new object(), false);
     }
 }
        public void TestCloseAndSaveDocumentWithoutVersioning()
        {
            using (WordApplicationController applicationController = new WordApplicationController(true))
            {
                applicationController.CreateHostApplication(2000);
                Word._Application application = (Word._Application)applicationController.HostApplication;

                string testDocument = m_testPath + "TestAutoVersionTest.doc";
                File.Copy(m_testPath + "AutoVersionTest.doc", testDocument, true);
                Word.Document wordDocument = (Word.Document)applicationController.OpenDocument(testDocument, false);
                Assert.IsNotNull(wordDocument);
                try
                {
                    if (!OfficeApplicationHelpers.IsWordVersion2007orLater(application))
                    {
                        Assert.AreEqual(1, application.Documents.Count);
                        Assert.AreEqual(1, wordDocument.Versions.Count);
                        Assert.AreEqual(Word.WdAutoVersions.wdAutoVersionOnClose, wordDocument.Versions.AutoVersion);

                        // Let's make a change to the document
                        wordDocument.Content.InsertAfter("This is a test to trigger the auto version");

                        wordDocument.Versions.AutoVersion = Word.WdAutoVersions.wdAutoVersionOff;

                        applicationController.CloseDocument(wordDocument, true);

                        // Let's re-open the document
                        wordDocument = (Word.Document)applicationController.OpenDocument(testDocument, false);

                        Assert.AreEqual(1, application.Documents.Count);
                        Assert.AreEqual(1, wordDocument.Versions.Count);
                        Assert.AreEqual(Word.WdAutoVersions.wdAutoVersionOff, wordDocument.Versions.AutoVersion);
                    }

                    applicationController.CloseDocument(wordDocument, false);

                    Assert.AreEqual(0, application.Documents.Count);
                }
                finally
                {
                    if (null != wordDocument)
                        Marshal.ReleaseComObject(wordDocument);

                    wordDocument = null;

                    File.Delete(testDocument);
                }
            }
        }
        public void TestCloseDocumentOnAlreadyClosedDocument()
        {
            using (WordApplicationController applicationController = new WordApplicationController(true))
            {
                applicationController.CreateHostApplication(2000);
                Word.Document document = (Word.Document)applicationController.OpenDocument(m_testPath + "test.doc", false);
                Assert.IsNotNull(document);

                try
                {
                    object boxedSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
                    object originalFormat = null;
                    object routeDocument = false;

                    (document as Word._Document).Close(ref boxedSaveChanges, ref originalFormat, ref routeDocument);

                    applicationController.CloseDocument(document, false);
                }
                finally
                {
                    if (null != document)
                        Marshal.ReleaseComObject(document);

                    document = null;
                }
            }
        }
 public void TestCloseNullDocument()
 {
     using (WordApplicationController applicationController = new WordApplicationController(true))
     {
         applicationController.CreateHostApplication(2000);
         applicationController.CloseDocument(null, false);
     }
 }
        public void TestOpenDocumentWithEncryptedTemplate()
        {
            using (WordApplicationController applicationController = new WordApplicationController(true))
            {
                applicationController.CreateHostApplication(2000);
                Word._Application application = (Word._Application)applicationController.HostApplication;

                object officeDocument = null;
                try
                {
                    string testFile = m_testPath + "TestTemplateWithPassword.dot";
                    Assert.IsTrue(IsEncrypted(testFile));

                    officeDocument = applicationController.OpenDocument(testFile, false, "a", "b");
                    Assert.IsNotNull(officeDocument);
                    Assert.AreEqual(1, application.Documents.Count);
                    Assert.IsInstanceOf(typeof(Word.Document), officeDocument);
                }
                finally
                {
                    if (null != officeDocument)
                        Marshal.ReleaseComObject(officeDocument);

                    officeDocument = null;
                }
            }
        }
        public void TestSaveDocumentWithEncryptedTemplate()
        {
            using (WordApplicationController applicationController = new WordApplicationController(true))
            {
                applicationController.CreateHostApplication(2000);
                Word._Application application = (Word._Application)applicationController.HostApplication;

                string testFile = m_testPath + "TestTemplateWithPassword.dot";
                string encryptedFile = m_testPath + "TestResultTemplateWithPassword.dot";

                if (File.Exists(encryptedFile))
                    File.Delete(encryptedFile);

                object officeDocument = null;
                try
                {
                    Assert.IsTrue(IsEncrypted(testFile));
                    officeDocument = applicationController.OpenDocument(testFile, false, "a", "b");
                    Assert.IsNotNull(officeDocument);
                    Assert.AreEqual(1, application.Documents.Count);
                    Assert.IsInstanceOf(typeof(Word.Document), officeDocument);

                    applicationController.SaveDocumentAs(officeDocument, encryptedFile, false, "x", "y");

                    applicationController.CloseDocument(officeDocument, false);

                    Assert.IsTrue(IsEncrypted(encryptedFile), "Expected the resultant document to be encrypted");
                }
                finally
                {
                    if (null != officeDocument)
                        Marshal.ReleaseComObject(officeDocument);

                    officeDocument = null;

                    File.Delete(encryptedFile);
                }
            }
        }
        public void TestOpenDocumentWithModifyPassword()
        {
            using (WordApplicationController applicationController = new WordApplicationController(true))
            {
                applicationController.CreateHostApplication(2000);
                Word._Application application = (Word._Application)applicationController.HostApplication;

                object officeDocument = null;
                try
                {
                    string testFile = m_testPath + "WriteProtected.doc";

                    // Here we expect false, since "IsEncrypted" will examine the "file.CanOpenContent" that is true
                    // since even if document has Modify Password we still can read it, but cannot wright to it.
                    Assert.IsFalse(IsEncrypted(testFile));

                    officeDocument = applicationController.OpenDocument(testFile, false, null, "Hello Ma");
                    Assert.IsNotNull(officeDocument);
                    Assert.AreEqual(1, application.Documents.Count);
                    Assert.IsInstanceOf(typeof(Word.Document), officeDocument);
                }
                finally
                {
                    if (null != officeDocument)
                        Marshal.ReleaseComObject(officeDocument);

                    officeDocument = null;
                }
            }
        }
        public void TestOpenDocumentWithEnvelopeWithOutlookOpen()
        {
            EmailSender sender = new EmailSender(); //NOT sending any email; Just using to wrap the application
            try
            {
                Outlook._Application outlookApplication = sender.Initialise() as Outlook._Application;

                using (WordApplicationController applicationController = new WordApplicationController(true))
                {
                    applicationController.CreateHostApplication(2000);
                    Word._Application application = (Word._Application)applicationController.HostApplication;

                    object officeDocument = null;
                    try
                    {
                        Outlook.MailItem mailItem = (Outlook.MailItem)outlookApplication.CreateItem(Outlook.OlItemType.olMailItem);
                        officeDocument = applicationController.OpenDocument(m_testPath + "TestEnvelope.doc", false);
                        Assert.IsNotNull(officeDocument);
                    }
                    finally
                    {
                        if (null != officeDocument)
                            Marshal.ReleaseComObject(officeDocument);

                        officeDocument = null;
                    }
                }
            }
            finally
            {
                sender.FinishWith();
            }
        }
        public void TestOpenDuplicateDocument()
        {
            using (WordApplicationController applicationController = new WordApplicationController(true))
            {
                applicationController.CreateHostApplication(2000);
                object officeDocument1 = applicationController.OpenDocument(m_testPath + "test.doc", false);
                Assert.IsNotNull(officeDocument1);
                object officeDocument2 = applicationController.OpenDocument(m_testPath + "test.doc", false);
                Assert.IsNotNull(officeDocument2);
                try
                {
                    Assert.IsInstanceOf(typeof(Word.Document), officeDocument1);
                    Assert.IsInstanceOf(typeof(Word.Document), officeDocument2);
                    Assert.AreSame(officeDocument1, officeDocument2);
                }
                finally
                {
                    if (null != officeDocument1)
                        Marshal.ReleaseComObject(officeDocument1);

                    officeDocument1 = null;

                    if (null != officeDocument2)
                        Marshal.ReleaseComObject(officeDocument2);

                    officeDocument2 = null;
                }
            }
        }
 public void TestOpenDocumentInvalidHostApplication()
 {
     OfficeApplicationHelpers.SetTestSettings(1000, 2000, true, false, false, true, false);
     using (WordApplicationController applicationController = new WordApplicationController(true))
     {
         applicationController.OpenDocument(m_testPath + "test.doc", false);
     }
 }
 public void TestOpenDocumentMissingFile()
 {
     using (WordApplicationController applicationController = new WordApplicationController(true))
     {
         applicationController.CreateHostApplication(2000);
         applicationController.OpenDocument(m_testPath + "doesNotExist.doc", false);
     }
 }