private static TextBox GetProjectSummaryTextControl(Application application, Log log)
        {
            const string prefix = "Project page - Get project summary control";
            var          tab    = TabProxies.GetProjectPageTabItem(application, log);

            if (tab == null)
            {
                return(null);
            }

            var projectSummarySearchCriteria = SearchCriteria
                                               .ByAutomationId(ProjectViewAutomationIds.ProjectSummary);

            return(Retry.Times(
                       () =>
            {
                log.Debug(prefix, "Trying to get the project summary control.");

                var textBox = (TextBox)tab.Get(projectSummarySearchCriteria);
                if (textBox == null)
                {
                    log.Error(prefix, "Failed to get the project summary control.");
                }

                return textBox;
            }));
        }
        /// <summary>
        /// Opens the project page tab via the start page 'New project' button.
        /// </summary>
        /// <param name="application">The application.</param>
        /// <param name="log">The log object.</param>
        /// <exception cref="RegressionTestFailedException">
        ///     Thrown if the 'New project' button could not be invoked for some reason.
        /// </exception>
        public static void OpenProjectPageViaWelcomePageButton(Application application, Log log)
        {
            const string prefix    = "Welcome page - Open project";
            var          startPage = TabProxies.GetStartPageTabItem(application, log);

            if (startPage == null)
            {
                throw new RegressionTestFailedException(prefix + ": Failed to get the start page.");
            }

            try
            {
                if (!startPage.IsSelected)
                {
                    log.Debug(prefix, "Selecting start page.");
                    startPage.Select();
                }
            }
            catch (Exception e)
            {
                throw new RegressionTestFailedException(prefix + ": Failed to select the start page.", e);
            }

            var newProjectSearchCriteria = SearchCriteria
                                           .ByAutomationId(WelcomeViewAutomationIds.NewProject)
                                           .AndControlType(ControlType.Button);
            var newProjectButton = Retry.Times(
                () =>
            {
                log.Debug(prefix, "Trying to get 'New project' button.");
                var button = (Button)startPage.Get(newProjectSearchCriteria);
                if (button == null)
                {
                    log.Error(prefix, "Failed to get 'New project' button.");
                }

                return(button);
            });

            if (newProjectButton == null)
            {
                newProjectButton = (Button)ControlProxies.FindItemManuallyInUIContainer(
                    startPage as UIItemContainer,
                    WelcomeViewAutomationIds.NewProject,
                    log);
                if (newProjectButton == null)
                {
                    throw new RegressionTestFailedException(prefix + ": Failed to get the 'New project' button.");
                }
            }

            try
            {
                newProjectButton.Click();
            }
            catch (Exception e)
            {
                throw new RegressionTestFailedException(prefix + ": Failed to click the 'New project' button.", e);
            }
        }
        private static Label GetProjectDatasetCountControl(Application application, Log log)
        {
            const string prefix = "Project page - Get dataset count control";
            var          tab    = TabProxies.GetProjectPageTabItem(application, log);

            if (tab == null)
            {
                return(null);
            }

            var datasetCountSearchCriteria = SearchCriteria
                                             .ByAutomationId(ProjectViewAutomationIds.DatasetCount);

            return(Retry.Times(
                       () =>
            {
                log.Debug(prefix, "Trying to get the dataset count control.");

                var label = (Label)tab.Get(datasetCountSearchCriteria);
                if (label == null)
                {
                    log.Error(prefix, "Failed to get the dataset count control.");
                }

                return label;
            }));
        }
        /// <summary>
        /// Activates the dataset with the given ID.
        /// </summary>
        /// <param name="application">The application.</param>
        /// <param name="log">The log object.</param>
        /// <param name="id">The ID of the dataset that should be activated.</param>
        public static void ActivateDataset(Application application, Log log, int id)
        {
            const string prefix = "Project page - Activate dataset";

            if (IsDatasetActivated(application, log, id))
            {
                log.Info(
                    prefix,
                    "Dataset is already activated.");
                return;
            }

            var tab = TabProxies.GetProjectPageTabItem(application, log);

            if (tab == null)
            {
                throw new RegressionTestFailedException(prefix + " - Failed to get project tab.");
            }

            var datasets = GetDatasetControls(application, log);

            if (datasets.Count == 0)
            {
                throw new RegressionTestFailedException(prefix + " - Failed to get dataset controls.");
            }

            if (!datasets.ContainsKey(id))
            {
                throw new RegressionTestFailedException(prefix + " - Failed to find dataset with id: " + id.ToString(CultureInfo.InvariantCulture));
            }

            var buttonId = string.Format(
                CultureInfo.InvariantCulture,
                "Button_[{0}_[DatasetId: [{1}]]]",
                DatasetViewAutomationIds.DatasetActivateDeactivate,
                id);
            var buttonSearchCriteria = SearchCriteria
                                       .ByAutomationId(buttonId);
            var button = tab.Get <Button>(buttonSearchCriteria);

            if (button == null)
            {
                throw new RegressionTestFailedException(prefix + " - Failed to get activate dataset button.");
            }

            try
            {
                button.Click();
            }
            catch (Exception e)
            {
                throw new RegressionTestFailedException(prefix + " - Failed to activate the dataset.", e);
            }

            // handle dialog
            SelectMachineForDatasetActivation(application, log);

            // Wait for the dataset to be activated
            WaitForDatasetActivation(application, log, id);
        }
        private static SortedList <int, IUIItem> GetDatasetControls(Application application, Log log)
        {
            var tab = TabProxies.GetProjectPageTabItem(application, log);

            if (tab == null)
            {
                return(new SortedList <int, IUIItem>());
            }

            var partialId = string.Format(
                CultureInfo.InvariantCulture,
                "Vertex_[{0}_[DatasetId: [",
                DatasetViewAutomationIds.GraphVertex);
            var controls = ControlProxies.FindItemsManuallyInUIContainerWithPartialId((UIItemContainer)tab, partialId, log);

            var result = new SortedList <int, IUIItem>();

            foreach (var control in controls)
            {
                var idText = control.Id.Substring(partialId.Length).TrimEnd(']');

                int id;
                if (int.TryParse(idText, out id))
                {
                    result.Add(id, control);
                }
            }

            return(result);
        }
        private static TextBox GetDatasetSummaryTextControl(Application application, Log log, int id)
        {
            const string prefix = "Project page - Get dataset summary control";
            var          tab    = TabProxies.GetProjectPageTabItem(application, log);

            if (tab == null)
            {
                return(null);
            }

            var textBoxId = string.Format(
                CultureInfo.InvariantCulture,
                "TextBox_[{0}_[DatasetId: [{1}]]]",
                DatasetViewAutomationIds.DatasetSummary,
                id);
            var textBoxSearchCriteria = SearchCriteria
                                        .ByAutomationId(textBoxId);

            return(Retry.Times(
                       () =>
            {
                log.Debug(prefix, "Trying to get the dataset summary control.");

                var textBox = (TextBox)tab.Get(textBoxSearchCriteria);
                if (textBox == null)
                {
                    log.Error(prefix, "Failed to get the dataset summary control.");
                }

                return textBox;
            }));
        }
        private static void WaitForDatasetDeactivation(Application application, Log log, int id)
        {
            const string prefix = "Project page - Wait for dataset deactivation";
            var          tab    = TabProxies.GetProjectPageTabItem(application, log);

            if (tab == null)
            {
                throw new RegressionTestFailedException(prefix + " - Failed to get project tab.");
            }

            var textBlockId = string.Format(
                CultureInfo.InvariantCulture,
                "TextBlock_[{0}_[DatasetId: [{1}]]]",
                DatasetViewAutomationIds.DatasetRunningOn,
                id);
            var textBlockSearchCriteria = SearchCriteria
                                          .ByAutomationId(textBlockId);
            var label = tab.Get <Label>(textBlockSearchCriteria);

            if (label == null)
            {
                throw new RegressionTestFailedException(prefix + " - Failed to get the dataset status label.");
            }

            var endTime = DateTimeOffset.Now + s_DatasetActivationTime;

            while (DateTimeOffset.Now < endTime)
            {
                try
                {
                    var text = label.Text;
                    if (text.Contains("is not activated"))
                    {
                        return;
                    }

                    Thread.Sleep(500);
                }
                catch (Exception e)
                {
                    log.Error(
                        prefix,
                        string.Format(
                            CultureInfo.InvariantCulture,
                            "Failed to read the dataset status for dataset: {0}. Error was: {1}",
                            id,
                            e));
                }
            }
        }
        /// <summary>
        /// Deletes the dataset with the given ID.
        /// </summary>
        /// <param name="application">The application.</param>
        /// <param name="log">The log object.</param>
        /// <param name="id">The ID of the dataset that should be deleted.</param>
        public static void DeleteDataset(Application application, Log log, int id)
        {
            const string prefix = "Project page - Delete dataset";
            var          tab    = TabProxies.GetProjectPageTabItem(application, log);

            if (tab == null)
            {
                throw new RegressionTestFailedException(prefix + " - Failed to get project tab.");
            }

            var datasets = GetDatasetControls(application, log);

            if (datasets.Count == 0)
            {
                throw new RegressionTestFailedException(prefix + " - Failed to get dataset controls.");
            }

            if (!datasets.ContainsKey(id))
            {
                throw new RegressionTestFailedException(prefix + " - Failed to find dataset with id: " + id.ToString(CultureInfo.InvariantCulture));
            }

            var buttonId = string.Format(
                CultureInfo.InvariantCulture,
                "Button_[{0}_[DatasetId: [{1}]]]",
                DatasetViewAutomationIds.DatasetDelete,
                id);
            var buttonSearchCriteria = SearchCriteria
                                       .ByAutomationId(buttonId);
            var button = tab.Get <Button>(buttonSearchCriteria);

            if (button == null)
            {
                throw new RegressionTestFailedException(prefix + " - Failed to get delete dataset button.");
            }

            try
            {
                var ids = GetDatasetIds(application, log);

                button.Click();

                WaitForDatasetCreationOrDeletion(application, log, ids.Count() - 1);
            }
            catch (Exception e)
            {
                throw new RegressionTestFailedException(prefix + " - Failed to delete the dataset.", e);
            }
        }
        /// <summary>
        /// Creates a new child dataset for the root dataset.
        /// </summary>
        /// <param name="application">The application.</param>
        /// <param name="log">The log object.</param>
        public static void CreateChildDatasetForRoot(Application application, Log log)
        {
            const string prefix = "Project page - Create child dataset for root";
            var          tab    = TabProxies.GetProjectPageTabItem(application, log);

            if (tab == null)
            {
                throw new RegressionTestFailedException(prefix + " - Failed to get project tab.");
            }

            var datasets = GetDatasetControls(application, log);

            if (datasets.Count == 0)
            {
                throw new RegressionTestFailedException(prefix + " - Failed to get dataset controls.");
            }

            // The root is always the lowest number, i.e. the first entry
            var buttonId = string.Format(
                CultureInfo.InvariantCulture,
                "Button_[{0}_[DatasetId: [{1}]]]",
                DatasetViewAutomationIds.DatasetCreateChild,
                datasets.Keys[0]);
            var buttonSearchCriteria = SearchCriteria
                                       .ByAutomationId(buttonId);
            var button = tab.Get <Button>(buttonSearchCriteria);

            if (button == null)
            {
                throw new RegressionTestFailedException(prefix + " - Failed to get create dataset button.");
            }

            try
            {
                var ids = GetDatasetIds(application, log);

                button.Click();

                WaitForDatasetCreationOrDeletion(application, log, ids.Count() + 1);
            }
            catch (Exception e)
            {
                throw new RegressionTestFailedException(prefix + " - Failed to create new dataset.", e);
            }
        }
        /// <summary>
        /// Checks the 'Keep welcome page open' checkbox.
        /// </summary>
        /// <param name="application">The application.</param>
        /// <param name="log">The log object.</param>
        /// <exception cref="RegressionTestFailedException">
        ///     Thrown if the 'Keep welcome page open' checkbox could not be checked for some reason.
        /// </exception>
        public static void CheckCloseWelcomePageOnProjectOpen(Application application, Log log)
        {
            const string prefix    = "Welcome page - Check close welcome page on project open";
            var          startPage = TabProxies.GetStartPageTabItem(application, log);

            if (startPage == null)
            {
                MenuProxies.SwitchToStartPageViaViewStartPageMenuItem(application, log);
            }

            startPage = TabProxies.GetStartPageTabItem(application, log);
            if (startPage == null)
            {
                throw new RegressionTestFailedException(prefix + ": Failed to get start page.");
            }

            try
            {
                if (!startPage.IsSelected)
                {
                    log.Debug(prefix, "Selecting start page.");
                    startPage.Select();
                }
            }
            catch (Exception e)
            {
                throw new RegressionTestFailedException(
                          prefix + ": Failed to select the start page",
                          e);
            }

            // Check 'keep open' flag
            var closePageSearchCriteria = SearchCriteria
                                          .ByAutomationId(WelcomeViewAutomationIds.ClosePageAfterLoad)
                                          .AndControlType(ControlType.CheckBox);
            var closePageCheckBox = Retry.Times(
                () =>
            {
                log.Debug(prefix, "Trying to get checkbox.");
                var checkBox = (CheckBox)startPage.Get(closePageSearchCriteria);
                if (checkBox == null)
                {
                    log.Error(prefix, "Failed to get checkbox.");
                }

                return(checkBox);
            });

            if (closePageCheckBox == null)
            {
                closePageCheckBox = (CheckBox)ControlProxies.FindItemManuallyInUIContainer(
                    startPage as UIItemContainer,
                    WelcomeViewAutomationIds.ClosePageAfterLoad,
                    log);
                if (closePageCheckBox == null)
                {
                    throw new RegressionTestFailedException(prefix + ": Failed to get checkbox.");
                }
            }

            try
            {
                if (!closePageCheckBox.Checked)
                {
                    log.Debug(prefix, "Checking 'Close welcome page on project open' checkbox.");
                    closePageCheckBox.Checked = true;
                }
            }
            catch (Exception e)
            {
                throw new RegressionTestFailedException(
                          prefix + ": Failed to check the 'Keep start page open' checkbox.",
                          e);
            }
        }
        /// <summary>
        /// Unchecks the 'Show welcome page on application start' checkbox.
        /// </summary>
        /// <param name="application">The application.</param>
        /// <param name="log">The log object.</param>
        /// <exception cref="RegressionTestFailedException">
        ///     Thrown if the 'Show welcome page on application start' checkbox could not be unchecked for some reason.
        /// </exception>
        public static void UncheckShowWelcomePageOnApplicationStart(Application application, Log log)
        {
            const string prefix    = "Welcome page - Uncheck show page on start";
            var          startPage = TabProxies.GetStartPageTabItem(application, log);

            if (startPage == null)
            {
                MenuProxies.SwitchToStartPageViaViewStartPageMenuItem(application, log);
            }

            startPage = TabProxies.GetStartPageTabItem(application, log);
            if (startPage == null)
            {
                throw new RegressionTestFailedException(prefix + ": Failed to get start page.");
            }

            try
            {
                if (!startPage.IsSelected)
                {
                    log.Debug(prefix, "Selecting start page.");
                    startPage.Select();
                }
            }
            catch (Exception e)
            {
                throw new RegressionTestFailedException(prefix + " - Failed to select the start page", e);
            }

            // Check 'Show welcome page on application start' flag
            var showStartPageSearchCriteria = SearchCriteria
                                              .ByAutomationId(WelcomeViewAutomationIds.ShowPageOnStartup)
                                              .AndControlType(ControlType.CheckBox);
            var showStartPageCheckBox = Retry.Times(
                () =>
            {
                log.Debug(prefix, "Trying to get checkbox.");
                var checkBox = (CheckBox)startPage.Get(showStartPageSearchCriteria);
                if (checkBox == null)
                {
                    log.Error(prefix, "Failed to get checkbox.");
                }

                return(checkBox);
            });

            if (showStartPageCheckBox == null)
            {
                showStartPageCheckBox = (CheckBox)ControlProxies.FindItemManuallyInUIContainer(
                    startPage as UIItemContainer,
                    WelcomeViewAutomationIds.ShowPageOnStartup,
                    log);
                if (showStartPageCheckBox == null)
                {
                    throw new RegressionTestFailedException(prefix + ": Failed to get checkbox.");
                }
            }

            try
            {
                if (showStartPageCheckBox.Checked)
                {
                    log.Debug(prefix, "Unchecking 'Show welcome page on application start' checkbox.");
                    showStartPageCheckBox.Checked = false;
                }
            }
            catch (Exception e)
            {
                throw new RegressionTestFailedException(
                          prefix + " - Failed to uncheck the 'Show welcome page on application start' checkbox.",
                          e);
            }
        }