Ejemplo n.º 1
0
 public void Parameters_ReturnsParameters()
 {
     var parameters = new CaptureParameters();
     using (var grabber = new ScreenGrabber(parameters))
     {
         Assert.AreSame(parameters, grabber.Parameters);
     }
 }
Ejemplo n.º 2
0
 public void ScreenshotWidth_ReturnsScaledWidth()
 {
     var parameters = new CaptureParameters() { Zoom = 0.25 };
     using (var grabber = new ScreenGrabber(parameters))
     {
         Assert.AreApproximatelyEqual(ScreenGrabber.GetScreenSize().Width / 2, grabber.ScreenshotWidth, 1);
     }
 }
Ejemplo n.º 3
0
        public void ScreenshotWidth_WhenDisposed_Throws()
        {
            var parameters = new CaptureParameters();
            var grabber = new ScreenGrabber(parameters);
            grabber.Dispose();

            int x;
            Assert.Throws<ObjectDisposedException>(() => x = grabber.ScreenshotWidth);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Captures an image of the entire desktop.
        /// </summary>
        /// <param name="parameters">The capture parameters.</param>
        /// <returns>The screenshot.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="parameters"/> is null.</exception>
        /// <exception cref="ScreenshotNotAvailableException">Thrown if a screenshot cannot be captured at this time.</exception>
        public static Bitmap Screenshot(CaptureParameters parameters)
        {
            if (parameters == null)
                throw new ArgumentNullException("parameters");

            using (var grabber = new ScreenGrabber(parameters))
            {
                grabber.OverlayManager.AddOverlay(GetOverlayManager().ToOverlay());
                return grabber.CaptureScreenshot(null);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Creates a screen grabber.
        /// </summary>
        /// <param name="parameters">The capture parameters.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="parameters"/> is null.</exception>
        public ScreenGrabber(CaptureParameters parameters)
        {
            if (parameters == null)
                throw new ArgumentNullException("parameters");

            this.parameters = parameters;

            Size screenSize = GetScreenSize();
            screenWidth = screenSize.Width;
            screenHeight = screenSize.Height;

            xyScale = Math.Sqrt(parameters.Zoom);
            screenshotWidth = (int) Math.Round(screenWidth * xyScale);
            screenshotHeight = (int) Math.Round(screenHeight * xyScale);

            overlayManager = new OverlayManager();
        }
        public void Zoom_CanGetAndSetValue()
        {
            var parameters = new CaptureParameters();

            Assert.AreEqual(1.0, parameters.Zoom);

            parameters.Zoom = 1.0 / 16;
            Assert.AreEqual(1.0 / 16, parameters.Zoom);

            parameters.Zoom = 16;
            Assert.AreEqual(16, parameters.Zoom);

            var ex = Assert.Throws<ArgumentOutOfRangeException>(() => parameters.Zoom = 1.0 / 17);
            Assert.Contains(ex.Message, "The zoom factor must be between 1/16 and 16.");

            ex = Assert.Throws<ArgumentOutOfRangeException>(() => parameters.Zoom = 17);
            Assert.Contains(ex.Message, "The zoom factor must be between 1/16 and 16.");
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Creates a screen grabber.
        /// </summary>
        /// <param name="parameters">The capture parameters.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="parameters"/> is null.</exception>
        public ScreenGrabber(CaptureParameters parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            this.parameters = parameters;

            Size screenSize = GetScreenSize();

            screenWidth  = screenSize.Width;
            screenHeight = screenSize.Height;

            xyScale          = Math.Sqrt(parameters.Zoom);
            screenshotWidth  = (int)Math.Round(screenWidth * xyScale);
            screenshotHeight = (int)Math.Round(screenHeight * xyScale);

            overlayManager = new OverlayManager();
        }
Ejemplo n.º 8
0
        public void CaptureScreenshot_WhenDisposed_Throws()
        {
            var parameters = new CaptureParameters();
            var grabber = new ScreenGrabber(parameters);
            grabber.Dispose();

            Assert.Throws<ObjectDisposedException>(() => grabber.CaptureScreenshot(null));
        }
Ejemplo n.º 9
0
        public void CaptureScreenshot_WhenBitmapIsNotTheRightSize_Throws()
        {
            var parameters = new CaptureParameters() { Zoom = 0.25 };
            using (var grabber = new ScreenGrabber(parameters))
            {
                var ex = Assert.Throws<ArgumentException>(() => grabber.CaptureScreenshot(new Bitmap(1, grabber.ScreenshotHeight)));
                Assert.Contains(ex.Message, "The bitmap dimensions must exactly match the screenshot dimensions.");

                ex = Assert.Throws<ArgumentException>(() => grabber.CaptureScreenshot(new Bitmap(grabber.ScreenshotWidth, 1)));
                Assert.Contains(ex.Message, "The bitmap dimensions must exactly match the screenshot dimensions.");
            }
        }
Ejemplo n.º 10
0
        public void CaptureScreenshot_WhenBitmapIsNotNull_CapturesScreenshotIntoProvidedBitmap()
        {
            var parameters = new CaptureParameters() { Zoom = 0.25 };
            using (var grabber = new ScreenGrabber(parameters))
            {
                using (Bitmap bitmap = new Bitmap(grabber.ScreenshotWidth, grabber.ScreenshotHeight))
                {
                    if (ScreenGrabber.CanCaptureScreenshot())
                    {
                        Bitmap returnedBitmap = grabber.CaptureScreenshot(bitmap);
                        TestLog.EmbedImage("Screenshot with 0.25x zoom", bitmap);

                        Assert.AreSame(bitmap, returnedBitmap);
                    }
                    else
                    {
                        Assert.Throws<ScreenshotNotAvailableException>(() => grabber.CaptureScreenshot(bitmap),
                            "CanCaptureScreenshot returned false so expected an exception to be thrown.");
                    }
                }
            }
        }
Ejemplo n.º 11
0
        public void CaptureScreenshot_WhenBitmapIsNullAndZoomed_CapturesScaledScreenshotIntoNewBitmap()
        {
            var parameters = new CaptureParameters() { Zoom = 0.25 };
            using (var grabber = new ScreenGrabber(parameters))
            {
                if (ScreenGrabber.CanCaptureScreenshot())
                {
                    using (Bitmap bitmap = grabber.CaptureScreenshot(null))
                    {
                        TestLog.EmbedImage("Screenshot with 0.25x zoom", bitmap);

                        Assert.Multiple(() =>
                        {
                            Assert.AreApproximatelyEqual(ScreenGrabber.GetScreenSize().Width / 2,
                                grabber.ScreenshotWidth, 1);
                            Assert.AreApproximatelyEqual(ScreenGrabber.GetScreenSize().Height / 2,
                                grabber.ScreenshotHeight, 1);
                            Assert.AreEqual(grabber.ScreenshotWidth, bitmap.Width);
                            Assert.AreEqual(grabber.ScreenshotHeight, bitmap.Height);
                        });
                    }
                }
                else
                {
                    Assert.Throws<ScreenshotNotAvailableException>(() => grabber.CaptureScreenshot(null),
                        "CanCaptureScreenshot returned false so expected an exception to be thrown.");
                }
            }
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Automatically embeds a video of the test run from this point forward when a trigger event occurs.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Recording a screen capture video can be very CPU and space intensive particularly
        /// when running tests on a single-core CPU.  We recommend calling
        /// <see cref="AutoEmbedRecording(TriggerEvent, string, CaptureParameters, double)" /> with
        /// a <see cref="CaptureParameters.Zoom" /> factor of 0.25 or less and a frame rate
        /// of no more than 5 to 10 frames per second.
        /// </para>
        /// <para>
        /// If screenshots cannot be captured, the method will embed a warning message to that effect.
        /// </para>
        /// </remarks>
        /// <param name="triggerEvent">The trigger event.</param>
        /// <param name="attachmentName">The name to give the video attachment, or null to assign one automatically.</param>
        /// <param name="parameters">The capture parameters.</param>
        /// <param name="framesPerSecond">The number of frames per second to capture.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="parameters"/> is null.</exception>
        /// <seealso cref="TestContext.AutoExecute(TriggerEvent, Action)"/>
        public static void AutoEmbedRecording(TriggerEvent triggerEvent, string attachmentName, CaptureParameters parameters, double framesPerSecond)
        {
            if (parameters == null)
                throw new ArgumentNullException("parameters");

            TestContext context = TestContext.CurrentContext;
            if (context != null)
            {
                try
                {
                    ScreenRecorder recorder = StartRecording(parameters, framesPerSecond);

                    context.AutoExecute(triggerEvent, () =>
                    {
                        recorder.Stop();

                        if (recorder.Video.FrameCount != 0)
                            context.LogWriter.Default.EmbedVideo(attachmentName, recorder.Video);
                    }, recorder.Dispose);
                }
                catch (ScreenshotNotAvailableException ex)
                {
                    context.AutoExecute(triggerEvent, () =>
                    {
                        context.LogWriter.Default.WriteException(ex, "Recording not available.");
                    });
                }
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Automatically embeds a screenshot when a trigger event occurs.
        /// </summary>
        /// <remarks>
        /// <para>
        /// If screenshots cannot be captured, the method will embed a warning message to that effect.
        /// </para>
        /// </remarks>
        /// <param name="triggerEvent">The trigger event.</param>
        /// <param name="attachmentName">The name to give to the image attachment, or null to assign one automatically.</param>
        /// <param name="parameters">The capture parameters.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="parameters"/> is null.</exception>
        /// <seealso cref="TestContext.AutoExecute(TriggerEvent, Action)"/>
        public static void AutoEmbedScreenshot(TriggerEvent triggerEvent, string attachmentName, CaptureParameters parameters)
        {
            if (parameters == null)
                throw new ArgumentNullException("parameters");

            TestContext context = TestContext.CurrentContext;
            if (context != null)
            {
                context.AutoExecute(triggerEvent, () =>
                {
                    try
                    {
                        Bitmap bitmap = Screenshot(parameters);
                        context.LogWriter.Default.EmbedImage(attachmentName, bitmap);
                    }
                    catch (ScreenshotNotAvailableException ex)
                    {
                        context.LogWriter.Default.WriteException(ex, "Screenshot not available.");
                    }
                });
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Starts recording a screen capture video of the entire desktop.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Recording a screen capture video can be very CPU and space intensive particularly
        /// when running tests on a single-core CPU.  We recommend calling
        /// <see cref="StartRecording(CaptureParameters, double)" /> with
        /// a <see cref="CaptureParameters.Zoom" /> factor of 0.25 or less and a frame rate
        /// of no more than 5 to 10 frames per second.
        /// </para>
        /// </remarks>
        /// <param name="parameters">The capture parameters.</param>
        /// <param name="framesPerSecond">The number of frames per second to capture.</param>
        /// <returns>The recorder.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="parameters"/> is null.</exception>
        /// <exception cref="ScreenshotNotAvailableException">Thrown if a screenshot cannot be captured at this time.</exception>
        public static ScreenRecorder StartRecording(CaptureParameters parameters, double framesPerSecond)
        {
            if (parameters == null)
                throw new ArgumentNullException("parameters");

            ScreenGrabber.ThrowIfScreenshotNotAvailable();

            ScreenGrabber grabber = new ScreenGrabber(parameters);
            try
            {
                FlashScreenVideo video = new FlashScreenVideo(new FlashScreenVideoParameters(
                    grabber.ScreenshotWidth, grabber.ScreenshotHeight, framesPerSecond));

                ScreenRecorder recorder = new ScreenRecorder(grabber, video);
                try
                {
                    recorder.OverlayManager.AddOverlay(GetOverlayManager().ToOverlay());

                    recorder.Start();
                    return recorder;
                }
                catch
                {
                    recorder.Dispose();
                    throw;
                }
            }
            catch
            {
                grabber.Dispose();
                throw;
            }
        }