Skip to content

Commit d376d4f

Browse files
authored
Added test for focusWindow (#177)
* Added test for focusWindow * Added test for focusWindow * Refined focus test * Refined test for focusWindow
1 parent 3a02763 commit d376d4f

File tree

4 files changed

+111
-36
lines changed

4 files changed

+111
-36
lines changed

test/window-integration-tests/package-lock.json

+36-36
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
6+
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
7+
<link href="index.css" rel="stylesheet"/>
8+
<title>libnut secondary window</title>
9+
</head>
10+
<body style="width: 100%; height: 100%">
11+
<div id="content">
12+
<p>I'm just a placeholder</p>
13+
</div>
14+
<script src="renderer.js"></script>
15+
</body>
16+
</html>
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const {app, ipcMain, BrowserWindow} = require('electron')
2+
const path = require('path');
3+
const { POS_X, POS_Y, WIDTH, HEIGTH } = require("./constants");
4+
5+
function createWindow() {
6+
const mainWindow = new BrowserWindow({
7+
width: WIDTH,
8+
height: HEIGTH,
9+
alwaysOnTop: true,
10+
webPreferences: {
11+
nodeIntegration: true,
12+
preload: path.join(__dirname, 'preload.js')
13+
}
14+
});
15+
mainWindow.loadFile(path.join(__dirname, "second.html"));
16+
mainWindow.setPosition(POS_X, POS_Y);
17+
}
18+
19+
ipcMain.on("main", (event, args) => {
20+
if (args === "quit") {
21+
app.quit();
22+
}
23+
});
24+
25+
app.whenReady().then(() => {
26+
setTimeout(() => app.exit(1), 15000);
27+
createWindow()
28+
29+
app.on('activate', function () {
30+
if (BrowserWindow.getAllWindows().length === 0) createWindow()
31+
})
32+
})
33+
34+
app.on('window-all-closed', function () {
35+
console.log("Bye!");
36+
app.quit();
37+
})

test/window-integration-tests/test.js

+22
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,28 @@ describe("getActiveWindow", () => {
9595
});
9696
});
9797

98+
describe("focusWindow", () => {
99+
it("should properly focus the correct window", async () => {
100+
// GIVEN
101+
const openWindowHandle = libnut.getActiveWindow();
102+
103+
// WHEN
104+
const secondApp = await electron.launch({args: ['second.js']});
105+
const secondPage = await secondApp.firstWindow({timeout: APP_TIMEOUT});
106+
107+
const result = libnut.focusWindow(openWindowHandle);
108+
109+
// THEN
110+
const activeWindowHandle = libnut.getActiveWindow();
111+
const activeWindowName = libnut.getWindowTitle(activeWindowHandle);
112+
expect(activeWindowName).toBe(TITLE);
113+
expect(result).toBeTruthy();
114+
if (secondApp) {
115+
await secondApp.close();
116+
}
117+
});
118+
});
119+
98120
afterEach(async () => {
99121
if (app) {
100122
await app.close();

0 commit comments

Comments
 (0)