Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
berryUITestApplication.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 BlueBerry Platform
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "berryUITestApplication.h"
18 
20 #include <berryPlatformUI.h>
22 #include <berryIExtension.h>
23 #include <berryStarter.h>
24 
25 #include "internal/berryUITestWorkbenchAdvisor.h"
26 
27 namespace berry
28 {
29 
30 class WorkbenchCloseRunnable: public Poco::Runnable
31 {
32 public:
33  WorkbenchCloseRunnable(IWorkbench* workbench) :
34  workbench(workbench)
35  {
36  }
37 
38  void run()
39  {
40  workbench->Close();
41  }
42 
43 private:
44  IWorkbench* workbench;
45 };
46 
47 UITestApplication::TestRunnable::TestRunnable(UITestApplication* app,
48  const QString& testPlugin) :
49  app(app), testPlugin(testPlugin)
50 {
51 }
52 
53 void UITestApplication::TestRunnable::run()
54 {
55  try
56  {
57  app->testDriverResult = BlueBerryTestDriver::Run(testPlugin, true);
58  }
59  catch (const ctkException& e)
60  {
61  qWarning() << e.printStackTrace();
62  }
63  catch (const std::exception& e)
64  {
65  qWarning() << e.what();
66  }
67 }
68 
70 {
71 
72 }
73 
75 {
76  // Get the plug-in to test
77  try
78  {
79  testPlugin = QString::fromStdString(Platform::GetConfiguration().getString(
80  Platform::ARG_TESTPLUGIN.toStdString()));
81  } catch (const Poco::NotFoundException& /*e*/)
82  {
83  BERRY_ERROR << "You must specify a test plug-in id via "
84  << Platform::ARG_TESTPLUGIN << "=<id>";
85  return 1;
86  }
87 
88  // Get the application to test
89  IApplication* application = GetApplication();
90  poco_assert(application);
91  int result = RunApplication(application);
92 
93  if (IApplication::EXIT_OK != result)
94  {
95  std::cerr << "UITestRunner: Unexpected result from running application " << application << ": " << result << std::endl;
96  }
97  return testDriverResult;
98 }
99 
101 {
102  IWorkbench* workbench = PlatformUI::GetWorkbench();
103  if (!workbench)
104  return;
105  Display* display = workbench->GetDisplay();
106  WorkbenchCloseRunnable runnable(workbench);
107  display->SyncExec(&runnable);
108 }
109 
111 {
112  TestRunnable runnable(this, testPlugin);
113  testableObject->TestingStarting();
114  testableObject->RunTest(&runnable);
115  testableObject->TestingFinished();
116 }
117 
118 IApplication* UITestApplication::GetApplication()
119 {
120 
121  IExtension::Pointer extension;
122  /*Platform::GetExtensionPointService()->GetExtension(
123  Starter::XP_APPLICATIONS, GetApplicationToRun());*/
124 
125  QList<IConfigurationElement::Pointer> extensions(
126  Platform::GetExtensionRegistry()->GetConfigurationElementsFor(Starter::XP_APPLICATIONS));
127 
128  QString appToRun = GetApplicationToRun();
129  QString id;
130  foreach (const IConfigurationElement::Pointer& configElem, extensions)
131  {
132  id = configElem->GetAttribute("id");
133  if(id == appToRun)
134  {
135  extension = configElem->GetDeclaringExtension();
136  break;
137  }
138  }
139 
140  IApplication* app = 0;
141 
142  if (extension)
143  {
144  QList<IConfigurationElement::Pointer> elements(
145  extension->GetConfigurationElements());
146  if (!elements.isEmpty())
147  {
148  QList<IConfigurationElement::Pointer> runs(
149  elements[0]->GetChildren("run"));
150  if (!runs.isEmpty())
151  {
152  app = runs[0]->CreateExecutableExtension<IApplication> ("class");
153  }
154  }
155  return app;
156  }
157 
158  return this;
159 }
160 
161 QString UITestApplication::GetApplicationToRun()
162 {
163 
164  QString testApp;
165  try
166  {
167  testApp = QString::fromStdString(Platform::GetConfiguration().getString(
168  Platform::ARG_TESTAPPLICATION.toStdString()));
169  }
170  catch (const Poco::NotFoundException&)
171  {
172  }
173 
174  return testApp;
175 }
176 
177 int UITestApplication::RunApplication(IApplication* application)
178 {
179  testableObject = PlatformUI::GetTestableObject();
180  testableObject->SetTestHarness(ITestHarness::Pointer(this));
181 
182  if (application == dynamic_cast<IApplication*>(this))
183  return RunUITestWorkbench();
184 
185  return application->Start();
186 }
187 
188 int UITestApplication::RunUITestWorkbench()
189 {
190  Display* display = PlatformUI::CreateDisplay();
191  UITestWorkbenchAdvisor advisor;
192  return PlatformUI::CreateAndRunWorkbench(display, &advisor);
193 }
194 
195 }
static TestableObject::Pointer GetTestableObject()
itk::SmartPointer< Self > Pointer
static const int EXIT_OK
static Display * CreateDisplay()
virtual void SyncExec(Poco::Runnable *)=0
#define BERRY_ERROR
Definition: berryLog.h:26
static IWorkbench * GetWorkbench()
static IExtensionRegistry * GetExtensionRegistry()
virtual Display * GetDisplay() const =0
static int CreateAndRunWorkbench(Display *display, WorkbenchAdvisor *advisor)