Modal Dialog Box By Reflection?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am writing unit tests that programmitically open and control a GUI. The
GUI puts up a modal dialog box while I am controlling it from my unit test.
Is there any way for me to close down that message box programmatically? Is
there even a way I can get a reference to the message box, perhaps by using
reflection?
 
Hi Andrew,

What kind of Dialog Box? If you're talking about the
System.Windows.Forms.MessageBox then I don't think you can get a reference to
it because it's actually just shown using the Windows API.

We've worked around the problem using a strategy pattern, instead of calling
MessageBox.Show() directly, we have a singleton object that changes behaviour
based on whether we're running UnitTests or not. Both return DialogResults
(you preset your desired DialogResult in the UnitTest), so it's transparent
to the Form being tested, and you can test the behaviour of your form for the
different DialogResults that can possibly be chosen by the user.

Hope that helps.

Regards,
Matt Garven
 
Thanks Matt. Yes it is a Windows Message box that is being displayed.
Luckily there is a modular boolean variable that is checked before the
message box is displayed, so my solution for now is to manipulate the value
of that boolean using reflection. This is not an ideal solution so if anyone
has any hints on how to get at the messagebox, please let me know.

And Matt from what I've read your solution is the most common however I
don't think it will work in my situation. Thanks for the response though.
 
Hi Andrew,

No worries. I'm interested to know why it won't work in your situation.

Part of the problem is that your code waits for the message box to close
before continuing execution - so unless you're going to manipulate it from a
different thread I don't see how the code would work.

Regards,
Matt Garven
 
No worries. I'm interested to know why it won't work in your situation.

Because I am unable to edit the production code to be able to tell if the
Unit Tests are running or not.
Part of the problem is that your code waits for the message box to close
before continuing execution - so unless you're going to manipulate it from a
different thread I don't see how the code would work.

Yes I am starting the app on a different thread, getting a reference to it,
and manipulating it that way. The rest is working fine. I can startup
sub-forms, close them down, and perform all other actions I need to. But
when one of the actions I perform causes the production code to display a
message box, I am no longer able to manipulate the application anymore.
 
You could use FindWindow/FindWindowEx to get to the dialog box. It will
require you to some information about top level window and the dialog
that can be used in FindWindow/FindWindowEx. Once you get a handle to
the dialog, you can use Win32 PostMessage( , WM_CLOSE,..). What I have
shown here is using Win32. .Net may have equivalent of this logic. If
not, you can use Win32 calls in your code using PInvoke.
 
Back
Top