csharp/kfrajtak/WinAppDriver/Tests/Input/TouchScenarios.cs

TouchScenarios.cs
//******************************************************************************
//
// Copyright (c) 2017 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//******************************************************************************

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Input
{
    [TestClast]
    public clast SingleTouchScenarios : TestBase
    {
        #region Test lifecycle code
        [ClastInitialize]
        public static void Setup(TestContext context)
        {
            BaseSetup(context);
        }

        [ClastCleanup]
        public static void TearDown()
        {
            BaseTearDown();
        }

        [TestInitialize]
        public void Clear()
        {
            var clear = AppSession.FindElementByAccessibilityId("Clear");
            TouchScreen.SingleTap(clear.Coordinates);
            astert.AreEqual(string.Empty, _GetResultText());
        }
        #endregion

        /// 
        /// One finger touches the screen and lifts up.
        /// Static gesture.
        /// 
        [TestMethod, TestCategory("SingleTouch")]
        public void Tap()
        {
            var touchable = AppSession.FindElementByAccessibilityId("Touchable");
            TouchScreen.SingleTap(touchable.Coordinates);
            astert.AreEqual("Tapped", _GetLastResultString());
        }

        /// 
        /// One finger touches the screen and lifts up twice, in quick succession.
        /// Static gesture.
        /// 
        [TestMethod, TestCategory("SingleTouch")]
        public void DoubleTap()
        {
            var touchable = AppSession.FindElementByAccessibilityId("Touchable");
            TouchScreen.DoubleTap(touchable.Coordinates);
            astert.AreEqual("DoubleTapped", _GetLastResultString());
        }

        /// 
        /// One finger touches the screen and stays in place.
        /// Static gesture.
        /// 
        [TestMethod, TestCategory("SingleTouch")]
        public void PressAndHold()
        {
            var touchable = AppSession.FindElementByAccessibilityId("Touchable");
            TouchScreen.LongPress(touchable.Coordinates);
            astert.AreEqual("Holding", _GetFirstResultString());
        }

        /// 
        /// One finger touches the screen and stays in place for a moment, then releases.
        /// Static gesture.
        /// 
        [TestMethod, TestCategory("SingleTouch")]
        public void RightTapped()
        {
            var touchable = AppSession.FindElementByAccessibilityId("Touchable");
            TouchScreen.LongPress(touchable.Coordinates);
            astert.AreEqual("RightTapped", _GetLastResultString());
        }
        
        /// 
        /// One finger touches the screen and moves in a direction.
        /// Manipulation gesture.
        /// 
        [TestMethod, TestCategory("SingleTouch")]
        public void Slide()
        {
            var touchable = AppSession.FindElementByAccessibilityId("Touchable");
            var startCoords = touchable.Coordinates.LocationInViewport;
            var endX = startCoords.X + TouchDistance.Long;
            var endY = startCoords.Y + TouchDistance.Long;

            TouchScreen.Down(startCoords.X, startCoords.Y);
            TouchScreen.Up(endX, endY);

            var endCoords = touchable.Coordinates.LocationInViewport;
            astert.IsTrue(endCoords.X > startCoords.X);
            astert.IsTrue(endCoords.Y > startCoords.Y);
        }

        /// 
        /// One finger touches the screen and moves a short distance in a direction.
        /// Manipulation gesture.
        /// 
        [TestMethod, TestCategory("SingleTouch")]
        public void Swipe()
        {
            var touchable = AppSession.FindElementByAccessibilityId("Touchable");
            var startCoords = touchable.Coordinates.LocationInViewport;

            // Not supported
            //var startX = startCoords.X + touchable.Size.Width / 2;
            //var startY = startCoords.Y + touchable.Size.Width / 2;
            //var endX = startX + TouchDistance.Short;
            //var endY = startY + TouchDistance.Short;
            //AppSession.Swipe(startX, startY, endX, endY, TouchDuration.Short);

            TouchScreen.Flick(touchable.Coordinates, TouchDistance.Short, TouchDistance.Short, TouchSpeed.Slow);
            
            var endCoords = touchable.Coordinates.LocationInViewport;
            astert.IsTrue(endCoords.X > startCoords.X);
            astert.IsTrue(endCoords.Y > startCoords.Y);
            astert.IsTrue(endCoords.X - startCoords.X