GH-373: Permissions Must Be Invoked on MainThread else Throw Exception (#487)

* Throw permission exception if not on main thread when we need to request.

* Add tests for not main thread permissions
This commit is contained in:
James Montemagno 2018-08-24 07:18:14 -07:00 коммит произвёл Jonathan Dick
Родитель ca6bd84895
Коммит fc17df9e61
2 изменённых файлов: 16 добавлений и 2 удалений

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

@ -37,5 +37,14 @@ namespace DeviceTests
Assert.Equal(expectedStatus, status);
}
[Fact]
public async Task Request_NotMainThread()
{
await Task.Run(async () =>
{
await Assert.ThrowsAsync<PermissionException>(() => Permissions.RequestAsync(PermissionType.LocationWhenInUse));
});
}
}
}

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

@ -12,8 +12,13 @@ namespace Xamarin.Essentials
internal static Task<PermissionStatus> CheckStatusAsync(PermissionType permission) =>
PlatformCheckStatusAsync(permission);
internal static Task<PermissionStatus> RequestAsync(PermissionType permission) =>
PlatformRequestAsync(permission);
internal static Task<PermissionStatus> RequestAsync(PermissionType permission)
{
if (!MainThread.IsMainThread)
throw new PermissionException("Permission request must be invoked on main thread.");
return PlatformRequestAsync(permission);
}
internal static async Task RequireAsync(PermissionType permission)
{