Skip to content

Commit

Permalink
Merge pull request #4 from tylerszabo/testable_main
Browse files Browse the repository at this point in the history
Add tests for RGBFusionTool.exe

  Resolves issue #1 

  - Refactor application to allow dependency injection
  - Add tests for v0.9.0.1 behavior
  - Minor test refactors
  • Loading branch information
tylerszabo authored Feb 12, 2018
2 parents fb2f6a1 + 05c93af commit 11cf0bc
Show file tree
Hide file tree
Showing 12 changed files with 556 additions and 113 deletions.
38 changes: 34 additions & 4 deletions GLedApiDotNetTests/GLedApiv1_0_0Mock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@

namespace GLedApiDotNetTests
{
class GLedApiv1_0_0Mock : GLedApiDotNet.Raw.IGLedAPIv1_0_0
public class GLedApiv1_0_0Mock : GLedApiDotNet.Raw.IGLedAPIv1_0_0
{
public static GLedApiDotNet.RGBFusionMotherboard RGBFusionMotherboardFactory(GLedApiv1_0_0Mock mock)
{
return new GLedApiDotNet.RGBFusionMotherboard(new GLedApiDotNet.Raw.GLedAPIv1_0_0Wrapper(mock));
}

public class Status
{
// Known status codes
Expand Down Expand Up @@ -41,6 +46,29 @@ public enum ControlState
private ControlState state;
public ControlState State { get => state; set => state = value; }

public bool IsInitialized
{
get
{
if (!stateTrackingEnabled)
{
throw new System.InvalidOperationException("State tracking not enabled");
}
switch (state)
{
case ControlState.Uninitialized:
case ControlState.DoneGetLedLayout:
case ControlState.DoneGetMaxDivision:
return false;
case ControlState.DoneInit:
case ControlState.DoneSetLedData:
return true;
default:
throw new System.InvalidOperationException(string.Format("Unexpected state {0}", state));
}
}
}

private uint nextReturn = Status.ERROR_SUCCESS;
public uint NextReturn { set => nextReturn = value; }

Expand All @@ -52,11 +80,13 @@ public enum ControlState

private bool applyCalled = false;
private int lastApply = 0;
public int LastApply {
get {
public int LastApply
{
get
{
if (!applyCalled)
{
throw new Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException("Apply was not called");
throw new AssertFailedException("Apply was not called");
}
return lastApply;
}
Expand Down
17 changes: 16 additions & 1 deletion GLedApiDotNetTests/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace GLedApiDotNetTests
{
internal class TestHelper
public class TestHelper
{
public static void AssertLedSettingsEqual(byte[] expected, byte[] actual)
{
Expand Down Expand Up @@ -70,5 +70,20 @@ public static void AssertLedSettingsEqual(byte[] expected, byte[] actual)
Assert.AreEqual(expected[14], actual[14], "Offset 14, CtrlVal0");
Assert.AreEqual(expected[15], actual[15], "Offset 15, CtrlVal1");
}

public static void AssertAllLeds(GLedApiv1_0_0Mock mock, byte[] expected, int ledCount)
{
Assert.AreEqual(ledCount * 16, mock.ConfiguredLeds.Length);

byte[] part = new byte[16];

for (int i = 0; i < ledCount; i++)
{
Array.Copy(mock.ConfiguredLeds, i * 16, part, 0, 16);
TestHelper.AssertLedSettingsEqual(expected, part);
}

Assert.AreEqual(-1, mock.LastApply, "Expect applied");
}
}
}
54 changes: 54 additions & 0 deletions GLedApiDotNetTests/Tests/LedSettingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,60 @@ public class SettingByteArrays
7, // CtrlVal0
0 }; // CtrlVal1

public static readonly byte[] ColorCycleA_1s = {
0x00, // Reserved0
3, // LedMode
100, // MaxBrightness
0, // MinBrightness
0x00, // dwColor BB
0x00, // dwColor GG
0x00, // dwColor RR
0x00, // dwColor WW
0x64, // wTime0
0x00, // wTime0
0, // wTime1
0, // wTime1
0, // wTime2
0, // wTime2
7, // CtrlVal0
0 }; // CtrlVal1

public static readonly byte[] ColorCycleA_4s = {
0x00, // Reserved0
3, // LedMode
100, // MaxBrightness
0, // MinBrightness
0x00, // dwColor BB
0x00, // dwColor GG
0x00, // dwColor RR
0x00, // dwColor WW
0x90, // wTime0
0x01, // wTime0
0, // wTime1
0, // wTime1
0, // wTime2
0, // wTime2
7, // CtrlVal0
0 }; // CtrlVal1

public static readonly byte[] ColorCycleA_500ms = {
0x00, // Reserved0
3, // LedMode
100, // MaxBrightness
0, // MinBrightness
0x00, // dwColor BB
0x00, // dwColor GG
0x00, // dwColor RR
0x00, // dwColor WW
0x32, // wTime0
0x00, // wTime0
0, // wTime1
0, // wTime1
0, // wTime2
0, // wTime2
7, // CtrlVal0
0 }; // CtrlVal1

public static readonly byte[] ColorCycleB = {
0x00, // Reserved0
3, // LedMode
Expand Down
38 changes: 9 additions & 29 deletions GLedApiDotNetTests/Tests/RGBFusionMotherboardTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,46 +61,26 @@ public void SetWithoutSettings()
{
motherboard.Set();

Assert.AreEqual(GLedApiv1_0_0Mock.DEFAULT_MAXDIVISIONS * 16, mock.ConfiguredLeds.Length);
for (int i = 0; i < GLedApiv1_0_0Mock.DEFAULT_MAXDIVISIONS; i++)
{
byte[] part = new byte[16];
Array.Copy(mock.ConfiguredLeds, i * 16, part, 0, 16);
TestHelper.AssertLedSettingsEqual(LedSettingTests.SettingByteArrays.Off, part);
}

// Ensure the LEDs got set
Assert.AreEqual(-1, mock.LastApply);
TestHelper.AssertAllLeds(mock,
LedSettingTests.SettingByteArrays.Off,
GLedApiv1_0_0Mock.DEFAULT_MAXDIVISIONS);
}

[TestMethod]
public void SetAll()
{
motherboard.SetAll(new StaticLedSetting(Color.Red, 50));

Assert.AreEqual(GLedApiv1_0_0Mock.DEFAULT_MAXDIVISIONS * 16, mock.ConfiguredLeds.Length);
for (int i = 0; i < GLedApiv1_0_0Mock.DEFAULT_MAXDIVISIONS; i++)
{
byte[] part = new byte[16];
Array.Copy(mock.ConfiguredLeds, i * 16, part, 0, 16);
TestHelper.AssertLedSettingsEqual(LedSettingTests.SettingByteArrays.StaticRed50, part);
}

// Ensure the LEDs got set
Assert.AreEqual(-1, mock.LastApply);
TestHelper.AssertAllLeds(mock,
LedSettingTests.SettingByteArrays.StaticRed50,
GLedApiv1_0_0Mock.DEFAULT_MAXDIVISIONS);

// Verify again
motherboard.Set();

Assert.AreEqual(GLedApiv1_0_0Mock.DEFAULT_MAXDIVISIONS * 16, mock.ConfiguredLeds.Length);
for (int i = 0; i < GLedApiv1_0_0Mock.DEFAULT_MAXDIVISIONS; i++)
{
byte[] part = new byte[16];
Array.Copy(mock.ConfiguredLeds, i * 16, part, 0, 16);
TestHelper.AssertLedSettingsEqual(LedSettingTests.SettingByteArrays.StaticRed50, part);
}

Assert.AreEqual(-1, mock.LastApply);
TestHelper.AssertAllLeds(mock,
LedSettingTests.SettingByteArrays.StaticRed50,
GLedApiv1_0_0Mock.DEFAULT_MAXDIVISIONS);
}

[DataRow(0, 1)]
Expand Down
8 changes: 7 additions & 1 deletion RGBFusionTool.sln
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2026
VisualStudioVersion = 15.0.27130.2027
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RGBFusionTool", "RGBFusionTool\RGBFusionTool.csproj", "{0050F000-3212-4E50-81C5-8B6D068F2E62}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GLedApiDotNet", "GLedApiDotNet\GLedApiDotNet.csproj", "{8F12301F-EC86-4A76-A6A1-AE3EE0A2945B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GLedApiDotNetTests", "GLedApiDotNetTests\GLedApiDotNetTests.csproj", "{0B2F3C9F-50A2-4F94-B6B3-6A2418E7DA2F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RGBFusionToolTests", "RGBFusionToolTests\RGBFusionToolTests.csproj", "{FD3F35DA-41D4-48B3-AF52-E5F4A2CAC01B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -27,6 +29,10 @@ Global
{0B2F3C9F-50A2-4F94-B6B3-6A2418E7DA2F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0B2F3C9F-50A2-4F94-B6B3-6A2418E7DA2F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0B2F3C9F-50A2-4F94-B6B3-6A2418E7DA2F}.Release|Any CPU.Build.0 = Release|Any CPU
{FD3F35DA-41D4-48B3-AF52-E5F4A2CAC01B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FD3F35DA-41D4-48B3-AF52-E5F4A2CAC01B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FD3F35DA-41D4-48B3-AF52-E5F4A2CAC01B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FD3F35DA-41D4-48B3-AF52-E5F4A2CAC01B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
110 changes: 110 additions & 0 deletions RGBFusionTool/Application.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright (C) 2018 Tyler Szabo
//
// This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

using GLedApiDotNet;
using GLedApiDotNet.LedSettings;
using System;
using System.IO;
using System.Drawing;
using Mono.Options;

namespace RGBFusionTool
{
public class Application
{
IRGBFusionMotherboard motherboardLEDs;
TextWriter stdout;
TextWriter stderr;

public Application(IRGBFusionMotherboard motherboardLEDs, TextWriter stdout, TextWriter stderr)
{
this.motherboardLEDs = motherboardLEDs;
this.stdout = stdout;
this.stderr = stderr;
}

static void ShowHelp(OptionSet options, TextWriter o)
{
o.WriteLine(string.Format("Usage: {0} [OPTION]...\nSet RGB Fusion motherboard LEDs\n\nOptions:", AppDomain.CurrentDomain.FriendlyName));
options.WriteOptionDescriptions(o);
}

public void Main(string[] args)
{
int opt_Verbose = 0;
string opt_Color = null;
string opt_ColorCycle = null;
bool flag_DoCycle = false;
bool flag_Help = false;
string opt_Brightness = null;

OptionSet options = new OptionSet
{
{"v|verbose", v => opt_Verbose++ },

{"c|color|static=", "set static color", v => opt_Color = v },
{"cycle|colorcycle:", "cycle colors, changing color every {SECONDS}", v => { flag_DoCycle = true; opt_ColorCycle = v; } },
{"b|brightness=", "brightness (0-100)", v => opt_Brightness = v },

{"?|h|help", "show help and exit", v => flag_Help = true }
};

try
{
byte brightness = 100;
LedSetting setting = null;

options.Parse(args);

if (flag_Help)
{
ShowHelp(options, stdout);
return;
}

if (!string.IsNullOrWhiteSpace(opt_Brightness))
{
brightness = byte.Parse(opt_Brightness);
}

if (flag_DoCycle)
{
TimeSpan cycleTime = TimeSpan.FromSeconds(1);
if (!string.IsNullOrWhiteSpace(opt_ColorCycle))
{
cycleTime = TimeSpan.FromSeconds(Double.Parse(opt_ColorCycle));
}
if (opt_Verbose > 0) { stdout.WriteLine("Color cycle, rotating every {0} seconds", cycleTime.TotalSeconds); }
setting = new ColorCycleLedSetting(brightness, 0, cycleTime);
}
else if (!string.IsNullOrEmpty(opt_Color))
{
Color realColor = Color.FromName(opt_Color.Trim());
if (realColor.A == 0)
{
realColor = Color.FromArgb(0xff, Color.FromArgb(Int32.Parse(opt_Color, System.Globalization.NumberStyles.HexNumber)));
}
if (opt_Verbose > 0) { stdout.WriteLine("Static color: {0}", realColor.ToString()); }
setting = new StaticLedSetting(realColor, brightness);
}

if (setting != null)
{
motherboardLEDs.SetAll(setting);
}
}
catch (Exception e)
{
ShowHelp(options, stderr);
stderr.WriteLine("Error: {0}", e.Message);
throw;
}
return;
}
}
}
Loading

0 comments on commit 11cf0bc

Please sign in to comment.