Skip to content

A games launcher for GOG, Amazon and Epic Games for Linux, Windows and macOS.

7.0 KB
Raw Blame
import { expect, test } from '@playwright/test'
import { electronTest } from './helpers'

electronTest('Settings', async (app, page) => {
  // stub `legendary --version`
  await app.evaluate(({ ipcMain }) => {
    ipcMain.emit('setLegendaryCommandStub', [
      {
        commandParts: ['--version'],
        stdout: 'legendary version "1.2.3", codename "Some Name"'
      }
    ])
  })

  // stub `gogdl --version`
  await app.evaluate(({ ipcMain }) => {
    ipcMain.emit('setGogdlCommandStub', [
      {
        commandParts: ['--version'],
        stdout: '2.3.4'
      }
    ])
  })

  // stub `nile --version`
  await app.evaluate(({ ipcMain }) => {
    ipcMain.emit('setNileCommandStub', [
      {
        commandParts: ['--version'],
        stdout: '1.1.1 JoJo'
      }
    ])
  })

  await test.step('opens the Settings screen', async () => {
    await page
      .locator('.Sidebar')
      .locator('.Sidebar__item', { hasText: 'Settings' })
      .click()
    await expect(page.getByRole('heading', { name: 'General' })).toBeVisible()
  })

  await test.step('searches settings by name', async () => {
    const search = page.getByRole('combobox', { name: 'Search settings' })

    await search.fill('discord')
    await expect(
      page.getByRole('option', { name: /Discord rich presence/ })
    ).toBeVisible()
    await search.press('Enter')
    await expect(page.getByLabel('Enable Discord rich presence')).toBeFocused()
  })

  await test.step('shows the Runners settings', async () => {
    await page.getByRole('link', { name: 'Runners' }).click()
  })

  await test.step('shows alternative binaries inputs', async () => {
    await expect(
      page.getByLabel('Choose an Alternative Legendary Binary')
    ).toBeVisible()
    await expect(
      page.getByLabel('Choose an Alternative GOGDL Binary to use')
    ).toBeVisible()
    await expect(
      page.getByLabel('Choose an Alternative Nile Binary')
    ).toBeVisible()
  })

  await test.step('shows the binaries versions from the binaries', async () => {
    await expect(
      page.getByText('Legendary Version: 1.2.3 Some Name')
    ).toBeVisible()
    await expect(page.getByText('GOGDL Version: 2.3.4')).toBeVisible()
    await expect(page.getByText('Nile Version: 1.1.1 JoJo')).toBeVisible()
  })

  await test.step('shows the default experimental features', async () => {
    await page.getByRole('link', { name: 'Advanced' }).click()
    await expect(page.getByLabel('Help component')).not.toBeChecked()
  })
})

electronTest(
  'Settings game defaults search and sticky header',
  async (app, page) => {
    // The Game defaults page is not part of the Windows build.
    if (process.platform === 'win32') return

    const setContentSize = (width: number, height: number) =>
      app.evaluate(
        ({ BrowserWindow }, [w, h]) => {
          BrowserWindow.getAllWindows()[0].setContentSize(w, h)
        },
        [width, height]
      )

    const scrollTo = (top: number) =>
      page.evaluate((value) => {
        document.body.scrollTop = value
      }, top)

    const rect = (selector: string) =>
      page.evaluate((sel) => {
        const element = document.querySelector(sel)
        if (!element) return null
        const { top, bottom } = element.getBoundingClientRect()
        return { top, bottom }
      }, selector)

    const activeInnerTab = page.locator(
      '.settingsTabs [role="tab"][aria-selected="true"]'
    )
    const focusedCardTitle = () =>
      page.evaluate(
        () =>
          document.activeElement
            ?.closest('.SettingsCard')
            ?.querySelector('.SettingsCard__title')?.textContent ?? ''
      )

    await setContentSize(1280, 820)
    await page
      .locator('.Sidebar')
      .locator('.Sidebar__item', { hasText: 'Settings' })
      .click()
    await expect(page.getByRole('heading', { name: 'General' })).toBeVisible()
    await page.evaluate(() => localStorage.removeItem('default'))
    await page.getByRole('link', { name: 'Game defaults' }).click()
    await page.locator('#setWineVersion').waitFor()

    await test.step('keeps the sticky stack continuous at 1280px', async () => {
      await scrollTo(800)
      await expect
        .poll(() => page.evaluate(() => document.body.scrollTop))
        .toBeGreaterThan(0)

      const header = await rect('.Settings .headerTitle')
      const search = await rect('.settingsSearchRow')
      const tabs = await rect('.Settings .settingsTabs')

      expect(header).not.toBeNull()
      expect(search).not.toBeNull()
      expect(tabs).not.toBeNull()
      expect(search!.top).toBeCloseTo(header!.bottom, 0)
      expect(tabs!.top).toBeCloseTo(search!.bottom, 0)
    })

    await test.step('keeps the sticky stack continuous at 600px', async () => {
      await setContentSize(600, 820)
      await scrollTo(1000)
      await expect
        .poll(() => page.evaluate(() => document.body.scrollTop))
        .toBeGreaterThan(0)

      const header = await rect('.Settings .headerTitle')
      const sidebar = await rect('.settingsSidebar')
      const tabs = await rect('.Settings .settingsTabs')
      const tabsDirection = await page
        .locator('.Settings .settingsTabs')
        .evaluate((element) => getComputedStyle(element).flexDirection)

      expect(header).not.toBeNull()
      expect(sidebar).not.toBeNull()
      expect(tabs).not.toBeNull()
      expect(tabsDirection).toBe('row')
      expect(sidebar!.top).toBeCloseTo(header!.bottom, 0)
      expect(tabs!.top).toBeCloseTo(sidebar!.bottom, 0)
    })

    await test.step('search opens the matching Game defaults tab', async () => {
      await setContentSize(1280, 820)
      await scrollTo(0)
      const search = page.getByRole('combobox', { name: 'Search settings' })

      await search.fill('wine version')
      await page
        .getByRole('option', { name: /Wine version/ })
        .first()
        .click()
      await expect(activeInnerTab).toHaveText('Wine')
      await expect(page.locator('#setWineVersion')).toBeFocused()

      await search.fill('wrapper command')
      await page.getByRole('option', { name: /Wrapper command/ }).click()
      await expect(activeInnerTab).toHaveText('Advanced')
      await expect.poll(focusedCardTitle).toBe('Wrapper & environment')

      if (process.platform === 'linux') {
        await search.fill('esync')
        await page.getByRole('option', { name: /Esync/ }).first().click()
        await expect(activeInnerTab).toHaveText('Legacy')
        await expect(page.locator('#esyncToggle')).toBeFocused()
      }
    })

    await test.step('remembers the last tab outside the search flow', async () => {
      await page.getByRole('link', { name: 'General' }).click()
      await page.getByRole('link', { name: 'Game defaults' }).click()
      await expect(activeInnerTab).toHaveText('Wine')

      await page
        .locator('.settingsTabs [role="tab"]', { hasText: 'Advanced' })
        .click()
      await page.getByRole('link', { name: 'General' }).click()
      await page.getByRole('link', { name: 'Game defaults' }).click()
      await expect(activeInnerTab).toHaveText('Advanced')
    })
  }
)