Skip to content

Commit

Permalink
another test
Browse files Browse the repository at this point in the history
  • Loading branch information
nornagon committed Nov 6, 2018
1 parent e19df88 commit 5c8187f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
12 changes: 12 additions & 0 deletions atom/browser/font_defaults.cc
Expand Up @@ -16,6 +16,14 @@

namespace {

// The following list of font defaults was copied from
// https://chromium.googlesource.com/chromium/src/+/69.0.3497.106/chrome/browser/ui/prefs/prefs_tab_helper.cc#152
//
// The only updates that should be made to this list are copying updates that
// were made in Chromium.
//
// vvvvv DO NOT EDIT vvvvv

struct FontDefault {
const char* pref_name;
int resource_id;
Expand Down Expand Up @@ -97,6 +105,8 @@ const FontDefault kFontDefaults[] = {
};
const size_t kFontDefaultsLength = arraysize(kFontDefaults);

// ^^^^^ DO NOT EDIT ^^^^^

std::string GetDefaultFontForPref(const char* pref_name) {
for (size_t i = 0; i < kFontDefaultsLength; ++i) {
FontDefault pref = kFontDefaults[i];
Expand All @@ -115,6 +125,8 @@ using ScriptFontMap = std::unordered_map<const char*, base::string16>;
// Key comparison uses pointer equality.
using FontFamilyMap = std::unordered_map<const char*, ScriptFontMap>;

// A lookup table mapping (font-family, script) -> font-name
// e.g. ("sans-serif", "Zyyy") -> "Arial"
FontFamilyMap g_font_cache;

base::string16 FetchFont(const char* script, const char* map_name) {
Expand Down
56 changes: 36 additions & 20 deletions spec/chromium-spec.js
Expand Up @@ -1315,24 +1315,9 @@ describe('chromium feature', () => {
})

describe('font fallback', () => {
it('should fall back to non-PingFang SC font for sans-serif japanese script', async () => {
async function getRenderedFonts (html) {
const w = new BrowserWindow({ show: false })
try {
const html = `
<html lang="ja-JP">
<head>
<meta charset="utf-8" />
</head>
<body style="font-family: sans-serif">
test 智史
</body>
</html>
`
const fallbackFontJa = {
'win32': 'Meiryo',
'darwin': 'Hiragino Kaku Gothic ProN',
'linux': 'VL PGothic'
}[process.platform]
const loaded = emittedOnce(w.webContents, 'did-finish-load')
w.loadURL(`data:text/html,${html}`)
await loaded
Expand All @@ -1344,12 +1329,43 @@ describe('font fallback', () => {
})
const { nodeId } = (await sendCommand('DOM.getDocument')).root.children[0]
await sendCommand('CSS.enable')
const { fonts } = await sendCommand('CSS.getPlatformFontsForNode', {nodeId})
expect(fonts).to.be.an('array')
expect(fonts).to.have.length(1)
expect(fonts[0].familyName).to.equal(fallbackFontJa)
const { fonts } = await sendCommand('CSS.getPlatformFontsForNode', { nodeId })
return fonts
} finally {
w.close()
}
}

it('should use Helvetica for sans-serif on mac, and Arial on windows and linux', async () => {
const html = `<span style="font-family: sans-serif">test</body>`
const fonts = await getRenderedFonts(html)
expect(fonts).to.be.an('array')
expect(fonts).to.have.length(1)
expect(fonts[0].familyName).to.equal({
'win32': 'Arial',
'darwin': 'Helvetica',
'linux': 'Arial'
}[process.platform])
})

it('should fall back to non-PingFang SC font for sans-serif japanese script', async () => {
if (process.platform === 'linux') {
return this.skip()
}
const html = `
<html lang="ja-JP">
<head>
<meta charset="utf-8" />
</head>
<body style="font-family: sans-serif">test 智史</body>
</html>
`
const fonts = await getRenderedFonts(html)
expect(fonts).to.be.an('array')
expect(fonts).to.have.length(1)
expect(fonts[0].familyName).to.equal({
'win32': 'Meiryo',
'darwin': 'Hiragino Kaku Gothic ProN'
}[process.platform])
})
})

0 comments on commit 5c8187f

Please sign in to comment.