Commit be085ab0 authored by Mark Cogan's avatar Mark Cogan Committed by Commit Bot

[iOS] Add CHECK for BVC layout crash

In some cases a crash is happening when building the BVC view hierarchy
(see the linked bug); this is due to UIKit throwing an exception when
a constraint is created between views that aren't in the same view tree.

This CL just adds an explicit CHECK for the views that were triggering
this crash, so that we can get more information about the state of these
views when this occurs.

Bug: 1136765
Change-Id: I9d9c0431663aaa26df38bac1b66c476938320d7a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2510213
Commit-Queue: Mark Cogan <marq@chromium.org>
Reviewed-by: default avatarGauthier Ambard <gambard@chromium.org>
Cr-Commit-Position: refs/heads/master@{#825440}
parent dde3defc
...@@ -2330,6 +2330,17 @@ NSString* const kBrowserViewControllerSnackbarCategory = ...@@ -2330,6 +2330,17 @@ NSString* const kBrowserViewControllerSnackbarCategory =
NamedGuide* contentAreaGuide = [NamedGuide guideWithName:kContentAreaGuide NamedGuide* contentAreaGuide = [NamedGuide guideWithName:kContentAreaGuide
view:self.view]; view:self.view];
// TODO(crbug.com/1136765): Sometimes, |contentAreaGuide| and
// |primaryToolbarView| aren't in the same view hierarchy; this seems to be
// impossible, but it does still happen. This will cause an exception in
// when activiating these constraints. To gather more information about this
// state, explciitly check the view hierarchy roots. Local variables are
// used so that the CHECK message is cleared.
UIView* rootViewForToolbar = ViewHierarchyRootForView(primaryToolbarView);
UIView* rootViewForContentGuide =
ViewHierarchyRootForView(contentAreaGuide.owningView);
CHECK_EQ(rootViewForToolbar, rootViewForContentGuide);
// Constrain top to bottom of top toolbar. // Constrain top to bottom of top toolbar.
[contentAreaGuide.topAnchor [contentAreaGuide.topAnchor
constraintEqualToAnchor:primaryToolbarView.bottomAnchor] constraintEqualToAnchor:primaryToolbarView.bottomAnchor]
......
...@@ -202,4 +202,8 @@ NSString* TextForTabCount(long count); ...@@ -202,4 +202,8 @@ NSString* TextForTabCount(long count);
// if a UIMenuItem with the same selector as |item| has already been registered. // if a UIMenuItem with the same selector as |item| has already been registered.
void RegisterEditMenuItem(UIMenuItem* item); void RegisterEditMenuItem(UIMenuItem* item);
// Finds the root of |view|'s view hierarchy -- its window if it has one, or
// the first (recursive) superview with no superview.
UIView* ViewHierarchyRootForView(UIView* view);
#endif // IOS_CHROME_BROWSER_UI_UTIL_UIKIT_UI_UTIL_H_ #endif // IOS_CHROME_BROWSER_UI_UTIL_UIKIT_UI_UTIL_H_
...@@ -401,3 +401,13 @@ void RegisterEditMenuItem(UIMenuItem* item) { ...@@ -401,3 +401,13 @@ void RegisterEditMenuItem(UIMenuItem* item) {
[menu setMenuItems:items]; [menu setMenuItems:items];
} }
UIView* ViewHierarchyRootForView(UIView* view) {
if (view.window)
return view.window;
if (!view.superview)
return view;
return ViewHierarchyRootForView(view.superview);
}
...@@ -121,4 +121,23 @@ TEST_F(UIKitUIUtilTest, TintImageKeepsImageProperties) { ...@@ -121,4 +121,23 @@ TEST_F(UIKitUIUtilTest, TintImageKeepsImageProperties) {
tintedImage.flipsForRightToLeftLayoutDirection); tintedImage.flipsForRightToLeftLayoutDirection);
} }
TEST_F(UIKitUIUtilTest, ViewHierarchyRootForView) {
UIView* view1 = [[UIView alloc] init];
EXPECT_EQ(ViewHierarchyRootForView(view1), view1);
UIView* view2 = [[UIView alloc] init];
[view1 addSubview:view2];
EXPECT_EQ(ViewHierarchyRootForView(view2), view1);
UIWindow* window = [[UIWindow alloc] init];
[window addSubview:view1];
EXPECT_EQ(ViewHierarchyRootForView(view1), window);
EXPECT_EQ(ViewHierarchyRootForView(view2), window);
[view1 removeFromSuperview];
EXPECT_EQ(ViewHierarchyRootForView(view1), view1);
EXPECT_EQ(ViewHierarchyRootForView(view2), view1);
}
} // namespace } // namespace
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment