Skip to content
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

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

Closed
kinex opened this issue Jul 23, 2021 · 1 comment
Labels
Milestone

Comments

@kinex
Copy link

kinex commented Jul 23, 2021

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;
                    .
                    .
                    .
@kinex kinex added the bug 🐛 Something isn't working label Jul 23, 2021
@ghost ghost added the needs triage 🔍 label Jul 23, 2021
@ghost
Copy link

ghost commented Jul 23, 2021

Hello kinex, thank you for opening an issue with us!

I have automatically added a "needs triage" label to help get things started. Our team will analyze and investigate the issue, and escalate it to the relevant team if possible. Other community members may also look into the issue and provide feedback 🙌

@ghost ghost added the In-PR 🚀 label Jul 29, 2021
@shweaver-MSFT shweaver-MSFT added this to the 7.1.0 milestone Jul 29, 2021
@ghost ghost added Completed 🔥 and removed In-PR 🚀 labels Jul 30, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Nov 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants