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

#import "ios/chrome/browser/omnibox/eg_tests/omnibox_matchers.h"

#import "base/strings/string_number_conversions.h"
#import "ios/chrome/browser/omnibox/public/omnibox_popup_accessibility_identifier_constants.h"
#import "ios/chrome/common/NSString+Chromium.h"
#import "ios/chrome/grit/ios_strings.h"
#import "ios/chrome/test/earl_grey/chrome_earl_grey.h"
#import "ios/chrome/test/earl_grey/chrome_matchers.h"
#import "ios/chrome/test/earl_grey/chrome_test_case.h"
#import "ios/testing/earl_grey/earl_grey_test.h"
#import "url/gurl.h"

namespace omnibox {

id<GREYMatcher> PopupRowPrimaryTextMatcher() {
  return grey_allOf(
      grey_accessibilityID(kOmniboxPopupRowPrimaryTextAccessibilityIdentifier),
      grey_sufficientlyVisible(), nil);
}

id<GREYMatcher> PopupRowSecondaryTextMatcher() {
  return grey_allOf(grey_accessibilityID(
                        kOmniboxPopupRowSecondaryTextAccessibilityIdentifier),
                    grey_sufficientlyVisible(), nil);
}

id<GREYMatcher> PopupRowAtIndex(NSIndexPath* index) {
  NSString* accessibilityID = [OmniboxPopupAccessibilityIdentifierHelper
      accessibilityIdentifierForRowAtIndexPath:index];
  return grey_allOf(grey_accessibilityID(accessibilityID), nil);
}

id<GREYMatcher> PopupRowWithUrlMatcher(GURL url) {
  NSString* url_string = [NSString cr_fromString:url.GetContent()];
  // Lower to visibility threshold for the visibility of the element, because
  // it can be overlaid by a blur effect view, which isn't considered
  // translucent by EG.
  id<GREYMatcher> url_matcher = grey_allOf(
      grey_descendant(
          chrome_test_util::StaticTextWithAccessibilityLabel(url_string)),
      grey_minimumVisiblePercent(0.6), nil);
  return grey_allOf(chrome_test_util::OmniboxPopupRow(), url_matcher, nil);
}

id<GREYMatcher> ClearButtonMatcher() {
  return chrome_test_util::ButtonWithAccessibilityLabelId(
      IDS_IOS_ACCNAME_CLEAR_TEXT);
}

}  // namespace omnibox
