0

I am relatively new to SWTBot for Tool Testing.I am runing JUnit Test case where TestCase should run on selection of project and files in the project should be loaded before the test case starts executing but Testcase starts executing without waiting for the Loading Process.

Loading of the files in the project was provided by Eclipse (Plugin :-org.eclipse.sphinx.emf.workspace.loading ) using ProgressBar. By surfing through the Internet i have foundout
bot.waitUntil() is used to halt TestCase if any operation have to be completed before proceeding further. I have tried multiple options but unable to get the result i was expecting.

Can any one help me on this one

El cucuy
  • 85
  • 9
  • I don't know the plug-in, but if you're using [this LoadJobScheduler](https://github.com/IncQueryLabs/org.eclipse.sphinx/blob/e622fe11f2047a31afac185964be816676bdac98/plugins/org.eclipse.sphinx.emf.workspace/src/org/eclipse/sphinx/emf/workspace/loading/LoadJobScheduler.java#L38) then `Job.getJobManager().join(IExtendedPlatformConstants.FAMILY_MODEL_LOADING, new NullProgressMonitor())` should work. `LoadJobScheduler` launches instances of `ModelLoadJob` in background which belong to the `FAMILY_MODEL_LOADING` family (I'll expand as an answer if that fits your needs). – Emmanuel Chebbi May 12 '20 at 11:42
  • Thank you for the reply Emmanuel.you are right it is using LoadJobScheduler provided by eclipse (org.eclipse.sphinx.emf.workspace.loading).in SWTBot, there was method called **bot.waitUntil()** which will except ProgressMonitor Shell i didnt know how to get it.can you elaborate your answer may be it might help in my quest for the answer. – El cucuy May 13 '20 at 11:15
  • I've submitted a more detailed answer. Please consider accepting it if it fulfils your needs or explaining why it doesn't so that it can be improved. – Emmanuel Chebbi May 25 '20 at 08:03

1 Answers1

0

I advise you to call:

Job.getJobManager().join(
    IExtendedPlatformConstants.FAMILY_MODEL_LOADING, 
    new NullProgressMonitor()
);

instead of using bot.waitUntil. It should block the current thread (i.e. your test) until the file loading is done.

The Progress View is automatically filled by the Eclipse UI when Eclipse Jobs are running in background. In your case, those jobs are scheduled by the LoadJobScheduler. The idea is to directly use Eclipse's Jobs API to wait for those jobs to end instead of querying the UI. This is exactly what Job.getJobManager().join does, see also this answer.

Note: join takes as argument the family to which belong the jobs to wait for. I found it in the implementation of the loading Job.

Preventing infinite loops

Since join is blocking it might lead to infinite loops. You can use JobManager::find to check running jobs without blocking the thread:

bot.waitUntil(new DefaultCondition() {
    @Override
    public boolean test() throws Exception {
        String family = IExtendedPlatformConstants.FAMILY_MODEL_LOADING;
        boolean allJobsAreDone = Job.getJobManager().find(family).length == 0;
        return allJobsAreDone;
    }
}); 

With the code above, SWTBot will throw a TimeoutException if Jobs are still found running after a few seconds.

Emmanuel Chebbi
  • 456
  • 3
  • 5
  • 13