using System;
using System.Collections.Generic;
using ArtOfTest.WebAii.Core;
using ArtOfTest.WebAii.Silverlight;
using ArtOfTest.WebAii.Silverlight.UI;
using ArtOfTest.WebAii.TestTemplates;
using NUnit.Framework;
using Telerik.WebAii.Controls.Xaml;

namespace ClassLibrary2
{
    /// <summary>
    /// Summary description for TelerikNUnitTest1
    /// </summary>
    [TestFixture]
    public class TelerikNUnitTest1 : BaseTest
    {

        #region [Setup / TearDown]

        /// <summary>
        /// Initialization for each test.
        /// </summary>
        [SetUp]
        public void MyTestInitialize()
        {
            #region WebAii Initialization

            // Initializes WebAii manager to be used by the test case.
            // If a WebAii configuration section exists, settings will be
            // loaded from it. Otherwise, will create a default settings
            // object with system defaults.
            //
            // Note: We are passing in a delegate to the NUnit's TestContext.Out.
            // WriteLine() method. This way any logging
            // done from WebAii (i.e. Manager.Log.WriteLine()) is
            // automatically logged to same output as NUnit.
            //
            // If you do not care about unifying the log, then you can simply
            // initialize the test by calling Initialize() with no parameters;
            // that will cause the log location to be picked up from the config
            // file if it exists or will use the default system settings.
            // You can also use Initialize(LogLocation) to set a specific log
            // location for this test.
            
            // Pass in 'true' to recycle the browser between test methods
            //Initialize(false, new TestContextWriteLine(Core.TestContext.Out.WriteLine));

            // If you need to override any other settings coming from the
            // config section or you don't have a config section, you can
            // comment the 'Initialize' line above and instead use the
            // following:

            

            // This will get a new Settings object. If a configuration
            // section exists, then settings from that section will be
            // loaded

            Settings settings = GetSettings();

            // Override the settings you want. For example:
            settings.DefaultBrowser = BrowserType.InternetExplorer;
            settings.Web.EnableSilverlight = true;
            settings.ClientReadyTimeout = 50000;
            settings.ExecuteCommandTimeout = 60000;
            settings.ExecutionDelay = 1;
            settings.WaitCheckInterval = 1000;
            settings.SimulatedMouseMoveSpeed = Single.MaxValue;
            settings.Web.EnableUILessRequestViewing = false;
            settings.Web.EnableScriptLogging = false;
            settings.Web.KillBrowserProcessOnClose = true;
            settings.Web.LocalWebServer = LocalWebServerType.None;
            settings.Web.EnableSilverlight = true;
            settings.Web.BaseUrl = "http://localhost:62922/index.html";
            settings.QueryEventLogErrorsOnExit = false;
            settings.AnnotateExecution = false;
            settings.AnnotationMode = AnnotationMode.All;
            settings.LogAnnotations = false;
            // Now call Initialize again with your updated settings object
            Initialize(settings);

            

            #endregion

            //
            // Place any additional initialization here
            //
        }

        /// <summary>
        /// Clean up after each test.
        /// </summary>
        [TearDown]
        public void MyTestCleanUp()
        {
            //
            // Place any additional cleanup here
            //

            #region WebAii CleanUp

            // Shuts down WebAii manager and closes all browsers currently running
            this.CleanUp();

            #endregion
        }

        /// <summary>
        /// Called after all tests in this class are executed.
        /// </summary>
        [TestFixtureTearDown]
        public void FixtureCleanup()
        {
            // This will shut down all browsers if
            // recycleBrowser is turned on. Else
            // will do nothing.
            ShutDown();
        }

        #endregion


        [Test]
        public void WorkingTest()
        {
            Manager.Settings.EnableSilverlight = true;

            // Launch a browser instance 
            Manager.LaunchNewBrowser();

            // The active browser 
            ActiveBrowser.NavigateTo("http://localhost:62922/index.html");

            this.ActiveBrowser.WaitUntilReady();
            this.ActiveBrowser.RefreshDomTree();

            SilverlightApp app = ActiveBrowser.SilverlightApps()[0];

            Button show = app.Find.ByName<Button>("showButton");
            show.User.Click();

            RadWindow window = app.Find.ByName<RadWindow>("AWindow");
            Button newcf = app.Find.ByName<Button>("populateButton");
            newcf.User.Click();
            try
            {
                ListBox lb = window.Find.ByName<ListBox>("Elements");
                Assert.AreEqual(3, lb.Items.Count);
                lb.Refresh();
                IList<RadButton> deleteButtons = lb.Find.AllByName<RadButton>("DeleteElementButton");
                Assert.AreEqual(3, deleteButtons.Count);

                Log.WriteLine("ListBox Items Count :   " + lb.Items.Count);
                Log.WriteLine("Delete Buttons Count :   " + deleteButtons.Count);

            }
            catch (TimeoutException e)
            {
                Log.WriteLine("Listbox : " + e);
            }
        }
        /*
        [Test]
        public void ListBoxInWindowWorkingTest()
        {
            // Launch a browser instance
            Manager.LaunchNewBrowser();

            // The active browser
            ActiveBrowser.NavigateTo("http://localhost:62922/index.html");
            SilverlightApp app = ActiveBrowser.SilverlightApps()[0];

            Button show = app.Find.ByName<Button>("showButton");
            show.User.Click();

            Button newcf = app.Find.ByName<Button>("populateButton");
            newcf.User.Click();
            RadWindow window = app.Find.ByName<RadWindow>("AWindow");

            try
            {
                ListBox lb = window.Find.ByName<ListBox>("Elements");
                Assert.AreEqual(3, lb.Items.Count);
                IList<RadButton> deleteButtons = lb.Find.AllByName<RadButton>("DeleteElementButton");
                Assert.AreEqual(3, deleteButtons.Count);
            }
            catch (TimeoutException e)
            {
                Log.WriteLine("Listbox : " + e);
            }

        }

        [Test]
        public void ListBoxInWindowFailingTest()
        {
            // Launch a browser instance
            Manager.LaunchNewBrowser();

            // The active browser
            ActiveBrowser.NavigateTo("http://localhost:62922/index.html");
            SilverlightApp app = ActiveBrowser.SilverlightApps()[0];

            Button show = app.Find.ByName<Button>("showButton");
            show.User.Click();

            RadWindow window = app.Find.ByName<RadWindow>("AWindow");
            Button newcf = app.Find.ByName<Button>("populateButton");
            newcf.User.Click();
            try
            {
                ListBox lb = window.Find.ByName<ListBox>("Elements");
                Assert.AreEqual(3, lb.Items.Count);
                IList<RadButton> deleteButtons = lb.Find.AllByName<RadButton>("DeleteElementButton");
                Assert.AreEqual(3, deleteButtons.Count);
            }
            catch (TimeoutException e)
            {
                Log.WriteLine("Listbox : " + e);
            }

        }
        */
    }
}
