Ejemplo n.º 1
0
        public void TestImageMatch()
        {
            // Create the provider
            ClientSideProviderDescription provider = new ClientSideProviderDescription(
                new ClientSideProviderFactoryCallback(
                    SampleButtonProvider.ButtonFactory),
                "Shell_TrayWnd",
                "EXPLORER.EXE",
                ClientSideProviderMatchIndicator.None);

            ClientSideProviderDescription[] providers = new ClientSideProviderDescription[1] {
                provider
            };

            try
            {
                // Register it
                ClientSettings.RegisterClientSideProviders(providers);

                // Get the overridden element
                AutomationElement startButton = AutomationElement.FromHandle(this.startButtonHwnd);

                // Validate that it is ours
                Assert.AreEqual(SampleButtonProvider.ButtonName, startButton.Current.Name);
            }
            finally
            {
                // Restore the status quo ante
                ClientSettings.RegisterClientSideProviders(new ClientSideProviderDescription[0]);
            }
        }
Ejemplo n.º 2
0
        public void TestPartialMatchNotPermitted()
        {
            // Create the provider
            ClientSideProviderDescription provider = new ClientSideProviderDescription(
                new ClientSideProviderFactoryCallback(
                    SampleButtonProvider.ButtonFactory),
                "Shell_",
                null /* image name */,
                ClientSideProviderMatchIndicator.None);

            ClientSideProviderDescription[] providers = new ClientSideProviderDescription[1] {
                provider
            };

            try
            {
                // Register it
                ClientSettings.RegisterClientSideProviders(providers);

                // Get the overridden element
                AutomationElement startButton = AutomationElement.FromHandle(this.startButtonHwnd);

                // Validate that it is not ours
                Assert.AreNotEqual(SampleButtonProvider.ButtonName, startButton.Current.Name);
            }
            finally
            {
                // Restore the status quo ante
                ClientSettings.RegisterClientSideProviders(new ClientSideProviderDescription[0]);
            }
        }
Ejemplo n.º 3
0
        public void MyTestInitialize()
        {
            // Find the taskbar, which will be our target
            AutomationElement taskBar = AutomationElementTest.GetTaskbar();

            this.targetHwnd = (IntPtr)taskBar.Current.NativeWindowHandle;

            // Register a client side provider
            ClientSideProviderDescription provider = new ClientSideProviderDescription(
                new ClientSideProviderFactoryCallback(MockPatternProvider.MockPatternFactory), "Shell_TrayWnd");

            ClientSideProviderDescription[] providers = new ClientSideProviderDescription[1] {
                provider
            };
            ClientSettings.RegisterClientSideProviders(providers);

            // Get the overridden element
            this.mockObject = AutomationElement.FromHandle(this.targetHwnd);
            Assert.IsNotNull(this.mockObject);
        }
Ejemplo n.º 4
0
        public void TestButtonClientSideProvider()
        {
            // Create the provider
            ClientSideProviderDescription provider = new ClientSideProviderDescription(
                new ClientSideProviderFactoryCallback(SampleButtonProvider.ButtonFactory), "BUTTON");

            ClientSideProviderDescription[] providers = new ClientSideProviderDescription[1] {
                provider
            };

            try
            {
                // Register it
                ClientSettings.RegisterClientSideProviders(providers);

                // Get the overridden element
                AutomationElement startButton = AutomationElement.FromHandle(this.startButtonHwnd);

                // Validate that it is ours
                Assert.AreEqual(SampleButtonProvider.ButtonName, startButton.Current.Name);

                // Unregister it
                ClientSettings.RegisterClientSideProviders(new ClientSideProviderDescription[0]);

                // Get the overridden element
                startButton = AutomationElement.FromHandle(this.startButtonHwnd);

                // Validate that it is not ours
                Assert.AreNotEqual(SampleButtonProvider.ButtonName, startButton.Current.Name);
            }
            finally
            {
                // Restore the status quo ante
                ClientSettings.RegisterClientSideProviders(new ClientSideProviderDescription[0]);
            }
        }
Ejemplo n.º 5
0
        // Given an entry from one of the hash-tables or lists, check if it matches the image/classname, and if so, call the
        // factory method to create the proxy.
        // (Because full classname matching is done via hash-table lookup, this only needs to do string comparisons
        // for partial classname matches.)
        static private IRawElementProviderSimple GetProxyFromEntry(ProxyScoping findType, object entry, ref string imageName, NativeMethods.HWND hwnd, int idChild, int idObject, string classNameForPartialMatch)
        {
            // First, determine if the entry matches, and if so, extract the factory callback...
            ClientSideProviderFactoryCallback factoryCallback = null;

            // The entry may be a ClientSideProviderFactoryCallback or ClientSideProviderDescription...
            if (findType == ProxyScoping.ImageOnlyHandlers || findType == ProxyScoping.FallbackHandlers)
            {
                // Handle the fallback and image cases specially. The array for these is an array
                // of ClientSideProviderFactoryCallbacks, not ClientSideProviderDescription.
                factoryCallback = (ClientSideProviderFactoryCallback)entry;
            }
            else
            {
                // Other cases use ClientSideProviderDescription...
                ClientSideProviderDescription pi = (ClientSideProviderDescription)entry;

                // Get the image name if necessary...
#pragma warning suppress 6507 // Null and Empty string mean different things here.
                if (imageName == null && pi.ImageName != null)
                {
                    imageName = GetImageName(hwnd);
                }

                if (pi.ImageName == null || pi.ImageName == imageName)
                {
                    // Check if we have a match for this entry...
                    switch (findType)
                    {
                    case ProxyScoping.ExactMatchApparentClassName:
                        factoryCallback = pi.ClientSideProviderFactoryCallback;
                        break;

                    case ProxyScoping.ExactMatchRealClassName:
                        if ((pi.Flags & ClientSideProviderMatchIndicator.DisallowBaseClassNameMatch) == 0)
                        {
                            factoryCallback = pi.ClientSideProviderFactoryCallback;
                        }
                        break;

                    case ProxyScoping.PartialMatchApparentClassName:
                        if (classNameForPartialMatch.IndexOf(pi.ClassName, StringComparison.Ordinal) >= 0)
                        {
                            factoryCallback = pi.ClientSideProviderFactoryCallback;
                        }
                        break;

                    case ProxyScoping.PartialMatchRealClassName:
                        if (classNameForPartialMatch.IndexOf(pi.ClassName, StringComparison.Ordinal) >= 0 &&
                            ((pi.Flags & ClientSideProviderMatchIndicator.DisallowBaseClassNameMatch) == 0))
                        {
                            factoryCallback = pi.ClientSideProviderFactoryCallback;
                        }
                        break;

                    default:
                        Debug.Assert(false, "unexpected switch() case:");
                        break;
                    }
                }
            }

            // Second part: did we get a match? If so, use the factory callback to obtain an instance...
            if (factoryCallback == null)
            {
                return(null);
            }

            // if we get an exception creating a proxy just don't create the proxy and let the UIAutomation default proxy be used
            // This will still allow the tree to be navigated  and some properties to be made availible.
            // Catching all exceptions here doesn't follow .NET guidelines, but is it ok in this scenario?
            try
            {
                return(factoryCallback(hwnd, idChild, idObject));
            }
            catch (Exception e)
            {
                if (Misc.IsCriticalException(e))
                {
                    throw;
                }

                return(null);
            }
        }
Ejemplo n.º 6
0
        // return an array representing the currently registered proxies
        internal static ClientSideProviderDescription[] GetProxyDescriptionTable()
        {
            // the ClientSideProviderDescription table is split into four different collections.  Bundle them all back
            // together to let them be manipulated

            // If a client gets the table before the defaults proxies  are loaded because of use, it should return the default proxies
            LoadDefaultProxies();

            lock (_lockObj)
            {
                int            count = 0;
                IEnumerable [] sourceProxyDescription = { _classHandlers, _partialClassHandlers, _imageOnlyHandlers, _fallbackHandlers };

                // figure out how many there are
                foreach (IEnumerable e in sourceProxyDescription)
                {
                    foreach (Object item in e)
                    {
                        Object o = item;
                        if (o is DictionaryEntry)
                        {
                            o = ((DictionaryEntry)o).Value;
                        }

                        if (o is ClientSideProviderDescription)
                        {
                            count++;
                        }
                        else if (o is ClientSideProviderFactoryCallback)
                        {
                            count++;
                        }
                        else
                        {
                            count += ((ArrayList)o).Count;
                        }
                    }
                }

                ClientSideProviderDescription[] proxyDescriptions = new ClientSideProviderDescription[count];
                count = 0;

                // Because the four collections have a simular stucture in common we can treat like they are the same
                // and build the array in the correct order from each one.
                foreach (IEnumerable e in sourceProxyDescription)
                {
                    foreach (Object item in e)
                    {
                        Object o = item;
                        if (o is DictionaryEntry)
                        {
                            o = ((DictionaryEntry)o).Value;
                        }

                        if (o is ClientSideProviderDescription)
                        {
                            proxyDescriptions[count++] = (ClientSideProviderDescription)o;
                        }
                        else if (o is ClientSideProviderFactoryCallback)
                        {
                            ClientSideProviderFactoryCallback pfc = (ClientSideProviderFactoryCallback)o;
                            proxyDescriptions[count++] = new ClientSideProviderDescription(pfc, null);
                        }
                        else
                        {
                            foreach (Object o1 in (ArrayList)o)
                            {
                                proxyDescriptions[count++] = (ClientSideProviderDescription)o1;
                            }
                        }
                    }
                }

                return(proxyDescriptions);
            }
        }