Skip to content

Commit

Permalink
Add some layout undo/redo tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 5, 2017
1 parent 8b490de commit 6471876
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
48 changes: 48 additions & 0 deletions tests/src/python/test_qgslayoutgridsettings.py
Expand Up @@ -72,6 +72,54 @@ def testReadWriteXml(self):
self.assertEqual(s2.offset().y(), 7.0)
self.assertEqual(s2.offset().units(), QgsUnitTypes.LayoutPixels)

def testUndoRedo(self):
p = QgsProject()
l = QgsLayout(p)
g = l.gridSettings()
g.setResolution(QgsLayoutMeasurement(15, QgsUnitTypes.LayoutPoints))

# these two commands should be 'collapsed'
g.setOffset(QgsLayoutPoint(555, 10, QgsUnitTypes.LayoutPoints))
g.setOffset(QgsLayoutPoint(5, 10, QgsUnitTypes.LayoutPoints))

# these two commands should be 'collapsed'
g.setResolution(QgsLayoutMeasurement(45, QgsUnitTypes.LayoutInches))
g.setResolution(QgsLayoutMeasurement(35, QgsUnitTypes.LayoutInches))

self.assertEqual(g.offset().x(), 5.0)
self.assertEqual(g.offset().y(), 10.0)
self.assertEqual(g.offset().units(), QgsUnitTypes.LayoutPoints)
self.assertEqual(g.resolution().length(), 35.0)
self.assertEqual(g.resolution().units(), QgsUnitTypes.LayoutInches)

l.undoStack().stack().undo()
self.assertEqual(g.offset().x(), 5.0)
self.assertEqual(g.offset().y(), 10.0)
self.assertEqual(g.offset().units(), QgsUnitTypes.LayoutPoints)
self.assertEqual(g.resolution().length(), 15.0)
self.assertEqual(g.resolution().units(), QgsUnitTypes.LayoutPoints)

l.undoStack().stack().undo()
self.assertEqual(g.offset().x(), 0.0)
self.assertEqual(g.offset().y(), 0.0)
self.assertEqual(g.offset().units(), QgsUnitTypes.LayoutMillimeters)
self.assertEqual(g.resolution().length(), 15.0)
self.assertEqual(g.resolution().units(), QgsUnitTypes.LayoutPoints)

l.undoStack().stack().redo()
self.assertEqual(g.offset().x(), 5.0)
self.assertEqual(g.offset().y(), 10.0)
self.assertEqual(g.offset().units(), QgsUnitTypes.LayoutPoints)
self.assertEqual(g.resolution().length(), 15.0)
self.assertEqual(g.resolution().units(), QgsUnitTypes.LayoutPoints)

l.undoStack().stack().redo()
self.assertEqual(g.offset().x(), 5.0)
self.assertEqual(g.offset().y(), 10.0)
self.assertEqual(g.offset().units(), QgsUnitTypes.LayoutPoints)
self.assertEqual(g.resolution().length(), 35.0)
self.assertEqual(g.resolution().units(), QgsUnitTypes.LayoutInches)


if __name__ == '__main__':
unittest.main()
50 changes: 50 additions & 0 deletions tests/src/python/test_qgslayoutpagecollection.py
Expand Up @@ -502,6 +502,56 @@ def testReadWriteXml(self):
self.assertEqual(collection2.pageStyleSymbol().symbolLayer(0).color().name(), '#00ff00')
self.assertEqual(collection2.pageStyleSymbol().symbolLayer(0).strokeColor().name(), '#ff0000')

def testUndoRedo(self):
p = QgsProject()
l = QgsLayout(p)
collection = l.pageCollection()

# add a page
page = QgsLayoutItemPage(l)
page.setPageSize('A4')
collection.addPage(page)
self.assertEqual(collection.pageCount(), 1)

l.undoStack().stack().undo()
self.assertEqual(collection.pageCount(), 0)

l.undoStack().stack().redo()
self.assertEqual(collection.pageCount(), 1)
# make sure page is accessible
self.assertEqual(collection.page(0).pageSize().width(), 210)

# add a second page
page2 = QgsLayoutItemPage(l)
page2.setPageSize('A5')
collection.addPage(page2)

# delete page
collection.deletePage(collection.page(0))
self.assertEqual(collection.pageCount(), 1)

l.undoStack().stack().undo()
self.assertEqual(collection.pageCount(), 2)
# make sure pages are accessible
self.assertEqual(collection.page(0).pageSize().width(), 210)
self.assertEqual(collection.page(1).pageSize().width(), 148)

l.undoStack().stack().undo()
self.assertEqual(collection.pageCount(), 1)
l.undoStack().stack().undo()
self.assertEqual(collection.pageCount(), 0)

l.undoStack().stack().redo()
self.assertEqual(collection.pageCount(), 1)
self.assertEqual(collection.page(0).pageSize().width(), 210)
l.undoStack().stack().redo()
self.assertEqual(collection.pageCount(), 2)
self.assertEqual(collection.page(0).pageSize().width(), 210)
self.assertEqual(collection.page(1).pageSize().width(), 148)
l.undoStack().stack().redo()
self.assertEqual(collection.pageCount(), 1)
self.assertEqual(collection.page(0).pageSize().width(), 148)


if __name__ == '__main__':
unittest.main()

0 comments on commit 6471876

Please sign in to comment.