When fixing the secondary toolbar animations in crrev.com/c/1208713, we encountered test failures in the CQ for AdaptiveToolbarTestCase.testToolbarUI. When debugging, it seems that this is because that test uses UIViewController's |-setsetOverrideTraitCollection:forChildViewController:|. It appears that when the trait collection is overridden, the view controller's view's trait collection is updated at a later point in the runloop that isn't triggered by |-setNeedsLayout| + |-layoutIfNeeded|. Thus when we call |-layoutIfNeeded| to trigger the animation, at that point, the SecondaryToolbarViewController has received its |-traitCollectionDidChange:| message, but the toolbar's subviews trait collections haven't been updated yet. The toolbar buttons' visibility is calculated based on the button's trait collection, and since the layout occurs before the subviews' trait collections have been updated, they hide themselves. This was fixed in crrev.com/c/1208713 by updating the button visibility from |-viewDidLayoutSubviews|, but this is a sub-optimal solution because this is performing excess work for layout events unrelated to trait collection changes. We should find a way to test trait collection variations on the adaptive toolbar where the animations work and we avoid this extra effort.
Example failure:
https://ci.chromium.org/p/chromium/builders/luci.chromium.try/ios-simulator-full-configs/4923