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;
            }));
        }
        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);
            }
        }