Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 28 additions & 38 deletions test/ipc.spec.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,3 @@
// Not tested:
// - keybinds
// - drag File
// - drag window
// - native close button
// - Notification
// - Sounds

// Test:
// - Main
// - 2nd instance
// - will-quit remove shortcuts
// - exit code
// - Menu
// - Dock
// - Always visible
// - Features
// - Functions
// - Create child window
// - Settings
// - IOHooks
// - Accelerators
// - moveX
// - Lock
// - duplicate
// - center
// - reset
// - changeDisplay
// - hide
// - IPC
// - reset_preferences
// - close_window
// - save_custom_image
// - get_crosshairs
// - save_crosshair
// - update_and_restart
// - quit

const { expect, test } = require( '@playwright/test' )
const { startApp, closeApp, wait, focusedMinimizedVisible, getBounds, delays, CHOOSER_WINDOW, SETTINGS_WINDOW } = require( './helpers.js' )
const { productName } = require( '../package.json' )
Expand Down Expand Up @@ -131,6 +93,34 @@ test( 'Validate open_settings + focus', async () => {

test( 'Validate set_preference + reset_preference', async () => {

// Test set_preference
await electronApp.evaluate( async app => app.ipcMain.emit( 'set_preference', { key: 'crosshair.opacity', value: 50 } ) )

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

In Electron, ipcMain.on listeners always receive the event object as the first argument, followed by the actual message payload.

In src/main/ipc.js, the set_preference listener is defined as:

ipcMain.on( 'set_preference', arg => { ... } )

Because it only accepts a single parameter, in production arg will be the event object, and the actual preference payload is lost, making the feature broken.

The test currently passes because it emits the event directly using app.ipcMain.emit( 'set_preference', { key: ... } ), which passes the payload as the first argument. To properly simulate Electron's IPC behavior and expose this bug, the test should pass a dummy event object as the first argument.

Suggested change
await electronApp.evaluate( async app => app.ipcMain.emit( 'set_preference', { key: 'crosshair.opacity', value: 50 } ) )
await electronApp.evaluate( async app => app.ipcMain.emit( 'set_preference', {}, { key: 'crosshair.opacity', value: 50 } ) )

await wait( delays.short )

const opacity = await electronApp.evaluate( async app => {

const preferences = require( './src/main/preferences.js' ).init()

return preferences.value( 'crosshair.opacity' )

} )
expect( opacity ).toBe( 50 )

// Test reset_preferences
await electronApp.evaluate( async app => app.ipcMain.emit( 'reset_preferences' ) )
await wait( delays.short )

// Verify reset
const newOpacity = await electronApp.evaluate( async app => {

const preferences = require( './src/main/preferences.js' ).init()

return preferences.value( 'crosshair.opacity' )

} )
// Assuming default opacity is 100
expect( newOpacity ).toBe( 100 )

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The default value for crosshair.opacity is defined as 80 in src/main/preferences.js (line 27), not 100. Asserting that the reset value is 100 will cause this test to fail.

Suggested change
// Assuming default opacity is 100
expect( newOpacity ).toBe( 100 )
expect( newOpacity ).toBe( 80 )


} )

test( 'Validate quit', async () => {
Expand Down