// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>

#import "base/apple/foundation_util.h"
#import "base/i18n/message_formatter.h"
#import "base/ios/ios_util.h"
#import "base/strings/sys_string_conversions.h"
#import "base/strings/utf_string_conversions.h"
#import "base/test/ios/wait_util.h"
#import "components/strings/grit/components_strings.h"
#import "ios/chrome/browser/authentication/test/signin_earl_grey.h"
#import "ios/chrome/browser/authentication/test/signin_earl_grey_ui_test_util.h"
#import "ios/chrome/browser/bookmarks/model/bookmark_storage_type.h"
#import "ios/chrome/browser/bookmarks/public/bookmarks_ui_constants.h"
#import "ios/chrome/browser/bookmarks/test/bookmark_earl_grey.h"
#import "ios/chrome/browser/bookmarks/test/bookmark_earl_grey_ui.h"
#import "ios/chrome/browser/shared/public/features/features.h"
#import "ios/chrome/browser/shared/public/snackbar/snackbar_constants.h"
#import "ios/chrome/browser/signin/model/fake_system_identity.h"
#import "ios/chrome/grit/ios_strings.h"
#import "ios/chrome/test/earl_grey/chrome_actions.h"
#import "ios/chrome/test/earl_grey/chrome_earl_grey.h"
#import "ios/chrome/test/earl_grey/chrome_earl_grey_ui.h"
#import "ios/chrome/test/earl_grey/chrome_matchers.h"
#import "ios/chrome/test/earl_grey/chrome_matchers_app_interface.h"
#import "ios/chrome/test/earl_grey/chrome_test_case.h"
#import "ios/testing/earl_grey/earl_grey_test.h"
#import "net/test/embedded_test_server/embedded_test_server.h"
#import "ui/base/l10n/l10n_util.h"

using chrome_test_util::BookmarksHomeDoneButton;
using chrome_test_util::BookmarksNavigationBarBackButton;
using chrome_test_util::BookmarksSaveEditDoneButton;
using chrome_test_util::BookmarksSaveEditFolderButton;
using chrome_test_util::ButtonWithAccessibilityLabelId;
using chrome_test_util::ContextBarCenterButtonWithLabel;
using chrome_test_util::ContextBarLeadingButtonWithLabel;
using chrome_test_util::KindOfTest;
using chrome_test_util::ScrollToTop;
using chrome_test_util::SearchBar;
using chrome_test_util::TappableBookmarkNodeWithLabel;

BookmarkStorageType kindOfTestToStorageType(KindOfTest kind) {
  switch (kind) {
    case KindOfTest::kSignedOut:
    case KindOfTest::kLocal:
      return BookmarkStorageType::kLocalOrSyncable;
    case KindOfTest::kAccount:
      return BookmarkStorageType::kAccount;
  }
}

// Bookmark folders integration tests for Chrome.
@interface BookmarksFolderChooserBaseTestCase : ChromeTestCase
@end

@implementation BookmarksFolderChooserBaseTestCase

- (AppLaunchConfiguration)appConfigurationForTestCase {
  AppLaunchConfiguration config = [super appConfigurationForTestCase];
  return config;
}

- (void)setUp {
  [super setUp];

  [BookmarkEarlGrey waitForBookmarkModelLoaded];
  [BookmarkEarlGrey clearBookmarks];
  [BookmarkEarlGrey clearBookmarksPositionCache];
}

// Tear down called once per test.
- (void)tearDownHelper {
  [super tearDownHelper];
  [BookmarkEarlGrey clearBookmarks];
  [BookmarkEarlGrey clearBookmarksPositionCache];
}

#pragma mark - utility functions

- (NSString*)getCreateNewFolderCellIdentifier:(KindOfTest)kind {
  switch (kind) {
    case KindOfTest::kSignedOut:
    case KindOfTest::kLocal:
      return kBookmarkCreateNewLocalOrSyncableFolderCellIdentifier;
    case KindOfTest::kAccount:
      return kBookmarkCreateNewAccountFolderCellIdentifier;
  }
}

#pragma mark - BookmarksFolderChooser Test Implementations

- (void)util_testCreateNewLocalOrSyncableFolderDefaultDestination:
    (KindOfTest)kindOfTest {
  [BookmarkEarlGrey
      setupStandardBookmarksInStorage:kindOfTestToStorageType(kindOfTest)];
  [BookmarkEarlGreyUI openBookmarks];
  [BookmarkEarlGreyUI openMobileBookmarks:kindOfTest];

  // Open `Folder 3` nested in `Folder 1->Folder 2`.
  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 1")]
      performAction:grey_tap()];
  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 2")]
      performAction:grey_tap()];

  // Long press on `Folder 3` and try to move it to a new folder.
  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 3")]
      performAction:grey_longPress()];
  [[EarlGrey selectElementWithMatcher:ButtonWithAccessibilityLabelId(
                                          IDS_IOS_BOOKMARK_CONTEXT_MENU_MOVE)]
      performAction:grey_tap()];
  [[EarlGrey selectElementWithMatcher:
                 grey_accessibilityID(
                     [self getCreateNewFolderCellIdentifier:kindOfTest])]
      performAction:grey_tap()];

  // Verify default parent folder is 'Mobile Bookmarks'.
  [BookmarkEarlGreyUI assertChangeFolderIsCorrectlySet:@"Mobile bookmarks"
                                            kindOfTest:kindOfTest];

  // Close folder editor.
  [[EarlGrey selectElementWithMatcher:BookmarksSaveEditFolderButton()]
      performAction:grey_tap()];
  // Close bookmarks
  [[EarlGrey selectElementWithMatcher:BookmarksHomeDoneButton()]
      performAction:grey_tap()];
}

- (void)util_testCreateNewFolderWhileMovingBookmarks:(KindOfTest)kindOfTest {
  [BookmarkEarlGrey
      setupStandardBookmarksInStorage:kindOfTestToStorageType(kindOfTest)];
  [BookmarkEarlGreyUI openBookmarks];
  [BookmarkEarlGreyUI openMobileBookmarks:kindOfTest];

  // Change to edit mode
  [[EarlGrey
      selectElementWithMatcher:grey_accessibilityID(
                                   kBookmarksHomeTrailingButtonIdentifier)]
      performAction:grey_tap()];

  // Select URLs.
  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Second URL")]
      performAction:grey_tap()];
  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"First URL")]
      performAction:grey_tap()];

  // Tap on "More".
  [[EarlGrey
      selectElementWithMatcher:ContextBarCenterButtonWithLabel(
                                   [BookmarkEarlGreyUI contextBarMoreString])]
      performAction:grey_tap()];

  // Tap on Move.
  [[EarlGrey selectElementWithMatcher:
                 chrome_test_util::ActionSheetItemWithAccessibilityLabelId(
                     IDS_IOS_BOOKMARK_CONTEXT_MENU_MOVE)]
      performAction:grey_tap()];

  // Choose to move the bookmark into a new folder.
  [[EarlGrey selectElementWithMatcher:
                 grey_accessibilityID(
                     [self getCreateNewFolderCellIdentifier:kindOfTest])]
      performAction:grey_tap()];

  // Enter custom new folder name.
  [BookmarkEarlGreyUI
      renameBookmarkFolderWithFolderTitle:@"Title For New Folder"];

  // Verify current parent folder (Change Folder) is Bookmarks folder.
  [BookmarkEarlGreyUI assertChangeFolderIsCorrectlySet:@"Mobile bookmarks"
                                            kindOfTest:kindOfTest];

  // Choose new parent folder (Change Folder).
  [BookmarkEarlGreyUI openFolderPicker];

  // Verify folder picker UI is displayed.
  [[EarlGrey
      selectElementWithMatcher:
          grey_accessibilityID(kBookmarkFolderPickerViewContainerIdentifier)]
      assertWithMatcher:grey_sufficientlyVisible()];

  // Verify Folder 2 only has one item.
  [BookmarkEarlGrey verifyChildCount:1
                    inFolderWithName:@"Folder 2"
                           inStorage:kindOfTestToStorageType(kindOfTest)];

  // Select Folder 2 as new Change Folder.
  [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"Folder 2")]
      performAction:grey_tap()];

  // Verify folder picker is dismissed and folder creator is now visible.
  [[EarlGrey
      selectElementWithMatcher:
          grey_accessibilityID(kBookmarkFolderCreateViewContainerIdentifier)]
      assertWithMatcher:grey_sufficientlyVisible()];
  [[EarlGrey
      selectElementWithMatcher:
          grey_accessibilityID(kBookmarkFolderPickerViewContainerIdentifier)]
      assertWithMatcher:grey_notVisible()];

  // Verify picked parent folder (Change Folder) is Folder 2.
  [BookmarkEarlGreyUI assertChangeFolderIsCorrectlySet:@"Folder 2"
                                            kindOfTest:kindOfTest];

  // Tap Done to close bookmark move flow.
  [[EarlGrey selectElementWithMatcher:BookmarksSaveEditFolderButton()]
      performAction:grey_tap()];

  // Verify all folder flow UI is now closed.
  [[EarlGrey
      selectElementWithMatcher:
          grey_accessibilityID(kBookmarkFolderCreateViewContainerIdentifier)]
      assertWithMatcher:grey_notVisible()];
  [[EarlGrey
      selectElementWithMatcher:
          grey_accessibilityID(kBookmarkFolderPickerViewContainerIdentifier)]
      assertWithMatcher:grey_notVisible()];
  [[EarlGrey
      selectElementWithMatcher:grey_accessibilityID(
                                   kBookmarkFolderEditViewContainerIdentifier)]
      assertWithMatcher:grey_notVisible()];

  // Verify new folder has been created under Folder 2.
  [BookmarkEarlGrey verifyChildCount:2
                    inFolderWithName:@"Folder 2"
                           inStorage:kindOfTestToStorageType(kindOfTest)];

  // Verify new folder has two bookmarks.
  [BookmarkEarlGrey verifyChildCount:2
                    inFolderWithName:@"Title For New Folder"
                           inStorage:kindOfTestToStorageType(kindOfTest)];
}

- (void)util_testCantDeleteFolderBeingEdited:(KindOfTest)kindOfTest {
  [BookmarkEarlGrey
      setupStandardBookmarksInStorage:kindOfTestToStorageType(kindOfTest)];
  [BookmarkEarlGreyUI openBookmarks];
  [BookmarkEarlGreyUI openMobileBookmarks:kindOfTest];

  // Create a new folder and type "New Folder 1" without pressing return.
  NSString* newFolderTitle = @"New folder";
  [BookmarkEarlGreyUI createNewBookmarkFolderWithFolderTitle:newFolderTitle
                                                 pressReturn:NO];

  // Swipe action to try to delete the newly created folder while its name its
  // being edited.
  [[EarlGrey
      selectElementWithMatcher:grey_allOf(grey_accessibilityID(@"New folder"),
                                          grey_minimumVisiblePercent(0.7), nil)]
      performAction:grey_swipeFastInDirection(kGREYDirectionLeft)];

  // Verify the delete confirmation button doesn't show up.
  [[EarlGrey selectElementWithMatcher:[ChromeMatchersAppInterface
                                          swipeActionDeleteButton]]
      assertWithMatcher:grey_nil()];
}

- (void)util_testNavigateAwayFromFolderBeingEdited:(KindOfTest)kindOfTest {
  [BookmarkEarlGrey
      setupBookmarksWhichExceedsScreenHeightInStorage:kindOfTestToStorageType(
                                                          kindOfTest)];
  [BookmarkEarlGreyUI openBookmarks];
  [BookmarkEarlGreyUI openMobileBookmarks:kindOfTest];

  // Verify bottom URL is not visible before scrolling to bottom (make sure
  // setupBookmarksWhichExceedsScreenHeight works as expected).
  [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"Bottom URL")]
      assertWithMatcher:grey_notVisible()];

  // Verify the top URL is visible (isn't covered by the navigation bar).
  [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"Top URL")]
      assertWithMatcher:grey_sufficientlyVisible()];

  // Test new folder could be created.  This verifies bookmarks scrolled to
  // bottom successfully for folder name editng.
  NSString* newFolderTitle = @"New folder";
  [BookmarkEarlGreyUI createNewBookmarkFolderWithFolderTitle:newFolderTitle
                                                 pressReturn:NO];

  // Scroll to top to navigate away from the folder being created.
  [[EarlGrey selectElementWithMatcher:grey_accessibilityID(
                                          kBookmarksHomeTableViewIdentifier)]
      performAction:ScrollToTop()];

  // Scroll back to the Folder being created.
  [BookmarkEarlGreyUI scrollToBottom];

  // Folder should still be in Edit mode, because of this match for Value.
  [[EarlGrey
      selectElementWithMatcher:grey_allOf(
                                   grey_accessibilityValue(@"New folder"),
                                   grey_sufficientlyVisible(), nil)]
      assertWithMatcher:grey_notNil()];
}

- (void)util_testDeleteSingleFolderNode:(KindOfTest)kindOfTest {
  [BookmarkEarlGrey
      setupStandardBookmarksInStorage:kindOfTestToStorageType(kindOfTest)];
  [BookmarkEarlGreyUI openBookmarks];
  [BookmarkEarlGreyUI openMobileBookmarks:kindOfTest];

  // Change to edit mode
  [[EarlGrey
      selectElementWithMatcher:grey_accessibilityID(
                                   kBookmarksHomeTrailingButtonIdentifier)]
      performAction:grey_tap()];

  // Select single URL.
  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 1")]
      performAction:grey_tap()];

  // Delete it.
  [[EarlGrey
      selectElementWithMatcher:ContextBarLeadingButtonWithLabel(
                                   [BookmarkEarlGreyUI contextBarDeleteString])]
      performAction:grey_tap()];

  // Wait until it's gone.
  [BookmarkEarlGreyUI waitForDeletionOfBookmarkWithTitle:@"Folder 1"];

  // Press undo
  [[EarlGrey
      selectElementWithMatcher:
          grey_allOf(grey_accessibilityID(kSnackbarButtonAccessibilityId),
                     grey_accessibilityLabel(l10n_util::GetNSString(
                         IDS_IOS_BOOKMARK_NEW_UNDO_BUTTON_TITLE)),
                     nil)] performAction:grey_tap()];

  // Verify it's back.
  [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"Folder 1")]
      assertWithMatcher:grey_notNil()];

  // Verify edit mode is closed (context bar back to default state).
  [BookmarkEarlGreyUI verifyContextBarInDefaultStateWithSelectEnabled:YES
                                                     newFolderEnabled:YES];
}

- (void)util_testSwipeDownToDismissFromEditFolder:(KindOfTest)kindOfTest {
  [BookmarkEarlGrey
      setupStandardBookmarksInStorage:kindOfTestToStorageType(kindOfTest)];
  [BookmarkEarlGreyUI openBookmarks];
  [BookmarkEarlGreyUI openMobileBookmarks:kindOfTest];

  // Invoke Move through long press.
  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 1.1")]
      performAction:grey_longPress()];

  [[EarlGrey selectElementWithMatcher:ButtonWithAccessibilityLabelId(
                                          IDS_IOS_BOOKMARK_CONTEXT_MENU_MOVE)]
      performAction:grey_tap()];

  // Check that the TableView is presented.
  [[EarlGrey
      selectElementWithMatcher:
          grey_accessibilityID(kBookmarkFolderPickerViewContainerIdentifier)]
      assertWithMatcher:grey_notNil()];

  // Swipe TableView down.
  [[EarlGrey
      selectElementWithMatcher:
          grey_accessibilityID(kBookmarkFolderPickerViewContainerIdentifier)]
      performAction:grey_swipeFastInDirection(kGREYDirectionDown)];

  // Check that the TableView has been dismissed.
  [[EarlGrey
      selectElementWithMatcher:
          grey_accessibilityID(kBookmarkFolderPickerViewContainerIdentifier)]
      assertWithMatcher:grey_nil()];
}

- (void)util_testWhenCurrentFolderDeletedInBackground:(KindOfTest)kindOfTest {
  [BookmarkEarlGrey
      setupStandardBookmarksInStorage:kindOfTestToStorageType(kindOfTest)];
  [BookmarkEarlGreyUI openBookmarks];
  [BookmarkEarlGreyUI openMobileBookmarks:kindOfTest];

  // Enter Folder 1
  [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"Folder 1")]
      performAction:grey_tap()];

  // Enter Folder 2
  [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"Folder 2")]
      performAction:grey_tap()];

  // Change to edit mode, using context menu.
  [[EarlGrey
      selectElementWithMatcher:grey_accessibilityID(
                                   kBookmarksHomeTrailingButtonIdentifier)]
      performAction:grey_tap()];

  // Delete the Folder 1 and Folder 2 programmatically in background.
  [BookmarkEarlGrey
      removeBookmarkWithTitle:@"Folder 2"
                    inStorage:kindOfTestToStorageType(kindOfTest)];
  // TODO(crbug.com/354761339): This shouldn't be necessary, needs more
  // investigation.
  [ChromeEarlGreyUI waitForAppToIdle];
  [BookmarkEarlGrey
      removeBookmarkWithTitle:@"Folder 1"
                    inStorage:kindOfTestToStorageType(kindOfTest)];

  // Verify edit mode is closed automatically (context bar switched back to
  // default state) and both select and new folder button are disabled.
  [BookmarkEarlGreyUI verifyContextBarInDefaultStateWithSelectEnabled:YES
                                                     newFolderEnabled:YES];

  // Ensure Folder 1.1 is seen, that means it successfully comes back to Mobile
  // Bookmarks.
  [BookmarkEarlGreyUI verifyBookmarkFolderIsSeen:@"Folder 1.1"];
}

- (void)util_testLongPressOnSingleFolder:(KindOfTest)kindOfTest {
  [BookmarkEarlGrey
      setupStandardBookmarksInStorage:kindOfTestToStorageType(kindOfTest)];
  [BookmarkEarlGreyUI openBookmarks];
  [BookmarkEarlGreyUI openMobileBookmarks:kindOfTest];

  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 1")]
      performAction:grey_longPress()];

  [BookmarkEarlGreyUI verifyContextMenuForSingleFolderWithEditEnabled:YES];

  [BookmarkEarlGreyUI dismissContextMenu];

  [ChromeEarlGrey waitForMatcher:grey_allOf(BookmarksNavigationBarBackButton(),
                                            grey_interactable(), nil)];

  // Come back to the root.
  [[EarlGrey selectElementWithMatcher:BookmarksNavigationBarBackButton()]
      performAction:grey_tap()];

  // Long press on Mobile Bookmarks.
  [[EarlGrey selectElementWithMatcher:TappableBookmarkNodeWithLabel(
                                          @"Mobile bookmarks", kindOfTest)]
      performAction:grey_longPress()];

  // We cannot locate new context menus any way, therefore we'll use the
  // 'Edit' action presence as proxy.
  [[EarlGrey selectElementWithMatcher:chrome_test_util::
                                          BookmarksContextMenuEditButton()]
      assertWithMatcher:grey_nil()];
}

- (void)util_testEditFunctionalityOnSingleFolder:(KindOfTest)kindOfTest {
  [BookmarkEarlGrey
      setupStandardBookmarksInStorage:kindOfTestToStorageType(kindOfTest)];
  [BookmarkEarlGreyUI openBookmarks];
  [BookmarkEarlGreyUI openMobileBookmarks:kindOfTest];

  // 1. Edit the folder title at edit page.

  // Invoke Edit through long press.
  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 1")]
      performAction:grey_longPress()];

  id<GREYMatcher> editFolderMatcher =
      chrome_test_util::BookmarksContextMenuEditButton();
  [ChromeEarlGrey waitForUIElementToAppearWithMatcher:editFolderMatcher];
  [[EarlGrey selectElementWithMatcher:editFolderMatcher]
      performAction:grey_tap()];

  // Verify that the editor is present.
  [ChromeEarlGrey
      waitForUIElementToAppearWithMatcher:
          grey_accessibilityID(kBookmarkFolderEditViewContainerIdentifier)];
  NSString* existingFolderTitle = @"Folder 1";
  NSString* newFolderTitle = @"New Folder Title";
  [BookmarkEarlGreyUI renameBookmarkFolderWithFolderTitle:newFolderTitle];

  [[EarlGrey selectElementWithMatcher:BookmarksSaveEditFolderButton()]
      performAction:grey_tap()];
  [ChromeEarlGreyUI waitForAppToIdle];

  // Verify that the change has been made.
  [BookmarkEarlGreyUI waitForDeletionOfBookmarkWithTitle:existingFolderTitle];
  [ChromeEarlGrey
      waitForUIElementToAppearWithMatcher:TappableBookmarkNodeWithLabel(
                                              newFolderTitle)];

  // Verify edit mode is closed (context bar back to default state).
  [BookmarkEarlGreyUI verifyContextBarInDefaultStateWithSelectEnabled:YES
                                                     newFolderEnabled:YES];

  // 2. Move a single folder at edit page.

  // Change to edit mode.
  [[EarlGrey
      selectElementWithMatcher:grey_accessibilityID(
                                   kBookmarksHomeTrailingButtonIdentifier)]
      performAction:grey_tap()];

  // Select single folder.
  [ChromeEarlGrey
      waitForUIElementToAppearWithMatcher:TappableBookmarkNodeWithLabel(
                                              newFolderTitle)];
  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(newFolderTitle)]
      performAction:grey_tap()];

  // Move the "New Folder Title" to "Folder 1.1".
  [BookmarkEarlGreyUI
      tapOnContextMenuButton:IDS_IOS_BOOKMARK_CONTEXT_MENU_EDIT_FOLDER
                  openEditor:kBookmarkFolderEditViewContainerIdentifier
           setParentFolderTo:@"Folder 1.1"
                        from:@"Mobile bookmarks"
                  kindOfTest:kindOfTest];

  // Verify edit mode remains.
  [BookmarkEarlGreyUI verifyContextBarInEditMode];

  // Close edit mode.
  [BookmarkEarlGreyUI closeContextBarEditMode];
  [ChromeEarlGreyUI waitForAppToIdle];

  [ChromeEarlGrey
      waitForUIElementToAppearWithMatcher:TappableBookmarkNodeWithLabel(
                                              @"Folder 1.1", kindOfTest)];

  // Navigate to "Folder 1.1" and verify "New Folder Title" is under it.
  [[EarlGrey selectElementWithMatcher:TappableBookmarkNodeWithLabel(
                                          @"Folder 1.1", kindOfTest)]
      performAction:grey_tap()];
  [ChromeEarlGrey
      waitForUIElementToAppearWithMatcher:TappableBookmarkNodeWithLabel(
                                              newFolderTitle)];

  // 3. Test the cancel button at edit page.

  // Change to edit mode
  [[EarlGrey
      selectElementWithMatcher:grey_accessibilityID(
                                   kBookmarksHomeTrailingButtonIdentifier)]
      performAction:grey_tap()];

  // Select single folder.
  [ChromeEarlGrey
      waitForUIElementToAppearWithMatcher:TappableBookmarkNodeWithLabel(
                                              newFolderTitle)];
  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(newFolderTitle)]
      performAction:grey_tap()];

  // Tap cancel after modifying the title.
  [BookmarkEarlGreyUI
      tapOnContextMenuButton:IDS_IOS_BOOKMARK_CONTEXT_MENU_EDIT_FOLDER
                  openEditor:kBookmarkFolderEditViewContainerIdentifier
             modifyTextField:@"Title_textField"
                          to:@"Dummy"
                 dismissWith:@"Cancel"];

  // Verify that the editor is dismissed.
  [ChromeEarlGrey
      waitForUIElementToDisappearWithMatcher:
          grey_accessibilityID(kBookmarkFolderEditViewContainerIdentifier)];

  // Verify that the bookmark was not updated.
  [ChromeEarlGrey
      waitForUIElementToAppearWithMatcher:TappableBookmarkNodeWithLabel(
                                              newFolderTitle)];

  // Verify edit mode is stayed.
  [BookmarkEarlGreyUI verifyContextBarInEditMode];

  // 4. Test the delete button at edit page.

  // Tap context menu.
  [[EarlGrey
      selectElementWithMatcher:ContextBarCenterButtonWithLabel(
                                   [BookmarkEarlGreyUI contextBarMoreString])]
      performAction:grey_tap()];

  id<GREYMatcher> actionSheetItem =
      chrome_test_util::ActionSheetItemWithAccessibilityLabelId(
          IDS_IOS_BOOKMARK_CONTEXT_MENU_EDIT_FOLDER);
  [ChromeEarlGrey waitForUIElementToAppearWithMatcher:actionSheetItem];
  [[EarlGrey selectElementWithMatcher:actionSheetItem]
      performAction:grey_tap()];

  // Verify that the editor is present.
  [[EarlGrey
      selectElementWithMatcher:grey_accessibilityID(
                                   kBookmarkFolderEditViewContainerIdentifier)]
      assertWithMatcher:grey_notNil()];

  [[EarlGrey
      selectElementWithMatcher:grey_accessibilityID(
                                   kBookmarkFolderEditorDeleteButtonIdentifier)]
      performAction:grey_tap()];

  [BookmarkEarlGreyUI closeUndoSnackbarAndWait];
  [ChromeEarlGreyUI waitForAppToIdle];

  // Verify that the folder is deleted.
  [BookmarkEarlGreyUI waitForDeletionOfBookmarkWithTitle:newFolderTitle];

  // 5. Verify that when adding a new folder, edit mode will not mistakenly come
  // back (crbug.com/781783).

  // Create a new folder.
  [BookmarkEarlGreyUI createNewBookmarkFolderWithFolderTitle:newFolderTitle
                                                 pressReturn:YES];
  [ChromeEarlGreyUI waitForAppToIdle];

  [ChromeEarlGrey
      waitForUIElementToAppearWithMatcher:TappableBookmarkNodeWithLabel(
                                              newFolderTitle)];

  // Tap on the new folder.
  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(newFolderTitle)]
      performAction:grey_tap()];

  // Verify we enter the new folder. (instead of selecting it in edit mode).
  [BookmarkEarlGreyUI verifyEmptyBackgroundAppears];
  // Close bookmarks
  [[EarlGrey selectElementWithMatcher:BookmarksHomeDoneButton()]
      performAction:grey_tap()];
}

- (void)util_testMoveAndUndoFromModel:(KindOfTest)sourceKind
                              toModel:(KindOfTest)destinationKind {
  [BookmarkEarlGrey
      setupStandardBookmarksInStorage:kindOfTestToStorageType(sourceKind)];
  if (sourceKind != destinationKind) {
    [BookmarkEarlGrey setupStandardBookmarksInStorage:kindOfTestToStorageType(
                                                          destinationKind)];
  }
  // Move to Mobile Bookmarks > Folder 1
  [BookmarkEarlGreyUI openBookmarks];
  [BookmarkEarlGreyUI openMobileBookmarks:sourceKind];
  [[EarlGrey selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 1",
                                                                    sourceKind)]
      performAction:grey_tap()];

  // Invoke Move through long press.
  [[EarlGrey selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 2",
                                                                    sourceKind)]
      performAction:grey_longPress()];

  [[EarlGrey selectElementWithMatcher:ButtonWithAccessibilityLabelId(
                                          IDS_IOS_BOOKMARK_CONTEXT_MENU_MOVE)]
      performAction:grey_tap()];

  // Select Mobile Bookmarks as new parent folder for "Title For New Folder".
  [[EarlGrey
      selectElementWithMatcher:
          grey_accessibilityID(kBookmarkFolderPickerViewContainerIdentifier)]
      assertWithMatcher:grey_sufficientlyVisible()];
  [[[EarlGrey selectElementWithMatcher:TappableBookmarkNodeWithLabel(
                                           @"Folder 1.1", destinationKind)]
         usingSearchAction:grey_scrollInDirection(kGREYDirectionDown, 200)
      onElementWithMatcher:grey_accessibilityID(
                               kBookmarkFolderPickerViewContainerIdentifier)]
      performAction:grey_tap()];

  // Verify folder picker is dismissed.
  [[EarlGrey
      selectElementWithMatcher:
          grey_accessibilityID(kBookmarkFolderPickerViewContainerIdentifier)]
      assertWithMatcher:grey_notVisible()];

  // Verify folder is in destination and not in source.
  [BookmarkEarlGrey verifyChildCount:1
                    inFolderWithName:@"Folder 1.1"
                           inStorage:kindOfTestToStorageType(destinationKind)];
  [BookmarkEarlGrey verifyChildCount:0
                    inFolderWithName:@"Folder 1"
                           inStorage:kindOfTestToStorageType(sourceKind)];

  [[EarlGrey
      selectElementWithMatcher:grey_allOf(grey_accessibilityID(@"Folder 2"),
                                          grey_sufficientlyVisible(), nil)]
      assertWithMatcher:grey_nil()];

  // Press undo
  [[EarlGrey
      selectElementWithMatcher:
          grey_allOf(grey_accessibilityID(kSnackbarButtonAccessibilityId),
                     grey_accessibilityLabel(l10n_util::GetNSString(
                         IDS_IOS_BOOKMARK_NEW_UNDO_BUTTON_TITLE)),
                     nil)] performAction:grey_tap()];
  // Verify folder is in source and not in destination.
  [BookmarkEarlGrey verifyChildCount:0
                    inFolderWithName:@"Folder 1.1"
                           inStorage:kindOfTestToStorageType(destinationKind)];
  [BookmarkEarlGrey verifyChildCount:1
                    inFolderWithName:@"Folder 1"
                           inStorage:kindOfTestToStorageType(sourceKind)];

  [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"Folder 2")]
      assertWithMatcher:grey_sufficientlyVisible()];
  // Close bookmarks
  [[EarlGrey selectElementWithMatcher:BookmarksHomeDoneButton()]
      performAction:grey_tap()];
}

- (void)util_testMoveFunctionalityOnSingleFolderFromModel:(KindOfTest)sourceKind
                                                  toModel:(KindOfTest)
                                                              destinationKind {
  [BookmarkEarlGrey
      setupStandardBookmarksInStorage:kindOfTestToStorageType(sourceKind)];
  if (sourceKind != destinationKind) {
    [BookmarkEarlGrey setupStandardBookmarksInStorage:kindOfTestToStorageType(
                                                          destinationKind)];
  }
  [BookmarkEarlGreyUI openBookmarks];
  [BookmarkEarlGreyUI openMobileBookmarks:sourceKind];

  // Invoke Move through long press.
  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 1.1")]
      performAction:grey_longPress()];

  [[EarlGrey selectElementWithMatcher:ButtonWithAccessibilityLabelId(
                                          IDS_IOS_BOOKMARK_CONTEXT_MENU_MOVE)]
      performAction:grey_tap()];

  // Choose to move the bookmark folder - "Folder 1" into a new folder.
  [[EarlGrey selectElementWithMatcher:
                 grey_accessibilityID(
                     [self getCreateNewFolderCellIdentifier:destinationKind])]
      performAction:grey_tap()];

  // Enter custom new folder name.
  NSString* newFolderTitle = @"Title For New Folder";
  [BookmarkEarlGreyUI renameBookmarkFolderWithFolderTitle:newFolderTitle];

  // Verify current parent folder for "Title For New Folder" folder is "Mobile
  // Bookmarks" folder.
  [BookmarkEarlGreyUI assertChangeFolderIsCorrectlySet:@"Mobile bookmarks"
                                            kindOfTest:destinationKind];

  // Choose new parent folder for "Title For New Folder" folder.
  [BookmarkEarlGreyUI openFolderPicker];

  // Verify folder picker UI is displayed.
  [[EarlGrey
      selectElementWithMatcher:
          grey_accessibilityID(kBookmarkFolderPickerViewContainerIdentifier)]
      assertWithMatcher:grey_sufficientlyVisible()];

  // Verify Folder 2 only has one item.
  [BookmarkEarlGrey verifyChildCount:1
                    inFolderWithName:@"Folder 2"
                           inStorage:kindOfTestToStorageType(destinationKind)];

  // Select Folder 2 as new parent folder for "Title For New Folder".
  [[EarlGrey selectElementWithMatcher:TappableBookmarkNodeWithLabel(
                                          @"Folder 2", destinationKind)]
      performAction:grey_tap()];

  // Verify folder picker is dismissed and folder creator is now visible.
  [[EarlGrey
      selectElementWithMatcher:
          grey_accessibilityID(kBookmarkFolderCreateViewContainerIdentifier)]
      assertWithMatcher:grey_sufficientlyVisible()];
  [[EarlGrey
      selectElementWithMatcher:
          grey_accessibilityID(kBookmarkFolderPickerViewContainerIdentifier)]
      assertWithMatcher:grey_notVisible()];

  // Verify picked parent folder is Folder 2.
  [BookmarkEarlGreyUI assertChangeFolderIsCorrectlySet:@"Folder 2"
                                            kindOfTest:destinationKind];
  // Tap Done to close bookmark move flow.
  [[EarlGrey selectElementWithMatcher:BookmarksSaveEditFolderButton()]
      performAction:grey_tap()];

  // Verify all folder flow UI is now closed.
  [BookmarkEarlGreyUI verifyFolderFlowIsClosed];

  // Verify new folder "Title For New Folder" has been created under Folder 2.
  [BookmarkEarlGrey verifyChildCount:2
                    inFolderWithName:@"Folder 2"
                           inStorage:kindOfTestToStorageType(destinationKind)];

  // Verify new folder "Title For New Folder" has one bookmark folder.
  [BookmarkEarlGrey verifyChildCount:1
                    inFolderWithName:@"Title For New Folder"
                           inStorage:kindOfTestToStorageType(destinationKind)];

  if (destinationKind != sourceKind) {
    [[EarlGrey selectElementWithMatcher:BookmarksNavigationBarBackButton()]
        performAction:grey_tap()];

    [BookmarkEarlGreyUI openMobileBookmarks:destinationKind];
  }

  // Drill down to where "Folder 1.1" has been moved and assert it's presence.
  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 1")]
      performAction:grey_tap()];
  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 2")]
      performAction:grey_tap()];
  [[EarlGrey selectElementWithMatcher:TappableBookmarkNodeWithLabel(
                                          @"Title For New Folder")]
      performAction:grey_tap()];
  [ChromeEarlGrey
      waitForSufficientlyVisibleElementWithMatcher:grey_accessibilityID(
                                                       @"Folder 1.1")];
}

- (void)util_testMoveFunctionalityOnMultipleFolder:(KindOfTest)kindOfTest {
  [BookmarkEarlGrey
      setupStandardBookmarksInStorage:kindOfTestToStorageType(kindOfTest)];
  [BookmarkEarlGreyUI openBookmarks];
  [BookmarkEarlGreyUI openMobileBookmarks:kindOfTest];

  // Change to edit mode, using context menu.
  [[EarlGrey
      selectElementWithMatcher:grey_accessibilityID(
                                   kBookmarksHomeTrailingButtonIdentifier)]
      performAction:grey_tap()];

  // Select multiple folders.
  [[EarlGrey selectElementWithMatcher:TappableBookmarkNodeWithLabel(
                                          @"Folder 1.1", kindOfTest)]
      performAction:grey_tap()];
  [[EarlGrey selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 1",
                                                                    kindOfTest)]
      performAction:grey_tap()];

  // Tap context menu.
  [[EarlGrey
      selectElementWithMatcher:ContextBarCenterButtonWithLabel(
                                   [BookmarkEarlGreyUI contextBarMoreString])]
      performAction:grey_tap()];

  [[EarlGrey selectElementWithMatcher:
                 chrome_test_util::ActionSheetItemWithAccessibilityLabelId(
                     IDS_IOS_BOOKMARK_CONTEXT_MENU_MOVE)]
      performAction:grey_tap()];

  // Choose to move into a new folder. By tapping on the New Folder Cell.
  [[EarlGrey selectElementWithMatcher:
                 grey_accessibilityID(
                     [self getCreateNewFolderCellIdentifier:kindOfTest])]
      performAction:grey_tap()];

  // Enter custom new folder name.
  [BookmarkEarlGreyUI
      renameBookmarkFolderWithFolderTitle:@"Title For New Folder"];

  // Verify current parent folder for "Title For New Folder" folder is "Mobile
  // Bookmarks" folder.
  [BookmarkEarlGreyUI assertChangeFolderIsCorrectlySet:@"Mobile bookmarks"
                                            kindOfTest:kindOfTest];

  // Tap Done to close bookmark move flow.
  [[EarlGrey selectElementWithMatcher:BookmarksSaveEditFolderButton()]
      performAction:grey_tap()];

  // Verify all folder flow UI is now closed.
  [BookmarkEarlGreyUI verifyFolderFlowIsClosed];

  [BookmarkEarlGreyUI closeUndoSnackbarAndWait];

  // Verify edit mode is closed (context bar back to default state).
  [BookmarkEarlGreyUI verifyContextBarInDefaultStateWithSelectEnabled:YES
                                                     newFolderEnabled:YES];

  // Verify new folder "Title For New Folder" has two bookmark folder.
  [BookmarkEarlGrey verifyChildCount:2
                    inFolderWithName:@"Title For New Folder"
                           inStorage:kindOfTestToStorageType(kindOfTest)];

  // Drill down to where "Folder 1.1" and "Folder 1" have been moved and assert
  // it's presence.
  [[EarlGrey selectElementWithMatcher:TappableBookmarkNodeWithLabel(
                                          @"Title For New Folder")]
      performAction:grey_tap()];
  [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"Folder 1.1")]
      assertWithMatcher:grey_sufficientlyVisible()];
  [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"Folder 1")]
      assertWithMatcher:grey_sufficientlyVisible()];

  // Close bookmarks
  [[EarlGrey selectElementWithMatcher:BookmarksHomeDoneButton()]
      performAction:grey_tap()];
}

- (void)util_testContextBarForSingleFolderSelection:(KindOfTest)kindOfTest {
  [BookmarkEarlGrey
      setupStandardBookmarksInStorage:kindOfTestToStorageType(kindOfTest)];
  [BookmarkEarlGreyUI openBookmarks];
  [BookmarkEarlGreyUI openMobileBookmarks:kindOfTest];

  // Change to edit mode
  [[EarlGrey
      selectElementWithMatcher:grey_accessibilityID(
                                   kBookmarksHomeTrailingButtonIdentifier)]
      performAction:grey_tap()];

  // Select Folder.
  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 1")]
      performAction:grey_tap()];

  // Tap context menu.
  [[EarlGrey
      selectElementWithMatcher:ContextBarCenterButtonWithLabel(
                                   [BookmarkEarlGreyUI contextBarMoreString])]
      performAction:grey_tap()];

  // Tap Edit Folder.
  [[EarlGrey selectElementWithMatcher:
                 chrome_test_util::ActionSheetItemWithAccessibilityLabelId(
                     IDS_IOS_BOOKMARK_CONTEXT_MENU_EDIT_FOLDER)]
      performAction:grey_tap()];

  // Verify it shows edit view controller.  Uses notNil() instead of
  // sufficientlyVisible() because the large title in the navigation bar causes
  // less than 75% of the table view to be visible.
  [[EarlGrey
      selectElementWithMatcher:grey_accessibilityID(
                                   kBookmarkFolderEditViewContainerIdentifier)]
      assertWithMatcher:grey_notNil()];
}

- (void)util_testContextMenuForMultipleFolderSelection:(KindOfTest)kindOfTest {
  [BookmarkEarlGrey
      setupStandardBookmarksInStorage:kindOfTestToStorageType(kindOfTest)];
  [BookmarkEarlGreyUI openBookmarks];
  [BookmarkEarlGreyUI openMobileBookmarks:kindOfTest];

  // Change to edit mode
  [[EarlGrey
      selectElementWithMatcher:grey_accessibilityID(
                                   kBookmarksHomeTrailingButtonIdentifier)]
      performAction:grey_tap()];

  // Select Folders.
  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 1")]
      performAction:grey_tap()];
  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 1.1")]
      performAction:grey_tap()];

  // Tap context menu.
  [[EarlGrey
      selectElementWithMatcher:ContextBarCenterButtonWithLabel(
                                   [BookmarkEarlGreyUI contextBarMoreString])]
      performAction:grey_tap()];

  // Verify it shows the context menu.
  [[EarlGrey selectElementWithMatcher:grey_accessibilityID(
                                          kBookmarksHomeContextMenuIdentifier)]
      assertWithMatcher:grey_sufficientlyVisible()];

  // Verify options on context menu.
  [[EarlGrey selectElementWithMatcher:
                 chrome_test_util::ActionSheetItemWithAccessibilityLabelId(
                     IDS_IOS_BOOKMARK_CONTEXT_MENU_MOVE)]
      assertWithMatcher:grey_sufficientlyVisible()];
}

- (void)util_testStickyDefaultFolder:(KindOfTest)kindOfTest {
  [BookmarkEarlGrey
      setupStandardBookmarksInStorage:kindOfTestToStorageType(kindOfTest)];
  [BookmarkEarlGreyUI openBookmarks];
  [BookmarkEarlGreyUI openMobileBookmarks:kindOfTest];

  // Invoke Edit through long press.
  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"First URL")]
      performAction:grey_longPress()];
  [[EarlGrey selectElementWithMatcher:chrome_test_util::
                                          BookmarksContextMenuEditButton()]
      performAction:grey_tap()];

  // Tap the Folder button.
  [BookmarkEarlGreyUI openFolderPicker];

  // Create a new folder.
  [BookmarkEarlGreyUI addFolderWithName:@"Sticky Folder"
                              inStorage:kindOfTestToStorageType(kindOfTest)];

  // Verify that the editor is present.  Uses notNil() instead of
  // sufficientlyVisible() because the large title in the navigation bar causes
  // less than 75% of the table view to be visible.
  [[EarlGrey selectElementWithMatcher:grey_accessibilityID(
                                          kBookmarkEditViewContainerIdentifier)]
      assertWithMatcher:grey_notNil()];

  // Tap the Done button.
  [[EarlGrey selectElementWithMatcher:BookmarksSaveEditDoneButton()]
      performAction:grey_tap()];
  [[EarlGrey selectElementWithMatcher:grey_accessibilityID(
                                          kBookmarkEditViewContainerIdentifier)]
      assertWithMatcher:grey_notVisible()];

  // Close bookmarks
  [[EarlGrey selectElementWithMatcher:BookmarksHomeDoneButton()]
      performAction:grey_tap()];

  // Second, bookmark a page.

  // Verify that the bookmark that is going to be added is not in the
  // BookmarkModel.
  GREYAssertTrue(self.testServer->Start(), @"Server did not start.");
  const GURL bookmarkedURL = self.testServer->GetURL("/fullscreen.html");
  NSString* bookmarkedTitle = @"Full Screen";  // See fullscreen.html.

  [BookmarkEarlGrey
      verifyBookmarksWithTitle:bookmarkedTitle
                 expectedCount:0
                     inStorage:kindOfTestToStorageType(kindOfTest)];
  // Open the page.
  [ChromeEarlGrey loadURL:bookmarkedURL];
  [ChromeEarlGrey waitForWebStateVisibleURL:bookmarkedURL];

  // Verify that the folder has only one element.
  NSString* folderTitle = @"Sticky Folder";
  [BookmarkEarlGrey verifyChildCount:1
                    inFolderWithName:folderTitle
                           inStorage:kindOfTestToStorageType(kindOfTest)];

  // Bookmark the page.
  [BookmarkEarlGreyUI starCurrentTab];

  // Verify the snackbar title.
  std::u16string title = base::SysNSStringToUTF16(folderTitle);
  std::u16string result;
  switch (kindOfTest) {
    case KindOfTest::kSignedOut:
      result = base::i18n::MessageFormatter::FormatWithNamedArgs(
          l10n_util::GetStringUTF16(IDS_IOS_BOOKMARK_PAGE_SAVED_FOLDER),
          "count", 1, "title", title);
      break;
    case KindOfTest::kLocal:
      result = base::i18n::MessageFormatter::FormatWithNamedArgs(
          l10n_util::GetStringUTF16(
              IDS_IOS_BOOKMARK_PAGE_SAVED_FOLDER_TO_DEVICE),
          "count", 1, "title", title);
      break;
    case KindOfTest::kAccount:
      result = base::i18n::MessageFormatter::FormatWithNamedArgs(
          l10n_util::GetStringUTF16(
              IDS_IOS_BOOKMARK_PAGE_SAVED_INTO_ACCOUNT_FOLDER),
          "count", 1, "title", title, "email", "foo1@gmail.com");
      break;
  }
  [[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(
                                          base::SysUTF16ToNSString(result))]
      assertWithMatcher:grey_sufficientlyVisible()];

  // Verify that the newly-created bookmark is in the BookmarkModel.
  [BookmarkEarlGrey
      verifyBookmarksWithTitle:bookmarkedTitle
                 expectedCount:1
                     inStorage:kindOfTestToStorageType(kindOfTest)];

  // Verify that the folder has now two elements.
  [BookmarkEarlGrey verifyChildCount:2
                    inFolderWithName:@"Sticky Folder"
                           inStorage:kindOfTestToStorageType(kindOfTest)];
}

- (void)util_testNewFolderNameCommittedOnNavigatingAway:(KindOfTest)kindOfTest {
  [BookmarkEarlGrey
      setupStandardBookmarksInStorage:kindOfTestToStorageType(kindOfTest)];
  [BookmarkEarlGreyUI openBookmarks];
  [BookmarkEarlGreyUI openMobileBookmarks:kindOfTest];

  // Create a new folder and type "New Folder 1" without pressing return.
  NSString* newFolderTitle = @"New Folder 1";
  [BookmarkEarlGreyUI createNewBookmarkFolderWithFolderTitle:newFolderTitle
                                                 pressReturn:NO];

  // Interrupt the folder name editing by tapping on back.
  [[EarlGrey selectElementWithMatcher:BookmarksNavigationBarBackButton()]
      performAction:grey_tap()];

  // Come back to Mobile Bookmarks.
  [BookmarkEarlGreyUI openMobileBookmarks:kindOfTest];

  // Verify folder name "New Folder 1" was committed.
  [BookmarkEarlGreyUI verifyFolderCreatedWithTitle:newFolderTitle];

  // Create a new folder and type "New Folder 2" without pressing return.
  newFolderTitle = @"New Folder 2";
  [BookmarkEarlGreyUI createNewBookmarkFolderWithFolderTitle:newFolderTitle
                                                 pressReturn:NO];

  // Interrupt the folder name editing by tapping on done.
  [[EarlGrey selectElementWithMatcher:BookmarksHomeDoneButton()]
      performAction:grey_tap()];

  // Wait for Bookmarks to close completely.
  [ChromeEarlGrey waitForUIElementToDisappearWithMatcher:
                      grey_accessibilityID(kBookmarksHomeTableViewIdentifier)];

  // Reopen bookmarks.
  [BookmarkEarlGreyUI openBookmarks];

  // Verify folder name "New Folder 2" was committed.
  [BookmarkEarlGreyUI verifyFolderCreatedWithTitle:newFolderTitle];

  // Create a new folder and type "New Folder 3" without pressing return.
  newFolderTitle = @"New Folder 3";
  [BookmarkEarlGreyUI createNewBookmarkFolderWithFolderTitle:newFolderTitle
                                                 pressReturn:NO];

  // Interrupt the folder name editing by entering Folder 1.
  [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"New Folder 2")]
      performAction:grey_tap()];

  // Come back to Mobile Bookmarks.
  [[EarlGrey selectElementWithMatcher:BookmarksNavigationBarBackButton()]
      performAction:grey_tap()];

  // Verify folder name "New Folder 3" was committed.
  [BookmarkEarlGreyUI verifyFolderCreatedWithTitle:newFolderTitle];

  // Create a new folder and type "New Folder 4" without pressing return.
  newFolderTitle = @"New Folder 4";
  [BookmarkEarlGreyUI createNewBookmarkFolderWithFolderTitle:newFolderTitle
                                                 pressReturn:NO];

  // Interrupt the folder name editing by tapping on First URL.
  [[EarlGrey selectElementWithMatcher:grey_accessibilityID(
                                          kBookmarksHomeTableViewIdentifier)]
      performAction:ScrollToTop()];

  [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"French URL")]
      performAction:grey_tap()];
  // Reopen bookmarks.
  [BookmarkEarlGreyUI openBookmarks];

  // Verify folder name "New Folder 4" was committed.
  [BookmarkEarlGreyUI verifyFolderCreatedWithTitle:newFolderTitle];
}

- (void)util_testCreateNewFolderWithContextBar:(KindOfTest)kindOfTest {
  [BookmarkEarlGrey
      setupStandardBookmarksInStorage:kindOfTestToStorageType(kindOfTest)];
  [BookmarkEarlGreyUI openBookmarks];
  [BookmarkEarlGreyUI openMobileBookmarks:kindOfTest];

  // Create a new folder and name it "New Folder 1".
  NSString* newFolderTitle = @"New Folder 1";
  [BookmarkEarlGreyUI createNewBookmarkFolderWithFolderTitle:newFolderTitle
                                                 pressReturn:YES];

  // Verify "New Folder 1" is created.
  [BookmarkEarlGreyUI verifyFolderCreatedWithTitle:newFolderTitle];

  // Create a new folder and name it "New Folder 2".
  newFolderTitle = @"New Folder 2";
  [BookmarkEarlGreyUI createNewBookmarkFolderWithFolderTitle:newFolderTitle
                                                 pressReturn:YES];

  // Verify "New Folder 2" is created.
  [BookmarkEarlGreyUI verifyFolderCreatedWithTitle:newFolderTitle];

  // Verify context bar does not change after editing folder name.
  [BookmarkEarlGreyUI verifyContextBarInDefaultStateWithSelectEnabled:YES
                                                     newFolderEnabled:YES];

  // Close bookmarks
  [[EarlGrey selectElementWithMatcher:BookmarksHomeDoneButton()]
      performAction:grey_tap()];
}

- (void)util_testAddBookmarkInNewFolder:(KindOfTest)kindOfTest {
  GREYAssertTrue(self.testServer->Start(), @"Server did not start.");
  const GURL bookmarkedURL = self.testServer->GetURL("/pony.html");
  NSString* expectedTitle = @"ponies";  // See pony.html.

  [ChromeEarlGrey loadURL:bookmarkedURL];
  [ChromeEarlGrey waitForWebStateVisibleURL:bookmarkedURL];


  std::u16string label;
  switch (kindOfTest) {
    case KindOfTest::kSignedOut:
      label = l10n_util::GetPluralStringFUTF16(IDS_IOS_BOOKMARKS_BULK_SAVED, 1);
      break;
    case KindOfTest::kLocal:
      label = base::i18n::MessageFormatter::FormatWithNamedArgs(
          l10n_util::GetStringUTF16(
              IDS_IOS_BOOKMARK_PAGE_SAVED_FOLDER_TO_DEVICE),
          "count", 1, "title", "Mobile bookmarks");
      break;
    case KindOfTest::kAccount:
      label = base::i18n::MessageFormatter::FormatWithNamedArgs(
          l10n_util::GetStringUTF16(IDS_IOS_BOOKMARK_PAGE_SAVED_INTO_ACCOUNT),
          "count", 1, "email", "foo1@gmail.com");
      break;
  }
  [BookmarkEarlGreyUI
      starAndEditCurrentTabWithSnackbarTitle:base::SysUTF16ToNSString(label)];

  // Verify that the newly-created bookmark is in the BookmarkModel.
  [BookmarkEarlGrey
      verifyBookmarksWithTitle:expectedTitle
                 expectedCount:1
                     inStorage:kindOfTestToStorageType(kindOfTest)];

  // Verify that the editor is present.

  [BookmarkEarlGreyUI assertChangeFolderIsCorrectlySet:@"Mobile bookmarks"
                                            kindOfTest:kindOfTest];

  // Tap the Folder button.
  [BookmarkEarlGreyUI openFolderPicker];

  // Create a new folder with default name.
  [BookmarkEarlGreyUI addFolderWithName:nil
                              inStorage:kindOfTestToStorageType(kindOfTest)];

  // Verify that the editor is present.
  [[EarlGrey selectElementWithMatcher:grey_accessibilityID(
                                          kBookmarkEditViewContainerIdentifier)]
      assertWithMatcher:grey_notNil()];

  [BookmarkEarlGrey
      verifyExistenceOfFolderWithTitle:@"New folder"
                             inStorage:kindOfTestToStorageType(kindOfTest)];
  // Tap the Done button.
  [[EarlGrey selectElementWithMatcher:BookmarksSaveEditDoneButton()]
      performAction:grey_tap()];
}

- (void)util_searchBookmarks:(KindOfTest)kindOfTest {
  [BookmarkEarlGrey
      setupStandardBookmarksInStorage:kindOfTestToStorageType(kindOfTest)];
  [BookmarkEarlGreyUI openBookmarks];
  [BookmarkEarlGreyUI openMobileBookmarks:kindOfTest];

  // Change to edit mode, using context menu.
  [ChromeEarlGrey
      waitForUIElementToAppearWithMatcher:
          grey_accessibilityID(kBookmarksHomeTrailingButtonIdentifier)];
  [[EarlGrey
      selectElementWithMatcher:grey_accessibilityID(
                                   kBookmarksHomeTrailingButtonIdentifier)]
      performAction:grey_tap()];

  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 1.1")]
      performAction:grey_tap()];

  [[EarlGrey
      selectElementWithMatcher:ContextBarCenterButtonWithLabel(
                                   [BookmarkEarlGreyUI contextBarMoreString])]
      performAction:grey_tap()];

  [[EarlGrey selectElementWithMatcher:
                 chrome_test_util::ActionSheetItemWithAccessibilityLabelId(
                     IDS_IOS_BOOKMARK_CONTEXT_MENU_MOVE)]
      performAction:grey_tap()];

  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 1")]
      assertWithMatcher:grey_sufficientlyVisible()];
  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 2")]
      assertWithMatcher:grey_sufficientlyVisible()];
  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 3")]
      assertWithMatcher:grey_sufficientlyVisible()];

  // Tap on the search bar and check that the scrim is visible.
  [[EarlGrey selectElementWithMatcher:SearchBar()] performAction:grey_tap()];

  [[EarlGrey
      selectElementWithMatcher:grey_accessibilityID(
                                   kBookmarksFolderPickerSearchScrimIdentifier)]
      assertWithMatcher:grey_minimumVisiblePercent(0.5)];

  // Search for "Folder 2" and check the others disappeared.
  [[EarlGrey selectElementWithMatcher:SearchBar()]
      performAction:grey_replaceText(@"Folder 2")];

  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 2")]
      assertWithMatcher:grey_sufficientlyVisible()];

  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 1")]
      assertWithMatcher:grey_notVisible()];
  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 3")]
      assertWithMatcher:grey_notVisible()];

  // Move it to folder 2
  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 2")]
      performAction:grey_tap()];

  [BookmarkEarlGreyUI verifyFolderFlowIsClosed];

  [BookmarkEarlGreyUI closeUndoSnackbarAndWait];

  // Verify edit mode is closed (context bar back to default state).
  [BookmarkEarlGreyUI verifyContextBarInDefaultStateWithSelectEnabled:YES
                                                     newFolderEnabled:YES];

  // Verify "Folder 2" has two bookmark folders (Folder 3 and Folder 1.1).
  [BookmarkEarlGrey verifyChildCount:2
                    inFolderWithName:@"Folder 2"
                           inStorage:kindOfTestToStorageType(kindOfTest)];
  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 1")]
      performAction:grey_tap()];
  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 2")]
      performAction:grey_tap()];
  [ChromeEarlGrey
      waitForUIElementToAppearWithMatcher:TappableBookmarkNodeWithLabel(
                                              @"Folder 1.1")];
  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 1.1")]
      assertWithMatcher:grey_sufficientlyVisible()];
  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 3")]
      assertWithMatcher:grey_sufficientlyVisible()];

  // Close bookmarks
  [[EarlGrey selectElementWithMatcher:BookmarksHomeDoneButton()]
      performAction:grey_tap()];
}

// Tests that new folder is created under `Mobile Bookmarks` by default.
// TODO(crbug.com/40266964): Add this test after support is available.
// - (void)testCreateNewAccountFolderDefaultDestination {}

@end

@interface BookmarksFolderChooserSignedOutTestCase
    : BookmarksFolderChooserBaseTestCase
@end

@implementation BookmarksFolderChooserSignedOutTestCase

// Tests that new folder is created under `Mobile Bookmarks` by default.
- (void)testCreateNewLocalOrSyncableFolderDefaultDestinationSignedOut {
  [self util_testCreateNewLocalOrSyncableFolderDefaultDestination:
            KindOfTest::kSignedOut];
}

// Tests moving bookmarks into a new folder created in the moving process.
- (void)testCreateNewFolderWhileMovingBookmarksSignedOut {
  [self util_testCreateNewFolderWhileMovingBookmarks:KindOfTest::kSignedOut];
}

- (void)testCantDeleteFolderBeingEditedSignedOut {
  [self util_testCantDeleteFolderBeingEdited:KindOfTest::kSignedOut];
}

- (void)testNavigateAwayFromFolderBeingEditedSignedOut {
  [self util_testNavigateAwayFromFolderBeingEdited:KindOfTest::kSignedOut];
}

- (void)testDeleteSingleFolderNodeSignedOut {
  [self util_testDeleteSingleFolderNode:KindOfTest::kSignedOut];
}

- (void)testSwipeDownToDismissFromEditSignedOut {
  [self util_testSwipeDownToDismissFromEditFolder:KindOfTest::kSignedOut];
}

// Test when current navigating folder is deleted in background, empty
// background should be shown with context bar buttons disabled.
- (void)testWhenCurrentFolderDeletedInBackgroundSignedOut {
  [self util_testWhenCurrentFolderDeletedInBackground:KindOfTest::kSignedOut];
}

- (void)testLongPressOnSingleSignedOut {
  [self util_testLongPressOnSingleFolder:KindOfTest::kSignedOut];
}

// Verify Edit functionality for single folder selection.
- (void)testEditFunctionalityOnSingleSignedOut {
  [self util_testEditFunctionalityOnSingleFolder:KindOfTest::kSignedOut];
}

// Verify undoing a move.
- (void)testMoveAndUndoSignedOut {
  [self util_testMoveAndUndoFromModel:KindOfTest::kSignedOut
                              toModel:KindOfTest::kSignedOut];
}

// Verify Move functionality on single folder through long press.
- (void)testMoveFunctionalityOnSingleFolderSignedOut {
  [self
      util_testMoveFunctionalityOnSingleFolderFromModel:KindOfTest::kSignedOut
                                                toModel:KindOfTest::kSignedOut];
}

// Verify Move functionality on multiple folder selection.
- (void)testMoveFunctionalityOnMultipleSignedOut {
  [self util_testMoveFunctionalityOnMultipleFolder:KindOfTest::kSignedOut];
}

- (void)testContextBarForSingleFolderSelectionSignedOut {
  [self util_testContextBarForSingleFolderSelection:KindOfTest::kSignedOut];
}

- (void)testContextMenuForMultipleFolderSelectionSignedOut {
  [self util_testContextMenuForMultipleFolderSelection:KindOfTest::kSignedOut];
}

// Tests that the default folder bookmarks are saved in is updated to the last
// used folder.
- (void)testStickyDefaultSignedOut {
  [BookmarkEarlGrey setLastUsedBookmarkFolderToMobileBookmarksInStorageType:
                        BookmarkStorageType::kLocalOrSyncable];
  [self util_testStickyDefaultFolder:KindOfTest::kSignedOut];
}

// Tests the new folder name is committed when name editing is interrupted by
// navigating away.
- (void)testNewFolderNameCommittedOnNavigatingAwaySignedOut {
  [self util_testNewFolderNameCommittedOnNavigatingAway:KindOfTest::kSignedOut];
}

// Tests the creation of new folders by tapping on 'New Folder' button of the
// context bar.
- (void)testCreateNewFolderWithContextBarSignedOut {
  [self util_testCreateNewFolderWithContextBar:KindOfTest::kSignedOut];
}

// Test the creation of a bookmark and new folder (by tapping on the star).
- (void)testAddBookmarkInNewSignedOut {
  [self util_testAddBookmarkInNewFolder:KindOfTest::kSignedOut];
}

// Verify Move functionality on search.
- (void)testSearchBookmarksSignedOut {
  [self util_searchBookmarks:KindOfTest::kSignedOut];
}

@end

@interface BookmarksFolderChooserLocalTestCase
    : BookmarksFolderChooserBaseTestCase
@end

@implementation BookmarksFolderChooserLocalTestCase

// Tests that new folder is created under `Mobile Bookmarks` by default.
- (void)testCreateNewLocalOrSyncableFolderDefaultDestinationLocal {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testCreateNewLocalOrSyncableFolderDefaultDestination:KindOfTest::
                                                                      kLocal];
}

// Tests moving bookmarks into a new folder created in the moving process.
- (void)testCreateNewFolderWhileMovingBookmarksLocal {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testCreateNewFolderWhileMovingBookmarks:KindOfTest::kLocal];
}

- (void)testCantDeleteFolderBeingEditedLocal {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testCantDeleteFolderBeingEdited:KindOfTest::kLocal];
}

- (void)testNavigateAwayFromFolderBeingEditedLocal {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testNavigateAwayFromFolderBeingEdited:KindOfTest::kLocal];
}

- (void)testDeleteSingleFolderNodeLocal {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testDeleteSingleFolderNode:KindOfTest::kLocal];
}

- (void)testSwipeDownToDismissFromEditFolderLocal {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testSwipeDownToDismissFromEditFolder:KindOfTest::kLocal];
}

// Test when current navigating folder is deleted in background, empty
// background should be shown with context bar buttons disabled.
- (void)testWhenCurrentFolderDeletedInBackgroundLocal {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testWhenCurrentFolderDeletedInBackground:KindOfTest::kLocal];
}

- (void)testLongPressOnSingleFolderLocal {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testLongPressOnSingleFolder:KindOfTest::kLocal];
}

// Verify Edit functionality for single folder selection.
- (void)testEditFunctionalityOnSingleFolderLocal {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testEditFunctionalityOnSingleFolder:KindOfTest::kLocal];
}

// Verify undoing a move.
- (void)testMoveAndUndoLocal {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testMoveAndUndoFromModel:KindOfTest::kLocal
                              toModel:KindOfTest::kLocal];
}

- (void)testMoveAndUndoLocalToAccount {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testMoveAndUndoFromModel:KindOfTest::kLocal
                              toModel:KindOfTest::kAccount];
}

// Verify Move functionality on single folder through long press.
- (void)testMoveFunctionalityOnSingleFolderLocal {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testMoveFunctionalityOnSingleFolderFromModel:KindOfTest::kLocal
                                                  toModel:KindOfTest::kLocal];
}

- (void)testMoveFunctionalityOnSingleFolderLocalToAccount {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testMoveFunctionalityOnSingleFolderFromModel:KindOfTest::kLocal
                                                  toModel:KindOfTest::kAccount];
}

// Verify Move functionality on multiple folder selection.
- (void)testMoveFunctionalityOnMultipleFolderLocal {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testMoveFunctionalityOnMultipleFolder:KindOfTest::kLocal];
}

- (void)testContextBarForSingleFolderSelectionLocal {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testContextBarForSingleFolderSelection:KindOfTest::kLocal];
}

- (void)testContextMenuForMultipleFolderSelectionLocal {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testContextMenuForMultipleFolderSelection:KindOfTest::kLocal];
}

// Tests that the default folder bookmarks are saved in is updated to the last
// used folder.
- (void)testStickyDefaultFolderLocal {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [BookmarkEarlGrey setLastUsedBookmarkFolderToMobileBookmarksInStorageType:
                        BookmarkStorageType::kLocalOrSyncable];
  [self util_testStickyDefaultFolder:KindOfTest::kLocal];
}

// Tests the new folder name is committed when name editing is interrupted by
// navigating away.
- (void)testNewFolderNameCommittedOnNavigatingAwayLocal {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testNewFolderNameCommittedOnNavigatingAway:KindOfTest::kLocal];
}

// Tests the creation of new folders by tapping on 'New Folder' button of the
// context bar.
- (void)testCreateNewFolderWithContextBarLocal {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testCreateNewFolderWithContextBar:KindOfTest::kLocal];
}

// Test the creation of a bookmark and new folder (by tapping on the star).
- (void)testAddBookmarkInNewFolderLocal {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [BookmarkEarlGrey setLastUsedBookmarkFolderToMobileBookmarksInStorageType:
                        BookmarkStorageType::kLocalOrSyncable];
  [self util_testAddBookmarkInNewFolder:KindOfTest::kLocal];
}

// Verify Move functionality on search.
- (void)testSearchBookmarksLocal {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_searchBookmarks:KindOfTest::kLocal];
}

// Tests that swiping down on BookmarksFolderChooserViewController when opened
// directly from Bookmarks Home (e.g. Move action) dismisses cleanly.
- (void)testSwipeDownToDismissFolderChooserStandaloneLocal {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [BookmarkEarlGrey
      setupStandardBookmarksInStorage:BookmarkStorageType::kLocalOrSyncable];
  [BookmarkEarlGreyUI openBookmarks];
  [BookmarkEarlGreyUI openMobileBookmarks:KindOfTest::kLocal];

  // Long press on folder and tap Move to open Folder Chooser standalone.
  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 1")]
      performAction:grey_longPress()];
  [[EarlGrey selectElementWithMatcher:ButtonWithAccessibilityLabelId(
                                          IDS_IOS_BOOKMARK_CONTEXT_MENU_MOVE)]
      performAction:grey_tap()];

  // Verify Folder Chooser is visible.
  [[EarlGrey
      selectElementWithMatcher:
          grey_accessibilityID(kBookmarkFolderPickerViewContainerIdentifier)]
      assertWithMatcher:grey_sufficientlyVisible()];

  // Swipe down to dismiss Folder Chooser.
  [[EarlGrey
      selectElementWithMatcher:
          grey_accessibilityID(kBookmarkFolderPickerViewContainerIdentifier)]
      performAction:grey_swipeFastInDirection(kGREYDirectionDown)];

  // Verify Folder Chooser is dismissed.
  [ChromeEarlGrey
      waitForUIElementToDisappearWithMatcher:
          grey_accessibilityID(kBookmarkFolderPickerViewContainerIdentifier)];

  // Dismiss Bookmarks Home.
  [[EarlGrey selectElementWithMatcher:BookmarksHomeDoneButton()]
      performAction:grey_tap()];
}

@end

@interface BookmarksFolderChooserAccountTestCase
    : BookmarksFolderChooserBaseTestCase
@end

@implementation BookmarksFolderChooserAccountTestCase

// Tests that new folder is created under `Mobile Bookmarks` by default.
- (void)testCreateNewLocalOrSyncableFolderDefaultDestinationAccount {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testCreateNewLocalOrSyncableFolderDefaultDestination:KindOfTest::
                                                                      kAccount];
}

// Tests moving bookmarks into a new folder created in the moving process.
- (void)testCreateNewFolderWhileMovingBookmarksAccount {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testCreateNewFolderWhileMovingBookmarks:KindOfTest::kAccount];
}

- (void)testCantDeleteFolderBeingEditedAccount {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testCantDeleteFolderBeingEdited:KindOfTest::kAccount];
}

- (void)testNavigateAwayFromFolderBeingEditedAccount {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testNavigateAwayFromFolderBeingEdited:KindOfTest::kAccount];
}

- (void)testDeleteSingleFolderNodeAccount {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testDeleteSingleFolderNode:KindOfTest::kAccount];
}

- (void)testSwipeDownToDismissFromEditFolderAccount {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testSwipeDownToDismissFromEditFolder:KindOfTest::kAccount];
}

// Test when current navigating folder is deleted in background, empty
// background should be shown with context bar buttons disabled.
- (void)testWhenCurrentFolderDeletedInBackgroundAccount {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testWhenCurrentFolderDeletedInBackground:KindOfTest::kAccount];
}

- (void)testLongPressOnSingleFolderAccount {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testLongPressOnSingleFolder:KindOfTest::kAccount];
}

- (void)testEditFunctionalityOnSingleFolderAccount {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testEditFunctionalityOnSingleFolder:KindOfTest::kAccount];
}

// Verify undoing a move.
- (void)testMoveAndUndoAccount {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testMoveAndUndoFromModel:KindOfTest::kAccount
                              toModel:KindOfTest::kAccount];
}

- (void)testMoveAndUndoAccountToLocal {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testMoveAndUndoFromModel:KindOfTest::kAccount
                              toModel:KindOfTest::kLocal];
}

// Verify Move functionality on single folder through long press.
- (void)testMoveFunctionalityOnSingleFolderAccount {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testMoveFunctionalityOnSingleFolderFromModel:KindOfTest::kAccount
                                                  toModel:KindOfTest::kAccount];
}

- (void)testMoveFunctionalityOnSingleFolderAccountToLocal {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testMoveFunctionalityOnSingleFolderFromModel:KindOfTest::kAccount
                                                  toModel:KindOfTest::kLocal];
}

// Verify Move functionality on multiple folder selection.
- (void)testMoveFunctionalityOnMultipleFolderAccount {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testMoveFunctionalityOnMultipleFolder:KindOfTest::kAccount];
}

- (void)testContextBarForSingleFolderSelectionAccount {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testContextBarForSingleFolderSelection:KindOfTest::kAccount];
}

- (void)testContextMenuForMultipleFolderSelectionAccount {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testContextMenuForMultipleFolderSelection:KindOfTest::kAccount];
}

// Tests that the default folder bookmarks are saved in is updated to the last
// used folder.
- (void)testStickyDefaultFolderAccount {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [BookmarkEarlGrey setLastUsedBookmarkFolderToMobileBookmarksInStorageType:
                        BookmarkStorageType::kAccount];
  [self util_testStickyDefaultFolder:KindOfTest::kAccount];
}

// Tests the new folder name is committed when name editing is interrupted by
// navigating away.
- (void)testNewFolderNameCommittedOnNavigatingAwayAccount {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testNewFolderNameCommittedOnNavigatingAway:KindOfTest::kAccount];
}

// Tests the creation of new folders by tapping on 'New Folder' button of the
// context bar.
- (void)testCreateNewFolderWithContextBarAccount {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testCreateNewFolderWithContextBar:KindOfTest::kAccount];
}

// Test the creation of a bookmark and new folder (by tapping on the star).
- (void)testAddBookmarkInNewFolderAccount {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_testAddBookmarkInNewFolder:KindOfTest::kAccount];
}

// Regression test for crbug.com/330345514
// Checks that Chrome does not crash when the user sign-out while in an account
// bookmark folder.
- (void)testSignOutInRecursiveBookmarkAccount {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [BookmarkEarlGrey setupStandardBookmarksInStorage:kindOfTestToStorageType(
                                                        KindOfTest::kAccount)];
  [BookmarkEarlGreyUI openBookmarks];
  [BookmarkEarlGreyUI openMobileBookmarks:KindOfTest::kAccount];

  // Open `Folder 3` nested in `Folder 1->Folder 2`.
  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 1")]
      performAction:grey_tap()];
  [[EarlGrey
      selectElementWithMatcher:TappableBookmarkNodeWithLabel(@"Folder 2")]
      performAction:grey_tap()];
  [SigninEarlGrey signOut];
  [BookmarkEarlGreyUI verifyEmptyBackgroundAppears];
}

// Verify Move functionality on search.
- (void)testSearchBookmarksAccount {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [self util_searchBookmarks:KindOfTest::kAccount];
}

// Tests that signing out while BookmarksFolderChooser is open dismisses the
// chooser gracefully without crash.
- (void)testSignOutWhileFolderChooserIsOpenAccount {
  [SigninEarlGreyUI signinWithFakeIdentity:[FakeSystemIdentity fakeIdentity1]];
  [BookmarkEarlGrey addBookmarkWithTitle:@"Account Bookmark"
                                     URL:@"https://www.example.com"
                               inStorage:BookmarkStorageType::kAccount];
  [BookmarkEarlGreyUI openBookmarks];
  [BookmarkEarlGreyUI
      openMobileBookmarks:chrome_test_util::KindOfTest::kAccount];

  // Long press on bookmark and tap Edit.
  [[EarlGrey
      selectElementWithMatcher:chrome_test_util::TappableBookmarkNodeWithLabel(
                                   @"Account Bookmark",
                                   chrome_test_util::KindOfTest::kAccount)]
      performAction:grey_longPress()];
  [[EarlGrey selectElementWithMatcher:chrome_test_util::
                                          BookmarksContextMenuEditButton()]
      performAction:grey_tap()];

  [BookmarkEarlGreyUI openFolderPicker];

  // Verify Folder Chooser is visible.
  [[EarlGrey
      selectElementWithMatcher:
          grey_accessibilityID(kBookmarkFolderPickerViewContainerIdentifier)]
      assertWithMatcher:grey_sufficientlyVisible()];

  // Sign out while Folder Chooser is open.
  [SigninEarlGrey signOut];

  // Folder Chooser and Editor should be dismissed gracefully.
  [ChromeEarlGrey
      waitForUIElementToDisappearWithMatcher:
          grey_accessibilityID(kBookmarkFolderPickerViewContainerIdentifier)];
  [ChromeEarlGrey
      waitForUIElementToDisappearWithMatcher:
          grey_accessibilityID(kBookmarkEditViewContainerIdentifier)];
}

@end
