def setUp(self):
        # Call superclass
        super(GhostWindowTests, self).setUp()

        # Set up
        self.window = TabbedWindow()
        self.window.addView(QtGui.QWidget(), "test")

        self.tabbar = self.window.tabs.tabBar()

        # Check if first tab has geometry and save top left corner
        self.assertFalse(self.tabbar.tabRect(0).isNull())

        self.tab_pos = self.tabbar.tabRect(0).topLeft()

        # Create ghost window
        self.ghost = GhostWindow(self.tabbar, self.tab_pos)
    def setUp(self):
        # Call superclass
        super(TabBarTests, self).setUp()

        # Set up
        self.window = TabbedWindow()
        self.window.addView(QtGui.QWidget(), "test")
        self.window.show()

        # Move the window just away from the screen's origin to avoid problems
        # with top/right toolbars
        self.window.move(QtCore.QPoint(100, 100))

        # Create ghost window
        tabbar = self.window.tabs.tabBar()
        local_pos = tabbar.tabRect(0).topLeft()

        self.tabbar = tabbar
        self.tab_pos = tabbar.mapToGlobal(local_pos)
        self.ghost = GhostWindow(tabbar, local_pos)
class TabBarTests(WidgetTestsMixin, unittest.TestCase):
    """
    TabBar test cases
    """

    def setUp(self):
        # Call superclass
        super(TabBarTests, self).setUp()

        # Set up
        self.window = TabbedWindow()
        self.window.addView(QtGui.QWidget(), "test")
        self.window.show()

        # Move the window just away from the screen's origin to avoid problems
        # with top/right toolbars
        self.window.move(QtCore.QPoint(100, 100))

        # Create ghost window
        tabbar = self.window.tabs.tabBar()
        local_pos = tabbar.tabRect(0).topLeft()

        self.tabbar = tabbar
        self.tab_pos = tabbar.mapToGlobal(local_pos)
        self.ghost = GhostWindow(tabbar, local_pos)

    def test_create_new_window(self):
        index = self.ghost.index()
        view = self.window.tabs.widget(index)
        text = self.window.tabs.tabText(index)

        # pylint: disable=W0212
        window = self.tabbar._create_new_window(self.ghost)
        # pylint: enable=W0212

        self.assertEqual(window.geometry(), self.ghost.geometry())
        self.assertFalse(self.tabbar.count())
        self.assertEqual(window.tabs.count(), 1)
        self.assertEqual(window.tabs.widget(0), view)
        self.assertEqual(window.tabs.tabText(0), text)

    def test_move_to_window(self):
        # Create destination window with one tab
        dest = TabbedWindow()
        dest.addView(QtGui.QWidget(), "test")

        # Get reference of the view to be moved
        index = self.ghost.index()
        view = self.window.tabs.widget(index)
        text = self.window.tabs.tabText(index)

        # Move tab into new window
        self.tabbar._move_to_window(dest, dest.tabs.tabBar().tabRect(0).topLeft(), self.ghost)  # pylint: disable=W0212

        # Check
        self.assertFalse(self.window.tabs.count())
        self.assertEqual(dest.tabs.count(), 2)
        self.assertEqual(dest.tabs.widget(0), view)
        self.assertEqual(dest.tabs.tabText(0), text)
        self.assertEqual(dest.currentView(), view)

    @patch.object(TabbedWindow, "close")
    def test_tab_removed(self, mock_close):
        """
        Close the window when the last tab is removed
        """
        # Remove tabs
        for i in xrange(self.window.tabs.count()):  # pylint: disable=W0612
            self.window.removeView(0)

        # Check
        mock_close.assert_called_once_with()

    def test_mouse_press_event(self):
        # Default state
        self.assertIsNone(self.tabbar._ghost)  # pylint: disable=W0212

        # Simulate mouse press event
        self.tabbar.mousePressEvent(MouseEvent(self.tab_pos))

        # Check
        # pylint: disable=W0212
        self.assertIsInstance(self.tabbar._ghost, GhostWindow)
        self.assertTrue(self.tabbar._ghost.isVisible())
        # pylint: enable=W0212

    def test_mouse_move_event(self):
        # Default state
        self.tabbar.mousePressEvent(MouseEvent(self.tab_pos))

        # pylint: disable=W0212
        self.assertIsInstance(self.tabbar._ghost, GhostWindow)
        # pylint: enable=W0212

        # Simulate mouse move
        pos = self.tab_pos + QtCore.QPoint(
            QtGui.QApplication.startDragDistance(), QtGui.QApplication.startDragDistance()
        )

        self.tabbar.mouseMoveEvent(MouseEvent(pos))

        # Check
        # pylint: disable=W0212
        self.assertEqual(self.tabbar._ghost.pos(), pos)
        # pylint: enable=W0212

    def test_mouse_release_new_window(self):
        """
        Release mouse create new window
        """
        # Add extra tab
        self.window.addView(QtGui.QWidget(), "test")

        self.assertGreater(self.window.tabs.count(), 1)

        # Simulate mouse press and move outside the window area
        pos = self.window.geometry().topRight()
        pos = self.window.mapToGlobal(pos)
        pos += QtCore.QPoint(10, 10)

        self.assertIsNone(QtGui.QApplication.widgetAt(pos))

        self.tabbar.mousePressEvent(MouseEvent(self.tab_pos))
        self.tabbar.mouseMoveEvent(MouseEvent(pos))

        with patch.object(self.tabbar, "_create_new_window") as mock_create:
            # Simulate mouse release
            self.tabbar.mouseReleaseEvent(MouseEvent(pos))

            # Check
            # pylint: disable=W0212
            mock_create.assert_called_once_with(self.tabbar._ghost)
            # pylint: enable=W0212

    def test_mouse_release_move_tab(self):
        """
        Release mouse moving a tab into another window
        """
        # Add extra window
        dest = TabbedWindow()
        dest.addView(QtGui.QWidget(), "test")
        dest.move(self.window.geometry().topRight())
        dest.show()

        # Simulate mouse press and move outside the window area
        pos = dest.tabs.tabBar().tabRect(0).topLeft()
        pos = dest.tabs.tabBar().mapToGlobal(pos)

        self.tabbar.mousePressEvent(MouseEvent(self.tab_pos))
        self.tabbar.mouseMoveEvent(MouseEvent(pos))

        with patch.object(self.tabbar, "_move_to_window") as mock_create:
            # Simulate mouse release
            event = MouseEvent(pos)
            ghost = self.tabbar._ghost  # pylint: disable=W0212

            self.tabbar.mouseReleaseEvent(event)

            # Check
            mock_create.assert_called_once_with(dest, event.globalPos(), ghost)  # pylint: disable=W0212
class GhostWindowTests(WidgetTestsMixin, unittest.TestCase):
    """
    GhostWindow test cases
    """

    def setUp(self):
        # Call superclass
        super(GhostWindowTests, self).setUp()

        # Set up
        self.window = TabbedWindow()
        self.window.addView(QtGui.QWidget(), "test")

        self.tabbar = self.window.tabs.tabBar()

        # Check if first tab has geometry and save top left corner
        self.assertFalse(self.tabbar.tabRect(0).isNull())

        self.tab_pos = self.tabbar.tabRect(0).topLeft()

        # Create ghost window
        self.ghost = GhostWindow(self.tabbar, self.tab_pos)

    def test_constructor(self):
        self.assertEqual(self.window.geometry(), self.ghost.geometry())
        self.assertLess(self.ghost.windowOpacity(), 1)
        self.assertTrue(self.ghost.testAttribute(Qt.WA_TransparentForMouseEvents))
        self.assertEqual(self.ghost.index(), 0)
        self.assertEqual(self.ghost.offset(), self.tabbar.mapToGlobal(self.tab_pos) - self.window.pos())

    def test_move_with_offset(self):
        pos = self.ghost.pos()
        movement = QtCore.QPoint(10, 10)

        self.ghost.moveWithOffset(movement)

        self.assertEqual(self.ghost.pos() - pos, movement)

    def test_drag_started(self):
        # No drag
        origin = self.ghost.pos()
        drag_distance = QtGui.QApplication.startDragDistance()
        pos = origin + QtCore.QPoint(drag_distance / 4, drag_distance / 4)

        self.assertFalse(self.ghost.dragStarted(pos))

        # Is dragging
        pos = origin + QtCore.QPoint(drag_distance, drag_distance)

        self.assertTrue(self.ghost.dragStarted(pos))