Skip to content

Commit

Permalink
Added test for focusWindow (#177)
Browse files Browse the repository at this point in the history
* Added test for focusWindow

* Added test for focusWindow

* Refined focus test

* Refined test for focusWindow
  • Loading branch information
s1hofmann authored Mar 11, 2024
1 parent 3a02763 commit d376d4f
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 36 deletions.
72 changes: 36 additions & 36 deletions test/window-integration-tests/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions test/window-integration-tests/second.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
<link href="index.css" rel="stylesheet"/>
<title>libnut secondary window</title>
</head>
<body style="width: 100%; height: 100%">
<div id="content">
<p>I'm just a placeholder</p>
</div>
<script src="renderer.js"></script>
</body>
</html>
37 changes: 37 additions & 0 deletions test/window-integration-tests/second.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const {app, ipcMain, BrowserWindow} = require('electron')
const path = require('path');
const { POS_X, POS_Y, WIDTH, HEIGTH } = require("./constants");

function createWindow() {
const mainWindow = new BrowserWindow({
width: WIDTH,
height: HEIGTH,
alwaysOnTop: true,
webPreferences: {
nodeIntegration: true,
preload: path.join(__dirname, 'preload.js')
}
});
mainWindow.loadFile(path.join(__dirname, "second.html"));
mainWindow.setPosition(POS_X, POS_Y);
}

ipcMain.on("main", (event, args) => {
if (args === "quit") {
app.quit();
}
});

app.whenReady().then(() => {
setTimeout(() => app.exit(1), 15000);
createWindow()

app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})

app.on('window-all-closed', function () {
console.log("Bye!");
app.quit();
})
22 changes: 22 additions & 0 deletions test/window-integration-tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,28 @@ describe("getActiveWindow", () => {
});
});

describe("focusWindow", () => {
it("should properly focus the correct window", async () => {
// GIVEN
const openWindowHandle = libnut.getActiveWindow();

// WHEN
const secondApp = await electron.launch({args: ['second.js']});
const secondPage = await secondApp.firstWindow({timeout: APP_TIMEOUT});

const result = libnut.focusWindow(openWindowHandle);

// THEN
const activeWindowHandle = libnut.getActiveWindow();
const activeWindowName = libnut.getWindowTitle(activeWindowHandle);
expect(activeWindowName).toBe(TITLE);
expect(result).toBeTruthy();
if (secondApp) {
await secondApp.close();
}
});
});

afterEach(async () => {
if (app) {
await app.close();
Expand Down

0 comments on commit d376d4f

Please sign in to comment.