Skip to content

Assert apartment state instead of using Clipboard in UI tests - #16270

Merged
Jakub Jareš (nohwnd) merged 1 commit into
mainfrom
assert-apartmentstate-in-uitest
Jul 20, 2026
Merged

Assert apartment state instead of using Clipboard in UI tests#16270
Jakub Jareš (nohwnd) merged 1 commit into
mainfrom
assert-apartmentstate-in-uitest

Conversation

@nohwnd

Copy link
Copy Markdown
Member

The apartment-state acceptance tests (ExecutionThreadApartmentStateTests) run SimpleTestProject3's UITestMethod and UITestWithSleep1, which called Clipboard.SetText as a stand-in for an STA-only API. On the shared CI agents that clipboard call intermittently throws ExternalException: Requested Clipboard operation did not succeed, and reddens the Windows integration test leg with a single flaky failure on UITestShouldPassIfApartmentStateIsSTA. It is a flake, not a regression, the same commit passes and fails across different builds.

Replace the clipboard call with Assert.AreEqual(ApartmentState.STA, Thread.CurrentThread.GetApartmentState()), which is exactly what these tests check, STA passes and MTA fails, just without touching the OS clipboard. Also remove the now unused System.Windows.Forms using and reference.

Verified locally: SimpleTestProject3 builds for net481, and ExecutionThreadApartmentStateTests runs green (UITestShouldPassIfApartmentStateIsSTA passes, UITestShouldFailWhenDefaultApartmentStateIsMTA still fails as expected).

The apartment-state acceptance tests (ExecutionThreadApartmentStateTests) run
SimpleTestProject3's UITestMethod and UITestWithSleep1, which called
Clipboard.SetText as a stand-in for an STA-only API. On the shared CI agents that
clipboard call intermittently throws ExternalException "Requested Clipboard
operation did not succeed", and reddens the Windows integration test leg with a
single flaky failure on UITestShouldPassIfApartmentStateIsSTA. It is a flake, not a
regression, the same commit passes and fails across different builds.

Replace the clipboard call with Assert.AreEqual(ApartmentState.STA,
Thread.CurrentThread.GetApartmentState()), which is exactly what these tests are
checking, STA passes and MTA fails, just without touching the OS clipboard. Remove
the now unused System.Windows.Forms using and reference.

Verified locally: SimpleTestProject3 builds for net481, and
ExecutionThreadApartmentStateTests runs green (UITestShouldPassIfApartmentStateIsSTA
passes, UITestShouldFailWhenDefaultApartmentStateIsMTA still fails as expected).

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: eac7b544-a9bb-483d-9fee-d443a2a467bb
Copilot AI review requested due to automatic review settings July 13, 2026 12:05

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR reduces flakiness in the Windows apartment-state acceptance coverage by removing reliance on the OS clipboard from SimpleTestProject3 UI test methods, while preserving the intended STA/MTA pass/fail behavior those acceptance tests validate.

Changes:

  • Replace Clipboard.SetText(...) usage with an explicit ApartmentState.STA assertion on the executing test thread.
  • Remove the now-unneeded System.Windows.Forms using and project reference from the test asset.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
test/TestAssets/SimpleTestProject3/UnitTest1.cs Replaces clipboard usage with an STA apartment-state assertion in UI test methods under #if NETFRAMEWORK.
test/TestAssets/SimpleTestProject3/SimpleTestProject3.csproj Removes the conditional System.Windows.Forms reference since it’s no longer required.

@nohwnd
Jakub Jareš (nohwnd) enabled auto-merge (squash) July 20, 2026 11:02

@Evangelink Amaury Levé (Evangelink) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The direct apartment-state assertions preserve the intended STA-pass/MTA-fail behavior while removing the flaky clipboard dependency. The Windows Forms reference removal is safe.

@nohwnd
Jakub Jareš (nohwnd) merged commit 11cb4cb into main Jul 20, 2026
20 of 21 checks passed
@nohwnd
Jakub Jareš (nohwnd) deleted the assert-apartmentstate-in-uitest branch July 20, 2026 11:20
This was referenced Sep 9, 2026
This was referenced Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants