Rebased version of !3014 with all the window management changes dropped.
This extends the stacking tests and gives us a reproducer for the long-standing #1627.