winforms icon indicating copy to clipboard operation
winforms copied to clipboard

Add test coverage for AxHost related classes

Open JeremyKuhne opened this issue 2 years ago • 5 comments

We have some tests, but there are significant gaps. Any additional coverage here will help keep this code in good shape for the next few decades. :)

Help here is appreciated. Live on the bleeding edge of the mid-nineties. :)

Supporting this scenario is important for Windows Forms customers with legacy dependencies. This code is how Visual Basic 6 (VB6) controls are supported, and we plan to add specific tests for that scenario as well. See #8253.

  • [x] Media Player https://github.com/dotnet/winforms/pull/8216 and https://github.com/dotnet/winforms/pull/8251
  • [x] Microsoft Web Browser https://github.com/dotnet/winforms/pull/11137
  • [x] System Monitor Control https://github.com/dotnet/winforms/pull/10675

JeremyKuhne avatar Nov 28 '22 23:11 JeremyKuhne

This issue is now marked as "help wanted", and we’re looking for a community volunteer to work on this issue. If we receive no interest in 180 days, we will close the issue. To learn more about how we handle feature requests, please see our documentation.

Happy Coding!

ghost avatar Nov 28 '22 23:11 ghost

One form of test coverage that we are lacking is test coverage against existing ActiveX controls such as Microsoft Web Browser, System Monitor Control, etc. Any additional coverage here would be immensely helpful and greatly appreciated. We have a core project that targets Framework called AxHosts so that dlls for these controls can be grabbed more easily to start writing tests. The process to do this would be as follows:

  1. In WinForms solution, find and open AxHosts form. Right click on the toolbox and click "Choose Items" then "COM Components"
  2. There will be a list of ActiveX controls. Check which control(s) you would like to add and click 'OK'
  3. After clicking 'OK', the control(s) should appear in the ToolBox. Add the control(s) to the form and save
  4. In AxHosts.csproj, COM reference(s) should've been added. Change the EmbedInteropTypes tag to false then save and build Debug configuration
  5. A successful build should've generated dlls in winforms\artifacts\bin\AxHosts\Debug\net472 folder.
  6. Paste the following into the .csproj of the project you will be writing tests in:
<ItemGroup>
    <Reference Include="<dll_name_without_.dll_extension>">
      <HintPath>$(ArtifactsBinDir)\AxHosts\$(Configuration)\net472\<dll_name_with_.dll_extension></HintPath>
    </Reference>
</ItemGroup>

The dll name can be found in the folder path in step 5. If there were multipe dlls generated, add another Reference tag under the same ItemGroup tag. 7. Start writing tests! :)

We have some tests written for Windows Media Player that may be helpful as examples. See #8216 , #8251 Note - the following code snipped may be necessary to add to the beginning of your tests for the ActiveX control you've added to behave as expected.

using Form form = new();
using <Your_Added_ActiveX_Control> control= new();
((ISupportInitialize)control).BeginInit();
form.Controls.Add(control);
((ISupportInitialize)control).EndInit();

lonitra avatar Nov 29 '22 17:11 lonitra

I will probably add some more testing as part of #10583 since we need to get AxHost working with the new COM source generators. There is the Microsoft RDP Client, which is an ActiveX control, but I don't know how much sense it makes to use it as a test scenario. I might just write more MFC based ActiveX code? Or if you know of Microsoft provided ActiveX controls (available on standard Windows out of the box) which are suitable for testing I could write bindings for them.

weltkante avatar Jan 04 '24 17:01 weltkante

@weltkante more MFC based code would certainly be good.

JeremyKuhne avatar Jan 18 '24 17:01 JeremyKuhne

@JeremyKuhne just to double check, we don't have any MFC control being hosted in AxHost yet, right? only inbox controls? The reverse case, hosting a WinForms UserControl in MFC had a test but we never managed to execute it on the CI machines, had to be run manually, I think due to issues with building C++ projects, but might misremember. We'll have to figure out how to build C++/MFC projects on CI if we want those included in automated testing.

~~Also the AxHost test project seems to be testing against Desktop Framework if I'm not mistaken? Its looks like it is targeting .NET 4.7.2, if theres previous discussion how it works feel free to link me to that.~~ (edit: saw that this project just generates the interop assemblies and that the tests are separate)

Anyways, looking into writing an MFC based control now to include in the unit tests if we can figure out how to build it on CI.

weltkante avatar Jan 22 '24 15:01 weltkante