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

Add Tests for System.Drawing.Common #22130

Closed
mellinoe opened this issue Jun 5, 2017 · 87 comments
Closed

Add Tests for System.Drawing.Common #22130

mellinoe opened this issue Jun 5, 2017 · 87 comments
Assignees
Labels
area-System.Drawing backlog-cleanup-candidate An inactive issue that has been marked for automated closure. help wanted [up-for-grabs] Good issue for external contributors increase-code-coverage Tracking need to add more test and increase code coverage of a component no-recent-activity test-enhancement Improvements of test source code
Milestone

Comments

@mellinoe
Copy link
Contributor

mellinoe commented Jun 5, 2017

This issue tracks porting some set of tests from mono's test suite, covering the portions of System.Drawing that we support on .NET Core.

Mono's test cases are in this folder: https://github.com/mono/mono/tree/master/mcs/class/System.Drawing/Test

We most likely want to convert the most useful tests from all of the sections here, with the exception of System.Drawing.Design, which we aren't going to support right now on .NET Core (it is mainly related to designer / WinForms support).

Mono's tests use NUnit, so we will need to convert them to Xunit when copying them.

Additionally, I've identified that there will need to be some functional changes made to the tests themselves, as they do not pass against the .NET Framework implementation. We consider the .NET Framework implementation to be the compatibility baseline, so we should change the tests to accomodate it, rather than the other way around. The test failures seemed mainly related to very small, subtle differences in things like floating-point precision, color values, offsets, etc. We should do the following when we have both Windows and Unix implementations up and running:

  • Ensure that all tests have enough "leniency" to accomodate floating-point differences (and other minor inconsistencies) where unavoidable.
  • Ensure that all minor inconsistencies are acceptable or otherwise very difficult / problematic to fix.
  • Ensure that all minor inconsistencies are listed somewhere, so that we can mention it both in the tests, and in the user documentation.

@hughbe @qmfrederik @marek-safar


Current Status

Code coverage:
Note that there is a large amount of internal and debug-only code which distorts these numbers. When the coverage is generally high, we can clean out a lot of dead code and then get more accurate data.

Date Branch Coverage Line Coverage
6/26/2017 33% 25%
7/11/2017 49% 54%
7/17/2017 55% 60%
8/2/2017 58% 66%
8/25/2017 62% 71%
9/21/2017 64.6% 75.3%

Namespaces and coverage, as of 9/21/2017:

  • System.Drawing: 76.3%
  • System.Drawing.Drawing2D: 93.8%
  • System.Drawing.Imaging: 86.1%
  • System.Drawing.Printing: 62.8%
  • System.Drawing.Text: 96%
@norek
Copy link
Contributor

norek commented Jun 5, 2017

Hi I can grab some part of those tests for example System.Drawing.Printing, on start. Then when all will be fine i can port next part. What do you think?

@mellinoe
Copy link
Contributor Author

mellinoe commented Jun 5, 2017

@norek Splitting up the work sounds like a good idea to me. I've created separate items above for each of the folders in mono's test bed. I've put your name next to System.Drawing.Printing.

Looking over the tests in that folder, I think they are fairly straightforward, with a couple of exceptions. I would ignore the following tests for now:

  • PrintingPermissionAttributeTest
  • PrintingPermissionTest

We don't care about any of this permissions stuff in .NET Core, nor do we enforce it.

  • PrintingServicesUnixTest

This one isn't going to work outside of Unix or the libgdiplus implemetation, so don't bother with it for now.

@norek
Copy link
Contributor

norek commented Jun 6, 2017

@mellinoe insln file is reference to test project but it is missing in source code. Did you forgot push it?

@mellinoe
Copy link
Contributor Author

mellinoe commented Jun 6, 2017

@norek Oops -- it was a mistake to include it in the solution file. I had a couple of tests written on my local machine, but they weren't worth merging.

@norek
Copy link
Contributor

norek commented Jun 8, 2017

I have little question about moving tests from Nunit to Xunit. In mono tests, very often Assert is used with description,prabobly beacuse there are more than one assert in test and description provide better debug (i think). What should I do with this case. Xunit doesn't provide description for Assert.Equal.
Should I remove this description?

@mellinoe
Copy link
Contributor Author

mellinoe commented Jun 8, 2017

Yes -- remove the description. If really necessary, just provide the message in a comment next to the assertion.

@hughbe
Copy link
Contributor

hughbe commented Jun 9, 2017

@norek a test project has been merged so feel free to get started. Mind if I ask what you're working on so we avoid conflicts?

Note that I'm gonna put some effort into beautifying and modernising these tests using xunit's features like parameter names in exceptions and theory/member data

Keep me up to date :)

@norek
Copy link
Contributor

norek commented Jun 9, 2017

@hughbe Thanks! Im working on System.Drawing.Printing namespace. In description of this issue You can see list of assigned items.

@norek
Copy link
Contributor

norek commented Jun 12, 2017

How about testing PageSettings class? In mono's test i found comment:

// Check for installed printers, because we need
// to have at least one to test
if (PrinterSettings.InstalledPrinters.Count == 0)

I dont know what to do with this.

@mellinoe
Copy link
Contributor Author

@norek We certainly don't have any printers attached to the machines that run our CI tests, if that is what you are alluding to. You can just keep that check in the test code. Are you asking about something else?

@mellinoe
Copy link
Contributor Author

mellinoe commented Jun 12, 2017

@hughbe Could you post something here as you start writing/porting tests for individual components? It would be good to have a list of "in-progress" work so that we know what's being covered.

@norek
Copy link
Contributor

norek commented Jun 13, 2017

@mellinoe yup, this is what I meant. Thanks

@norek
Copy link
Contributor

norek commented Jun 13, 2017

@hughbe Can you tell me what is this?
public static IEnumerable<(int, Color)> SystemColors_TestData() in ColorTranslatorTests.cs
build is passing, but VS highlight this syntax. I see this syntax for first time ;o

second: In my machine after pull your changes, I have failing your tests for example:
do you have idea why? clean, build on root completed.

<failure exception-type="Xunit.Sdk.ThrowsException">
<message><![CDATA[Assert.Throws() Failure\r\nExpected: typeof(System.ArgumentException)\r\nActual:   typeof(System.Exception): 1,256,3 is not a valid value for Int32.]]></message>
<stack-trace><![CDATA[at System.Drawing.ColorConverterCommon.IntFromString(String text, CultureInfo culture) in C:\Projects\corefx\src\Common\src\System\Drawing\ColorConverterCommon.cs:line 131
at System.Drawing.ColorConverterCommon.ConvertFromString(String strValue, CultureInfo culture) in C:\Projects\corefx\src\Common\src\System\Drawing\ColorConverterCommon.cs:line 64
at System.Drawing.ColorTranslator.FromHtml(String htmlColor) in C:\Projects\corefx\src\System.Drawing.Common\src\System\Drawing\ColorTranslator.cs:line 271
at System.Drawing.Tests.ColorTranslatorTests.<>c__DisplayClass10_0.<FromHtml_Invalid_Throws>b__0() in C:\Projects\corefx\src\System.Drawing.Common\tests\ColorTranslatorTests.cs:line 222]]></stack-trace>
</failure>
</test>```

@mellinoe
Copy link
Contributor Author

Those are ValueTuples, a new C#7 feature. You will need VS 2017 to edit that, I believe...

@mellinoe
Copy link
Contributor Author

@norek As for the test failure: what language settings does your computer use? We might be assuming that English is used, and number parsing could fail with other culture settings.

@norek
Copy link
Contributor

norek commented Jun 13, 2017

@mellinoe oh my god...of course you are right, VS17 is answer in this case.
And in second case... you are right too. Its written in fail message!
I work on polish settings.

That was easy... I dont know what is happening to me this night! Thanks alot.

@mellinoe
Copy link
Contributor Author

@norek Yep, the tests fail if your computer's language is Polish. This also happens on .NET Framework:

string htmlColor = "1,256,3";
var color = ColorTranslator.FromHtml(htmlColor);
Console.WriteLine("Color: " + color);

Unhandled Exception: System.Exception: 1,256,3 is not a valid value for Int32. ---> System.FormatException: Input string was not in a correct format.

We should override the current culture in the tests that check this stuff.

@hughbe
Copy link
Contributor

hughbe commented Jun 13, 2017

If it's not pressing I can do this by tomorrow

@mellinoe
Copy link
Contributor Author

I have a fix here: dotnet/corefx#21016

@mellinoe
Copy link
Contributor Author

@norek No problem. I have seen enough problems exactly like this that I've come to expect it 😄

@mellinoe mellinoe self-assigned this Jun 14, 2017
@norek
Copy link
Contributor

norek commented Jun 15, 2017

In PrinterUnitConvert.cs -> method UnitsPerDisplay have this default block in switch statement:

                    default:
                    Debug.Fail("Unknown PrinterUnit " + unit);
                    result = 1.0;

I know that for backward compatibility it shouldn't be converted to Exception but i think we can just remove this Debug.Fail. Otherwise we cannot test this block. What do you think?

@hughbe
Copy link
Contributor

hughbe commented Jun 17, 2017

We should delete Debug statements that are failing, I agree. They were written before there were any unit tests.

@hughbe
Copy link
Contributor

hughbe commented Jun 17, 2017

Eric, I've also given a stab at cleaning up some of the codebase with a view to use modern C# features etc. etc.

It's a big change but fairly montonous - removing devdocs, inline variables, fixup usings etc.

If you take a look here I've done an attempt for System.Drawing.Drawing2D. Do you think I should submit a PR?
dotnet/corefx@master...hughbe:drawing2d-cleanup-example

@norek
Copy link
Contributor

norek commented Jun 17, 2017

@hughbe it should be different PR or can I attach this change to dotnet/corefx#21015?

@norek
Copy link
Contributor

norek commented Jun 17, 2017

@hughbe for style changes it is created separate issue #22129. Hm its this good idea to completely remove comments? I thought they should be converted from doc for <summary>

@norek
Copy link
Contributor

norek commented Jun 17, 2017

Due waiting on dotnet/corefx#21015 feedback I can grab System.Drawing.Common.Imaging.

@hughbe
Copy link
Contributor

hughbe commented Jun 17, 2017

Yeah I'll submit a PR linking to that issue

@KostaVlev
Copy link
Contributor

@mellinoe Do you mean I can do something like

<PropertyGroup>
    <TestsDataVersion>1.0.3</TestsDataVersion>
</PropertyGroup>

in the System.Drawing.Common.Tests.csproj file?

@mellinoe
Copy link
Contributor Author

@KostaVlev Yes, that's right. For now, though, just go ahead and hard-code the new version. I may actually want to create a property all the way up in https://github.com/dotnet/corefx/blob/master/dependencies.props, and use that property everywhere in the repo, including the test-runtime project file. That way it would only be specified once.

pjanotti referenced this issue in pjanotti/corefx Jul 20, 2017
…tnet#21866)

* Adding System.Drawing.Common.Drawing2D GraphicsPath tests. #20711

* Adding ConditionalFact and ConditionalTheory. Tests will run if IsNotWindowsNanoServer. (dotnet#21866)

* Removing dead comments. Removing skips, they don't work. Wrapping GraphicsPath in using block. Adding param names to argument exceptions. Use Assert.Equal(IEnumerable<T>, IEnumerable<T> instead of asserting each element one by one in an array. dotnet#21866

* Pluralizing the name GraphicsPathTest.cs dotnet#21866

* Changing the namespace to System.Drawing.Drawing2D.Tests

* Some spelling corrections. Removing unnecessary float casts. Removing single InlineData attributes. (dotnet#21866)

* Removing arguments from test methods that are not [Theory].

* Adding System.Drawing.Common.Drawing2D GraphicsPathIteratorTests tests. Test CopyData_StartEndIndexesOutOfRange_ReturnsExpeced() fails with exception System.EntryPointNotFoundException : Unable to find an entry point named 'ZeroMemory' in DLL 'kernel32.dll' , issue is tracked in (#22026). (#20711)

* Updating System.Drawing.Common.Tests.csproj.

* Removing not related change to dotnet#21866. I will commit this when the pull request is merged.

* Reverting changes to System.Drawing.Common.Tests.csproj.
pjanotti referenced this issue in pjanotti/corefx Jul 20, 2017
…0711 (dotnet#22157)

* Adding System.Drawing.Common.Drawing2D GraphicsPathIteratorTests tests. Test CopyData_StartEndIndexesOutOfRange_ReturnsExpeced() fails with exception System.EntryPointNotFoundException : Unable to find an entry point named 'ZeroMemory' in DLL 'kernel32.dll' , issue is tracked in (#22026). (#20711)

* Adding [ActiveIssue(22026)] attribute to CopyData_StartEndIndexesOutOfRange_ReturnsExpeced()
pjanotti referenced this issue in pjanotti/corefx Jul 20, 2017
dotnet#22199)

* Adding System.Drawing.Common.Drawing2D PathGradientBrush tests. #20711

* Adding test for Dispose().

* Fixing some spellings and splinting long statement to multiple lines.

* Completing tests for Disposed.

* Spelling correction.

* Spelling correction.
@KostaVlev
Copy link
Contributor

KostaVlev commented Jul 25, 2017

Hi, the next one which isn't tested in Imaging is Metafile. I started working on it but :) I need a little bit help here.

I wrote this test:

[ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotWindowsNanoServer))]
public void Ctor_IntPtrWmfPlaceableFileHeader_Success()
{
    using (var bufferGraphics = Graphics.FromHwndInternal(IntPtr.Zero))
    using (var metafile = new Metafile(bufferGraphics.GetHdc(), new WmfPlaceableFileHeader()))
    {
        Assert.Equal(new Rectangle(0, 0, 0, 0), metafile.GetMetafileHeader().Bounds);
    }
}

My idea is to create blank Metafile but I am getting System.Runtime.InteropServices.ExternalException : A generic error occurred in GDI+. when calling the constructor.

The exception occurs on every platform.

Any hints how to make this to work?

@mellinoe
Copy link
Contributor Author

@KostaVlev Which call is throwing the exception? I'm pretty sure it is not valid to call FromHwndInternal using a null handle like that, so I'm not sure such a test is really valuable. A test could be added to assert that an ExternalException is thrown, but again, I don't think it is valuable, and I don't think we should care about that level of behavior compat to emulate it on the Unix version.

@KostaVlev
Copy link
Contributor

@mellinoe The graphics object is created successfully var metafile = new Metafile(bufferGraphics.GetHdc(), new WmfPlaceableFileHeader()) throws the exception.

It tried this way too but still getting exception.

[ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotWindowsNanoServer))]
 public void Ctor_IntPtrWmfPlaceableFileHeader_Success()
{
    using (var bitmap = new Bitmap(_rectangle.Width, _rectangle.Height))
    using (var bufferGraphics = Graphics.FromImage(bitmap))
    using (var metafile = new Metafile(bufferGraphics.GetHdc(), new WmfPlaceableFileHeader()))
    {
        Assert.Equal(new Rectangle(0, 0, 0, 0), metafile.GetMetafileHeader().Bounds);
    }
}

Since we have same behavior for netfx and netcoreapp should I skip this test for now?

@mellinoe
Copy link
Contributor Author

Since we have same behavior for netfx and netcoreapp should I skip this test for now?

Yes

mellinoe referenced this issue in dotnet/corefx Jul 26, 2017
…22491)

* Adding System.Drawing.Common.Imaging ImageAttributesTests tests. #20711

* Reverting some not related changes.

* Updating XUnit.Runtime.depproj System.Drawing.Common.TestData version to 1.0.3

* Fixing SetOutputChannelColorProfile_InvalidPath_ThrowsOutOfMemoryException() test.

* Renaming _colorMap to _greenCmponentToZeroColorMap. Removing commented out code. Updating  Ctor_Default_Success() test.

* Renaming _colorMap to _yellowToRedColorMap and _greenCmponentToZeroColorMap to _greenComponentToZeroColorMatrix
mellinoe referenced this issue in dotnet/corefx Jul 31, 2017
* Adding System.Drawing.Common.Imaging Metafile tests. (#20711)

* Changing [ConditionalTheory(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotWindowsNanoServer))] to [ConditionalTheory(Helpers.GdiplusIsAvailable)]

* Changing [ConditionalTheory(Helpers.GdiplusIsAvailable)] to [ConditionalFact(Helpers.IsNotLinuxAndGdiplusIsAvailable)]

* Changing the way tests will skip Unix.
@KostaVlev
Copy link
Contributor

I moved on System.Drawing.Printing.

@hughbe
Copy link
Contributor

hughbe commented Aug 2, 2017

<3 good stuff

mellinoe referenced this issue in dotnet/corefx Aug 4, 2017
* Adding System.Drawing.Printing PrintDocument tests. (#20711)

* Disabling some tests on Unix and making some Facts/Theories conditional.

* Fix tests conditional attributes.

* Fix tests conditional attributes.

* Adding missed test attributes.

* Fixing test attributes.

* Changing Assertion in AssertDefaultPageSettings() to expect PaperKind Letter. And skipping tests that fail on Unix.

* Updating PageSettings.PrintableArea assertions.

* Moving the check for  PrinterSettings.InstalledPrinters.Count == 0 to Helpers.cs and updating test attributes.
@mellinoe
Copy link
Contributor Author

Code coverage numbers are looking really good to me (updated at the top), considering there are a few large files full of dead code (or code that doesn't really need to be tested). Thanks for all of the contributions, everyone!

@norek
Copy link
Contributor

norek commented Sep 19, 2017

How nice, a lot of work was made since my "vacation". I just bought new computer and i'm ready to go!

@mellinoe
Copy link
Contributor Author

Some more steady progress was made in the past month. I think @qmfrederik's current effort will give us enough coverage to fill in the remaining gaps and let us close this issue out.

@ghost
Copy link

ghost commented Feb 10, 2023

Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

This process is part of our issue cleanup automation.

@ghost ghost added backlog-cleanup-candidate An inactive issue that has been marked for automated closure. no-recent-activity labels Feb 10, 2023
@ghost
Copy link

ghost commented Feb 24, 2023

This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.

@ghost ghost closed this as completed Feb 24, 2023
@dotnet dotnet locked as resolved and limited conversation to collaborators Mar 26, 2023
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Drawing backlog-cleanup-candidate An inactive issue that has been marked for automated closure. help wanted [up-for-grabs] Good issue for external contributors increase-code-coverage Tracking need to add more test and increase code coverage of a component no-recent-activity test-enhancement Improvements of test source code
Projects
None yet
Development

No branches or pull requests

5 participants