A games launcher for GOG, Amazon and Epic Games for Linux, Windows and macOS.
TypeScript amazon-primeelectronepic-games-launcherepic-games-storeepic-storegaminggoggog-galaxyheroic-games-launcherlinuxplay-gamesprotonsteamwine
/**
* @jest-environment jsdom
*/
import {
focusFirstControlIn,
focusFirstSettingsControl,
GAMEPAD_SKIP_ATTR,
getActiveSettingsScope,
getFocusableControls,
getPreferredTabList,
beginSettingsGamepadFocus,
canContinueSettingsFocusRetry,
hasSwitchableTablist,
isSettingsFocusTakeover,
isTabSwitchingBlocked,
retrySettingsFocus,
shouldEnterSettingsGamepadFocus,
moveSettingsFocus,
moveSettingsFocusHorizontal,
moveFocusSequentially,
rememberGamepadTabListForFocus,
scrollTabPanelToTop
} from '../settingsFocus'
const rects = [{ width: 10, height: 10 }] as unknown as DOMRectList
const mockRects = () => {
jest.spyOn(HTMLElement.prototype, 'getClientRects').mockReturnValue(rects)
}
const buildSettings = () => {
document.body.innerHTML = `
<div class="settingsPane">
<div role="tablist" data-gamepad-tabs="true">
<button role="tab" aria-selected="false" aria-controls="tabpanel-wine">Wine</button>
<button role="tab" aria-selected="true" aria-controls="tabpanel-advanced">Advanced</button>
</div>
<div role="tabpanel" id="tabpanel-wine" hidden>
<div class="SettingRow"><div class="SettingRow__control">
<button id="hidden-button">Hidden</button>
</div></div>
</div>
<div role="tabpanel" id="tabpanel-advanced">
<div class="SettingRow" id="row-toggle">
<div class="SettingRow__text"><span class="SettingRow__label">Toggle</span></div>
<div class="SettingRow__control">
<span class="InfoTooltip" data-gamepad-skip="true" tabindex="0">i</span>
<span class="ToggleSwitch">
<input class="ToggleSwitch__input" id="toggle" type="checkbox" />
<label class="ToggleSwitch__track"></label>
</span>
</div>
</div>
<div class="SettingRow" id="row-buttons">
<div class="SettingRow__control">
<button id="first-button">First</button>
<button id="second-button">Second</button>
</div>
</div>
<button id="disabled" disabled>Disabled</button>
</div>
</div>
`
}
describe('settingsFocus', () => {
beforeEach(() => {
mockRects()
buildSettings()
})
afterEach(() => {
jest.restoreAllMocks()
document.body.innerHTML = ''
})
it('scopes focus to the active tabpanel and skips the info icon', () => {
const scope = getActiveSettingsScope()
expect(scope?.id).toBe('tabpanel-advanced')
const controls = getFocusableControls(scope!)
const ids = controls.map((element) => element.id)
expect(ids).toContain('toggle')
expect(ids).toContain('first-button')
expect(ids).not.toContain('hidden-button')
expect(ids).not.toContain('disabled')
expect(
controls.some((element) => element.matches(`[${GAMEPAD_SKIP_ATTR}]`))
).toBe(false)
})
it('focuses the first toggle before other controls', () => {
expect(focusFirstSettingsControl()).toBe(true)
expect(document.activeElement?.id).toBe('toggle')
expect(document.querySelector('#row-toggle')?.classList).toContain(
'gamepad-focus'
)
})
it('walks controls vertically and horizontally', () => {
focusFirstControlIn(document.getElementById('tabpanel-advanced')!)
expect(document.activeElement?.id).toBe('toggle')
moveSettingsFocus(1)
expect(document.activeElement?.id).toBe('first-button')
moveSettingsFocus(1)
expect(document.activeElement?.id).toBe('second-button')
moveSettingsFocus(-1)
expect(document.activeElement?.id).toBe('first-button')
moveSettingsFocusHorizontal(1)
expect(document.activeElement?.id).toBe('second-button')
})
it('lets focus leave the panel at the first and last control', () => {
focusFirstSettingsControl()
expect(document.activeElement?.id).toBe('toggle')
expect(moveSettingsFocus(-1)).toBe(false)
expect(document.activeElement?.id).toBe('toggle')
document.getElementById('second-button')?.focus()
expect(moveSettingsFocus(1)).toBe(false)
})
it('does not swallow horizontal moves on a single-control row', () => {
document.getElementById('toggle')?.focus()
expect(moveSettingsFocusHorizontal(1)).toBe(false)
expect(moveSettingsFocusHorizontal(-1)).toBe(false)
})
it('walks the focus order out of a slider outside the settings', () => {
document.body.innerHTML = `
<div class="discountFilters">
<button id="before">Before</button>
<div class="rangeSlider">
<span class="MuiSlider-root">
<span class="MuiSlider-thumb" id="thumb" tabindex="0"></span>
</span>
</div>
<button id="after">After</button>
</div>
`
document.getElementById('thumb')?.focus()
expect(moveSettingsFocus(1)).toBe(false)
expect(moveFocusSequentially(1)).toBe(true)
expect(document.activeElement?.id).toBe('after')
expect(moveFocusSequentially(-1)).toBe(true)
expect(document.activeElement?.id).toBe('thumb')
})
it('keeps the sequential walk inside the settings pane', () => {
document.body.innerHTML = `
<button id="page-control">Page</button>
<div class="settingsPane"><button id="pane-control">Pane</button></div>
`
document.getElementById('pane-control')?.focus()
expect(moveFocusSequentially(-1)).toBe(false)
expect(document.activeElement?.id).toBe('pane-control')
})
it('does not switch the page tabs from inside a dialog', () => {
document.body.innerHTML = `
<div role="tablist" data-gamepad-tabs="true">
<button role="tab" aria-selected="true" aria-controls="tabpanel-library">Library</button>
<button role="tab" aria-controls="tabpanel-store">Store</button>
</div>
<div role="tabpanel" id="tabpanel-library"><button id="card">Card</button></div>
<div class="MuiDialog-root">
<div class="MuiDialog-container">
<div class="MuiDialog-paper Modal" role="dialog">
<button id="confirm">Uninstall</button>
</div>
</div>
</div>
`
rememberGamepadTabListForFocus(
document.querySelector<HTMLElement>('[role="tab"]')
)
document.getElementById('confirm')?.focus()
expect(getPreferredTabList()).toBeNull()
})
it('keeps switching the tabs owned by the open dialog', () => {
document.body.innerHTML = `
<div role="tablist" data-gamepad-tabs="true">
<button role="tab" aria-selected="true" aria-controls="tabpanel-page">Library</button>
<button role="tab" aria-controls="tabpanel-page-two">Store</button>
</div>
<div class="MuiDialog-root">
<div role="dialog" class="MuiDialog-paper">
<div role="tablist" id="dialog-tabs" data-gamepad-tabs="true">
<button role="tab" aria-selected="true" aria-controls="tabpanel-game">Game</button>
<button role="tab" aria-controls="tabpanel-saves">Saves</button>
</div>
<div role="tabpanel" id="tabpanel-game">
<button id="dialog-control">Control</button>
</div>
</div>
</div>
`
document.getElementById('dialog-control')?.focus()
expect(getPreferredTabList()?.id).toBe('dialog-tabs')
})
it('forgets the page tablist when another dialog takes the focus', () => {
document.body.innerHTML = `
<div role="tablist" data-gamepad-tabs="true">
<button role="tab" aria-selected="true" aria-controls="tabpanel-library">Library</button>
<button role="tab" aria-controls="tabpanel-store">Store</button>
</div>
<div class="MuiDialog-root">
<div role="dialog" class="MuiDialog-paper">
<button id="confirm">Uninstall</button>
</div>
</div>
`
rememberGamepadTabListForFocus(
document.querySelector<HTMLElement>('[role="tab"]')
)
rememberGamepadTabListForFocus(document.getElementById('confirm'))
document.querySelector('.MuiDialog-root')?.remove()
expect(getPreferredTabList()).toBeNull()
})
it('focuses the controlling tablist for the active panel', () => {
document.getElementById('first-button')?.focus()
expect(getPreferredTabList()).toBe(
document.querySelector('[role="tablist"]')
)
expect(hasSwitchableTablist()).toBe(true)
})
it('prefers the inner tablist when legacy ids collide with the sidebar', () => {
document.body.innerHTML = `
<div class="settingsSidebar">
<div role="tablist" id="sidebar-list" data-gamepad-tabs="true">
<button role="tab" aria-selected="false" aria-controls="tabpanel-advanced">Games</button>
<button role="tab" aria-selected="true" aria-controls="tabpanel-advanced">Advanced</button>
</div>
</div>
<div class="settingsPane">
<div role="tablist" id="inner-list" data-gamepad-tabs="true">
<button role="tab" aria-selected="false" aria-controls="tabpanel-wine">Wine</button>
<button role="tab" aria-selected="true" aria-controls="tabpanel-advanced">Advanced</button>
</div>
<div role="tabpanel" id="tabpanel-advanced">
<button id="inner-focus">Focus</button>
</div>
</div>
`
document.getElementById('inner-focus')?.focus()
expect(getPreferredTabList()?.id).toBe('inner-list')
})
it('is a no-op when nothing has focused a tablist yet', () => {
expect(getPreferredTabList()).toBeNull()
})
it('keeps the last focused tablist after focus leaves it', () => {
const tab = document.querySelector<HTMLElement>('[role="tab"]')!
const list = document.querySelector<HTMLElement>('[role="tablist"]')!
rememberGamepadTabListForFocus(tab)
const outside = document.createElement('button')
document.body.append(outside)
outside.focus()
expect(getPreferredTabList()).toBe(list)
list.remove()
expect(getPreferredTabList()).toBeNull()
})
it('associates a route-based settings pane with the sidebar tabs', () => {
document.body.innerHTML = `
<div class="settingsSidebar">
<div role="tablist" data-gamepad-tabs="true" id="section-tabs">
<button role="tab" id="tab-general" aria-selected="true" aria-controls="tabpanel-general">General</button>
<button role="tab" id="tab-gamepad" aria-controls="tabpanel-gamepad">Gamepad</button>
</div>
</div>
<div class="settingsPane" id="tabpanel-general" role="tabpanel" aria-labelledby="tab-general">
<div class="SettingRow"><div class="SettingRow__control">
<span class="ToggleSwitch">
<input class="ToggleSwitch__input" id="pane-toggle" type="checkbox" />
<label class="ToggleSwitch__track"></label>
</span>
</div></div>
</div>
`
document.getElementById('pane-toggle')?.focus()
expect(getPreferredTabList()?.id).toBe('section-tabs')
expect(hasSwitchableTablist()).toBe(true)
})
it('does not fall back when focus is in a non-opted tablist', () => {
document.body.innerHTML = `
<div role="tablist" data-gamepad-tabs="true" id="opted">
<button role="tab" aria-selected="true">Opted one</button>
<button role="tab">Opted two</button>
</div>
<div role="tablist" id="plain">
<button role="tab" aria-selected="true">Plain one</button>
<button role="tab">Plain two</button>
</div>
`
rememberGamepadTabListForFocus(
document.querySelector<HTMLElement>('#opted [role="tab"]')
)
document.querySelector<HTMLElement>('#plain [role="tab"]')?.focus()
expect(getPreferredTabList()).toBeNull()
})
it('blocks tab switching in console launch and overlays', () => {
expect(isTabSwitchingBlocked()).toBe(false)
document.body.classList.add('console-launching')
expect(isTabSwitchingBlocked()).toBe(true)
document.body.classList.remove('console-launching')
const overlay = document.createElement('div')
overlay.className = 'consoleLaunchOverlay'
document.body.append(overlay)
expect(isTabSwitchingBlocked()).toBe(true)
overlay.remove()
})
it('lets an empty search enter the active panel with Down', () => {
document.body.innerHTML = `
<div class="settingsPane">
<div class="settingsSearchRow"><input id="search" type="text" /></div>
<div id="panel"><button id="panel-first">First</button></div>
</div>
`
document.getElementById('search')?.focus()
expect(moveSettingsFocus(1)).toBe(true)
expect(document.activeElement?.id).toBe('panel-first')
expect(moveSettingsFocus(-1)).toBe(true)
expect(document.activeElement?.id).toBe('search')
expect(moveSettingsFocus(-1)).toBe(false)
})
it('does not switch tabs while a field has focus', () => {
document.body.innerHTML = `
<div role="tablist" data-gamepad-tabs="true">
<button role="tab" aria-selected="true" aria-controls="tabpanel-fields">Fields</button>
<button role="tab" aria-controls="tabpanel-other">Other</button>
</div>
<div role="tabpanel" id="tabpanel-fields">
<input id="field" type="text" />
</div>
`
document.getElementById('field')?.focus()
expect(getPreferredTabList()).toBeNull()
expect(hasSwitchableTablist()).toBe(false)
})
it('does not fall back to the outer tabs for a single inner tab', () => {
document.body.innerHTML = `
<div class="settingsSidebar">
<div role="tablist" data-gamepad-tabs="true">
<button role="tab" aria-selected="true" aria-controls="tabpanel-advanced">General</button>
<button role="tab" aria-controls="tabpanel-gamepad">Gamepad</button>
</div>
</div>
<div class="settingsPane">
<div role="tablist" data-gamepad-tabs="true">
<button role="tab" aria-selected="true" aria-controls="tabpanel-advanced">Only</button>
</div>
<div role="tabpanel" id="tabpanel-advanced">
<button id="single-control">Control</button>
</div>
</div>
`
document.getElementById('single-control')?.focus()
expect(getPreferredTabList()).toBeNull()
})
it('does not consider a single-tab list switchable', () => {
document.body.innerHTML = `
<div role="tablist"><button role="tab" aria-selected="true">Only</button></div>
`
expect(getPreferredTabList()).toBeNull()
expect(hasSwitchableTablist()).toBe(false)
})
it('scrolls the controlled panel back to the top', () => {
const tab = document.querySelector<HTMLElement>(
'[role="tab"][aria-controls="tabpanel-advanced"]'
)!
const panel = document.getElementById('tabpanel-advanced')!
panel.scrollTop = 240
scrollTabPanelToTop(tab)
expect(panel.scrollTop).toBe(0)
})
})
describe('settings focus retry', () => {
afterEach(() => {
jest.useRealTimers()
})
it('stops once focus succeeds', () => {
jest.useFakeTimers()
const focus = jest
.fn()
.mockReturnValueOnce(false)
.mockReturnValueOnce(false)
.mockReturnValue(true)
retrySettingsFocus(focus, () => true, { retryMs: 50 })
expect(focus).toHaveBeenCalledTimes(1)
jest.advanceTimersByTime(100)
expect(focus).toHaveBeenCalledTimes(3)
jest.advanceTimersByTime(500)
expect(focus).toHaveBeenCalledTimes(3)
})
it('stops when the stop condition becomes false', () => {
jest.useFakeTimers()
const focus = jest.fn().mockReturnValue(false)
let canContinue = true
retrySettingsFocus(focus, () => canContinue, { retryMs: 50 })
canContinue = false
jest.advanceTimersByTime(500)
expect(focus).toHaveBeenCalledTimes(1)
})
it('keeps retrying while the body or nav is still valid', () => {
expect(canContinueSettingsFocusRetry(false, document.body, true)).toBe(true)
expect(canContinueSettingsFocusRetry(false, document.body, false)).toBe(
false
)
expect(canContinueSettingsFocusRetry(true, document.body, false)).toBe(true)
document.body.innerHTML = `
<div class="settingsNav" id="nav">nav</div>
<div class="settingsSearchRow"><input id="search" /></div>
<button id="control">control</button>
`
expect(
canContinueSettingsFocusRetry(false, document.getElementById('nav'), true)
).toBe(true)
expect(
canContinueSettingsFocusRetry(
false,
document.getElementById('search'),
true
)
).toBe(false)
expect(
canContinueSettingsFocusRetry(
false,
document.getElementById('control'),
true
)
).toBe(false)
})
})
describe('settings gamepad entry after a takeover', () => {
it('lets the next entry refocus after a real controller action', () => {
expect(
shouldEnterSettingsGamepadFocus({
gamepadActive: true,
gamepadConnected: true,
bodyFocused: true,
userTookOver: true
})
).toBe(true)
})
it('stays out of the way for reconnects and config reloads', () => {
expect(
shouldEnterSettingsGamepadFocus({
gamepadActive: false,
gamepadConnected: true,
bodyFocused: true,
userTookOver: true
})
).toBe(false)
})
it('enters on mount with a connected controller and body focused', () => {
expect(
shouldEnterSettingsGamepadFocus({
gamepadActive: false,
gamepadConnected: true,
bodyFocused: true,
userTookOver: false
})
).toBe(true)
expect(
shouldEnterSettingsGamepadFocus({
gamepadActive: false,
gamepadConnected: true,
bodyFocused: false,
userTookOver: false
})
).toBe(false)
})
it('counts real keyboard input but not the synthetic gamepad keys', () => {
expect(isSettingsFocusTakeover('pointer', true)).toBe(true)
expect(isSettingsFocusTakeover('keyboard', true)).toBe(false)
expect(isSettingsFocusTakeover('keyboard', false)).toBe(true)
})
})
describe('settings gamepad entry retry', () => {
afterEach(() => {
jest.useRealTimers()
document.body.innerHTML = ''
})
const createEntry = (
overrides: Partial<Parameters<typeof beginSettingsGamepadFocus>[0]> = {}
) => {
let userTookOver = overrides.userTookOver ?? false
const focusFirst = jest.fn().mockReturnValue(false)
const clearTakeOver = jest.fn(() => {
userTookOver = false
})
const start = beginSettingsGamepadFocus({
gamepadActive: true,
gamepadConnected: true,
bodyFocused: true,
focusFirst,
clearTakeOver,
isTakenOver: () => userTookOver,
getActiveElement: () => document.body,
isGamepadActive: () => true,
retryMs: 50,
...overrides,
userTookOver
})
return {
start,
focusFirst,
clearTakeOver,
isTakenOver: () => userTookOver,
setTakenOver: (value: boolean) => {
userTookOver = value
}
}
}
it('refocuses after a retake with gamepad-active and a delayed pane', () => {
jest.useFakeTimers()
const { start, focusFirst, clearTakeOver, isTakenOver } = createEntry({
userTookOver: true
})
expect(start).not.toBeNull()
// The first attempt runs while the pane is still missing. Without the
// clear, the retry would stop on the stale takeover flag instead.
expect(focusFirst).toHaveBeenCalledTimes(1)
expect(clearTakeOver).toHaveBeenCalledTimes(1)
expect(isTakenOver()).toBe(false)
focusFirst.mockReturnValue(true)
jest.advanceTimersByTime(50)
expect(focusFirst).toHaveBeenCalledTimes(2)
// Once the focus lands, the retry stops.
jest.advanceTimersByTime(500)
expect(focusFirst).toHaveBeenCalledTimes(2)
})
it('does not start or clear the flag for a bare connected controller', () => {
const { start, focusFirst, clearTakeOver, isTakenOver } = createEntry({
gamepadActive: false,
userTookOver: true
})
expect(start).toBeNull()
expect(focusFirst).not.toHaveBeenCalled()
expect(clearTakeOver).not.toHaveBeenCalled()
expect(isTakenOver()).toBe(true)
})
it('stops the retry when the search takes focus during the entry', () => {
jest.useFakeTimers()
document.body.innerHTML = `
<div class="settingsSearchRow"><input id="search" /></div>
<div class="settingsNav" id="nav">nav</div>
`
const search = document.getElementById('search')
const { start, focusFirst } = createEntry({
getActiveElement: () => search
})
expect(start).not.toBeNull()
jest.advanceTimersByTime(500)
expect(focusFirst).not.toHaveBeenCalled()
})
it('stops the retry when the user takes over mid-entry', () => {
jest.useFakeTimers()
const { start, focusFirst, setTakenOver } = createEntry()
expect(start).not.toBeNull()
jest.advanceTimersByTime(50)
expect(focusFirst).toHaveBeenCalledTimes(2)
setTakenOver(true)
jest.advanceTimersByTime(500)
expect(focusFirst).toHaveBeenCalledTimes(2)
})
})