mirror of
https://github.com/electron/electron.git
synced 2026-04-10 03:01:51 -04:00
test: add tests for 'restored-window-state' event emission
This commit is contained in:
committed by
Keeley Hammond
parent
4c1808eeea
commit
2868c69f4d
@@ -7423,6 +7423,27 @@ describe('BrowserWindow module', () => {
|
||||
}
|
||||
};
|
||||
|
||||
const createAndSaveWindowState = async (preferencesPath: string, windowName: string, options?: BrowserWindowConstructorOptions) => {
|
||||
const w = new BrowserWindow({
|
||||
name: windowName,
|
||||
windowStatePersistence: {
|
||||
displayMode: false
|
||||
},
|
||||
show: false,
|
||||
...options
|
||||
});
|
||||
if (!fs.existsSync(preferencesPath)) {
|
||||
// File doesn't exist, wait for creation
|
||||
await waitForPrefsFileCreation(preferencesPath);
|
||||
} else {
|
||||
// File exists, wait for update
|
||||
const initialModTime = getPrefsModTime(preferencesPath);
|
||||
await waitForPrefsUpdate(initialModTime, preferencesPath);
|
||||
}
|
||||
// Ensure window is destroyed because we can't create another window with the same name otherwise
|
||||
w.destroy();
|
||||
};
|
||||
|
||||
describe('save window state', () => {
|
||||
const fixturesPath = path.resolve(__dirname, 'fixtures', 'api', 'window-state-save');
|
||||
const sharedUserDataPath = path.join(os.tmpdir(), 'electron-window-state-test');
|
||||
@@ -7841,27 +7862,6 @@ describe('BrowserWindow module', () => {
|
||||
const preferencesPath = path.join(app.getPath('userData'), 'Local State');
|
||||
const windowName = 'test-restore-window';
|
||||
|
||||
const createAndSaveWindowState = async (options?: BrowserWindowConstructorOptions) => {
|
||||
const w = new BrowserWindow({
|
||||
name: windowName,
|
||||
windowStatePersistence: {
|
||||
displayMode: false
|
||||
},
|
||||
show: false,
|
||||
...options
|
||||
});
|
||||
if (!fs.existsSync(preferencesPath)) {
|
||||
// File doesn't exist, wait for creation
|
||||
await waitForPrefsFileCreation(preferencesPath);
|
||||
} else {
|
||||
// File exists, wait for update
|
||||
const initialModTime = getPrefsModTime(preferencesPath);
|
||||
await waitForPrefsUpdate(initialModTime, preferencesPath);
|
||||
}
|
||||
// Ensure window is destroyed because we can't create another window with the same name otherwise
|
||||
w.destroy();
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
// Timeout here plays nice with CI
|
||||
await setTimeout(2000);
|
||||
@@ -7878,7 +7878,7 @@ describe('BrowserWindow module', () => {
|
||||
it('should restore bounds when windowStatePersistence is true', async () => {
|
||||
const workArea = screen.getPrimaryDisplay().workArea;
|
||||
const bounds = { width: 100, height: 100, x: workArea.x, y: workArea.y };
|
||||
await createAndSaveWindowState(bounds);
|
||||
await createAndSaveWindowState(preferencesPath, windowName, bounds);
|
||||
// Should override default constructor bounds
|
||||
const w = new BrowserWindow({
|
||||
name: windowName,
|
||||
@@ -7913,7 +7913,7 @@ describe('BrowserWindow module', () => {
|
||||
});
|
||||
|
||||
it('should restore fullscreen state when windowStatePersistence is true', async () => {
|
||||
await createAndSaveWindowState({ fullscreen: true });
|
||||
await createAndSaveWindowState(preferencesPath, windowName, { fullscreen: true });
|
||||
await setTimeout(2000);
|
||||
const w = new BrowserWindow({
|
||||
name: windowName,
|
||||
@@ -7929,7 +7929,7 @@ describe('BrowserWindow module', () => {
|
||||
});
|
||||
|
||||
it('should restore kiosk state when windowStatePersistence is true', async () => {
|
||||
await createAndSaveWindowState({ kiosk: true });
|
||||
await createAndSaveWindowState(preferencesPath, windowName, { kiosk: true });
|
||||
await setTimeout(2000);
|
||||
const w = new BrowserWindow({
|
||||
name: windowName,
|
||||
@@ -7948,7 +7948,7 @@ describe('BrowserWindow module', () => {
|
||||
it('should restore maximized state when windowStatePersistence is true', async () => {
|
||||
const width = screen.getPrimaryDisplay().workArea.width;
|
||||
const height = screen.getPrimaryDisplay().workArea.height;
|
||||
await createAndSaveWindowState({ width, height });
|
||||
await createAndSaveWindowState(preferencesPath, windowName, { width, height });
|
||||
|
||||
const w = new BrowserWindow({
|
||||
name: windowName,
|
||||
@@ -7966,7 +7966,7 @@ describe('BrowserWindow module', () => {
|
||||
|
||||
it('should not restore state when windowStatePersistence is false', async () => {
|
||||
const bounds = { width: 400, height: 300, x: 100, y: 150 };
|
||||
await createAndSaveWindowState(bounds);
|
||||
await createAndSaveWindowState(preferencesPath, windowName, bounds);
|
||||
|
||||
const defaultBounds = { width: 500, height: 400, x: 200, y: 250 };
|
||||
const w = new BrowserWindow({
|
||||
@@ -8015,7 +8015,7 @@ describe('BrowserWindow module', () => {
|
||||
});
|
||||
|
||||
it('should restore display modes when bounds is disabled', async () => {
|
||||
await createAndSaveWindowState({ fullscreen: true });
|
||||
await createAndSaveWindowState(preferencesPath, windowName, { fullscreen: true });
|
||||
await setTimeout(2000);
|
||||
const w = new BrowserWindow({
|
||||
name: windowName,
|
||||
@@ -8032,7 +8032,7 @@ describe('BrowserWindow module', () => {
|
||||
});
|
||||
|
||||
it('should respect fullscreenable property', async () => {
|
||||
await createAndSaveWindowState({ fullscreen: true });
|
||||
await createAndSaveWindowState(preferencesPath, windowName, { fullscreen: true });
|
||||
|
||||
const w = new BrowserWindow({
|
||||
name: windowName,
|
||||
@@ -8050,7 +8050,7 @@ describe('BrowserWindow module', () => {
|
||||
});
|
||||
|
||||
it('should respect minWidth and minHeight properly', async () => {
|
||||
await createAndSaveWindowState({ width: 200, height: 200 });
|
||||
await createAndSaveWindowState(preferencesPath, windowName, { width: 200, height: 200 });
|
||||
|
||||
const w = new BrowserWindow({
|
||||
name: windowName,
|
||||
@@ -8066,7 +8066,7 @@ describe('BrowserWindow module', () => {
|
||||
});
|
||||
|
||||
it('should respect maxWidth and maxHeight properly', async () => {
|
||||
await createAndSaveWindowState({ width: 800, height: 800 });
|
||||
await createAndSaveWindowState(preferencesPath, windowName, { width: 800, height: 800 });
|
||||
|
||||
const w = new BrowserWindow({
|
||||
name: windowName,
|
||||
@@ -8149,7 +8149,7 @@ describe('BrowserWindow module', () => {
|
||||
y: workArea.y + workArea.height + 10
|
||||
};
|
||||
|
||||
await createAndSaveWindowState(offscreenBounds);
|
||||
await createAndSaveWindowState(preferencesPath, windowName, offscreenBounds);
|
||||
|
||||
const w = new BrowserWindow({
|
||||
name: windowName,
|
||||
@@ -8179,7 +8179,7 @@ describe('BrowserWindow module', () => {
|
||||
y: workArea.y + workArea.height - 20
|
||||
};
|
||||
|
||||
await createAndSaveWindowState(overflowBounds);
|
||||
await createAndSaveWindowState(preferencesPath, windowName, overflowBounds);
|
||||
|
||||
const w = new BrowserWindow({
|
||||
name: windowName,
|
||||
@@ -8208,7 +8208,7 @@ describe('BrowserWindow module', () => {
|
||||
y: workArea.y + workArea.height - 50
|
||||
};
|
||||
|
||||
await createAndSaveWindowState(overflowBounds);
|
||||
await createAndSaveWindowState(preferencesPath, windowName, overflowBounds);
|
||||
|
||||
const w = new BrowserWindow({
|
||||
name: windowName,
|
||||
@@ -8235,7 +8235,7 @@ describe('BrowserWindow module', () => {
|
||||
});
|
||||
|
||||
it('should respect show:false when restoring display modes', async () => {
|
||||
await createAndSaveWindowState({ fullscreen: true });
|
||||
await createAndSaveWindowState(preferencesPath, windowName, { fullscreen: true });
|
||||
|
||||
const w = new BrowserWindow({
|
||||
name: windowName,
|
||||
@@ -8822,5 +8822,73 @@ describe('BrowserWindow module', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('event emitters', () => {
|
||||
const preferencesPath = path.join(app.getPath('userData'), 'Local State');
|
||||
const windowName = 'test-restore-window';
|
||||
|
||||
it('should emit restored-window-state when windowStatePersistence is enabled and state exists', async () => {
|
||||
await createAndSaveWindowState(preferencesPath, windowName, { width: 300, height: 200 });
|
||||
|
||||
const restoredPromise = new Promise<void>((resolve) => {
|
||||
const w = new BrowserWindow({
|
||||
name: windowName,
|
||||
windowStatePersistence: true,
|
||||
show: false
|
||||
});
|
||||
|
||||
w.once('restored-window-state', () => {
|
||||
resolve();
|
||||
w.destroy();
|
||||
});
|
||||
});
|
||||
|
||||
await restoredPromise;
|
||||
});
|
||||
|
||||
it('should not emit restored-window-state when windowStatePersistence is disabled', async () => {
|
||||
await createAndSaveWindowState(preferencesPath, windowName, { width: 300, height: 200 });
|
||||
|
||||
let eventEmitted = false;
|
||||
|
||||
const w = new BrowserWindow({
|
||||
name: windowName,
|
||||
windowStatePersistence: false,
|
||||
show: false
|
||||
});
|
||||
|
||||
w.on('restored-window-state', () => {
|
||||
eventEmitted = true;
|
||||
});
|
||||
|
||||
// Wait for the event to be emitted for 5 seconds
|
||||
await setTimeout(5000);
|
||||
|
||||
expect(eventEmitted).to.equal(false);
|
||||
w.destroy();
|
||||
});
|
||||
|
||||
it('should not emit restored-window-state when no window state exists on disk', async () => {
|
||||
// Clear any existing state to ensure no state exists
|
||||
BrowserWindow.clearWindowState(windowName);
|
||||
|
||||
let eventEmitted = false;
|
||||
|
||||
const w = new BrowserWindow({
|
||||
name: windowName,
|
||||
windowStatePersistence: true,
|
||||
show: false
|
||||
});
|
||||
|
||||
w.on('restored-window-state', () => {
|
||||
eventEmitted = true;
|
||||
});
|
||||
|
||||
// Wait for the event to be emitted for 5 seconds
|
||||
await setTimeout(5000);
|
||||
|
||||
expect(eventEmitted).to.equal(false);
|
||||
w.destroy();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user