Commit b44f2041 authored by Jason Williams's avatar Jason Williams
Browse files

Add Xbox Screenshot endpoint

Adds associated test and updates XboxWDPDriver documentation.
parent 8a362114
Loading
Loading
Loading
Loading
+69 −0
Original line number Diff line number Diff line
//----------------------------------------------------------------------------------------------
// <copyright file="ScreenshotOperation.cs" company="Microsoft Corporation">
//     Licensed under the MIT License. See LICENSE.TXT in the project root license information.
// </copyright>
//----------------------------------------------------------------------------------------------

using System;
using System.Threading.Tasks;
using Microsoft.Tools.WindowsDevicePortal;
using static Microsoft.Tools.WindowsDevicePortal.DevicePortal;
using System.IO;

namespace XboxWdpDriver
{
    /// <summary>
    /// Helper for Screenshot related operations
    /// </summary>
    public class ScreenshotOperation
    {
        /// <summary>
        /// Usage message for this operation
        /// </summary>
        private const string XblScreenshotUsageMessage = "Usage:\n" +
            "  [/filepath:<filepath> [/override]]\n" +
            "        Saves a screenshot from the console to the destination file specified\n" +
            "        by /filepath. This filename should end in .png so the file can be\n" +
            "        correctly read. If this parameter is not provided the screenshot is\n" +
            "        saved in the current directory as xbox_screenshot.png. This operation\n" +
            "        will fail if the file already exists unless the /override flag is specified.\n";

        /// <summary>
        /// Main entry point for handling a Screenshot operation
        /// </summary>
        /// <param name="portal">DevicePortal reference for communicating with the device.</param>
        /// <param name="parameters">Parsed command line parameters.</param>
        public static void HandleOperation(DevicePortal portal, ParameterHelper parameters)
        {
            if (parameters.HasFlag(ParameterHelper.HelpFlag))
            {
                Console.WriteLine(XblScreenshotUsageMessage);
                return;
            }

            string filepath = parameters.GetParameterValue("filepath");

            if (string.IsNullOrEmpty(filepath))
            {
                filepath = "screenshot.png";
            }

            if (File.Exists(filepath) && !parameters.HasFlag("override"))
            {
                Console.WriteLine("Found an existing file: {0}. Specify /override flag to override this file.", filepath);
            }
            else
            {

                Task<Stream> screenshotTask = portal.TakeXboxScreenshot();
                screenshotTask.Wait();

                using (var fileStream = new FileStream(filepath, FileMode.Create))
                {
                    screenshotTask.Result.CopyTo(fileStream);
                }
                Console.WriteLine("Screenshot saved as {0}.", filepath);
            }
        }
    }
}
 No newline at end of file
+15 −1
Original line number Diff line number Diff line
@@ -31,7 +31,8 @@ namespace XboxWdpDriver
                "processes\n" +
                "systemPerf\n" +
                "config\n" +
                "file";
                "file\n" +
                "screenshot\n";

        /// <summary>
        /// Usage string
@@ -103,6 +104,11 @@ namespace XboxWdpDriver
            /// Uses the same registry setting as XbConnect tool.
            /// </summary>
            ConnectOperation,

            /// <summary>
            /// Takes a screenshot from the current Xbox One console.
            /// </summary>
            ScreenshotOperation,
        }

        /// <summary>
@@ -359,6 +365,10 @@ namespace XboxWdpDriver
                        Console.WriteLine("Connected to Default console: {0}", targetConsole);
                    }
                }
                else if (operation == OperationType.ScreenshotOperation)
                {
                    ScreenshotOperation.HandleOperation(portal, parameters);
                }
                else
                {
                    Console.WriteLine("Successfully connected to console but no operation was specified. \n" +
@@ -425,6 +435,10 @@ namespace XboxWdpDriver
            {
                return OperationType.FileOperation;
            }
            else if (operation.Equals("screenshot", StringComparison.OrdinalIgnoreCase))
            {
                return OperationType.ScreenshotOperation;
            }

            throw new Exception("Unknown Operation Type. " + AvailableOperationsText);
        }
+2 −1
Original line number Diff line number Diff line
@@ -52,6 +52,7 @@
    <Compile Include="DevicePortalConnection.cs" />
    <Compile Include="NetworkShare.cs" />
    <Compile Include="Operations\FileOperation.cs" />
    <Compile Include="Operations\ScreenshotOperation.cs" />
    <Compile Include="Operations\SettingOperation.cs" />
    <Compile Include="Operations\InstallOperation.cs" />
    <Compile Include="ParameterHelper.cs" />
+10 −0
Original line number Diff line number Diff line
@@ -583,6 +583,16 @@ namespace Microsoft.Tools.WindowsDevicePortal.Tests
            Assert.AreEqual("No", recievedSetting.RequiresReboot);
        }

        public void XboxScreenshotTest()
        {
            TestHelpers.MockHttpResponder.AddMockResponse(DevicePortal.GetXboxScreenshotApi, HttpMethods.Get);

            Task<Stream> screenshotTask = TestHelpers.Portal.TakeXboxScreenshot();
            screenshotTask.Wait();

            Assert.AreEqual(TaskStatus.RanToCompletion, screenshotTask.Status);
        }

        /// <summary>
        /// Validate the <see cref="RunningProcesses" /> returned from the tests.
        /// </summary>
+1 −0
Original line number Diff line number Diff line
@@ -48,6 +48,7 @@
    <Compile Include="$(MSBuildThisFileDirectory)Xbox\SmbShare.cs" />
    <Compile Include="$(MSBuildThisFileDirectory)Xbox\UserManagement.cs" />
    <Compile Include="$(MSBuildThisFileDirectory)Xbox\XboxAppDeployment.cs" />
    <Compile Include="$(MSBuildThisFileDirectory)Xbox\XboxMediaCapture.cs" />
    <Compile Include="$(MSBuildThisFileDirectory)Xbox\XboxSettings.cs" />
  </ItemGroup>
</Project>
 No newline at end of file
Loading