ax_mac: no direct NSAccessibilityImageRole check

Relying on internal role method makes faster and reliable code. Set of
assertions will help to catch unexpected things faster.

Followup of
https://chromium-review.googlesource.com/c/chromium/src/+/3485201

Change-Id: I1880b838c660a410bf4458f5763236a9b8122de8
Bug: 1254906
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3517536
Commit-Queue: Alexander Surkkov <asurkov@igalia.com>
Reviewed-by: Nektarios Paisios <nektar@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1001526}
diff --git a/ui/accessibility/platform/ax_platform_node_cocoa.mm b/ui/accessibility/platform/ax_platform_node_cocoa.mm
index 7342d7b..d6fae03 100644
--- a/ui/accessibility/platform/ax_platform_node_cocoa.mm
+++ b/ui/accessibility/platform/ax_platform_node_cocoa.mm
@@ -208,6 +208,10 @@
 
 // Returns the native wrapper for the given node id.
 - (AXPlatformNodeCocoa*)fromNodeID:(ui::AXNodeID)id;
+
+// Returns true if this object is an image.
+- (BOOL)isImage;
+
 @end
 
 @implementation AXPlatformNodeCocoa {
@@ -673,6 +677,19 @@
   return nil;
 }
 
+- (BOOL)isImage {
+  bool isImage =
+      ui::IsImage(_node->GetRole()) &&
+      !_node->GetBoolAttribute(ax::mojom::BoolAttribute::kCanvasHasFallback);
+  DCHECK(!([[self accessibilityRole] isEqualToString:NSAccessibilityImageRole] ^
+           isImage))
+      << "Internal and native roles do not match when determining if this "
+         "object is an image. "
+      << "Chrome role: " << ui::ToString(_node->GetRole())
+      << ", NSAccessibility role: " << [self accessibilityRole];
+  return isImage;
+}
+
 - (NSString*)getName {
   return base::SysUTF8ToNSString(_node->GetName());
 }
@@ -1770,7 +1787,7 @@
 
   // Given an image where there's no other title, return the base part
   // of the filename as the description.
-  if ([[self accessibilityRole] isEqualToString:NSAccessibilityImageRole]) {
+  if ([self isImage]) {
     std::string url;
     if (_node->GetStringAttribute(ax::mojom::StringAttribute::kUrl, &url)) {
       // Given a url like http://foo.com/bar/baz.png, just return the