Skip to content

WindowsProvider.GetTokenAsync crashes in unit test and if called from a non-UI thread #132

@kinex

Description

@kinex

I have created an Unit Test App (UWP) referencing this library. The Unit Test app crashes to "Element not found" exception at this line in WindowsProvider.cs (method ShowAccountManagementPaneAsync):

pane = AccountsSettingsPane.GetForCurrentView();

The unit test app runs the tests with the following code:

    Microsoft.VisualStudio.TestPlatform.TestExecutor.UnitTestClient.CreateDefaultUI();
    Window.Current.Activate();
    Microsoft.VisualStudio.TestPlatform.TestExecutor.UnitTestClient.Run(e.Arguments);

I tried first wrapping the call Microsoft.VisualStudio.TestPlatform.TestExecutor.UnitTestClient.Run(e.Arguments); with CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync, but it didn't solve the issue.

So I finally fixed this in WindowsProvider.GetTokenAsync by ensuring that AuthenticateInteractiveAsync is called from UI thread. As the method name suggests, there will be UI shown in the method. So it must be called from UI thread only:

        public override async Task<string> GetTokenAsync(bool silentOnly = false)
        {
                    .
                    .
                    .
                    // Attempt to authenticate interactively.
                    var tcs = new TaskCompletionSource<WebTokenRequestResult>();
                    await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
                        Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
                        {
                            var result = await AuthenticateInteractiveAsync(_scopes);
                            tcs.SetResult(result);
                        });
                    authResult = await tcs.Task;
                    .
                    .
                    .

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions