chromium/content/browser/font_access/font_access_manager_browsertest.cc

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

#include "content/browser/font_access/font_access_manager.h"

#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "base/task/thread_pool.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "base/threading/sequence_bound.h"
#include "build/build_config.h"
#include "content/browser/font_access/font_access_test_utils.h"
#include "content/browser/font_access/font_enumeration_cache.h"
#include "content/browser/font_access/font_enumeration_data_source.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/browser/storage_partition_impl.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/shell/browser/shell.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/mojom/font_access/font_access.mojom.h"

namespace content {

// This class exists so that tests can be written without enabling
// the kFontAccess feature flag. This will be redundant once the flag
// goes away.
class FontAccessManagerBrowserBase : public ContentBrowserTest {};

class FontAccessManagerBrowserTest : public FontAccessManagerBrowserBase {};

// Disabled test: https://crbug.com/1224238
IN_PROC_BROWSER_TEST_F(FontAccessManagerBrowserBase,
                       DISABLED_RendererInterfaceIsBound) {}

IN_PROC_BROWSER_TEST_F(FontAccessManagerBrowserTest, EnumerationTest) {}

IN_PROC_BROWSER_TEST_F(FontAccessManagerBrowserTest,
                       EnumerationTestWithInvalidSelect) {}

#if BUILDFLAG(IS_WIN)
IN_PROC_BROWSER_TEST_F(FontAccessManagerBrowserTest, LocaleTest) {
  ASSERT_TRUE(NavigateToURL(shell(), GetTestUrl(nullptr, "simple_page.html")));
  OverrideFontAccessLocale("zh-cn");
  font_access_manager()->SkipPrivacyChecksForTesting(true);

  EvalJsResult result =
      EvalJs(shell(),
             "(async () => {"
             "  let fullName = '';"
             "  const fonts = await self.queryLocalFonts();"
             "  for (const item of fonts) {"
             "    if (item.postscriptName == 'MicrosoftYaHei') {"
             "      fullName = item.fullName;"
             "      break;"
             "    }"
             "  }"
             "  return fullName;"
             "})()");
  std::string ms_yahei_utf8 = "微软雅黑";
  EXPECT_EQ(ms_yahei_utf8, result);
}

IN_PROC_BROWSER_TEST_F(FontAccessManagerBrowserTest, UnlocalizedFamilyTest) {
  ASSERT_TRUE(NavigateToURL(shell(), GetTestUrl(nullptr, "simple_page.html")));
  OverrideFontAccessLocale("zh-cn");
  font_access_manager()->SkipPrivacyChecksForTesting(true);

  EvalJsResult result =
      EvalJs(shell(),
             "(async () => {"
             "  let family = '';"
             "  const fonts = await self.queryLocalFonts();"
             "  for (const item of fonts) {"
             "    if (item.postscriptName == 'MicrosoftYaHei') {"
             "      family = item.family;"
             "      break;"
             "    }"
             "  }"
             "  return family;"
             "})()");
  std::string unlocalized_family = "Microsoft YaHei";
  EXPECT_EQ(unlocalized_family, result);
}
#endif  // BUILDFLAG(IS_WIN)

}  // namespace content