text: /*===================================================================
  
  BlueBerry Platform
  
  Copyright (c) German Cancer Research Center,
  Division of Medical and Biological Informatics.
  All rights reserved.
  
  This software is distributed WITHOUT ANY WARRANTY; without
  even the implied warranty of MERCHANTABILITY or FITNESS FOR
  A PARTICULAR PURPOSE.
  
  See LICENSE.txt or http://www.mitk.org for details.
  
  ===================================================================*/
  
  #include "berryUITestApplication.h"
  
  #include <berryBlueBerryTestDriver.h>
  #include <berryPlatformUI.h>
  #include <berryIExtensionPointService.h>
  #include <berryStarter.h>
  
  #include "internal/berryUITestWorkbenchAdvisor.h"
  
  namespace berry
  {
  
  class WorkbenchCloseRunnable: public Poco::Runnable
  {
  public:
    WorkbenchCloseRunnable(IWorkbench* workbench) :
      workbench(workbench)
    {
    }
  
    void run()
    {
      workbench->Close();
    }
  
  private:
    IWorkbench* workbench;
  };
  
  UITestApplication::TestRunnable::TestRunnable(UITestApplication* app,
      const std::string& testPlugin) :
    app(app), testPlugin(testPlugin)
  {
  }
  
  void UITestApplication::TestRunnable::run()
  {
    try
    {
      app->testDriverResult = BlueBerryTestDriver::Run(testPlugin, true);
    } catch (Poco::IOException& e)
    {
      std::cerr << e.displayText() << std::endl;
      //TODO print stack trace
      //Debug::PrintStackTrace();
    }
  }
  
  UITestApplication::UITestApplication()
  {
  
  }
  
  UITestApplication::UITestApplication(const UITestApplication& other)
  {
    Q_UNUSED(other)
    throw std::logic_error("Copy constructor not implemented");
  }
  
  int UITestApplication::Start()
  {
    // Get the plug-in to test
    try
    {
      testPlugin = Platform::GetConfiguration().getString(
          Platform::ARG_TESTPLUGIN);
    } catch (const Poco::NotFoundException& /*e*/)
    {
      BERRY_ERROR << "You must specify a test plug-in id via "
          << Platform::ARG_TESTPLUGIN << "=<id>";
      return 1;
    }
  
    // Get the application to test
    IApplication* application = GetApplication();
    poco_assert(application);
    int result = RunApplication(application);
  
    if (IApplication::EXIT_OK != result)
    {
      std::cerr << "UITestRunner: Unexpected result from running application " << application << ": " << result << std::endl;
    }
    return testDriverResult;
  }
  
  void UITestApplication::Stop()
  {
    IWorkbench* workbench = PlatformUI::GetWorkbench();
    if (!workbench)
    return;
    Display* display = workbench->GetDisplay();
    WorkbenchCloseRunnable runnable(workbench);
    display->SyncExec(&runnable);
  }
  
  void UITestApplication::RunTests()
  {
    TestRunnable runnable(this, testPlugin);
    testableObject->TestingStarting();
    testableObject->RunTest(&runnable);
    testableObject->TestingFinished();
  }
  
  IApplication* UITestApplication::GetApplication() throw (CoreException)
  {
  
    const IExtension* extension = 0;
    /*Platform::GetExtensionPointService()->GetExtension(
        Starter::XP_APPLICATIONS, GetApplicationToRun());*/
  
      IConfigurationElement::vector extensions(
          Platform::GetExtensionPointService()->GetConfigurationElementsFor(Starter::XP_APPLICATIONS));
      IConfigurationElement::vector::iterator iter;
  
      std::string appToRun = GetApplicationToRun();
      std::string id;
      for (iter = extensions.begin(); iter != extensions.end(); ++iter)
      {
        if((*iter)->GetAttribute("id", id))
        {
          if(id == appToRun)
          {
            extension = (*iter)->GetDeclaringExtension();
            break;
          }
        }
      }
  
    IApplication* app = 0;
  
    if (extension)
    {
      std::vector<IConfigurationElement::Pointer> elements(
          extension->GetConfigurationElements());
      if (elements.size() > 0)
      {
        std::vector<IConfigurationElement::Pointer> runs(
            elements[0]->GetChildren("run"));
        if (runs.size() > 0)
        {
          app = runs[0]->CreateExecutableExtension<IApplication> ("class"); //$NON-NLS-1$
          if (app == 0)
          {
            // support legacy BlueBerry extensions
            app = runs[0]->CreateExecutableExtension<IApplication> ("class", IApplication::GetManifestName());
          }
        }
      }
      return app;
    }
  
    return this;
  }
  
  std::string UITestApplication::GetApplicationToRun()
  {
  
    std::string testApp;
    try
    {
      testApp = Platform::GetConfiguration().getString(
          Platform::ARG_TESTAPPLICATION);
    }
    catch (Poco::NotFoundException)
    {
    }
  
    return testApp;
  }
  
  int UITestApplication::RunApplication(IApplication* application)
  {
    testableObject = PlatformUI::GetTestableObject();
    testableObject->SetTestHarness(ITestHarness::Pointer(this));
  
    if (application == dynamic_cast<IApplication*>(this))
    return RunUITestWorkbench();
  
    return application->Start();
  }
  
  int UITestApplication::RunUITestWorkbench()
  {
    Display* display = PlatformUI::CreateDisplay();
    UITestWorkbenchAdvisor advisor;
    return PlatformUI::CreateAndRunWorkbench(display, &advisor);
  }
  
  }
  