GH-166 Fix UWP tests and Cleanup Battery API (#169)

* Only run sensor tests when they exist on UWP. Add new not present on UWP for batter

* Ensure no divide by 0 issues

* Fix up docs
This commit is contained in:
James Montemagno 2018-04-10 14:12:39 -07:00 коммит произвёл Jonathan Dick
Родитель 083d1f65a7
Коммит 45d2ed6063
14 изменённых файлов: 50 добавлений и 6 удалений

Просмотреть файл

@ -8,6 +8,9 @@ namespace DeviceTests
{
bool TestSupported =>
DeviceInfo.Platform == DeviceInfo.Platforms.Android ||
#if WINDOWS_UWP
(Windows.Devices.Sensors.Accelerometer.GetDefault() != null) ||
#endif
(DeviceInfo.DeviceType == DeviceType.Physical && DeviceInfo.Platform == DeviceInfo.Platforms.iOS);
public Accelerometer_Tests()

Просмотреть файл

@ -13,7 +13,10 @@ namespace DeviceTests
#endif
public void Charge_Level()
{
Assert.InRange(Battery.ChargeLevel, 0.01, 100.0);
if (Battery.State == BatteryState.Unknown || Battery.State == BatteryState.NotPresent)
Assert.Equal(-1.0, Battery.ChargeLevel);
else
Assert.InRange(Battery.ChargeLevel, 0, 1.0);
}
[Fact]
@ -22,7 +25,7 @@ namespace DeviceTests
#endif
public void Charge_State()
{
Assert.NotEqual(BatteryState.Unknown, Battery.State);
Assert.NotEqual(BatteryState.Unknown, Battery.State);
}
[Fact]

Просмотреть файл

@ -8,7 +8,9 @@ namespace DeviceTests
{
bool TestSupported =>
DeviceInfo.Platform == DeviceInfo.Platforms.Android ||
DeviceInfo.Platform == DeviceInfo.Platforms.UWP ||
#if WINDOWS_UWP
(Windows.Devices.Sensors.Compass.GetDefault() != null) ||
#endif
(DeviceInfo.DeviceType == DeviceType.Physical && DeviceInfo.Platform == DeviceInfo.Platforms.iOS);
public Compass_Tests()

Просмотреть файл

@ -12,6 +12,8 @@ namespace DeviceTests
[Trait(Traits.InteractionType, Traits.InteractionTypes.Human)]
public async Task Compose_Shows_New_Window()
{
if (DeviceInfo.Platform == DeviceInfo.Platforms.UWP)
return;
await Email.ComposeAsync();
}
@ -19,6 +21,9 @@ namespace DeviceTests
[Trait(Traits.InteractionType, Traits.InteractionTypes.Human)]
public async Task Compose_With_Message_Shows_New_Window()
{
if (DeviceInfo.Platform == DeviceInfo.Platforms.UWP)
return;
var email = new EmailMessage
{
Subject = "Hello World!",

Просмотреть файл

@ -8,6 +8,9 @@ namespace DeviceTests
{
bool TestSupported =>
(DeviceInfo.DeviceType == DeviceType.Physical && DeviceInfo.Platform == DeviceInfo.Platforms.Android) ||
#if WINDOWS_UWP
(Windows.Devices.Sensors.Gyrometer.GetDefault() != null) ||
#endif
(DeviceInfo.DeviceType == DeviceType.Physical && DeviceInfo.Platform == DeviceInfo.Platforms.iOS);
public Gyroscope_Tests()

Просмотреть файл

@ -9,6 +9,9 @@ namespace DeviceTests
{
bool TestSupported =>
DeviceInfo.Platform == DeviceInfo.Platforms.Android ||
#if WINDOWS_UWP
(Windows.Devices.Sensors.Magnetometer.GetDefault() != null) ||
#endif
(DeviceInfo.DeviceType == DeviceType.Physical && DeviceInfo.Platform == DeviceInfo.Platforms.iOS);
public Magnetometer_Tests()

Просмотреть файл

@ -46,6 +46,9 @@ namespace Xamarin.Essentials
var level = battery.GetIntExtra(BatteryManager.ExtraLevel, -1);
var scale = battery.GetIntExtra(BatteryManager.ExtraScale, -1);
if (scale <= 0)
return -1;
return (double)level / (double)scale;
}
}

Просмотреть файл

@ -69,7 +69,8 @@ namespace Xamarin.Essentials
Charging,
Discharging,
Full,
NotCharging
NotCharging,
NotPresent
}
public enum BatteryPowerSource

Просмотреть файл

@ -47,7 +47,7 @@
return BatteryState.Full;
return BatteryState.NotCharging;
case Windows.System.Power.BatteryStatus.NotPresent:
return BatteryState.Unknown;
return BatteryState.NotPresent;
}
if (ChargeLevel >= 1.0)
@ -62,7 +62,7 @@
get
{
var currentStatus = State;
if (currentStatus == BatteryState.Full || currentStatus == BatteryState.Charging)
if (currentStatus == BatteryState.Full || currentStatus == BatteryState.Charging || currentState == BatteryState.NotPresent)
return BatteryPowerSource.Ac;
if (currentStatus == BatteryState.Unknown)

Просмотреть файл

@ -46,6 +46,7 @@
<Member Id="F:Xamarin.Essentials.BatteryState.Discharging" />
<Member Id="F:Xamarin.Essentials.BatteryState.Full" />
<Member Id="F:Xamarin.Essentials.BatteryState.NotCharging" />
<Member Id="F:Xamarin.Essentials.BatteryState.NotPresent" />
<Member Id="F:Xamarin.Essentials.BatteryState.Unknown" />
</Type>
<Type Name="Xamarin.Essentials.Browser" Id="T:Xamarin.Essentials.Browser">

Просмотреть файл

@ -46,6 +46,7 @@
<Member Id="F:Xamarin.Essentials.BatteryState.Discharging" />
<Member Id="F:Xamarin.Essentials.BatteryState.Full" />
<Member Id="F:Xamarin.Essentials.BatteryState.NotCharging" />
<Member Id="F:Xamarin.Essentials.BatteryState.NotPresent" />
<Member Id="F:Xamarin.Essentials.BatteryState.Unknown" />
</Type>
<Type Name="Xamarin.Essentials.Browser" Id="T:Xamarin.Essentials.Browser">

Просмотреть файл

@ -46,6 +46,7 @@
<Member Id="F:Xamarin.Essentials.BatteryState.Discharging" />
<Member Id="F:Xamarin.Essentials.BatteryState.Full" />
<Member Id="F:Xamarin.Essentials.BatteryState.NotCharging" />
<Member Id="F:Xamarin.Essentials.BatteryState.NotPresent" />
<Member Id="F:Xamarin.Essentials.BatteryState.Unknown" />
</Type>
<Type Name="Xamarin.Essentials.Browser" Id="T:Xamarin.Essentials.Browser">

Просмотреть файл

@ -46,6 +46,7 @@
<Member Id="F:Xamarin.Essentials.BatteryState.Discharging" />
<Member Id="F:Xamarin.Essentials.BatteryState.Full" />
<Member Id="F:Xamarin.Essentials.BatteryState.NotCharging" />
<Member Id="F:Xamarin.Essentials.BatteryState.NotPresent" />
<Member Id="F:Xamarin.Essentials.BatteryState.Unknown" />
</Type>
<Type Name="Xamarin.Essentials.Browser" Id="T:Xamarin.Essentials.Browser">

Просмотреть файл

@ -84,6 +84,23 @@
<summary>Battery is not charging or discharging, but in an inbetween state.</summary>
</Docs>
</Member>
<Member MemberName="NotPresent">
<MemberSignature Language="C#" Value="NotPresent" />
<MemberSignature Language="ILAsm" Value=".field public static literal valuetype Xamarin.Essentials.BatteryState NotPresent = int32(5)" />
<MemberSignature Language="DocId" Value="F:Xamarin.Essentials.BatteryState.NotPresent" />
<MemberType>Field</MemberType>
<AssemblyInfo>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<AssemblyName>Xamarin.Essentials</AssemblyName>
</AssemblyInfo>
<ReturnValue>
<ReturnType>Xamarin.Essentials.BatteryState</ReturnType>
</ReturnValue>
<MemberValue>5</MemberValue>
<Docs>
<summary>Battery does not exist on the device.</summary>
</Docs>
</Member>
<Member MemberName="Unknown">
<MemberSignature Language="C#" Value="Unknown" />
<MemberSignature Language="ILAsm" Value=".field public static literal valuetype Xamarin.Essentials.BatteryState Unknown = int32(0)" />