add more unit tests for rectangular selection system

This commit is contained in:
Chikashi Miyama 2019-10-01 23:01:56 +02:00
parent 1267a9715a
commit b7fa663c97
6 changed files with 129 additions and 54 deletions

View file

@ -64,6 +64,7 @@
<Compile Include="Assets\Editor\RectangularSelection.cs" /> <Compile Include="Assets\Editor\RectangularSelection.cs" />
<Compile Include="Assets\Editor\SpectrumGenerator.cs" /> <Compile Include="Assets\Editor\SpectrumGenerator.cs" />
<Compile Include="Assets\Editor\UnitTest\UnitTest_ChangeObserver.cs" /> <Compile Include="Assets\Editor\UnitTest\UnitTest_ChangeObserver.cs" />
<Compile Include="Assets\Editor\UnitTest\UnitTest_RectangularSelection.cs" />
<Compile Include="Assets\Editor\UnitTest\UnitTest_SpectrumGenerator.cs" /> <Compile Include="Assets\Editor\UnitTest\UnitTest_SpectrumGenerator.cs" />
<Compile Include="Assets\Scripts\VideoInput\Editor\UnitTest\ComponentFactoryTestCase.cs" /> <Compile Include="Assets\Scripts\VideoInput\Editor\UnitTest\ComponentFactoryTestCase.cs" />
<Compile Include="Assets\Scripts\VideoInput\Editor\UnitTest\KinectSensorTestCase.cs" /> <Compile Include="Assets\Scripts\VideoInput\Editor\UnitTest\KinectSensorTestCase.cs" />

View file

@ -37,11 +37,13 @@ namespace cylvester
var width = selectedArea_.width / paintSpace.width; var width = selectedArea_.width / paintSpace.width;
var height = selectedArea_.height / paintSpace.height; var height = selectedArea_.height / paintSpace.height;
var selectionRect = new Rect(); var selectionRect = new Rect
selectionRect.x = xPos * textureWidth_; {
selectionRect.y = yPos * textureHeight_; x = xPos * textureWidth_,
selectionRect.width = width * textureWidth_; y = yPos * textureHeight_,
selectionRect.height = height * textureHeight_; width = width * textureWidth_,
height = height * textureHeight_
};
return selectionRect; return selectionRect;
} }

View file

@ -1,3 +1,4 @@
using System;
using UnityEngine; using UnityEngine;
namespace cylvester namespace cylvester
@ -19,20 +20,23 @@ namespace cylvester
public int Update(IPdArray pdArray, Rect selectionRect) public int Update(IPdArray pdArray, Rect selectionRect)
{ {
var numPixels = 0;
for (var x = 0; x < Spectrum.width; x++)
{
for (var y = 0; y < Spectrum.height; y++)
{
var color = Color.black;
var validPixel = false;
if (pdArray != null) if (pdArray != null)
{ {
var magnitude = pdArray.Data[x] * 20f; return UpdatePlayMode(pdArray, selectionRect);
validPixel = magnitude > y;
color = validPixel ? Color.green : Color.black;
} }
UpdateEditMode(selectionRect);
return 0;
}
private int UpdatePlayMode(IPdArray pdArray, Rect selectionRect)
{
var numPixels = 0;
var data = pdArray.Data;
OnAllPixels((x, y) =>
{
var magnitude = data[x] * 20f;
var validPixel = magnitude > y;
var color = validPixel ? Color.green : Color.black;
if (IsInSelection(x, y, ref selectionRect)) if (IsInSelection(x, y, ref selectionRect))
{ {
@ -44,12 +48,32 @@ namespace cylvester
color.a = 0.2f; color.a = 0.2f;
Spectrum.SetPixel(x, y, color); Spectrum.SetPixel(x, y, color);
} });
}
Spectrum.Apply(); Spectrum.Apply();
return numPixels; return numPixels;
} }
private void UpdateEditMode(Rect selectionRect)
{
OnAllPixels((x, y) =>
{
var color = Color.black;
if (IsInSelection(x, y, ref selectionRect))
color.a = 1f;
else
color.a = 0.2f;
Spectrum.SetPixel(x, y, color);
});
Spectrum.Apply();
}
private void OnAllPixels(Action<int, int> action)
{
for (var x = 0; x < Spectrum.width; x++)
for (var y = 0; y < Spectrum.height; y++)
action(x, y);
}
private bool IsInSelection(int x, int y, ref Rect selectionRect) private bool IsInSelection(int x, int y, ref Rect selectionRect)
{ {
var inRectHorizontally = selectionRect.x < x && x < selectionRect.x + (selectionRect.width-1); var inRectHorizontally = selectionRect.x < x && x < selectionRect.x + (selectionRect.width-1);

View file

@ -0,0 +1,21 @@
using NUnit.Framework;
using UnityEngine;
namespace cylvester
{
[TestFixture]
public class UnitTest_RectangularSelection
{
[Test]
public void Update()
{
var paintSpace = new Rect(0, 0, 100, 100); // GUI
var rectangularSelection = new RectangularSelection(1000, 1000); // texture 10 times larger
rectangularSelection.Start(new Vector2(10, 10));
var selectionInTexture = rectangularSelection.Update(new Vector2(20, 20), ref paintSpace);
var expected = new Rect(100, 100, 100, 100);
Assert.AreEqual(expected, selectionInTexture);
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 5cc4a0c2b4a342a7920a3aab6bf77c25
timeCreated: 1569963352

View file

@ -9,6 +9,7 @@ namespace cylvester
{ {
private IPdArray pdArray_; private IPdArray pdArray_;
private Rect selectionRect_; private Rect selectionRect_;
private Rect noSelectionRect_;
private float[] dummyData_; private float[] dummyData_;
private Color standardColor_; private Color standardColor_;
private Color selectedColor_; private Color selectedColor_;
@ -16,14 +17,11 @@ namespace cylvester
[SetUp] [SetUp]
public void SetUp() public void SetUp()
{ {
pdArray_ = Substitute.For<IPdArray>();
selectionRect_ = new Rect {x = 9, y = 9, width = 3, height = 3};
dummyData_ = new float[100]; dummyData_ = new float[100];
pdArray_ = Substitute.For<IPdArray>();
for (var i = 0; i < dummyData_.Length; ++i) pdArray_.Data.Returns(dummyData_);
{ selectionRect_ = new Rect {x = 9, y = 9, width = 3, height = 3};
dummyData_[i] = i / (float)dummyData_.Length; noSelectionRect_ = new Rect {width = 0, height = 0};
}
standardColor_ = new Color(0f, 0f, 0f, 0.2f); standardColor_ = new Color(0f, 0f, 0f, 0.2f);
selectedColor_ = new Color(0f, 0f, 0f, 1f); selectedColor_ = new Color(0f, 0f, 0f, 1f);
@ -39,20 +37,47 @@ namespace cylvester
} }
[Test] [Test]
public void Update_array_available() public void Update_array_available_loud()
{ {
for (var i = 0; i < dummyData_.Length; ++i)
dummyData_[i] = 100f; // loud sound
var spectrumGenerator = new SpectrumGenerator(100, 100); var spectrumGenerator = new SpectrumGenerator(100, 100);
spectrumGenerator.Update(pdArray_, selectionRect_); var validPixel = spectrumGenerator.Update(pdArray_, selectionRect_);
Assert.AreEqual(1, validPixel);
} }
[Test]
public void Update_array_available_soft()
{
for (var i = 0; i < dummyData_.Length; ++i)
dummyData_[i] = 0.001f; // soft sound
var spectrumGenerator = new SpectrumGenerator(100, 100);
var validPixel = spectrumGenerator.Update(pdArray_, selectionRect_);
Assert.AreEqual(0, validPixel);
}
[Test]
public void Update_array_available_loud_no_selection()
{
for (var i = 0; i < dummyData_.Length; ++i)
dummyData_[i] = 100f; // loud sound
var spectrumGenerator = new SpectrumGenerator(100, 100);
var validPixel = spectrumGenerator.Update(pdArray_, noSelectionRect_);
Assert.AreEqual(0, validPixel);
}
[Test] [Test]
public void Update_array_unavailable() public void Update_array_unavailable()
{ {
var noSelection = new Rect {width = 0, height = 0};
var spectrumGenerator = new SpectrumGenerator(100, 100); var spectrumGenerator = new SpectrumGenerator(100, 100);
var validPixels = spectrumGenerator.Update(null, noSelection); var validPixels = spectrumGenerator.Update(null, noSelectionRect_);
Assert.AreEqual(0, validPixels); Assert.AreEqual(0, validPixels);
var texture = spectrumGenerator.Spectrum; var texture = spectrumGenerator.Spectrum;
@ -79,7 +104,6 @@ namespace cylvester
Assert.AreEqual(selectedColor_, texture.GetPixel(x, y)); Assert.AreEqual(selectedColor_, texture.GetPixel(x, y));
else else
Assert.AreEqual(standardColor_, texture.GetPixel(x, y)); Assert.AreEqual(standardColor_, texture.GetPixel(x, y));
} }
} }
} }