-
Notifications
You must be signed in to change notification settings - Fork 2k
[Essentials] Use mean sea level altitude on Android API 34+ #35097
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
63444de
0d46844
753afe9
cd12808
93c354b
1e76470
d711d26
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| using System; | ||
| using Microsoft.Maui.Devices.Sensors; | ||
| using Xunit; | ||
| using AndroidLocation = Android.Locations.Location; | ||
|
|
||
| namespace Microsoft.Maui.Essentials.DeviceTests | ||
| { | ||
| [Category("Geolocation")] | ||
| public class Android_Geolocation_Tests | ||
| { | ||
| [Fact] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❌ Error — When This is precisely the failure the gate captured ( Fix: Drop the space, e.g. |
||
| public void ToLocation_NoAltitude_UsesUnspecifiedReferenceSystem() | ||
| { | ||
| var androidLocation = new AndroidLocation("test"); | ||
|
|
||
| var location = androidLocation.ToLocation(); | ||
|
|
||
| Assert.Null(location.Altitude); | ||
| Assert.Equal(AltitudeReferenceSystem.Unspecified, location.AltitudeReferenceSystem); | ||
| Assert.Null(location.VerticalAccuracy); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ToLocation_EllipsoidalAltitude_UsesEllipsoidReferenceSystem() | ||
| { | ||
| var androidLocation = new AndroidLocation("test") | ||
| { | ||
| Altitude = 123.45, | ||
| }; | ||
|
|
||
| if (OperatingSystem.IsAndroidVersionAtLeast(26)) | ||
| androidLocation.VerticalAccuracyMeters = 5.0f; | ||
|
|
||
| var location = androidLocation.ToLocation(); | ||
|
|
||
| Assert.Equal(123.45, location.Altitude); | ||
| Assert.Equal(AltitudeReferenceSystem.Ellipsoid, location.AltitudeReferenceSystem); | ||
|
|
||
| if (OperatingSystem.IsAndroidVersionAtLeast(26)) | ||
| Assert.Equal(5.0, location.VerticalAccuracy); | ||
| else | ||
| Assert.Null(location.VerticalAccuracy); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ToLocation_MslAltitude_UsesGeoidReferenceSystem() | ||
| { | ||
| var androidLocation = new AndroidLocation("test") | ||
| { | ||
| Altitude = 123.45, | ||
| }; | ||
|
|
||
| // Baseline: without MSL altitude set, we must get Ellipsoid on any API level. | ||
| // This guarantees an assertion runs even on pre-34 devices so the test cannot | ||
| // silently pass if the API-34 branch is accidentally taken or the fallback | ||
| // regresses. | ||
| var baseline = androidLocation.ToLocation(); | ||
| Assert.Equal(123.45, baseline.Altitude); | ||
| Assert.Equal(AltitudeReferenceSystem.Ellipsoid, baseline.AltitudeReferenceSystem); | ||
|
|
||
| // The remaining assertions exercise the API 34+ MSL path. MslAltitudeMeters is | ||
| // only meaningful on API 34+, so below that we stop after validating the | ||
| // fallback above. | ||
| if (!OperatingSystem.IsAndroidVersionAtLeast(34)) | ||
| return; | ||
|
|
||
| androidLocation.MslAltitudeMeters = 100.0; | ||
| androidLocation.MslAltitudeAccuracyMeters = 2.5f; | ||
| androidLocation.VerticalAccuracyMeters = 5.0f; // ellipsoidal accuracy, must NOT be surfaced | ||
|
|
||
| var location = androidLocation.ToLocation(); | ||
|
|
||
| Assert.Equal(100.0, location.Altitude); | ||
| Assert.Equal(AltitudeReferenceSystem.Geoid, location.AltitudeReferenceSystem); | ||
| // VerticalAccuracy must be paired with the chosen altitude reference system, | ||
| // so the MSL accuracy is used rather than the ellipsoidal one. | ||
| Assert.Equal(2.5, location.VerticalAccuracy); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ToLocation_MslAltitudeWithoutMslAccuracy_ReportsNullVerticalAccuracy() | ||
| { | ||
| var androidLocation = new AndroidLocation("test"); | ||
|
|
||
| // Baseline: a location with no altitude reports Unspecified on any API level. | ||
| // This keeps the test meaningful on pre-34 devices instead of silently passing. | ||
| var baseline = androidLocation.ToLocation(); | ||
| Assert.Null(baseline.Altitude); | ||
| Assert.Equal(AltitudeReferenceSystem.Unspecified, baseline.AltitudeReferenceSystem); | ||
| Assert.Null(baseline.VerticalAccuracy); | ||
|
|
||
| if (!OperatingSystem.IsAndroidVersionAtLeast(34)) | ||
| return; | ||
|
|
||
| // On API 34+, an MSL altitude without an MSL accuracy must NOT surface the | ||
| // ellipsoidal VerticalAccuracyMeters — they describe a different reference system. | ||
| androidLocation.MslAltitudeMeters = 100.0; | ||
| androidLocation.VerticalAccuracyMeters = 5.0f; | ||
|
|
||
| var location = androidLocation.ToLocation(); | ||
|
|
||
| Assert.Equal(100.0, location.Altitude); | ||
| Assert.Equal(AltitudeReferenceSystem.Geoid, location.AltitudeReferenceSystem); | ||
| Assert.Null(location.VerticalAccuracy); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void LocationCopyConstructor_PreservesAltitudeReferenceSystem() | ||
| { | ||
| var original = new Location(51.5, -0.1) | ||
| { | ||
| Altitude = 100.0, | ||
| AltitudeReferenceSystem = AltitudeReferenceSystem.Geoid, | ||
| VerticalAccuracy = 2.5 | ||
| }; | ||
|
|
||
| var copy = new Location(original); | ||
|
|
||
| Assert.Equal(original.Altitude, copy.Altitude); | ||
| Assert.Equal(original.AltitudeReferenceSystem, copy.AltitudeReferenceSystem); | ||
| Assert.Equal(original.VerticalAccuracy, copy.VerticalAccuracy); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Suggestion — release-note this behavior change.
Adding
AltitudeReferenceSystemto the copy constructor is correct (the previous omission was a latent bug — copies were silently coerced to the defaultUnspecified). Worth calling out in release notes alongside the AndroidEllipsoid → Unspecifiedchange so consumers know to re-check any code that comparedAltitudeReferenceSystemafter copying aLocation.