Package Camelot :: Package camelot :: Package test |
[frames] | no frames]

# Source Code for Package Camelot.camelot.test

      1  """
      2  Camelot unittest framework
      3  """
      4
      5  import unittest
      6
      7  _application_ = []
      8
      9 -def get_application():
     10      """Get the singleton QApplication"""
     11      from PyQt4.QtGui import QApplication
     12      if not len(_application_):
     13          import sys
     14          _application_.append(QApplication(sys.argv))
     15      return _application_[0]
     16
     17 -class ModelThreadTestCase(unittest.TestCase):
     18      """Base class for implementing test cases that need a running model_thread.
     19      """
     20
     21      images_path = ''
     22
     23 -    def grab_widget(self, widget, suffix=None, subdir=None):
     24          """Save a widget as a png file :
     25      :param widget: the widget to take a screenshot of
     26      :param suffix: string to add to the default filename of the image
     27      :param subdir: subdirectory of images_path in which to put the image file, defaults to
     28      the name of the test class
     29      - the name of the png file is the name of the test case, without 'test_'
     30      - it is stored in the directory with the same name as the class, without 'test'
     31          """
     32          import sys
     33          import os
     34          from PyQt4 import QtGui
     35          from PyQt4.QtGui import QPixmap
     36          if not subdir:
     37              images_path = os.path.join(self.images_path, self.__class__.__name__.lower()[:-len('Test')])
     38          else:
     39              images_path = os.path.join(self.images_path, subdir)
     40          if not os.path.exists(images_path):
     41              os.makedirs(images_path)
     42          test_case_name = sys._getframe(1).f_code.co_name[5:]
     43          image_name = '%s.png'%test_case_name
     44          if suffix:
     45              image_name = '%s_%s.png'%(test_case_name, suffix)
     46          widget.adjustSize()
     47          self.process()
     48          QtGui.QApplication.flush()
     49          inner_pixmap = QPixmap.grabWidget(widget)
     50          #
     51          # we'll create a label that contains a screenshot of our widget and
     52          # take a screenshot of that label, for the sole purpose of adding a border
     53          #
     54          parent_widget = QtGui.QLabel()
     55          parent_widget.setPixmap(inner_pixmap)
     56          parent_widget.setFrameStyle(QtGui.QFrame.Panel | QtGui.QFrame.Plain)
     57          parent_widget.setObjectName('grab_widget_parent')
     58          parent_widget.setLineWidth(2)
     59          parent_widget.setStyleSheet("""
     60          #grab_widget_parent {
     61          border: 2px solid gray;
     62          }""")
     63          parent_widget.adjustSize()
     64          outer_pixmap = QPixmap.grabWidget(parent_widget)
     65          outer_pixmap.save(os.path.join(images_path, image_name), 'PNG')
     66
     67 -    def process(self):
     68          """Wait until all events are processed and the queues of the model thread are empty"""
     69          self.mt.wait_on_work()
     70
     71 -    def setUp(self):
     72          self.app = get_application()
     73          from camelot.view import model_thread
     74          from camelot.view.model_thread.no_thread_model_thread import NoThreadModelThread
     75          from camelot.view.model_thread import get_model_thread, construct_model_thread, has_model_thread
     76          from camelot.view.remote_signals import construct_signal_handler, has_signal_handler
     77          if not has_model_thread():
     78              #
     79              # Run the tests without real threading, to avoid timing problems with screenshots etc.
     80              #
     81              model_thread._model_thread_.insert( 0, NoThreadModelThread() )
     82          if not has_signal_handler():
     83              construct_signal_handler()
     84          self.mt = get_model_thread()
     85          if not self.mt.isRunning():
     86              self.mt.start()
     87          # make sure the startup sequence has passed
     88          self.process()
     89
     90 -    def tearDown(self):
     91          self.process()
     92          #self.mt.exit(0)
     93          #self.mt.wait()
     94
     95 -class SchemaTest(ModelThreadTestCase):
     96      """Test the database schema"""
     97
     98 -    def test_schema_display(self):
     99
    100          def schema_display_task():
    101              import os
    102              from camelot.bin.camelot_manage import schema_display
    103              schema_display(os.path.join(self.images_path, 'schema.png'))
    104
    105          from camelot.view.model_thread import get_model_thread, post
    106          post( schema_display_task )
    107          get_model_thread().wait_on_work()
    108
    109 -class ApplicationViewsTest(ModelThreadTestCase):
    110      """Test various application level views, like the main window, the
    111      sidepanel"""
    112
    113 -    def get_application_admin(self):
    114          """Overwrite this method to make use of a custom application admin"""
    115          from camelot.admin.application_admin import ApplicationAdmin
    116          return ApplicationAdmin()
    117
    129
    130 -    def test_main_window(self):
    131          from camelot.view.mainwindow import MainWindow
    132          from PyQt4 import QtCore
    133          translator = self.get_application_admin().get_translator()
    134          QtCore.QCoreApplication.installTranslator(translator)
    135          app_admin = self.get_application_admin()
    136          #widget = MainWindow(app_admin)
    137          #self.grab_widget(widget, subdir='applicationviews')
    138
    139 -    def test_tool_bar(self):
    140          from camelot.view.mainwindow import MainWindow
    141          from PyQt4 import QtCore
    142          translator = self.get_application_admin().get_translator()
    143          QtCore.QCoreApplication.installTranslator(translator)
    144          app_admin = self.get_application_admin()
    145          main_window = MainWindow(app_admin)
    146          self.grab_widget(main_window.get_tool_bar(), subdir='applicationviews')
    147
    148 -class EntityViewsTest(ModelThreadTestCase):
    149      """Test the views of all the Entity subclasses, subclass this class to test all views
    150      in your application.  This is done by calling the create_table_view and create_new_view
    151      on a set of admin objects.  To tell the test case which admin objects should be tested,
    152      overwrite the get_admins method ::
    153
    154      class MyEntityViewsTest(EntityViewsTest):
    155
    156          def get_admins(self):
    157            from elixir import entities
    158            application_admin import MyApplicationAdmin
    159            self.app_admin = MyApplicationAdmin()
    160            return [self.app_admin.get_entity_admin(e) for e in entities if self.app_admin.get_entity_admin(e)]
    161
    162      """
    163
    164 -    def setUp(self):
    165          super(EntityViewsTest, self).setUp()
    166          from PyQt4 import QtCore
    167          translator = self.get_application_admin().get_translator()
    168          QtCore.QCoreApplication.installTranslator(translator)
    169
    170 -    def get_application_admin(self):
    171          """Overwrite this method to make use of a custom application admin"""
    172          from camelot.admin.application_admin import ApplicationAdmin
    173          return ApplicationAdmin()
    174
    175 -    def get_admins(self):
    176          """Should return all admin for which a table and a form view should be displayed,
    177          by default, returns for all entities their default admin"""
    178          from elixir import entities
    179          app_admin = self.get_application_admin()
    180          return [app_admin.get_entity_admin(e) for e in entities if app_admin.get_entity_admin(e)]
    181
    182 -    def test_select_view(self):
    183          from PyQt4 import QtCore
    184          translator = self.get_application_admin().get_translator()
    185          QtCore.QCoreApplication.installTranslator(translator)
    186          #for admin in self.get_admins():
    187          #    widget = admin.create_select_view()
    188          #    self.grab_widget(widget, suffix=admin.entity.__name__.lower(), subdir='entityviews')
    189
    190 -    def test_table_view(self):
    191          from PyQt4 import QtCore
    192          translator = self.get_application_admin().get_translator()
    193          QtCore.QCoreApplication.installTranslator(translator)
    194          #for admin in self.get_admins():
    195          #    widget = admin.create_table_view()
    196          #    self.grab_widget(widget, suffix=admin.entity.__name__.lower(), subdir='entityviews')
    197
    198 -    def test_new_view(self):
    199          from PyQt4 import QtCore
    200          translator = self.get_application_admin().get_translator()
    201          QtCore.QCoreApplication.installTranslator(translator)
    202          for admin in self.get_admins():
    203              widget = admin.create_new_view()
    204              self.grab_widget(widget, suffix=admin.entity.__name__.lower(), subdir='entityviews')
    205