Skip to content

Conversation

Arlodotexe
Copy link
Member

@Arlodotexe Arlodotexe commented Jun 14, 2022

Background

Closes #143

This PR adds the [LabsUITestMethod] attribute and source generator. When the source generator sees this attribute, it will:

  • Generate a test method that always invokes on the UI thread
  • If needed, pulls type info from the constructor, and provides a control instance.
  • Works with both synchronous and asynchronous method.

Basic usage

Synchronous, with control:

[LabsUITestMethod]
public void MyControl_TestSomething(MyControl testControl)
{
    var scrollViewer = testControl.FindDescendant<ScrollViewer>();

    Assert.AreEqual(200, scrollViewer.HorizontalOffset);
}

Synchronous, without control:

[LabsUITestMethod]
public void SimpleTest()
{
    // Runs on the UI thread
    Assert.AreEqual(1, 1);
}

Asynchronous, with control:

[LabsUITestMethod]
public async Task MyControl_TestSomething(MyControl testControl)
{
    var contentDialog = testControl.FindDescendant<ContentDialog>();

    await contentDialog.ShowAsync();

    Assert.IsTrue(contentDialog.IsPrimaryButtonEnabled);
}

Asynchronous, without control:

[LabsUITestMethod]
public async Task PropertySizer_TestChangeBinding()
{
    // Runs on the UI thread
    await Task.Delay(1000);
    Assert.AreEqual(1, 1);
}

@michael-hawker michael-hawker added testing 🏗 build 🔥 dev loop ➰ For issues that impact the core dev-loop of building experiments templating source generator ⚙️ labels Jun 14, 2022
@michael-hawker michael-hawker added this to the Initial Release milestone Jun 14, 2022
@michael-hawker
Copy link
Member

@Arlodotexe should we add an example test to the template as well to show this being used for folks?

Copy link
Member

@michael-hawker michael-hawker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few small things and some suggestions for more tests to shore things up.

/// <returns>When UI is loaded.</returns>
protected Task SetTestContentAsync(FrameworkElement content)
{
return App.DispatcherQueue.EnqueueAsync(() =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, also really confused why the extension isn't working as it's a bit odd for us to re-implement this. Can you sync with @Sergio0694 quick sometime for 10 minutes on a call and trying and diagnose/go through the issue?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll set that up in the morning, see if we can pick out exactly what went wrong. I can't repro the issue yet, but we can use the code from before this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it only this overload taking a Func<Task<T>> that's causing issues, or all of them?

@michael-hawker
Copy link
Member

Looks like a test failure isn't getting caught by the MSTest infrastructure at the moment now with the new attributed method? We should have a case to try where use the attribute to expect a known exception and then throw that exception and make sure that works...

@michael-hawker
Copy link
Member

Hmm, the error may be getting eaten in the CI as well, as I just see the general abort message as well: https://github.com/CommunityToolkit/Labs-Windows/runs/6914398636?check_suite_focus=true#step:15:227 - hopefully we can fix the error bubbling, and then get more info out of the CI about the failure... though I'll be sad if we can't run the input injection APIs in the CI... 😥

@Arlodotexe
Copy link
Member Author

Looks like a test failure isn't getting caught by the MSTest infrastructure at the moment now with the new attributed method? We should have a case to try where use the attribute to expect a known exception and then throw that exception and make sure that works...

That's odd. I suppose we should add this to #160, but that means this PR can't be closed until that one is.

@Arlodotexe
Copy link
Member Author

@michael-hawker Sergio and I weren't able to reproduce the issue with the EnqueueAsync extension in the toolkit. It's been reverted and test now succeed and fail as expected. Feel free to rebase your branch as needed.

@Sergio0694
Copy link
Member

> Test fail for no reason
> Write a workaround
> Test pass
> Remove workaround
> Test pass
> *shrug*
> LGTM!

@michael-hawker
Copy link
Member

@Arlodotexe don't think the build ran as there's conflicts to resolve?

Copy link
Member

@michael-hawker michael-hawker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, will see how the CI does (and I'll try on my other branch to see what happens too). Just a few file-scoped namespaces that were missed.

@Arlodotexe
Copy link
Member Author

@Arlodotexe don't think the build ran as there's conflicts to resolve?

I ran into some issues with Visual Studio refusing to build after resolving conflicts, complaining about missing types. Should be good now.

@michael-hawker
Copy link
Member

My other PR helped to exercise this a bit more too. I'll do a quick check locally, but then I think we'll be good!

Copy link
Member

@michael-hawker michael-hawker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay, I see the failures now in VS! 🎉🎉🎉

Seems to be working great. Awesome job! Thanks @Arlodotexe!

@michael-hawker michael-hawker merged commit 9e17ce8 into main Jun 22, 2022
@delete-merged-branch delete-merged-branch bot deleted the feature/ui-control-test-generator branch June 22, 2022 22:46
Martin1994 pushed a commit to Martin1994/Labs-Windows that referenced this pull request Sep 2, 2023
…lkit#156)

* Created UIControlTestMethod generator + unit tests

* Added new source generators to template

* Migrated existing tests to new source generator attribute

* Refactored to pull control instance directly from method params. Improved control lifetime.

* Cleanup outdated comments

* Added mandatory content cleanup for UI tests

* Fixed diagnostic ID to match project name initials

* Fixed source generator to only check public constructors for LUITM0001

* Fixed test setup/cleanup invoking on the wrong thread

* Added new source generator to individual project solutions

* Reverted to toolkit extension for dispatcher EnqueueAsync

* Remove whitespace

* Fixed non file-scoped namespaces

* Added missing file header

* Removed unused using directives
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build 🔥 dev loop ➰ For issues that impact the core dev-loop of building experiments source generator ⚙️ templating testing 🏗
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MSTest Source Generator: UITestMethodWithXamlPage
3 participants