public void SetLiveModeDataContext_NoopsIfElementAlreadyHasComparableDataContext()
        {
            mockDataContext.Mode = DataContextMode.Live;

            using (new CaptureActionTestHookOverrides(() => mockDataManager, null, null))
            {
                CaptureAction.SetLiveModeDataContext(mockElementContext.Id, mockTreeViewMode, force: false);
            }

            Assert.AreSame(mockDataContext, mockElementContext.DataContext);
        }
        public void SetLiveModeDataContext_UsesTreeWalkerForLiveToCreateNewDataContext(
            bool force,
            bool startWithNullDataContext,
            DataContextMode originalDataContextMode,
            TreeViewMode originalDataContextTreeViewMode)
        {
            // Arrange
            if (startWithNullDataContext)
            {
                mockElementContext.DataContext = null;
            }
            mockDataContext.Mode     = originalDataContextMode;
            mockDataContext.TreeMode = originalDataContextTreeViewMode;
            TreeViewMode treeViewMode = TreeViewMode.Raw;

            var mockTreeWalkerForLive = new Mock <ITreeWalkerForLive>();
            var mockTopMostElement    = new A11yElement();
            var mockElementsItem1     = new A11yElement();

            mockElementsItem1.UniqueId = 101;
            var mockElementsItem2 = new A11yElement();

            mockElementsItem2.UniqueId = 102;
            mockTreeWalkerForLive.Setup(w => w.Elements).Returns(new List <A11yElement> {
                mockElementsItem1, mockElementsItem2
            });
            mockTreeWalkerForLive.Setup(w => w.RootElement).Returns(mockTopMostElement);

            using (new CaptureActionTestHookOverrides(() => mockDataManager, () => mockTreeWalkerForLive.Object, null))
            {
                // Act
                CaptureAction.SetLiveModeDataContext(mockElementContext.Id, treeViewMode, force);

                // Assert
                Assert.IsNotNull(mockElementContext.DataContext);
                Assert.AreNotSame(mockDataContext, mockElementContext.DataContext);
                var result = mockElementContext.DataContext;
                Assert.AreSame(mockElement, result.Element);
                Assert.AreEqual(20000, result.ElementCounter.UpperBound);
                Assert.AreEqual(treeViewMode, result.TreeMode);
                Assert.AreEqual(DataContextMode.Live, result.Mode);
                Assert.AreEqual(mockTopMostElement, result.RootElment);
                mockTreeWalkerForLive.Verify(w => w.GetTreeHierarchy(mockElement, treeViewMode));
                Assert.AreEqual(2, result.Elements.Count);
                Assert.AreSame(mockElementsItem1, result.Elements[mockElementsItem1.UniqueId]);
                Assert.AreSame(mockElementsItem2, result.Elements[mockElementsItem2.UniqueId]);
            }
        }
Example #3
0
        /// <summary>
        /// Update selected element
        /// </summary>
        /// <param name="ecId">Element Context Id</param>
        public async Task SetElement(Guid ecId)
        {
            this.ElementContext                      = null;
            this.SelectedInHierarchyElement          = null;
            EnableSelectorWhenPOISelectedInHierarchy = false;

            var ec = GetDataAction.GetElementContext(ecId);

            if (ec != null)
            {
                var prevFocus = Keyboard.FocusedElement;

                try
                {
                    this.ctrlProgressRing.Activate();

                    this.ctrlHierarchy.IsEnabled = false;
                    ctrlHierarchy.Visibility     = Visibility.Visible;
                    svInstructions.Visibility    = Visibility.Collapsed;
                    await Task.Run(() =>
                    {
                        CaptureAction.SetLiveModeDataContext(ecId, Configuration.TreeViewMode);
                    }).ConfigureAwait(false);

                    Application.Current.Dispatcher.Invoke(() => {
                        this.ctrlHierarchy.DataContext = ec.DataContext;
                        this.ElementContext            = ec;

                        // make sure that  when selected node is changed in hiearchy tree, enable selector.
                        EnableSelectorWhenPOISelectedInHierarchy = true;

                        this.ctrlHierarchy.SetElement(ec);

                        AutomationProperties.SetName(this, string.Format(CultureInfo.InvariantCulture, "Live inspect with {0}", ec.Element.Glimpse));
                    });
                }
#pragma warning disable CA1031 // Do not catch general exception types
                catch (Exception e)
                {
                    e.ReportException();
                    // if there was any exception, make sure that we enable selector later.
                    EnableSelectorWhenPOISelectedInHierarchy = false;
                }
#pragma warning restore CA1031 // Do not catch general exception types
                finally
                {
                    this.ctrlProgressRing.Deactivate();
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        this.ctrlHierarchy.IsEnabled = true;

                        // if the previously focused element is no longer visible and focus has gone to the window,
                        // we will set focus back to the hierarchy control. We do this because disabling the hierarchy control
                        // will throw keyboard focus to mainwindow, where it is not very useful.
                        if (prevFocus is FrameworkElement fe && !fe.IsVisible && Keyboard.FocusedElement is Window)
                        {
                            this.ctrlHierarchy.Focus();
                        }
                    });
                }
            }
            else
            {
                Clear();
            }

            if (EnableSelectorWhenPOISelectedInHierarchy == false)
            {
                Application.Current.Dispatcher.Invoke(() => UpdateStateMachineForLiveMode());
            }
        }