#!/bin/bash set -euo pipefail source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" run_node_test <<'JS' const model = requireFromRoot('shell/plugins/bar/widgets/KeyboardLayoutModel.js') // Trimmed from xkbcli list, keeping the format of every section it prints, // including the options nested under an option group: those quote their // description and print an empty brief, one indent deeper than a layout's. const listing = [ 'models:', '- name: pc105', ' vendor: Generic', ' description: Generic 105-key PC', 'layouts:', "- layout: 'us'", " variant: ''", " brief: 'en'", ' description: English (US)', " iso639: ['eng']", " iso3166: ['USA']", "- layout: 'us'", " variant: 'intl'", " brief: 'en'", ' description: English (US, intl., with dead keys)', "- layout: 'br'", " variant: ''", " brief: 'pt'", ' description: Portuguese (Brazil)', "- layout: 'epo'", " variant: ''", " brief: 'eo'", ' description: Esperanto', "- layout: 'latam'", " variant: ''", " brief: 'es'", ' description: Spanish (Latin American)', "- layout: 'mm'", " variant: 'zawgyi'", " brief: 'my-zwg'", ' description: Burmese (Zawgyi)', 'option_groups:', "- name: 'grp'", ' description: Switching to another layout', ' allows_multiple: true', ' options:', " - name: 'grp:switch'", " brief: ''", " description: 'Right Alt (while pressed)'", ' layout-specific: false', ].join('\n') const briefs = model.layoutBriefs(listing) assertEqual(briefs['English (US)'], 'en', 'the table reads a layout brief') assertEqual(briefs['English (US, intl., with dead keys)'], 'en', 'the table reads a variant brief') assertEqual(briefs['Generic 105-key PC'], undefined, 'the table skips keyboard models') assertEqual(briefs['Switching to another layout'], undefined, 'the table skips option groups') assertEqual(briefs["'Right Alt (while pressed)'"], undefined, 'the table skips the options under a group') assertEqual(Object.keys(briefs).length, 6, 'the table holds nothing but the layouts') assertEqual(model.shortLabel('English (US)', briefs), 'EN', 'the label is the language, not the country') assertEqual(model.shortLabel('Portuguese (Brazil)', briefs), 'PT', 'a country variant keeps its language') assertEqual(model.shortLabel('Esperanto', briefs), 'EO', 'a layout without a country still gets a code') assertEqual(model.shortLabel('Spanish (Latin American)', briefs), 'ES', 'a layout spanning countries still gets a code') assertEqual(model.shortLabel('Burmese (Zawgyi)', briefs), 'MY', 'a brief carrying a script drops it') assertEqual(model.shortLabel('A user-defined custom Layout', { 'A user-defined custom Layout': 'custom' }), 'CUS', 'a brief that is a word is cut to size') // A brief pairs with the description printed under it, so a block that prints // one without the other must not hand its code to the block that follows. const orphaned = model.layoutBriefs([ 'layouts:', "- layout: 'us'", " brief: 'en'", "- layout: 'gr'", ' description: Greek', ].join('\n')) assertEqual(orphaned['Greek'], undefined, 'a brief stops at the end of its block') assertEqual(model.shortLabel('Elvish (Tengwar)', briefs), 'ELV', 'an unlisted layout falls back to its description') assertEqual(model.shortLabel('English (US)', {}), 'ENG', 'the label survives an empty table') assertEqual(model.shortLabel('constructor', {}), 'CON', 'a description naming a built-in still falls back') assertEqual(model.shortLabel('', briefs), '', 'no keymap means no label') JS