try:
    import unittest2 as ut
except ImportError:
    import unittest as ut

import sys
import os
import platform
import locale

# Hack to circumvent OS X locale bug
if platform.system() == 'Darwin':
    if os.getenv('LC_CTYPE') == 'UTF-8':
        os.environ['LC_CTYPE'] = ''
else:
    locale.setlocale(locale.LC_ALL, '')

# Use new style API
import sip
sip.setapi('QString', 2)
sip.setapi('QDate', 2)
sip.setapi('QDateTime', 2)
sip.setapi('QTextStream', 2)
sip.setapi('QTime', 2)
sip.setapi('QVariant', 2)
sip.setapi('QUrl', 2)
sip.setapi('QVariant', 2)

from PyQt4 import QtGui
from PyQt4.QtTest import QTest
from PyQt4.QtCore import Qt
from spykeviewer.ui.main_window_neo import MainWindowNeo


class TestMainWindow(ut.TestCase):
    @classmethod
    def setUpClass(cls):
        print 'warte mal'
        cls.app = QtGui.QApplication(sys.argv)
        print "It's a setup!"
        cls.win = MainWindowNeo()

    @classmethod
    def tearDownClass(cls):
        cls.win.close()

    def test_object_selections(self):
        self.assertNotEqual(self.win.neo_blocks(), None)
        self.assertNotEqual(self.win.neo_segments(), None)
        self.assertNotEqual(self.win.neo_channel_groups(), None)
        self.assertNotEqual(self.win.neo_channels(), None)
        self.assertNotEqual(self.win.neo_units(), None)

    def test_plugins_loaded(self):
        self.assert_(self.win.plugin_model.hasIndex(0,0),
            'Plugin model is empty')

    def test_filter_tree(self):
        self.assertEqual(self.win.filterTreeWidget.topLevelItemCount(), 5)

    def test_file_tree(self):
        self.assert_(self.win.fileTreeView.model().hasIndex(0,0),
            'Filesystem model is empty')

    def test_console(self):
        QTest.keyClick(self.win.console, Qt.Key_1)
        QTest.keyClick(self.win.console, Qt.Key_Plus)
        QTest.keyClick(self.win.console, Qt.Key_1)
        lines = self.win.console.get_line_count()
        self.assertEqual(self.win.console.get_text_line(lines-1), '>>> 1+1')

        QTest.keyClick(self.win.console, Qt.Key_Enter)
        lines = self.win.console.get_line_count()
        self.assertEqual(self.win.console.get_text_line(lines-1), '>>> ')

    def test_history(self):
        history_lines = self.win.history.get_line_count()
        QTest.keyClick(self.win.console, Qt.Key_5)
        QTest.keyClick(self.win.console, Qt.Key_Enter)
        self.assertEqual(history_lines+1, self.win.history.get_line_count())
        self.assertEqual(self.win.history.get_text_line(history_lines), '5')

    def test_plugin_editor(self):
        self.assertEqual(self.win.pluginEditorDock.tabs.currentWidget(), None)
        self.win.actionNewPlugin.trigger()
        self.assertNotEqual(self.win.pluginEditorDock.tabs.currentWidget(),
            None)
        self.assertEqual(u'\n'.join(self.win.pluginEditorDock.code()),
            unicode(self.win.pluginEditorDock.template_code+'\n'))


if __name__ == '__main__':
    ut.main()
@@ -1362,32 +1362,45 @@
    self.assert_(
+%0A            
 self.win.plugin_
@@ -1408,32 +1408,33 @@
 odel.hasIndex(0,
+ 
 0),%0A            
@@ -1516,24 +1516,48 @@
 rtEqual(
-self.win
+%0A            self.win.filterDock
 .filterT
@@ -1642,16 +1642,29 @@
 assert_(
+%0A            
 self.win
@@ -1696,16 +1696,17 @@
 Index(0,
+ 
 0),%0A    
@@ -1994,32 +1994,45 @@
 elf.assertEqual(
+%0A            
 self.win.console
@@ -2043,33 +2043,35 @@
 _text_line(lines
--
+ - 
 1), '%3E%3E%3E 1+1')%0A%0A
@@ -2236,17 +2236,19 @@
 ne(lines
--
+ - 
 1), '%3E%3E%3E
@@ -2469,16 +2469,29 @@
 rtEqual(
+%0A            
 history_
@@ -2495,17 +2495,19 @@
 ry_lines
-+
+ + 
 1, self.
@@ -2553,32 +2553,45 @@
 elf.assertEqual(
+%0A            
 self.win.history
@@ -2678,32 +2678,45 @@
 elf.assertEqual(
+%0A            
 self.win.pluginE
@@ -2816,32 +2816,45 @@
 .assertNotEqual(
+%0A            
 self.win.pluginE
@@ -2884,28 +2884,16 @@
 idget(),
-%0A           
  None)%0A 
@@ -2916,16 +2916,29 @@
 rtEqual(
+%0A            
 u'%5Cn'.jo
@@ -3034,17 +3034,19 @@
 ate_code
-+
+ + 
 '%5Cn'))%0A%0A

Test adapted to filter dock