[iOS] Temporary UI to add Search Engine Snippet

Low-Coverage-Reason: TESTS_IN_SEPARATE_CL it will be added later
Bug: b/312459357
Change-Id: Icca383a6673f78faacaec75ee0ae09e50172a912
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5067594
Auto-Submit: Jérôme Lebel <jlebel@chromium.org>
Commit-Queue: Jérôme Lebel <jlebel@chromium.org>
Reviewed-by: Samar Chehade-Lepleux <samarchehade@google.com>
Cr-Commit-Position: refs/heads/main@{#1230253}
diff --git a/ios/chrome/browser/ui/search_engine_choice/search_engine_choice_table/cells/snippet_search_engine_cell.h b/ios/chrome/browser/ui/search_engine_choice/search_engine_choice_table/cells/snippet_search_engine_cell.h
index 56fecb91..d682f3ff 100644
--- a/ios/chrome/browser/ui/search_engine_choice/search_engine_choice_table/cells/snippet_search_engine_cell.h
+++ b/ios/chrome/browser/ui/search_engine_choice/search_engine_choice_table/cells/snippet_search_engine_cell.h
@@ -26,11 +26,6 @@
 // Unique identifier that matches with one URLItem.
 @property(nonatomic, strong) NSString* cellUniqueIdentifier;
 
-// Properly configure the subview layouts once all labels' properties have been
-// configured. This must be called at the end of configureCell: for all items
-// that use TableViewURLCell.
-- (void)configureUILayout;
-
 @end
 
 #endif  // IOS_CHROME_BROWSER_UI_SEARCH_ENGINE_CHOICE_SEARCH_ENGINE_CHOICE_TABLE_CELLS_SNIPPET_SEARCH_ENGINE_CELL_H_
diff --git a/ios/chrome/browser/ui/search_engine_choice/search_engine_choice_table/cells/snippet_search_engine_cell.mm b/ios/chrome/browser/ui/search_engine_choice/search_engine_choice_table/cells/snippet_search_engine_cell.mm
index 86a2a42..b19f8f6 100644
--- a/ios/chrome/browser/ui/search_engine_choice/search_engine_choice_table/cells/snippet_search_engine_cell.mm
+++ b/ios/chrome/browser/ui/search_engine_choice/search_engine_choice_table/cells/snippet_search_engine_cell.mm
@@ -24,84 +24,75 @@
     UIView* contentView = self.contentView;
 
     _faviconContainerView = [[FaviconContainerView alloc] init];
+    _faviconContainerView.translatesAutoresizingMaskIntoConstraints = NO;
     [_faviconContainerView
         setFaviconBackgroundColor:[UIColor colorNamed:kBackgroundColor]];
-    _faviconContainerView.translatesAutoresizingMaskIntoConstraints = NO;
     [contentView addSubview:_faviconContainerView];
+
+    // Add name label and snippet label.
+    UIView* nameSnippetLabelContainer = [[UIView alloc] init];
+    nameSnippetLabelContainer.translatesAutoresizingMaskIntoConstraints = NO;
+    [contentView addSubview:nameSnippetLabelContainer];
     _nameLabel = [[UILabel alloc] init];
+    _nameLabel.translatesAutoresizingMaskIntoConstraints = NO;
     _nameLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
     _nameLabel.adjustsFontForContentSizeCategory = YES;
+    [nameSnippetLabelContainer addSubview:_nameLabel];
     _snippetLabel = [[UILabel alloc] init];
+    _snippetLabel.translatesAutoresizingMaskIntoConstraints = NO;
     _snippetLabel.textColor = [UIColor colorNamed:kTextSecondaryColor];
     _snippetLabel.font =
         [UIFont preferredFontForTextStyle:UIFontTextStyleFootnote];
     _snippetLabel.adjustsFontForContentSizeCategory = YES;
     _snippetLabel.textColor = [UIColor colorNamed:kTextSecondaryColor];
-    _snippetLabel.hidden = YES;
+    _snippetLabel.numberOfLines = 1;
+    [nameSnippetLabelContainer addSubview:_snippetLabel];
 
-    // Horizontal view holds vertical stack view and metadata views.
-    UIView* horizontalView = [[UIView alloc] init];
-    horizontalView.translatesAutoresizingMaskIntoConstraints = NO;
-    [contentView addSubview:horizontalView];
-
-    // Use stack views to layout the subviews except for the favicon.
-    UIStackView* verticalStack = [[UIStackView alloc]
-        initWithArrangedSubviews:@[ _nameLabel, _snippetLabel ]];
-    verticalStack.axis = UILayoutConstraintAxisVertical;
-    verticalStack.translatesAutoresizingMaskIntoConstraints = NO;
-    [horizontalView addSubview:verticalStack];
-
-    NSLayoutConstraint* heightConstraint = [self.contentView.heightAnchor
-        constraintGreaterThanOrEqualToConstant:kChromeTableViewCellHeight];
-    // Don't set the priority to required to avoid clashing with the estimated
-    // height.
-    heightConstraint.priority = UILayoutPriorityRequired - 1;
-
+    NSLayoutConstraint* heightConstraint = [_snippetLabel.bottomAnchor
+        constraintEqualToAnchor:nameSnippetLabelContainer.bottomAnchor
+                       constant:0];
+    heightConstraint.priority = UILayoutPriorityDefaultLow;
     [NSLayoutConstraint activateConstraints:@[
       [_faviconContainerView.leadingAnchor
           constraintEqualToAnchor:self.contentView.leadingAnchor
                          constant:kTableViewHorizontalSpacing],
       [_faviconContainerView.centerYAnchor
           constraintEqualToAnchor:self.contentView.centerYAnchor],
-
-      // The stack view fills the remaining space, has an intrinsic height, and
-      // is centered vertically.
-      [horizontalView.leadingAnchor
-          constraintEqualToAnchor:_faviconContainerView.trailingAnchor
-                         constant:kTableViewSubViewHorizontalSpacing],
-      [horizontalView.trailingAnchor
-          constraintEqualToAnchor:self.contentView.trailingAnchor
-                         constant:-kTableViewHorizontalSpacing],
-      [horizontalView.centerYAnchor
-          constraintEqualToAnchor:self.contentView.centerYAnchor],
-      [verticalStack.topAnchor
-          constraintEqualToAnchor:horizontalView.topAnchor],
-      [verticalStack.bottomAnchor
-          constraintEqualToAnchor:horizontalView.bottomAnchor],
-      [verticalStack.leadingAnchor
-          constraintEqualToAnchor:horizontalView.leadingAnchor],
-      [horizontalView.topAnchor
+      [nameSnippetLabelContainer.topAnchor
           constraintGreaterThanOrEqualToAnchor:self.contentView.topAnchor
                                       constant:
                                         kTableViewTwoLabelsCellVerticalSpacing],
-      [horizontalView.bottomAnchor
+      [nameSnippetLabelContainer.bottomAnchor
           constraintLessThanOrEqualToAnchor:self.contentView.bottomAnchor
                                    constant:
                                        -kTableViewTwoLabelsCellVerticalSpacing],
-      heightConstraint
+      [nameSnippetLabelContainer.leadingAnchor
+          constraintEqualToAnchor:_faviconContainerView.trailingAnchor],
+      [nameSnippetLabelContainer.trailingAnchor
+          constraintEqualToAnchor:contentView.trailingAnchor],
+      [_nameLabel.leadingAnchor
+          constraintEqualToAnchor:nameSnippetLabelContainer.leadingAnchor
+                         constant:kTableViewSubViewHorizontalSpacing],
+      [_nameLabel.trailingAnchor
+          constraintEqualToAnchor:nameSnippetLabelContainer.trailingAnchor
+                         constant:-kTableViewHorizontalSpacing],
+      [_snippetLabel.leadingAnchor
+          constraintEqualToAnchor:nameSnippetLabelContainer.leadingAnchor
+                         constant:kTableViewSubViewHorizontalSpacing],
+      [_snippetLabel.trailingAnchor
+          constraintEqualToAnchor:nameSnippetLabelContainer.trailingAnchor
+                         constant:-kTableViewHorizontalSpacing],
+      [_nameLabel.topAnchor
+          constraintEqualToAnchor:nameSnippetLabelContainer.topAnchor
+                         constant:0],
+      [_nameLabel.bottomAnchor constraintEqualToAnchor:_snippetLabel.topAnchor
+                                              constant:0],
+      heightConstraint,
     ]];
   }
   return self;
 }
 
-- (void)configureUILayout {
-  if ([self.snippetLabel.text length]) {
-    self.snippetLabel.hidden = NO;
-  } else {
-    self.snippetLabel.hidden = YES;
-  }
-}
-
 #pragma mark - Properties
 
 - (FaviconView*)faviconView {
@@ -113,7 +104,7 @@
 - (void)prepareForReuse {
   [super prepareForReuse];
   [self.faviconView configureWithAttributes:nil];
-  self.snippetLabel.hidden = YES;
+  self.snippetLabel.numberOfLines = 1;
 }
 
 #pragma mark - Accessibility
diff --git a/ios/chrome/browser/ui/search_engine_choice/search_engine_choice_table/cells/snippet_search_engine_item.h b/ios/chrome/browser/ui/search_engine_choice/search_engine_choice_table/cells/snippet_search_engine_item.h
index 75d1cf0a..9b3c534 100644
--- a/ios/chrome/browser/ui/search_engine_choice/search_engine_choice_table/cells/snippet_search_engine_item.h
+++ b/ios/chrome/browser/ui/search_engine_choice/search_engine_choice_table/cells/snippet_search_engine_item.h
@@ -17,7 +17,7 @@
 // The name of the search engine.
 @property(nonatomic, readwrite, copy) NSString* name;
 // The text for the search engine snippet.
-@property(nonatomic, readwrite, copy) NSString* snippet;
+@property(nonatomic, readwrite, copy) NSString* snippetDescription;
 // The URL to fetch the favicon. This can be the favicon's URL, or a "fake" web
 // page URL created by filling empty query word into the search engine's
 // searchable URL template(e.g. "http://www.google.com/?q=").
diff --git a/ios/chrome/browser/ui/search_engine_choice/search_engine_choice_table/cells/snippet_search_engine_item.mm b/ios/chrome/browser/ui/search_engine_choice/search_engine_choice_table/cells/snippet_search_engine_item.mm
index 34051b5..cc30f90e 100644
--- a/ios/chrome/browser/ui/search_engine_choice/search_engine_choice_table/cells/snippet_search_engine_item.mm
+++ b/ios/chrome/browser/ui/search_engine_choice/search_engine_choice_table/cells/snippet_search_engine_item.mm
@@ -43,6 +43,7 @@
   SnippetSearchEngineCell* cell =
       base::apple::ObjCCastStrict<SnippetSearchEngineCell>(tableCell);
   cell.nameLabel.text = self.name;
+  cell.snippetLabel.text = self.snippetDescription;
   cell.cellUniqueIdentifier = self.uniqueIdentifier;
   cell.accessibilityTraits |= UIAccessibilityTraitButton;
   cell.contentView.alpha = 1.0;
@@ -52,8 +53,6 @@
   if (styler.cellTitleColor) {
     cell.nameLabel.textColor = styler.cellTitleColor;
   }
-
-  [cell configureUILayout];
 }
 
 - (BOOL)isEqual:(SnippetSearchEngineItem*)otherItem {
diff --git a/ios/chrome/browser/ui/search_engine_choice/search_engine_choice_table/search_engine_choice_table_mediator.mm b/ios/chrome/browser/ui/search_engine_choice/search_engine_choice_table/search_engine_choice_table_mediator.mm
index 7c564dd..91beef4 100644
--- a/ios/chrome/browser/ui/search_engine_choice/search_engine_choice_table/search_engine_choice_table_mediator.mm
+++ b/ios/chrome/browser/ui/search_engine_choice/search_engine_choice_table/search_engine_choice_table_mediator.mm
@@ -39,6 +39,10 @@
     item.URL = template_url->favicon_url();
   }
   item.name = base::SysUTF16ToNSString(template_url->short_name());
+  item.snippetDescription = [[NSString alloc]
+      initWithFormat:@"** Description for %@ that can be very very very very "
+                     @"very very long. **",
+                     item.name];
   return item;
 }
 
diff --git a/ios/chrome/browser/ui/search_engine_choice/search_engine_choice_table/search_engine_choice_table_view_controller.mm b/ios/chrome/browser/ui/search_engine_choice/search_engine_choice_table/search_engine_choice_table_view_controller.mm
index f710487b..8640919 100644
--- a/ios/chrome/browser/ui/search_engine_choice/search_engine_choice_table/search_engine_choice_table_view_controller.mm
+++ b/ios/chrome/browser/ui/search_engine_choice/search_engine_choice_table/search_engine_choice_table_view_controller.mm
@@ -121,8 +121,11 @@
       textItem.accessoryType = UITableViewCellAccessoryNone;
       UITableViewCell* cell =
           [tableView cellForRowAtIndexPath:[model indexPathForItem:item]];
+      SnippetSearchEngineCell* urlCell =
+          base::apple::ObjCCastStrict<SnippetSearchEngineCell>(cell);
       UIImageView* circleView = CreateEmptyCircle();
-      [cell setAccessoryView:circleView];
+      [urlCell setAccessoryView:circleView];
+      urlCell.snippetLabel.numberOfLines = 1;
     }
   }
 
@@ -132,13 +135,18 @@
           [model itemAtIndexPath:indexPath]);
   newDefaultEngine.accessoryType = UITableViewCellAccessoryCheckmark;
   UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
+  SnippetSearchEngineCell* urlCell =
+      base::apple::ObjCCastStrict<SnippetSearchEngineCell>(cell);
 
   cell.accessoryType = UITableViewCellAccessoryCheckmark;
   UIImageView* checkedCircleView = CreateCheckedCircle();
-  [cell setAccessoryView:checkedCircleView];
+  [urlCell setAccessoryView:checkedCircleView];
+  urlCell.snippetLabel.numberOfLines = 0;
 
   CHECK(self.delegate);
   [self.delegate selectSearchEngineAtRow:_selectedRow];
+  [self.tableView beginUpdates];
+  [self.tableView endUpdates];
 }
 
 #pragma mark - UITableViewDataSource