Skip to content
Permalink
Browse files

Add some layout undo/redo tests

  • Loading branch information
nyalldawson committed Sep 5, 2017
1 parent 8b490de commit 64718766fcc99a58278abb7c7eae6505f1a9b3ab
Showing with 98 additions and 0 deletions.
  1. +48 −0 tests/src/python/test_qgslayoutgridsettings.py
  2. +50 −0 tests/src/python/test_qgslayoutpagecollection.py
@@ -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()
@@ -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.
You can’t perform that action at this time.