Application.CommandManager.ControlDefinitions.AddButtonDefinition throws COM Exception when it stands in unit tests
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello people,
in the development process of an Add-in to Autodesk Inventor we decide to write some integration tests using MS Test unit test framework. Here is a simplified excerpt of test code:
using Autodesk.ADN.InvUtility.WinUtils;
using Inventor;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Windows.Forms;
using System.Drawing;
namespace AddIn.Tests
{
[TestClass]
public class UnitTest1
{
private AssemblyDocument _AsmDoc;
private const string _IAM_PATH = @"pathToAssemblyFileForTest";
[TestMethod]
public void TestMethod1()
{
Inventor.Application _Application = InvTestFuncLib.StartInventor(); // This method opens Inventor using System.Activator.CreateInstance
_AsmDoc= application.Documents.Open(System.IO.Path.GetFullPath(_IAM_PATH ), OpenVisible: true) as AssemblyDocument;
_AsmDoc.Activate();
Icon icon = new Icon(@"sample.ico");
stdole.IPictureDisp plusIcon = ImageConverter.IconToPicture(icon);
Inventor.ControlDefinitions controlDefs = _Application.CommandManager.ControlDefinitions;
ButtonDefinition _ButtonApply = controlDefs.AddButtonDefinition("Apply", "ButtonRadialMenuApply", CommandTypesEnum.kNonShapeEditCmdType, null, "", "" , plusIcon);
}
}
}
The last line of code throws an COM Exception with message:
" System.Runtime.InteropServices.COMException: Catastrophic error (Exception of HRESULT: 0x8000FFFF (E_UNEXPECTED))".
The strange things are:
1. The same codes of using AddButtonDefinition are present in the source codes (not in test codes) and it works normally there.
2. If plusIcon is not passed over to Application.CommandManager.ControlDefinitions.AddButtonDefinition, i.e. the last line will be like the following, then no exception is thrown, but obviously the icon will be missing in the Inventor UI.
ButtonDefinition _ButtonApply = controlDefs.AddButtonDefinition(Text.Apply, "ButtonRadialMenuApply", CommandTypesEnum.kNonShapeEditCmdType, null, "", "");
Could anyone please give me any suggestion: Why does the icon parameter cause problem, when it is run under the unit test framework, and how can I solve it?