Skip to content

Commit 774d46b

Browse files
authored
Merge pull request #47 from nathanpovo/defaultMulticastIpAddress
2 parents 45360c7 + ab90f95 commit 774d46b

File tree

8 files changed

+766
-393
lines changed

8 files changed

+766
-393
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Xunit;
7+
using YeelightAPI.Models;
8+
9+
namespace YeelightAPI.UnitTests
10+
{
11+
public class DeviceTests
12+
{
13+
14+
private readonly Xunit.Abstractions.ITestOutputHelper _output;
15+
16+
public DeviceTests(Xunit.Abstractions.ITestOutputHelper testOutputHelper)
17+
{
18+
this._output = testOutputHelper;
19+
}
20+
21+
#region TESTS
22+
23+
[Fact]
24+
public async Task Device_should_turnon_and_turnoff()
25+
{
26+
using (Device testedDevice = await GetRandomConnectedDevice())
27+
{
28+
await testedDevice.TurnOn();
29+
Assert.Equal("on", await testedDevice.GetProp(PROPERTIES.power));
30+
await testedDevice.TurnOff();
31+
Assert.Equal("off", await testedDevice.GetProp(PROPERTIES.power));
32+
}
33+
}
34+
35+
[Fact]
36+
public async Task Device_should_change_rgb_color_to_red() => await DoWithRandomDevice(async (device) =>
37+
{
38+
await device.SetRGBColor(255, 0, 0);
39+
Assert.Equal((255 << 16).ToString(), await device.GetProp(PROPERTIES.rgb));
40+
}, METHODS.SetRGBColor);
41+
42+
[Fact]
43+
public async Task Device_should_change_hsv_color_to_red() => await DoWithRandomDevice(async (device) =>
44+
{
45+
await device.SetHSVColor(0, 100);
46+
Assert.Equal((255 << 16).ToString(), await device.GetProp(PROPERTIES.rgb));
47+
48+
}, METHODS.SetHSVColor);
49+
50+
[Fact]
51+
public async Task Device_should_change_brightness() => await DoWithRandomDevice(async (device) =>
52+
{
53+
await device.SetBrightness(52);
54+
Assert.Equal(52, await device.GetProp(PROPERTIES.bright));
55+
56+
}, METHODS.SetBrightness);
57+
58+
[Fact]
59+
public async Task Device_should_change_colortemperature() => await DoWithRandomDevice(async (device) =>
60+
{
61+
await device.SetColorTemperature(4654);
62+
Assert.Equal(4654, await device.GetProp(PROPERTIES.ct));
63+
64+
}, METHODS.SetBrightness);
65+
66+
#endregion TESTS
67+
68+
#region PRIVATE
69+
70+
private async Task DoWithRandomDevice(Action<Device> a, METHODS? supportedMethod = null)
71+
{
72+
using (Device testedDevice = await GetRandomConnectedDevice(supportedMethod))
73+
{
74+
await testedDevice.TurnOn();
75+
76+
a?.Invoke(testedDevice);
77+
78+
await testedDevice.TurnOff();
79+
}
80+
}
81+
82+
private async Task<Device> GetRandomConnectedDevice(METHODS? supportedMethod = null)
83+
{
84+
List<Device> devices = (await DeviceLocator.DiscoverAsync()).Where(d => !supportedMethod.HasValue || d.SupportedOperations.Contains(supportedMethod.Value)).ToList();
85+
86+
Assert.NotEmpty(devices);
87+
88+
int randomIndex = new Random().Next(0, devices.Count);
89+
Device d = devices.ElementAt(randomIndex);
90+
_output.WriteLine($"Used device : {d}");
91+
await d.Connect();
92+
return d;
93+
}
94+
95+
#endregion PRIVATE
96+
}
97+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
using Microsoft.Extensions.Configuration;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.ComponentModel;
5+
using System.Threading.Tasks;
6+
using Xunit;
7+
using System.Linq;
8+
using System.Diagnostics;
9+
10+
namespace YeelightAPI.UnitTests
11+
{
12+
public class DiscoveryTests
13+
{
14+
private readonly IConfigurationRoot _config;
15+
16+
public DiscoveryTests()
17+
{
18+
this._config = new ConfigurationBuilder()
19+
.AddJsonFile("config.json")
20+
.Build();
21+
}
22+
23+
#region TESTS
24+
25+
[Fact]
26+
public async Task Discovery_enumerate_async_should_find_devices()
27+
{
28+
int expectedDevicesCount = GetConfig<int>("discovery_devices_expected");
29+
int count = 0;
30+
await foreach(Device device in DeviceLocator.EnumerateDevicesAsync())
31+
{
32+
++count;
33+
}
34+
35+
Assert.Equal(expectedDevicesCount, count);
36+
}
37+
38+
[Fact]
39+
public async Task Discovery_should_find_devices()
40+
{
41+
int expectedDevicesCount = GetConfig<int>("discovery_devices_expected");
42+
var devices = (await DeviceLocator.DiscoverAsync()).ToList();
43+
44+
Assert.Equal(expectedDevicesCount, devices?.Count);
45+
}
46+
47+
[Fact]
48+
public async Task Discovery_should_find_devices_with_multiple_tries()
49+
{
50+
int expectedDevicesCount = GetConfig<int>("discovery_devices_expected");
51+
52+
DeviceLocator.MaxRetryCount = 3;
53+
var devices = (await DeviceLocator.DiscoverAsync()).ToList();
54+
DeviceLocator.MaxRetryCount = 1;
55+
56+
Assert.Equal(expectedDevicesCount, devices?.Count);
57+
}
58+
59+
[Fact]
60+
public async Task Discovery_should_throw_when_using_wrong_multicast_address()
61+
{
62+
int expectedDevicesCount = GetConfig<int>("discovery_devices_expected");
63+
string initialMulticastAddress = DeviceLocator.DefaultMulticastIPAddress;
64+
DeviceLocator.DefaultMulticastIPAddress = "foo";
65+
66+
_ = await Assert.ThrowsAsync<FormatException>(async () =>
67+
{
68+
_ = (await DeviceLocator.DiscoverAsync()).ToList();
69+
});
70+
71+
//reset to default to ensure next tests don't crash
72+
DeviceLocator.DefaultMulticastIPAddress = initialMulticastAddress;
73+
}
74+
75+
[Fact]
76+
public async Task Discovery_should_find_devices_on_all_multicast_addresses()
77+
{
78+
int expectedDevicesCount = GetConfig<int>("discovery_devices_expected");
79+
DeviceLocator.UseAllAvailableMulticastAddresses = true;
80+
var devices = (await DeviceLocator.DiscoverAsync()).ToList();
81+
82+
Assert.Equal(expectedDevicesCount, devices?.Count);
83+
}
84+
85+
[Fact]
86+
public async Task Discovery_should_not_last_long()
87+
{
88+
Stopwatch sw = Stopwatch.StartNew();
89+
_ = await DeviceLocator.DiscoverAsync();
90+
sw.Stop();
91+
92+
Assert.InRange(sw.ElapsedMilliseconds, 0, 1500);
93+
}
94+
95+
#endregion TESTS
96+
97+
#region PRIVATE METHODS
98+
99+
private T GetConfig<T>(string key)
100+
{
101+
Type t = typeof(T);
102+
var value = _config[key];
103+
104+
TypeConverter converter = TypeDescriptor.GetConverter(t);
105+
try
106+
//if (value != null && converter.CanConvertTo(t) && converter.CanConvertFrom(typeof(string)))
107+
{
108+
return (T)converter.ConvertFromString(value);
109+
}
110+
catch (Exception ex)
111+
{
112+
throw new Exception($"Cannot convert '{value}' (key: {key}) to {t}", ex);
113+
}
114+
}
115+
116+
#endregion PRIVATE METHODS
117+
}
118+
}

YeelightAPI.UnitTests/YeelightUnitTest.cs

Lines changed: 0 additions & 171 deletions
This file was deleted.

0 commit comments

Comments
 (0)