using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows.Input;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard;
using System.Windows.Automation;
using System.Diagnostics;


namespace C1SLAutomation_Demo
{
    /// <summary>
    /// Summary description for OutlookBarTest
    /// </summary>
    [CodedUITest]
    public class OutlookBarTest
    {
        public OutlookBarTest()
        {
        }

        Process SearchIEProcess(string title)
        {
            foreach (Process process in System.Diagnostics.Process.GetProcessesByName("iexplore"))
            {
                if (process.MainWindowHandle != IntPtr.Zero && process.MainWindowTitle.StartsWith(title))
                    return process;
            }
            throw new Exception("Can not find the IE process");
        }

        [TestMethod]
        public void OutlookBarCollapseExpand()
        {
            BrowserWindow.CurrentBrowser = "IE";
            // Run Browser
            BrowserWindow win = BrowserWindow.Launch("about:blank");
            System.Diagnostics.Process process = SearchIEProcess("Blank Page");
            try
            {

                win.NavigateToUrl(new Uri("http://localhost:7777"));
                // Wait some time to complete
                System.Threading.Thread.Sleep(5000);
                AutomationElement mainElement = System.Windows.Automation.AutomationElement.FromHandle(process.MainWindowHandle);

                AutomationElement outlookBar = mainElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "C1OutlookBar"));
                Assert.IsNotNull(outlookBar, "Can not find control.");

                object pattern = null;
                outlookBar.TryGetCurrentPattern(ExpandCollapsePattern.Pattern, out pattern);
                Assert.IsNotNull(pattern, "Can not find ExpandCollapsePattern.");
                ExpandCollapsePattern expandCollapsePattern = (ExpandCollapsePattern)pattern;

                bool collapsed = expandCollapsePattern.Current.ExpandCollapseState == ExpandCollapseState.Collapsed;
                Assert.IsFalse(collapsed, "OutlookBar should be expanded on start");

                expandCollapsePattern.Collapse();
                collapsed = expandCollapsePattern.Current.ExpandCollapseState == ExpandCollapseState.Collapsed;
                Assert.IsTrue(collapsed, "OutlookBar should be collapsed now");


                var items = mainElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "C1OutlookItem"));
                Assert.AreEqual(5, items.Count, "5 OutlookItems expected");


                System.Threading.Thread.Sleep(2000);
            }
            finally
            {
                // Close browser
                process.CloseMainWindow();
            }
        }

        [ClassInitialize]
        static public void ClassInit(TestContext testContext)
        {
            Playback.Initialize();
        }

        /// <summary>
        /// Close IE.
        /// </summary>
        [ClassCleanup]
        static public void ClassClean()
        {
            if (Playback.IsInitialized)
            {
                Playback.Cleanup();
            }
        }



        #region Additional test attributes

        // You can use the following additional attributes as you write your tests:

        ////Use TestInitialize to run code before running each test 
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{        
        //    // To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.
        //    // For more information on generated code, see http://go.microsoft.com/fwlink/?LinkId=179463
        //}

        ////Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{        
        //    // To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.
        //    // For more information on generated code, see http://go.microsoft.com/fwlink/?LinkId=179463
        //}

        #endregion

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }
        private TestContext testContextInstance;
    }
}
