Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added test for focusWindow #177

Merged
merged 4 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Loading