diff --git a/Packages/com.github.homuler.mediapipe/Tests/EditMode/Gpu/GlCalculatorHelperTest.cs b/Packages/com.github.homuler.mediapipe/Tests/EditMode/Gpu/GlCalculatorHelperTest.cs index 9b6b3b8dd..bb3283cab 100644 --- a/Packages/com.github.homuler.mediapipe/Tests/EditMode/Gpu/GlCalculatorHelperTest.cs +++ b/Packages/com.github.homuler.mediapipe/Tests/EditMode/Gpu/GlCalculatorHelperTest.cs @@ -7,18 +7,18 @@ public class GlCalculatorHelperTest { #region Constructor [Test, GpuOnly] public void Ctor_ShouldInstantiateGlCalculatorHelper() { - var glCalculatorHelper = new GlCalculatorHelper(); - - Assert.AreNotEqual(glCalculatorHelper.mpPtr, IntPtr.Zero); + using (var glCalculatorHelper = new GlCalculatorHelper()) { + Assert.AreNotEqual(glCalculatorHelper.mpPtr, IntPtr.Zero); + } } #endregion #region #isDisposed [Test, GpuOnly] public void isDisposed_ShouldReturnFalse_When_NotDisposedYet() { - var glCalculatorHelper = new GlCalculatorHelper(); - - Assert.False(glCalculatorHelper.isDisposed); + using (var glCalculatorHelper = new GlCalculatorHelper()) { + Assert.False(glCalculatorHelper.isDisposed); + } } [Test, GpuOnly] @@ -33,125 +33,133 @@ public void isDisposed_ShouldReturnTrue_When_AlreadyDisposed() { #region #InitializeForTest [Test, GpuOnly] public void InitializeForTest_ShouldInitialize() { - var glCalculatorHelper = new GlCalculatorHelper(); - - Assert.False(glCalculatorHelper.Initialized()); - glCalculatorHelper.InitializeForTest(GpuResources.Create().Value()); - Assert.True(glCalculatorHelper.Initialized()); + using (var glCalculatorHelper = new GlCalculatorHelper()) { + Assert.False(glCalculatorHelper.Initialized()); + glCalculatorHelper.InitializeForTest(GpuResources.Create().Value()); + Assert.True(glCalculatorHelper.Initialized()); + } } #endregion #region #RunInGlContext [Test, GpuOnly] public void RunInGlContext_ShouldReturnOk_When_FunctionReturnsOk() { - var glCalculatorHelper = new GlCalculatorHelper(); - glCalculatorHelper.InitializeForTest(GpuResources.Create().Value()); + using (var glCalculatorHelper = new GlCalculatorHelper()) { + glCalculatorHelper.InitializeForTest(GpuResources.Create().Value()); - var status = glCalculatorHelper.RunInGlContext(() => { return Status.Ok(); }); - Assert.True(status.ok); + var status = glCalculatorHelper.RunInGlContext(() => { return Status.Ok(); }); + Assert.True(status.ok); + } } [Test, GpuOnly] public void RunInGlContext_ShouldReturnInternal_When_FunctionReturnsInternal() { - var glCalculatorHelper = new GlCalculatorHelper(); - glCalculatorHelper.InitializeForTest(GpuResources.Create().Value()); + using (var glCalculatorHelper = new GlCalculatorHelper()) { + glCalculatorHelper.InitializeForTest(GpuResources.Create().Value()); - var status = glCalculatorHelper.RunInGlContext(() => { return Status.Build(Status.StatusCode.Internal, "error"); }); - Assert.AreEqual(status.code, Status.StatusCode.Internal); + var status = glCalculatorHelper.RunInGlContext(() => { return Status.Build(Status.StatusCode.Internal, "error"); }); + Assert.AreEqual(status.code, Status.StatusCode.Internal); + } } [Test, GpuOnly] public void RunInGlContext_ShouldReturnFailedPreCondition_When_FunctionThrows() { - var glCalculatorHelper = new GlCalculatorHelper(); - glCalculatorHelper.InitializeForTest(GpuResources.Create().Value()); + using (var glCalculatorHelper = new GlCalculatorHelper()) { + glCalculatorHelper.InitializeForTest(GpuResources.Create().Value()); - GlCalculatorHelper.GlStatusFunction glStatusFunction = () => { throw new InvalidProgramException(); }; - var status = glCalculatorHelper.RunInGlContext(glStatusFunction); - Assert.AreEqual(status.code, Status.StatusCode.FailedPrecondition); + GlCalculatorHelper.GlStatusFunction glStatusFunction = () => { throw new InvalidProgramException(); }; + var status = glCalculatorHelper.RunInGlContext(glStatusFunction); + Assert.AreEqual(status.code, Status.StatusCode.FailedPrecondition); + } } #endregion #region #CreateSourceTexture [Test, GpuOnly] public void CreateSourceTexture_ShouldReturnGlTexture_When_CalledWithImageFrame() { - var glCalculatorHelper = new GlCalculatorHelper(); - glCalculatorHelper.InitializeForTest(GpuResources.Create().Value()); + using (var glCalculatorHelper = new GlCalculatorHelper()) { + glCalculatorHelper.InitializeForTest(GpuResources.Create().Value()); - var imageFrame = new ImageFrame(ImageFormat.Format.SRGBA, 32, 24); - var status = glCalculatorHelper.RunInGlContext(() => { - var texture = glCalculatorHelper.CreateSourceTexture(imageFrame); + var imageFrame = new ImageFrame(ImageFormat.Format.SRGBA, 32, 24); + var status = glCalculatorHelper.RunInGlContext(() => { + var texture = glCalculatorHelper.CreateSourceTexture(imageFrame); - Assert.AreEqual(texture.width, 32); - Assert.AreEqual(texture.height, 24); + Assert.AreEqual(texture.width, 32); + Assert.AreEqual(texture.height, 24); - texture.Dispose(); - return Status.Ok(); - }); + texture.Dispose(); + return Status.Ok(); + }); - Assert.True(status.ok); + Assert.True(status.ok); + } } [Test, GpuOnly] [Ignore("Skip because a thread hangs")] public void CreateSourceTexture_ShouldFail_When_ImageFrameFormatIsInvalid() { - var glCalculatorHelper = new GlCalculatorHelper(); - glCalculatorHelper.InitializeForTest(GpuResources.Create().Value()); - - var imageFrame = new ImageFrame(ImageFormat.Format.SBGRA, 32, 24); - var status = glCalculatorHelper.RunInGlContext(() => { - using (var texture = glCalculatorHelper.CreateSourceTexture(imageFrame)) { - texture.Release(); - } - return Status.Ok(); - }); - - Assert.AreEqual(status.code, Status.StatusCode.FailedPrecondition); + using (var glCalculatorHelper = new GlCalculatorHelper()) { + glCalculatorHelper.InitializeForTest(GpuResources.Create().Value()); + + var imageFrame = new ImageFrame(ImageFormat.Format.SBGRA, 32, 24); + var status = glCalculatorHelper.RunInGlContext(() => { + using (var texture = glCalculatorHelper.CreateSourceTexture(imageFrame)) { + texture.Release(); + } + return Status.Ok(); + }); + + Assert.AreEqual(status.code, Status.StatusCode.FailedPrecondition); + } } #endregion #region #CreateDestinationTexture [Test, GpuOnly] public void CreateDestinationTexture_ShouldReturnGlTexture_When_GpuBufferFormatIsValid() { - var glCalculatorHelper = new GlCalculatorHelper(); - glCalculatorHelper.InitializeForTest(GpuResources.Create().Value()); + using (var glCalculatorHelper = new GlCalculatorHelper()) { + glCalculatorHelper.InitializeForTest(GpuResources.Create().Value()); - var status = glCalculatorHelper.RunInGlContext(() => { - var glTexture = glCalculatorHelper.CreateDestinationTexture(32, 24, GpuBufferFormat.kBGRA32); + var status = glCalculatorHelper.RunInGlContext(() => { + var glTexture = glCalculatorHelper.CreateDestinationTexture(32, 24, GpuBufferFormat.kBGRA32); - Assert.AreEqual(glTexture.width, 32); - Assert.AreEqual(glTexture.height, 24); - return Status.Ok(); - }); + Assert.AreEqual(glTexture.width, 32); + Assert.AreEqual(glTexture.height, 24); + return Status.Ok(); + }); - Assert.True(status.ok); + Assert.True(status.ok); + } } #endregion #region #framebuffer [Test, GpuOnly] public void framebuffer_ShouldReturnGLName() { - var glCalculatorHelper = new GlCalculatorHelper(); - glCalculatorHelper.InitializeForTest(GpuResources.Create().Value()); + using (var glCalculatorHelper = new GlCalculatorHelper()) { + glCalculatorHelper.InitializeForTest(GpuResources.Create().Value()); - // default frame buffer - Assert.AreEqual(glCalculatorHelper.framebuffer, 0); + // default frame buffer + Assert.AreEqual(glCalculatorHelper.framebuffer, 0); + } } #endregion #region #GetGlContext [Test, GpuOnly] public void GetGlContext_ShouldReturnCurrentContext() { - var glCalculatorHelper = new GlCalculatorHelper(); - glCalculatorHelper.InitializeForTest(GpuResources.Create().Value()); + using (var glCalculatorHelper = new GlCalculatorHelper()) { + glCalculatorHelper.InitializeForTest(GpuResources.Create().Value()); - var glContext = glCalculatorHelper.GetGlContext(); + var glContext = glCalculatorHelper.GetGlContext(); #if UNITY_EDITOR_LINUX || UNITY_STANDALONE_LINUX || UNITY_ANDROID - Assert.AreNotEqual(glContext.eglContext, IntPtr.Zero); + Assert.AreNotEqual(glContext.eglContext, IntPtr.Zero); #elif UNITY_STANDALONE_OSX - Assert.AreNotEqual(glContext.nsglContext, IntPtr.Zero); + Assert.AreNotEqual(glContext.nsglContext, IntPtr.Zero); #elif UNITY_IOS - Assert.AreNotEqual(glContext.eaglContext, IntPtr.Zero); + Assert.AreNotEqual(glContext.eaglContext, IntPtr.Zero); #endif + } } #endregion } diff --git a/Packages/com.github.homuler.mediapipe/Tests/EditMode/Gpu/GlContextTest.cs b/Packages/com.github.homuler.mediapipe/Tests/EditMode/Gpu/GlContextTest.cs index 2e982913e..e209d668f 100644 --- a/Packages/com.github.homuler.mediapipe/Tests/EditMode/Gpu/GlContextTest.cs +++ b/Packages/com.github.homuler.mediapipe/Tests/EditMode/Gpu/GlContextTest.cs @@ -14,16 +14,17 @@ public void GetCurrent_ShouldReturnNull_When_CalledOutOfGlContext() { [Test, GpuOnly] public void GetCurrent_ShouldReturnCurrentContext_When_CalledInGlContext() { - var glCalculatorHelper = new GlCalculatorHelper(); - glCalculatorHelper.InitializeForTest(GpuResources.Create().Value()); + using (var glCalculatorHelper = new GlCalculatorHelper()) { + glCalculatorHelper.InitializeForTest(GpuResources.Create().Value()); - glCalculatorHelper.RunInGlContext(() => { - var glContext = GlContext.GetCurrent(); + glCalculatorHelper.RunInGlContext(() => { + var glContext = GlContext.GetCurrent(); - Assert.NotNull(glContext); - Assert.True(glContext.IsCurrent()); - return Status.Ok(); - }).AssertOk(); + Assert.NotNull(glContext); + Assert.True(glContext.IsCurrent()); + return Status.Ok(); + }).AssertOk(); + } } #endregion @@ -56,17 +57,11 @@ public void ShouldReturnProperties() { #endregion private GlContext GetGlContext() { - GlContext glContext = null; + using (var glCalculatorHelper = new GlCalculatorHelper()) { + glCalculatorHelper.InitializeForTest(GpuResources.Create().Value()); - var glCalculatorHelper = new GlCalculatorHelper(); - glCalculatorHelper.InitializeForTest(GpuResources.Create().Value()); - - glCalculatorHelper.RunInGlContext(() => { - glContext = GlContext.GetCurrent(); - return Status.Ok(); - }).AssertOk(); - - return glContext; + return glCalculatorHelper.GetGlContext(); + } } } } diff --git a/Packages/com.github.homuler.mediapipe/Tests/EditMode/Gpu/GpuResourcesTest.cs b/Packages/com.github.homuler.mediapipe/Tests/EditMode/Gpu/GpuResourcesTest.cs index 3ceaf61fc..79787acae 100644 --- a/Packages/com.github.homuler.mediapipe/Tests/EditMode/Gpu/GpuResourcesTest.cs +++ b/Packages/com.github.homuler.mediapipe/Tests/EditMode/Gpu/GpuResourcesTest.cs @@ -6,18 +6,18 @@ public class GpuResourcesTest { #region Create [Test, GpuOnly] public void Create_ShouldReturnStatusOrGpuResources() { - var statusOrGpuResources = GpuResources.Create(); - - Assert.True(statusOrGpuResources.ok); + using (var statusOrGpuResources = GpuResources.Create()) { + Assert.True(statusOrGpuResources.ok); + } } #endregion #region #isDisposed [Test, GpuOnly] public void isDisposed_ShouldReturnFalse_When_NotDisposedYet() { - var gpuResources = GpuResources.Create().Value(); - - Assert.False(gpuResources.isDisposed); + using (var gpuResources = GpuResources.Create().Value()) { + Assert.False(gpuResources.isDisposed); + } } [Test, GpuOnly]