Esempio n. 1
0
        protected override string DoImpl()
        {
            if (!this.ExecutedCommand.Parameters.ContainsKey("x") &&
                this.ExecutedCommand.Parameters.ContainsKey("y"))
            {
                // TODO: in the future '400 : invalid argument' will be used
                return(this.JsonResponse(ResponseStatus.UnknownError, "WRONG PARAMETERS"));
            }

            var x = this.ExecutedCommand.GetParameterAsInt("x");
            var y = this.ExecutedCommand.GetParameterAsInt("y");

            bool success;

            if (this.ExecutedCommand.Parameters.ContainsKey("element"))
            {
                var registeredKey = this.ExecutedCommand.Parameters["element"].ToString();
                var element       = this.Automator.ElementsRegistry.GetRegisteredElement(registeredKey);

                success = TouchSimulator.TouchUp(element, x, y);
            }
            else
            {
                success = TouchSimulator.TouchUp(x, y);
            }

            return(success
                ? this.JsonResponse()
                : this.JsonResponse(ResponseStatus.UnknownError, "Touch input failed ('x' and 'y' must match preceeding 'down' or 'move' request)"));
        }
        private static bool Perform(List <TouchAction> actions)
        {
            var    previousX      = 0;
            var    previousY      = 0;
            var    havePrevious   = false;
            string previousAction = null;

            for (var i = 0; i < actions.Count; i++)
            {
                var action = actions[i];

                Point point;

                switch (action.Action)
                {
                case TouchAction.LongPress:
                    point = action.GetLocation();
                    TouchSimulator.LongTap((int)point.X, (int)point.Y, action.MiliSeconds);
                    havePrevious = false;
                    break;

                case TouchAction.MoveTo:
                    int?duration = null;
                    if (i > 0 && actions[i - 1].Action == TouchAction.Wait)
                    {
                        duration = actions[i - 1].MiliSeconds;
                    }
                    point = action.GetLocation();
                    var x = (int)point.X;
                    var y = (int)point.Y;
                    TouchSimulator.MoveTo(previousX, previousY, previousX + x, previousY + y, duration);
                    previousX   += x;
                    previousY   += y;
                    havePrevious = true;
                    break;

                case TouchAction.Press:
                    point = action.GetLocation();
                    TouchSimulator.TouchDown((int)point.X, (int)point.Y);
                    previousX    = (int)point.X;
                    previousY    = (int)point.Y;
                    havePrevious = true;
                    break;

                case TouchAction.Release:
                    if (previousAction == TouchAction.Tap || previousAction == TouchAction.LongPress)
                    {
                        break;
                    }
                    TouchSimulator.TouchUp(previousX, previousY);
                    havePrevious = false;
                    break;

                case TouchAction.Tap:
                    point = action.GetLocation();
                    for (var n = 1; n <= action.Count; n++)
                    {
                        TouchSimulator.Tap((int)point.X, (int)point.Y);
                        Thread.Sleep(250);
                    }
                    havePrevious = false;
                    break;

                case TouchAction.Wait:
                    if (actions.Count > i + 1 && actions[i + 1].Action == TouchAction.MoveTo)
                    {
                        break;
                    }
                    if (havePrevious)
                    {
                        var startTime = DateTime.Now;
                        while (DateTime.Now < startTime + TimeSpan.FromMilliseconds(action.MiliSeconds))
                        {
                            TouchSimulator.TouchUpdate(previousX, previousY);
                            Thread.Sleep(16);
                        }
                    }
                    else
                    {
                        Thread.Sleep(action.MiliSeconds);
                    }
                    break;

                default:
                    throw new AutomationException($"unrecognised action {action.Action}");
                }

                previousAction = action.Action;
            }

            return(true);
        }
Esempio n. 3
0
        protected override string DoImpl()
        {
            var haveElement = this.ExecutedCommand.Parameters.ContainsKey("element");
            var haveOffset  = this.ExecutedCommand.Parameters.ContainsKey("xoffset") &&
                              this.ExecutedCommand.Parameters.ContainsKey("yoffset");

            if (!haveOffset)
            {
                // TODO: in the future '400 : invalid argument' will be used
                return(this.JsonResponse(ResponseStatus.UnknownError, "WRONG PARAMETERS"));
            }

            var element = CruciatusFactory.Root;

            if (haveElement)
            {
                var registeredKey = this.ExecutedCommand.Parameters["element"].ToString();
                element = this.Automator.ElementsRegistry.GetRegisteredElement(registeredKey);
            }

            var rect = element.Properties.BoundingRectangle;

            var startPoint = new Point(
                rect.Left + (rect.Width / 2),
                rect.Top + (rect.Height / 2));

            var xOffset = this.ExecutedCommand.GetParameterAsInt("xoffset");
            var yOffset = this.ExecutedCommand.GetParameterAsInt("yoffset");

            var endPoint = new Point(
                startPoint.X + xOffset,
                startPoint.Y + yOffset);

            if (!TouchSimulator.TouchDown((int)startPoint.X, (int)startPoint.Y))
            {
                return(this.JsonResponse(ResponseStatus.UnknownError, "Touch input failed"));
            }

            var distance = Math.Sqrt(Math.Pow(startPoint.X - endPoint.X, 2) + Math.Pow(startPoint.Y - endPoint.Y, 2));

            for (var soFar = 6; soFar < distance; soFar += 6)
            {
                var soFarFraction = soFar / distance;

                var x = (int)(startPoint.X + (xOffset * soFarFraction));
                var y = (int)(startPoint.Y + (yOffset * soFarFraction));
                if (!TouchSimulator.TouchUpdate(x, y))
                {
                    return(this.JsonResponse(ResponseStatus.UnknownError, "Touch input failed"));
                }

                Thread.Sleep(8);
            }

            var startTime = DateTime.Now;

            while (DateTime.Now < (startTime + TimeSpan.FromMilliseconds(500)))
            {
                if (!TouchSimulator.TouchUpdate((int)endPoint.X, (int)endPoint.Y))
                {
                    return(this.JsonResponse(ResponseStatus.UnknownError, "Touch input failed"));
                }
                Thread.Sleep(16);
            }

            return(TouchSimulator.TouchUp((int)endPoint.X, (int)endPoint.Y)
                ? this.JsonResponse()
                : this.JsonResponse(ResponseStatus.UnknownError, "Touch input failed"));
        }