Re: [TFUI] Dialogs and EventHandlers

Anthony Williams <anthony_w.geo-/[email protected]> Mon, 09 Jan 2006 14:08:12 +0000
Newsgroups gmane.comp.programming.test-first-user-interfaces
Organization Just Software Solutions Ltd
Message-ID <[email protected]>
Owen Rogers <[email protected]> writes:

> On 08/11/05, Anthony Williams <anthony_w.geo-/[email protected]> wrote:
>> I've done this several ways. One way that works quite well is to run the
>> function with the modal dialog in a separate thread, check that it displays
>> the dialog, send messages to the controls from the test thread, and verify the
>> result.
>
> what would this look like in code?  how would you ensure that the
> modal dialog thread is properly in sync with the test thread?  just
> curious to see what this would look like.
>
> i've done this kind of thing before (testing dialogs), but i would try
> to skip the modal call.  but if you have a good, reliable and simple
> way to test modal dialogs, i'm definitely interested.

Sorry for the long time before replying to this message. I was hoping to give
an example straight from code, but couldn't find one that was easy to
cut&paste. Other things then got in the way.

Anyway, if you're still interested, here's an outline of the code (C++/Windows).

void someFuncThatShowsAModalDialog();

void testModalDialog()
{
    ThreadHandle modalThread=
        runOnBackgroundThread(someFuncThatShowsAModalDialog);

    TEST_ASSERT_NOT_EQUALS_TIMED(findDialogBoxThisProcess(),(HWND)0,dialogDisplayTimeout);

    HWND const modalDialog=findDialogBoxThisProcess();

    simulateUserTextEntry(modalDialog,IDC_SOME_EDIT_FIELD,"some text");
    simulateUserClick(modalDialog,IDOK);

    TEST_ASSERT_TIMED(!IsWindow(modalDialog),dialogDestroyTime);

    TEST_ASSERT(someConsequenceOfModalDialog);
}

The support routines are here:

DWORD getWindowProcess(HWND windowHandle)
{
    DWORD processId=0;
    GetWindowThreadProcessId(windowHandle,&processId);
    return processId;
}

HWND findProcessWindow(DWORD processId,std::string const& caption,std::string const& className)
{
    HWND windowHandle=FindWindowEx(NULL,NULL,getCStringOrNull(className),getCStringOrNull(caption));
    while(windowHandle && getWindowProcess(windowHandle)!=processId)
    {
        windowHandle=FindWindowEx(NULL,windowHandle,getCStringOrNull(className),getCStringOrNull(caption));
    }
    return windowHandle;
}

inline char const* getCStringOrNull(std::string const& s)
{
    return s.empty()?NULL:s.c_str();
}

std::string const dialogBoxWindowClass="#32770";

HWND findDialogBoxThisProcess()
{
    return findProcessWindow(GetCurrentProcessId(),"",dialogBoxWindowClass);
}

void simulateUserClick(HWND window,WORD buttonId)
{
    SendMessage(window,WM_COMMAND,
                MAKELONG(buttonId,BN_CLICKED),
                (LPARAM)GetDlgItem(window,buttonId));
}

I don't have the source for simulateUserTextEntry to hand, but it's
essentially the same idea --- post the appropriate windows messages to the
control to simulate what's needed.

Anthony
-- 
Anthony Williams
Software Developer
Just Software Solutions Ltd
http://www.justsoftwaresolutions.co.uk



To unsubscribe, email:
TestFirstUserInterfaces-unsubscribe-hHKSG33TihhbjbujkaE4pw@public.gmane.org
 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/TestFirstUserInterfaces/

<*> To unsubscribe from this group, send an email to:
    TestFirstUserInterfaces-unsubscribe-hHKSG33TihhbjbujkaE4pw@public.gmane.org

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/