private static Action ExpectMethodCall(IUIAutomationPatternInstance patternInstance, UiaMethodInfoHelper methodHelper, object[] inArgs, object[] outArgs)
        {
            if (inArgs.Length != outArgs.Length)
            {
                throw new ArgumentException();
            }


            Action <IUIAutomationPatternInstance> substituteCall
                = instance => instance.CallMethod(methodHelper.Index,
                                                  Arg.Any <UIAutomationParameter[]>(),
                                                  (uint)inArgs.Length);

            patternInstance.When(substituteCall)
            .Do(ci =>
            {
                // imitate what the native UIA part does after server side finishes the call
                var marshalled = (UIAutomationParameter[])ci.Args()[1];
                Assert.IsTrue(DoParamsMatch(marshalled, inArgs));
                var paramList = new UiaParameterListHelper(marshalled);
                for (int i = 0; i < marshalled.Length; i++)
                {
                    if (UiaTypesHelper.IsOutType(marshalled[i].type))
                    {
                        paramList[i] = outArgs[i];
                    }
                }
            });

            return(() =>
            {
                substituteCall(patternInstance.Received());
                patternInstance.ClearReceivedCalls();
            });
        }
        private static bool DoParamsMatch(UIAutomationParameter[] pParams, object[] inArgs)
        {
            if (pParams.Length != inArgs.Length)
            {
                return(false);
            }
            var paramList = new UiaParameterListHelper(pParams);

            for (int i = 0; i < paramList.Count; i++)
            {
                if (UiaTypesHelper.IsInType(pParams[i].type))
                {
                    if (!inArgs[i].Equals(paramList[i]))
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }