-
Notifications
You must be signed in to change notification settings - Fork 992
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
Untangle test utils refs #3013
Merged
Merged
Untangle test utils refs #3013
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
afac7b5
Create infra
RussKie 6965038
System.Windows.Forms.Primitives.Tests compile, some tests commented out
RussKie 469a58b
System.Windows.Forms.Primitives.Tests compile, some tests commented out
RussKie 40d7e3f
System.Windows.Forms.Tests compile
RussKie 990232a
Microsoft.VisualBasic.Tests compile
RussKie f5194ed
Work
RussKie 857ab80
fixup! System.Windows.Forms.Primitives.Tests compile, some tests comm…
RussKie ac17eab
Fixes after rebase
RussKie 545217c
More fixes
RussKie 87981c0
.
RussKie ad8258d
.
RussKie 3dbb158
..
RussKie 0151ba2
fix tests
RussKie 30594a1
Fix IPictureTests tests
RussKie 37eaa08
fixup! Fix IPictureTests tests
RussKie 0791c3c
Switch to StaFact
RussKie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix IPictureTests tests
- Loading branch information
commit 30594a1f03fd3f748d56b095705a9b367028a4d9
There are no files selected for viewing
File renamed without changes.
141 changes: 141 additions & 0 deletions
141
src/System.Windows.Forms.Primitives/tests/UnitTests/Interop/Mocks/MockAxHost.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Drawing; | ||
using System.Drawing.Imaging; | ||
using System.Runtime.InteropServices; | ||
using static Interop; | ||
using static Interop.Ole32; | ||
|
||
namespace System.Windows.Forms.Primitives.Tests.Interop.Mocks | ||
{ | ||
[ComVisible(true)] | ||
[ClassInterface(ClassInterfaceType.AutoDispatch)] | ||
internal class MockAxHost | ||
{ | ||
private static Guid ipictureDisp_Guid = typeof(IPictureDisp).GUID; | ||
private static Guid ipicture_Guid = typeof(IPicture).GUID; | ||
|
||
public MockAxHost(string clsidString) | ||
{ | ||
} | ||
|
||
public static IPictureDisp GetIPictureDispFromPicture(Image image) | ||
{ | ||
PICTDESC desc = GetPICTDESCFromPicture(image); | ||
return (IPictureDisp)OleCreatePictureIndirect(ref desc, ref ipictureDisp_Guid, fOwn: BOOL.TRUE); | ||
} | ||
|
||
public static IPicture GetIPictureFromCursor(IntPtr cursorHandle) | ||
{ | ||
PICTDESC desc = PICTDESC.FromIcon(Icon.FromHandle(cursorHandle), copy: true); | ||
return (IPicture)OleCreatePictureIndirect(ref desc, ref ipicture_Guid, fOwn: BOOL.TRUE); | ||
} | ||
|
||
public static IPicture GetIPictureFromPicture(Image image) | ||
{ | ||
PICTDESC desc = GetPICTDESCFromPicture(image); | ||
return (IPicture)OleCreatePictureIndirect(ref desc, ref ipicture_Guid, fOwn: BOOL.TRUE); | ||
} | ||
|
||
public static Image GetPictureFromIPicture(object picture) | ||
{ | ||
int hPal = default; | ||
IPicture pict = (IPicture)picture; | ||
PICTYPE type = (PICTYPE)pict.Type; | ||
if (type == PICTYPE.BITMAP) | ||
{ | ||
try | ||
{ | ||
hPal = pict.hPal; | ||
} | ||
catch (COMException) | ||
{ | ||
} | ||
} | ||
|
||
return GetPictureFromParams(pict.Handle, type, hPal, pict.Width, pict.Height); | ||
} | ||
|
||
public static Image GetPictureFromIPictureDisp(object picture) | ||
{ | ||
if (picture == null) | ||
{ | ||
return null; | ||
} | ||
|
||
int hPal = default; | ||
IPictureDisp pict = (IPictureDisp)picture; | ||
PICTYPE type = (PICTYPE)pict.Type; | ||
if (type == PICTYPE.BITMAP) | ||
{ | ||
try | ||
{ | ||
hPal = pict.hPal; | ||
} | ||
catch (COMException) | ||
{ | ||
} | ||
} | ||
|
||
Image image = GetPictureFromParams(pict.Handle, type, hPal, pict.Width, pict.Height); | ||
GC.KeepAlive(pict); | ||
return image; | ||
} | ||
|
||
private static PICTDESC GetPICTDESCFromPicture(Image image) | ||
{ | ||
if (image is Bitmap bmp) | ||
{ | ||
return PICTDESC.FromBitmap(bmp); | ||
} | ||
|
||
if (image is Metafile mf) | ||
{ | ||
return PICTDESC.FromMetafile(mf); | ||
} | ||
|
||
throw new ArgumentException("AXUnknownImage", nameof(image)); | ||
} | ||
|
||
private static Image GetPictureFromParams( | ||
int handle, | ||
PICTYPE type, | ||
int paletteHandle, | ||
int width, | ||
int height) | ||
{ | ||
switch (type) | ||
{ | ||
case PICTYPE.ICON: | ||
return (Image)Icon.FromHandle((IntPtr)handle).Clone(); | ||
case PICTYPE.METAFILE: | ||
WmfPlaceableFileHeader header = new WmfPlaceableFileHeader | ||
{ | ||
BboxRight = (short)width, | ||
BboxBottom = (short)height | ||
}; | ||
|
||
using (var metafile = new Metafile((IntPtr)handle, header, deleteWmf: false)) | ||
{ | ||
return (Image)metafile.Clone(); | ||
} | ||
case PICTYPE.ENHMETAFILE: | ||
using (var metafile = new Metafile((IntPtr)handle, deleteEmf: false)) | ||
{ | ||
return (Image)metafile.Clone(); | ||
} | ||
case PICTYPE.BITMAP: | ||
return Image.FromHbitmap((IntPtr)handle, (IntPtr)paletteHandle); | ||
case PICTYPE.NONE: | ||
// MSDN says this should not be a valid value, but comctl32 returns it... | ||
return null; | ||
case PICTYPE.UNINITIALIZED: | ||
return null; | ||
default: | ||
throw new ArgumentException("AXUnknownImage", nameof(type)); | ||
} | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/System.Windows.Forms.Primitives/tests/UnitTests/Interop/Mocks/MockCursor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Drawing; | ||
using static Interop.User32; | ||
|
||
namespace System.Windows.Forms.Primitives.Tests.Interop.Mocks | ||
{ | ||
public class MockCursor : IDisposable | ||
{ | ||
private IntPtr _handle = IntPtr.Zero; // handle to loaded image | ||
private bool _ownHandle = true; | ||
private readonly int _resourceId = 0; | ||
|
||
public MockCursor(int nResourceId) | ||
{ | ||
// We don't delete stock cursors. | ||
_ownHandle = false; | ||
_resourceId = nResourceId; | ||
_handle = LoadCursorW(IntPtr.Zero, (IntPtr)nResourceId); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_handle != IntPtr.Zero) | ||
{ | ||
if (_ownHandle) | ||
{ | ||
DestroyCursor(_handle); | ||
} | ||
_handle = IntPtr.Zero; | ||
} | ||
} | ||
|
||
public IntPtr Handle | ||
{ | ||
get | ||
{ | ||
if (_handle == IntPtr.Zero) | ||
{ | ||
throw new ObjectDisposedException(nameof(MockCursor)); | ||
} | ||
return _handle; | ||
} | ||
} | ||
|
||
public Size Size | ||
{ | ||
get => new Size(GetSystemMetrics(SystemMetric.SM_CXCURSOR), GetSystemMetrics(SystemMetric.SM_CYCURSOR)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its not introduced by the PR, but this seems wrong, GetIPictureFromCursor calls OleCreatePictureIndirect so the test should be WinFormsFact (or at the very least StaFact). Even if this API is robust enough to be called from MTA it doesn't feel right to rely on it, normally all OLE API is required to have OLE initialized.
Also adding
[Collection("Sequential")]
is probably not required here, its not testing some globally shared resource like clipboard or drag'n'drop, so I don't see why it should coordinate with other tests.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that there were a few tests which were intentionally testing for failure when called on the wrong COM apartment, these may need to remain
Fact
and not turned toWinFormsFact
. I don't see any of these tests in this file though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This being
Fact
is also tracked in #3271 so if you want you can defer it and not update it in this PRThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't believe any of
IPictureTests
,IDispatchTests
orITypeInfoTests
test x-thread access, or failures for that matter.I copied
[Collection("Sequential")]
from the other two types, I think it ensures that these tests aren't run in parallel.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are unexplained E_FAIL returns from OleCreatePictureIndirect on the CI machine (#3271). The only theory I currently have is that OLE isn't initialized and for some reason the CI machine is less robust than running locally, otherwise I have no idea why the initial call already fails with E_FAIL.
It ensures that the tests are not run in parallel with other tests having that attribute, so it can protect process wide shared resources. xunit docs say that tests on the same class are never run in parallel, only multiple test classes are run in parallel (can look up the doc link if you need it)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct, this is what I meant (sorry for not being clearer).
Given that all of the tests from these test classes test more of less the same area we probably don't want them to run in parallel.