Test for font family on mac using NSFontManager::availableFontsForFamily

On Mac OS 10.15 Catalina, NSFontManager::availableFamilies does not list
"Hiragino Kaku Gothic ProN" any more but only lists it as "Hiragino
Sans". However, NSFontManager::availableFontsForFamily still shows
results when searching for this name. Use this as a workaround for
finding a result for the standard "sans-serif" Japanese font that is
stored in the preferences, which is "Hiragino Kaku Gothic ProN", until
we update this font matching code to be only CoreText based.

Since the previous code mentions enumerating families being essential
for performing case-insensitive comparisons, add a test to ensure that
the new API actually performs case-insensitive matches on all Mac OS
versions (at this point back to 10.10) that we support.

Bug: 1000542
Change-Id: I43aac2ce74cdc40a50d11ff075bad71a7e7a5d38
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1872055
Commit-Queue: Emil A Eklund <eae@chromium.org>
Reviewed-by: Emil A Eklund <eae@chromium.org>
Reviewed-by: Ben Wagner <bungeman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#708317}
diff --git a/third_party/blink/renderer/platform/fonts/mac/font_matcher_mac.mm b/third_party/blink/renderer/platform/fonts/mac/font_matcher_mac.mm
index 0b9c2ef..8d01704 100644
--- a/third_party/blink/renderer/platform/fonts/mac/font_matcher_mac.mm
+++ b/third_party/blink/renderer/platform/fonts/mac/font_matcher_mac.mm
@@ -194,16 +194,19 @@
   NSString* desired_family = desired_family_string;
   NSFontManager* font_manager = [NSFontManager sharedFontManager];
 
-  // Do a simple case insensitive search for a matching font family.
-  // NSFontManager requires exact name matches.
-  // This addresses the problem of matching arial to Arial, etc., but perhaps
-  // not all the issues.
-  NSEnumerator* e = [[font_manager availableFontFamilies] objectEnumerator];
-  NSString* available_family;
-  while ((available_family = [e nextObject])) {
-    if ([desired_family caseInsensitiveCompare:available_family] ==
-        NSOrderedSame)
-      break;
+  // From Mac OS 10.15 [NSFontManager availableFonts] does not list certain
+  // fonts that availableMembersOfFontFamily actually shows results for, for
+  // example "Hiragino Kaku Gothic ProN" is not listed, only Hiragino Sans is
+  // listed. We previously enumerated availableFontFamilies and looked for a
+  // case-insensitive string match here, but instead, we can rely on
+  // availableMembersOfFontFamily here to do a case-insensitive comparison, then
+  // set available_family to desired_family if the result was not empty.
+  // See https://crbug.com/1000542
+  NSString* available_family = nil;
+  NSArray* fonts_in_family =
+      [font_manager availableMembersOfFontFamily:desired_family];
+  if (fonts_in_family && [fonts_in_family count]) {
+    available_family = desired_family;
   }
 
   int app_kit_font_weight = ToAppKitFontWeight(desired_weight);
diff --git a/third_party/blink/web_tests/NeverFixTests b/third_party/blink/web_tests/NeverFixTests
index 192cde9..f2ea74f 100644
--- a/third_party/blink/web_tests/NeverFixTests
+++ b/third_party/blink/web_tests/NeverFixTests
@@ -144,6 +144,10 @@
 # Linux layout tests do not have a Myanmar fallback font.
 [ Linux ] inspector-protocol/layout-fonts/fallback-myanmar.js [ WontFix ]
 
+# Mac only test for case-insensitive font-matching.
+[ Linux ] inspector-protocol/layout-fonts/mac-case-insensitive-matching.js [ WontFix ]
+[ Win ] inspector-protocol/layout-fonts/mac-case-insensitive-matching.js [ WontFix ]
+
 # Segoe UI matching is only relevant on Windows
 [ Mac ] inspector-protocol/layout-fonts/font-weight-granularity-matching.js [ WontFix ]
 [ Linux ] inspector-protocol/layout-fonts/font-weight-granularity-matching.js [ WontFix ]
diff --git a/third_party/blink/web_tests/inspector-protocol/layout-fonts/mac-case-insensitive-matching.js b/third_party/blink/web_tests/inspector-protocol/layout-fonts/mac-case-insensitive-matching.js
new file mode 100644
index 0000000..ffee61a1
--- /dev/null
+++ b/third_party/blink/web_tests/inspector-protocol/layout-fonts/mac-case-insensitive-matching.js
@@ -0,0 +1,37 @@
+(async function(testRunner) {
+  var page = await testRunner.createPage();
+  await page.loadHTML(`
+    <html>
+    <meta charset="UTF-8">
+    <style>
+    </style>
+    <body>
+        <div class="test">
+            <!-- Use a different font size in order to avoid caching the FontDescriptions and skipping matching. -->
+            <div id="mixed_case_1" style="font-family: HIragINO KakU GothiC ProN; font-size: 12px;">クローミアム</div>
+            <div id="mixed_case_2" style="font-family: hiRAGino kAKu gOTHIc pROn; font-size: 13px;">クローミアム</div>
+            <div id="all_lower_case" style="font-family: hiragino kaku gothic pron; font-size: 14px;">クローミアム</div>
+            <div id="all_upper_case" style="font-family: HIRAGINO KAKU GOTHIC PRON; font-size: 15px;">クローミアム</div>
+            <div id="exact_case" style="font-family: Hiragino Kaku Gothic ProN; font-size: 16px;">クローミアム</div>
+        </div>
+    </body>
+    </html>
+  `);
+  var session = await page.createSession();
+  testRunner.log('Test passes if each of the test divs uses the Hiragino Kaku Gothic ProN ' +
+                 'independently of capitalization of the font family name..');
+
+  var helper = await testRunner.loadScript('./resources/layout-font-test.js');
+  var results = await helper(testRunner, session);
+
+  var passed = results.length == 5;
+  const reduce_match_regexp = (passed, currentValue) => {
+    var current_value_passes = /Hiragino Kaku Gothic ProN/.test(currentValue.usedFonts[0].familyName)
+        && currentValue.usedFonts[0].glyphCount == 6;
+    return passed && current_value_passes;
+  }
+  passed = passed && results.reduce(reduce_match_regexp);
+
+  testRunner.log(passed ? 'PASS' : 'FAIL');
+  testRunner.completeTest();
+})
diff --git a/third_party/blink/web_tests/platform/mac/inspector-protocol/layout-fonts/mac-case-insensitive-matching-expected.txt b/third_party/blink/web_tests/platform/mac/inspector-protocol/layout-fonts/mac-case-insensitive-matching-expected.txt
new file mode 100644
index 0000000..2fe13cd
--- /dev/null
+++ b/third_party/blink/web_tests/platform/mac/inspector-protocol/layout-fonts/mac-case-insensitive-matching-expected.txt
@@ -0,0 +1,23 @@
+Test passes if each of the test divs uses the Hiragino Kaku Gothic ProN independently of capitalization of the font family name..
+クローミアム
+#mixed_case_1:
+"Hiragino Kaku Gothic ProN" : 6
+
+クローミアム
+#mixed_case_2:
+"Hiragino Kaku Gothic ProN" : 6
+
+クローミアム
+#all_lower_case:
+"Hiragino Kaku Gothic ProN" : 6
+
+クローミアム
+#all_upper_case:
+"Hiragino Kaku Gothic ProN" : 6
+
+クローミアム
+#exact_case:
+"Hiragino Kaku Gothic ProN" : 6
+
+PASS
+