|
| 1 | +using System.Threading.Tasks; |
| 2 | +using Microsoft.Maui.ApplicationModel; |
| 3 | +using Xunit; |
| 4 | + |
| 5 | +namespace Microsoft.Maui.Essentials.DeviceTests; |
| 6 | + |
| 7 | +[Category("Permissions")] |
| 8 | +public class Microphone_Tests |
| 9 | +{ |
| 10 | + [Fact] |
| 11 | + [Trait(Traits.InteractionType, Traits.InteractionTypes.Human)] |
| 12 | + public async Task Request_Microphone_Denied_Returns_Denied() |
| 13 | + { |
| 14 | + // If microphone permission was previously granted, the OS won't show a new prompt. |
| 15 | + // In that case we can't exercise the prompt flow, so skip the test. |
| 16 | + var initial = await Permissions.CheckStatusAsync<Permissions.Microphone>(); |
| 17 | + if (initial == PermissionStatus.Granted) |
| 18 | + return; // nothing to prompt; skip |
| 19 | + |
| 20 | + // This test requires human interaction: When prompted, choose Deny for microphone access |
| 21 | + var status = await MainThread.InvokeOnMainThreadAsync(Permissions.RequestAsync<Permissions.Microphone>); |
| 22 | + Assert.Equal(PermissionStatus.Denied, status); |
| 23 | + |
| 24 | + // And subsequent checks should reflect Denied |
| 25 | + var check = await Permissions.CheckStatusAsync<Permissions.Microphone>(); |
| 26 | + Assert.Equal(PermissionStatus.Denied, check); |
| 27 | + } |
| 28 | + |
| 29 | + [Fact] |
| 30 | + [Trait(Traits.InteractionType, Traits.InteractionTypes.Human)] |
| 31 | + public async Task Request_Microphone_Allowed_Returns_Granted() |
| 32 | + { |
| 33 | + // If microphone permission was previously granted, the OS won't show a new prompt. |
| 34 | + // In that case we can't exercise the prompt flow, so skip the test. |
| 35 | + var initial = await Permissions.CheckStatusAsync<Permissions.Microphone>(); |
| 36 | + if (initial == PermissionStatus.Granted) |
| 37 | + return; // nothing to prompt; skip |
| 38 | + |
| 39 | + // This test requires human interaction: When prompted, choose Allow for microphone access |
| 40 | + var status = await MainThread.InvokeOnMainThreadAsync(Permissions.RequestAsync<Permissions.Microphone>); |
| 41 | + Assert.Equal(PermissionStatus.Granted, status); |
| 42 | + |
| 43 | + // And subsequent checks should reflect Granted |
| 44 | + var check = await Permissions.CheckStatusAsync<Permissions.Microphone>(); |
| 45 | + Assert.Equal(PermissionStatus.Granted, check); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + [Trait(Traits.InteractionType, Traits.InteractionTypes.Human)] |
| 50 | + public async Task Request_Microphone_Granted_Then_RequestAgain_Returns_Granted() |
| 51 | + { |
| 52 | + // Ensure we have the Granted state first (may require human interaction on first request) |
| 53 | + var status = await Permissions.CheckStatusAsync<Permissions.Microphone>(); |
| 54 | + if (status != PermissionStatus.Granted) |
| 55 | + { |
| 56 | + // Human: choose Allow when prompted |
| 57 | + status = await MainThread.InvokeOnMainThreadAsync(Permissions.RequestAsync<Permissions.Microphone>); |
| 58 | + if (status != PermissionStatus.Granted) |
| 59 | + return; // Can't proceed with this test if user denied; treat as skip |
| 60 | + } |
| 61 | + |
| 62 | + // Second request should short-circuit and still return Granted without a new prompt |
| 63 | + var second = await MainThread.InvokeOnMainThreadAsync(Permissions.RequestAsync<Permissions.Microphone>); |
| 64 | + Assert.Equal(PermissionStatus.Granted, second); |
| 65 | + |
| 66 | + // Status check remains Granted |
| 67 | + var check = await Permissions.CheckStatusAsync<Permissions.Microphone>(); |
| 68 | + Assert.Equal(PermissionStatus.Granted, check); |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + [Trait(Traits.InteractionType, Traits.InteractionTypes.Human)] |
| 73 | + public async Task Request_Microphone_Denied_Then_RequestAgain_Returns_Denied() |
| 74 | + { |
| 75 | + // If already granted we can't meaningfully test denied flow again without application/OS settings reset |
| 76 | + var initial = await Permissions.CheckStatusAsync<Permissions.Microphone>(); |
| 77 | + if (initial == PermissionStatus.Granted) |
| 78 | + return; // skip |
| 79 | + |
| 80 | + // First request: Human choose Deny when prompted (if prompt appears) |
| 81 | + var first = await MainThread.InvokeOnMainThreadAsync(Permissions.RequestAsync<Permissions.Microphone>); |
| 82 | + if (first != PermissionStatus.Denied) |
| 83 | + return; // can't assert repeat deny path if user allowed or unknown |
| 84 | + |
| 85 | + // Second request should return Denied again (idempotent) and not elevate permission |
| 86 | + var second = await MainThread.InvokeOnMainThreadAsync(Permissions.RequestAsync<Permissions.Microphone>); |
| 87 | + Assert.Equal(PermissionStatus.Denied, second); |
| 88 | + |
| 89 | + var check = await Permissions.CheckStatusAsync<Permissions.Microphone>(); |
| 90 | + Assert.Equal(PermissionStatus.Denied, check); |
| 91 | + } |
| 92 | +} |
0 commit comments