[iOS][MF] Detect if the keyboard view changes on iPads

Bug: 924100
Change-Id: Ia8a47361f95dc590345ac458e1f458570bea6fea
Reviewed-on: https://chromium-review.googlesource.com/c/1446654
Reviewed-by: Olivier Robin <olivierrobin@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#627913}(cherry picked from commit 49699e6500b4adc31b688059111fee2dd572ad8f)
Reviewed-on: https://chromium-review.googlesource.com/c/1452768
Reviewed-by: Javier Ernesto Flores Robles <javierrobles@chromium.org>
Cr-Commit-Position: refs/branch-heads/3683@{#184}
Cr-Branched-From: e51029943e0a38dd794b73caaf6373d5496ae783-refs/heads/master@{#625896}
diff --git a/ios/chrome/browser/autofill/form_input_accessory_view_controller.mm b/ios/chrome/browser/autofill/form_input_accessory_view_controller.mm
index e6dea1c..46f54ef 100644
--- a/ios/chrome/browser/autofill/form_input_accessory_view_controller.mm
+++ b/ios/chrome/browser/autofill/form_input_accessory_view_controller.mm
@@ -401,9 +401,19 @@
   if (self.isPaused) {
     return;
   }
-  if (self.inputAccessoryView && !self.inputAccessoryView.superview) {
+  if (self.inputAccessoryView) {
     if (IsIPadIdiom()) {
+      // On iPad the keyboard view can change so this updates it when needed.
       UIView* keyboardView = [self getKeyboardView];
+      if (self.inputAccessoryView.superview) {
+        if (keyboardView == self.inputAccessoryView.superview) {
+          return;
+        }
+        // The keyboard view is a different one.
+        [self.manualFillAccessoryViewController resetAnimated:NO];
+        [self.inputAccessoryView removeFromSuperview];
+        [self.grayBackgroundView removeFromSuperview];
+      }
       self.inputAccessoryView.translatesAutoresizingMaskIntoConstraints = NO;
       [keyboardView addSubview:self.inputAccessoryView];
       [NSLayoutConstraint activateConstraints:@[
@@ -421,7 +431,7 @@
         [keyboardView sendSubviewToBack:self.grayBackgroundView];
         AddSameConstraints(self.grayBackgroundView, keyboardView);
       }
-    } else {
+    } else if (!self.inputAccessoryView.superview) {  // Is not an iPad.
       UIResponder* firstResponder = GetFirstResponder();
       if (firstResponder.inputAccessoryView) {
         [firstResponder.inputAccessoryView addSubview:self.inputAccessoryView];
diff --git a/ios/chrome/browser/ui/autofill/manual_fill/fallback_coordinator_egtest.mm b/ios/chrome/browser/ui/autofill/manual_fill/fallback_coordinator_egtest.mm
index 8323ddb..50f50ab 100644
--- a/ios/chrome/browser/ui/autofill/manual_fill/fallback_coordinator_egtest.mm
+++ b/ios/chrome/browser/ui/autofill/manual_fill/fallback_coordinator_egtest.mm
@@ -474,6 +474,62 @@
       assertWithMatcher:grey_sufficientlyVisible()];
 }
 
+// Same as before but with the keyboard undocked.
+- (void)testUndockedInputAccessoryBarIsPresentAfterPickers {
+  // No need to run if not iPad.
+  if (!IsIPadIdiom()) {
+    EARL_GREY_TEST_SKIPPED(@"Test not applicable for iPhone.");
+  }
+  // Add the profile to be used.
+  AddAutofillProfile(_personalDataManager);
+
+  // Bring up the keyboard by tapping the city, which is the element before the
+  // picker.
+  [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewMatcher()]
+      performAction:chrome_test_util::TapWebElement(kFormElementCity)];
+
+  UndockKeyboard();
+
+  // Tap on the profiles icon.
+  [[EarlGrey selectElementWithMatcher:ProfilesIconMatcher()]
+      performAction:grey_tap()];
+
+  // Verify the profiles controller table view is visible.
+  [[EarlGrey selectElementWithMatcher:ProfilesTableViewMatcher()]
+      assertWithMatcher:grey_sufficientlyVisible()];
+
+  // Tap any option.
+  [[EarlGrey selectElementWithMatcher:ProfileTableViewButtonMatcher()]
+      performAction:grey_tap()];
+
+  // Verify the profiles controller table view is not visible.
+  [[EarlGrey selectElementWithMatcher:ProfilesTableViewMatcher()]
+      assertWithMatcher:grey_notVisible()];
+
+  // On iPad the picker is a table view in a popover, we need to dismiss that
+  // first. Tap in the previous field, so the popover dismisses.
+  [[EarlGrey selectElementWithMatcher:grey_keyWindow()]
+      performAction:grey_tapAtPoint(CGPointMake(0, 0))];
+
+  // Verify the table view is not visible.
+  [[EarlGrey selectElementWithMatcher:grey_kindOfClass([UITableView class])]
+      assertWithMatcher:grey_notVisible()];
+
+  // Bring up the regular keyboard again.
+  [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewMatcher()]
+      performAction:chrome_test_util::TapWebElement(kFormElementName)];
+
+  // Wait for the accessory icon to appear.
+  [GREYKeyboard waitForKeyboardToAppear];
+
+  // Verify the profiles icon is visible, and therefore also the input accessory
+  // bar.
+  [[EarlGrey selectElementWithMatcher:ProfilesIconMatcher()]
+      assertWithMatcher:grey_sufficientlyVisible()];
+
+  DockKeyboard();
+}
+
 // Test the input accessory bar is present when undocking the keyboard.
 - (void)testInputAccessoryBarIsPresentAfterUndockingKeyboard {
   if (!IsIPadIdiom()) {