Skip to content

Commit

Permalink
Fix GetFrameBuffer which dispose Framebuffer at finish
Browse files Browse the repository at this point in the history
Add test of GetFrameBuffer
  • Loading branch information
wherewhere committed Jun 17, 2023
1 parent ce27f5d commit fa1087f
Show file tree
Hide file tree
Showing 11 changed files with 241 additions and 89 deletions.
70 changes: 70 additions & 0 deletions AdvancedSharpAdbClient.Tests/AdbClientTests.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -343,6 +346,73 @@ public async void ExecuteRemoteCommandAsyncUnresponsiveTest()
() => TestClient.ExecuteRemoteCommandAsync("echo Hello, World", device, receiver, CancellationToken.None)));
}

[Fact]
public void GetFrameBufferAsyncTest()
{
DeviceData device = new()
{
Serial = "169.254.109.177:5555",
State = DeviceState.Online
};

DummyAdbSocket socket = new();

socket.Responses.Enqueue(AdbResponse.OK);
socket.Responses.Enqueue(AdbResponse.OK);

socket.Requests.Add("host:transport:169.254.109.177:5555");
socket.Requests.Add("framebuffer:");

socket.SyncDataReceived.Enqueue(File.ReadAllBytes("Assets/framebufferheader.bin"));
socket.SyncDataReceived.Enqueue(File.ReadAllBytes("Assets/framebuffer.bin"));

Framebuffer framebuffer = null;

lock (FactoriesTests.locker)
{
Factories.AdbSocketFactory = (endPoint) => socket;
framebuffer = TestClient.GetFrameBufferAsync(device).Result;
}

Assert.NotNull(framebuffer);
Assert.Equal(device, framebuffer.Device);

FramebufferHeader header = framebuffer.Header;

Assert.Equal(8u, header.Alpha.Length);
Assert.Equal(24u, header.Alpha.Offset);
Assert.Equal(8u, header.Green.Length);
Assert.Equal(8u, header.Green.Offset);
Assert.Equal(8u, header.Red.Length);
Assert.Equal(0u, header.Red.Offset);
Assert.Equal(8u, header.Blue.Length);
Assert.Equal(16u, header.Blue.Offset);
Assert.Equal(32u, header.Bpp);
Assert.Equal(4u, header.Size);
Assert.Equal(1u, header.Height);
Assert.Equal(1u, header.Width);
Assert.Equal(1u, header.Version);
Assert.Equal(0u, header.ColorSpace);

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
using Bitmap image = (Bitmap)framebuffer.ToImage();
Assert.NotNull(image);
Assert.Equal(PixelFormat.Format32bppArgb, image.PixelFormat);

Assert.Equal(1, image.Width);
Assert.Equal(1, image.Height);

Color pixel = image.GetPixel(0, 0);
Assert.Equal(0x35, pixel.R);
Assert.Equal(0x4a, pixel.G);
Assert.Equal(0x4c, pixel.B);
Assert.Equal(0xff, pixel.A);
}

framebuffer.Dispose();
}

[Fact]
public async void RunLogServiceAsyncTest()
{
Expand Down
83 changes: 83 additions & 0 deletions AdvancedSharpAdbClient.Tests/AdbClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Xml;
using Xunit;
Expand Down Expand Up @@ -505,6 +508,86 @@ public void ExecuteRemoteCommandUnresponsiveTest()
() => TestClient.ExecuteRemoteCommand("echo Hello, World", device, receiver)));
}

[Fact]
public void CreateRefreshableFramebufferTest()
{
DeviceData device = new()
{
Serial = "169.254.109.177:5555",
State = DeviceState.Online
};
Framebuffer framebuffer = TestClient.CreateRefreshableFramebuffer(device);
Assert.NotNull(framebuffer);
Assert.Equal(device, framebuffer.Device);
}

[Fact]
public void GetFrameBufferTest()
{
DeviceData device = new()
{
Serial = "169.254.109.177:5555",
State = DeviceState.Online
};

DummyAdbSocket socket = new();

socket.Responses.Enqueue(AdbResponse.OK);
socket.Responses.Enqueue(AdbResponse.OK);

socket.Requests.Add("host:transport:169.254.109.177:5555");
socket.Requests.Add("framebuffer:");

socket.SyncDataReceived.Enqueue(File.ReadAllBytes("Assets/framebufferheader.bin"));
socket.SyncDataReceived.Enqueue(File.ReadAllBytes("Assets/framebuffer.bin"));

Framebuffer framebuffer = null;

lock (FactoriesTests.locker)
{
Factories.AdbSocketFactory = (endPoint) => socket;
framebuffer = TestClient.GetFrameBuffer(device);
}

Assert.NotNull(framebuffer);
Assert.Equal(device, framebuffer.Device);

FramebufferHeader header = framebuffer.Header;

Assert.Equal(8u, header.Alpha.Length);
Assert.Equal(24u, header.Alpha.Offset);
Assert.Equal(8u, header.Green.Length);
Assert.Equal(8u, header.Green.Offset);
Assert.Equal(8u, header.Red.Length);
Assert.Equal(0u, header.Red.Offset);
Assert.Equal(8u, header.Blue.Length);
Assert.Equal(16u, header.Blue.Offset);
Assert.Equal(32u, header.Bpp);
Assert.Equal(4u, header.Size);
Assert.Equal(1u, header.Height);
Assert.Equal(1u, header.Width);
Assert.Equal(1u, header.Version);
Assert.Equal(0u, header.ColorSpace);

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
using Bitmap image = (Bitmap)framebuffer.ToImage();
Assert.NotNull(image);
Assert.Equal(PixelFormat.Format32bppArgb, image.PixelFormat);

Assert.Equal(1, image.Width);
Assert.Equal(1, image.Height);

Color pixel = image.GetPixel(0, 0);
Assert.Equal(0x35, pixel.R);
Assert.Equal(0x4a, pixel.G);
Assert.Equal(0x4c, pixel.B);
Assert.Equal(0xff, pixel.A);
}

framebuffer.Dispose();
}

[Fact]
public void RunLogServiceTest()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
<None Update="Assets\framebufferheader-empty.bin">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Assets\framebufferheader-v1.bin">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Assets\framebufferheader-v2.bin">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
Binary file not shown.
Binary file modified AdvancedSharpAdbClient.Tests/Assets/framebufferheader.bin
Binary file not shown.
12 changes: 6 additions & 6 deletions AdvancedSharpAdbClient.Tests/Models/FramebufferHeaderTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Drawing;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
Expand All @@ -15,7 +16,7 @@ public class FramebufferHeaderTests
[Fact]
public void ReadFramebufferTest()
{
byte[] data = File.ReadAllBytes("Assets/framebufferheader.bin");
byte[] data = File.ReadAllBytes("Assets/framebufferheader-v1.bin");

FramebufferHeader header = FramebufferHeader.Read(data);

Expand All @@ -28,6 +29,7 @@ public void ReadFramebufferTest()
Assert.Equal(8u, header.Blue.Length);
Assert.Equal(16u, header.Blue.Offset);
Assert.Equal(32u, header.Bpp);
Assert.Equal(14745600u, header.Size);
Assert.Equal(2560u, header.Height);
Assert.Equal(1440u, header.Width);
Assert.Equal(1u, header.Version);
Expand All @@ -50,6 +52,7 @@ public void ReadFramebufferV2Test()
Assert.Equal(8u, header.Blue.Length);
Assert.Equal(16u, header.Blue.Offset);
Assert.Equal(32u, header.Bpp);
Assert.Equal(8294400u, header.Size);
Assert.Equal(1920u, header.Height);
Assert.Equal(1080u, header.Width);
Assert.Equal(2u, header.Version);
Expand All @@ -64,9 +67,6 @@ public void ToImageTest()

byte[] data = File.ReadAllBytes("Assets/framebufferheader.bin");
FramebufferHeader header = FramebufferHeader.Read(data);
header.Width = 1;
header.Height = 1;
header.Size = 4;
byte[] framebuffer = File.ReadAllBytes("Assets/framebuffer.bin");
using Bitmap image = (Bitmap)header.ToImage(framebuffer);
Assert.NotNull(image);
Expand All @@ -91,7 +91,7 @@ public void ToImageEmptyTest()
byte[] data = File.ReadAllBytes("Assets/framebufferheader-empty.bin");
FramebufferHeader header = FramebufferHeader.Read(data);

byte[] framebuffer = new byte[] { };
byte[] framebuffer = Array.Empty<byte>();

Image image = header.ToImage(framebuffer);
Assert.Null(image);
Expand Down
48 changes: 26 additions & 22 deletions AdvancedSharpAdbClient.Tests/SocketBasedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,38 @@ public class SocketBasedTests

protected SocketBasedTests(bool integrationTest, bool doDispose)
{
// this.EndPoint = AdbClient.Instance.EndPoint;

#if DEBUG
// Use the tracing adb socket factory to run the tests on an actual device.
// use the dummy socket factory to run unit tests.
if (integrationTest)
lock (FactoriesTests.locker)
{
TracingAdbSocket tracingSocket = new(EndPoint) { DoDispose = doDispose };
// this.EndPoint = AdbClient.Instance.EndPoint;
#if DEBUG
// Use the tracing adb socket factory to run the tests on an actual device.
// use the dummy socket factory to run unit tests.
if (integrationTest)
{
TracingAdbSocket tracingSocket = new(EndPoint) { DoDispose = doDispose };

Factories.AdbSocketFactory = (endPoint) => tracingSocket;
}
else
{
DummyAdbSocket socket = new();
Factories.AdbSocketFactory = (endPoint) => socket;
}
Factories.AdbSocketFactory = (endPoint) => tracingSocket;
}
else
{
DummyAdbSocket socket = new();
Factories.AdbSocketFactory = (endPoint) => socket;
}

IntegrationTest = integrationTest;
IntegrationTest = integrationTest;
#else
// In release mode (e.g. on the build server),
// never run integration tests.
DummyAdbSocket socket = new DummyAdbSocket();
Factories.AdbSocketFactory = (endPoint) => socket;
IntegrationTest = false;
// In release mode (e.g. on the build server),
// never run integration tests.
DummyAdbSocket socket = new DummyAdbSocket();
Factories.AdbSocketFactory = (endPoint) => socket;
IntegrationTest = false;
#endif
Socket = (IDummyAdbSocket)Factories.AdbSocketFactory(EndPoint);
Socket = (IDummyAdbSocket)Factories.AdbSocketFactory(EndPoint);

TestClient = new AdbClient();
TestClient = new AdbClient();

Factories.Reset();
}
}

protected static AdbResponse[] NoResponses { get; } = Array.Empty<AdbResponse>();
Expand Down
2 changes: 1 addition & 1 deletion AdvancedSharpAdbClient/AdbClient.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public async Task<Framebuffer> GetFrameBufferAsync(DeviceData device, Cancellati
{
EnsureDevice(device);

using Framebuffer framebuffer = CreateRefreshableFramebuffer(device);
Framebuffer framebuffer = CreateRefreshableFramebuffer(device);
await framebuffer.RefreshAsync(cancellationToken).ConfigureAwait(false);

// Convert the framebuffer to an image, and return that.
Expand Down
2 changes: 1 addition & 1 deletion AdvancedSharpAdbClient/AdbClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ public Framebuffer GetFrameBuffer(DeviceData device)
{
EnsureDevice(device);

using Framebuffer framebuffer = CreateRefreshableFramebuffer(device);
Framebuffer framebuffer = CreateRefreshableFramebuffer(device);
framebuffer.Refresh();

// Convert the framebuffer to an image, and return that.
Expand Down
12 changes: 3 additions & 9 deletions AdvancedSharpAdbClient/Models/Framebuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Framebuffer : IDisposable
private readonly AdbClient client;
private byte[] headerData;
private bool headerInitialized;
private bool disposed;
private bool disposed = false;

/// <summary>
/// Initializes a new instance of the <see cref="Framebuffer"/> class.
Expand All @@ -32,7 +32,7 @@ public Framebuffer(DeviceData device, AdbClient client)

// Initialize the headerData buffer
int size = Marshal.SizeOf(default(FramebufferHeader));
this.headerData = new byte[size];
headerData = new byte[size];
}

/// <summary>
Expand Down Expand Up @@ -226,12 +226,6 @@ public void Dispose()
/// <summary>
/// Throws an exception if this <see cref="Framebuffer"/> has been disposed.
/// </summary>
protected void EnsureNotDisposed()
{
if (disposed)
{
ExceptionExtensions.ThrowIf(disposed, this);
}
}
protected void EnsureNotDisposed() => ExceptionExtensions.ThrowIf(disposed, this);
}
}
Loading

0 comments on commit fa1087f

Please sign in to comment.