Condition for Editor ready

WindowTester allows you to easily create and run unit tests for every GUI they build. It can also be used to generate system level tests.

Moderators: gnebling, Eric Clayberg, Dan Rubel, keertip, Phil Quitslund

Condition for Editor ready

Postby mwa » Fri Jul 18, 2008 3:04 am

Hi all,

I have written a helper method for testing if an editor is dirty.
Code: Select all
   
    public static boolean anyUnsavedChanges() throws Exception {
   final boolean dirtyEditors[] = new boolean[]{false};
   final String errorMessages[] = new String[]{""};
   
   Display.getDefault().syncExec(new Runnable() {
            public void run() {
                try {
                    IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
                    for (int i = 0; i < windows.length; i++) {
                   IWorkbenchPage[] pages = windows[i].getPages();
                   for (int j = 0; j < pages.length; j++) {
                       IEditorReference[] editorRefs = pages[j].getEditorReferences();
                       for (int k = 0; k < editorRefs.length; k++) {
                      IEditorReference each = editorRefs[k];
                      if (each.isDirty()){
                          dirtyEditors[0] = true;
                      }
                       }
                   }
                    }
                } catch(Exception e){
                    errorMessages[0] += e.toString() + e.getMessage();
                }
            }
   });
   if(errorMessages[0].length() > 0){
       throw new Exception(errorMessages[0]);
   }
        return dirtyEditors[0];
    }


Now I have the following test:
Code: Select all
   
    public void testDirtyAfterTextInput_TC_EX_r04() throws Exception {
        IUIContext ui = getUI();
        ui.keyClick(WT.CR);
        ui.enterText("foobar");
        assertTrue(EclipseHelper.anyUnsavedChanges());
    }

The test is successful if running in normal mode. If running with a delay the test fails. The test should always fail, because after some input the editor is dirty of course.
That means I have to do some delay after the text is entered giving the editor a chance to signalize its status.
How can I do that? Anyone ideas?

Must be something like this:
Code: Select all
       
    ui.enterText("foobar");
    getUI().wait(ActiveEditorCondition.forName("asmod_eng_inl1_fs.XML"));
    assertTrue(EclipseHelper.anyUnsavedChanges());

or
Code: Select all
       
   ui.enterText("foobar");
   getUI().wait(new JobsCompleteCondition());
        assertTrue(EclipseHelper.anyUnsavedChanges());

But the editor is active of course and has no jobs running.
Probably I have to implement my own condition. But for what should I wait?

Michael
mwa
 
Posts: 24
Joined: Mon Jun 16, 2008 4:38 am

Re: Condition for Editor ready

Postby Phil Quitslund » Fri Jul 18, 2008 9:20 am

Hi Michael,

Interesting problem!

What makes this especially tricky is that you essentially want to get notified when the editor has processed all "fresh input" but how could the editor (or you) easily determine that? (Nevermind what "processed" means exactly...)

But I have another angle. We have a special method on the UI:

http://downloads.instantiations.com/Win ... ICondition)

that may come in handy.

The API specifies:

Code: Select all
    Assert that the given condition either is true or becomes true in a reasonable amount of time. Some examples include:

           ui.assertThat(new IsEnabledCondition(new ButtonLocator("OK"), false));
           ui.assertThat(new HasTextCondition(new WizardErrorMessageLocator(), "File extension is missing"));
     

    Parameters:
        condition - the condition to assert
    Throws:
        WaitTimedOutException - if the default timeout (3 seconds) is exceeded.


While a little imprecise this might be just what we need. So rather than using Assert.assertTrue(..), you would use assertThat(..) with a condition. In other words, something like:

Code: Select all
ui.assertThat(new ICondition() {
   public boolean test() {
      return EclipseHelper.anyUnsavedChanges();
   }
});


This is actually the preferred way to test aspects of the UI since it's generally true that we are asserting facts about a state of the UI that may or may not have had a chance to be realized.

I realize this sort of side-steps your original question. If you still think you need to craft such an Editor content condition, by all means say so, and we can all brainstorm about how we might get it implemented.

Thanks!
--
Phil Quitslund
Software Engineer
Google, Inc.
Phil Quitslund
Moderator
 
Posts: 491
Joined: Fri Apr 28, 2006 6:26 am

Re: Condition for Editor ready

Postby mwa » Thu Jul 24, 2008 7:56 am

Yes. That is another aproach, thanks. More with the api than mine. After trying this in various ways I must give up now. Nothing of the following works with my activex editor plugin:

Code: Select all

ui.enterText("foobar");
Thread.sleep(5000);


Code: Select all
ui.enterText("foobar");
ui.pause(3000);


Code: Select all
ui.enterText("foobar");
ui.assertThat(new ICondition() {
    public boolean test() {
        try {
            return EclipseHelper.anyUnsavedChanges();
        } catch (Exception e) {
            return false;
        }
   }
});


Code: Select all
new java.awt.Robot().keyPress('f');
new java.awt.Robot().keyRelease('f');
ui.assertThat(new ICondition() {
    public boolean test() {
        try {
            return EclipseHelper.anyUnsavedChanges();
        } catch (Exception e) {
            return false;
        }
   }
});


Only if I set up the delay in the windowtester runtime preferences to e.g. 2000ms the tests work as expected. I have no clue what the difference between this delay in the setup and ui.pause or the waiting for the condition is. Is this not the same thing? How I can wait for the editor to signalize its dirty state? Wondering why the condition breaks even if I set the timeout to 30 seconds. I cant see an asterisk with my eyes either. But I can see the input is done. The asterisk is not appearing after the input was made. Save button stays disabled.

Michael
mwa
 
Posts: 24
Joined: Mon Jun 16, 2008 4:38 am

Re: Condition for Editor ready

Postby Phil Quitslund » Thu Jul 24, 2008 9:09 am

This jumps out at me:

activex editor plugin


This could very well be a limitation of embedded editors... They may not do the same kinds of notifications that the standard inline editors do. Or maybe they just take a long time to notify... In this case you may just need to up the timeout for the assertion. Turning the asserThat into a wait will lengthen the amount of time before the condition testing times out:
Code: Select all
ui.wait( new ICondition() {
    public boolean test() {
        try {
            return EclipseHelper.anyUnsavedChanges();
        } catch (Exception e) {
            return false;
        }
   }
});


Maybe this is all we need to do?

Still puzzling on this one...
--
Phil Quitslund
Software Engineer
Google, Inc.
Phil Quitslund
Moderator
 
Posts: 491
Joined: Fri Apr 28, 2006 6:26 am

Re: Condition for Editor ready

Postby mwa » Fri Jul 25, 2008 12:37 am

Yes. activex is just bad, I know:-( I have other problems with this, too. Like getting the editors content etc.

Like you mentioned I have tried this with ui.wait(). But even waiting for minutes doesnt make the editor change his status. The asterisk is not visible and the save button stays disabled. It seems like anything of the wt runtime is preventing the editor of signalizing its status. If I run with an delay it just works fine.

Michael
mwa
 
Posts: 24
Joined: Mon Jun 16, 2008 4:38 am

Re: Condition for Editor ready

Postby Phil Quitslund » Fri Jul 25, 2008 8:25 am

Strange.... I'll keep puzzling on this. In the meantime, if you have any insights, please post them.

Thanks!
--
Phil Quitslund
Software Engineer
Google, Inc.
Phil Quitslund
Moderator
 
Posts: 491
Joined: Fri Apr 28, 2006 6:26 am


Return to Window Tester

Who is online

Users browsing this forum: No registered users and 1 guest