2024-10-02 19:10:44 -07:00
|
|
|
import { BaseWindow, View } from 'electron/main';
|
|
|
|
|
|
2024-05-09 15:47:47 +02:00
|
|
|
import { expect } from 'chai';
|
2024-10-02 19:10:44 -07:00
|
|
|
|
2023-01-25 22:01:25 +01:00
|
|
|
import { closeWindow } from './lib/window-helpers';
|
2019-08-27 00:37:30 -07:00
|
|
|
|
|
|
|
|
describe('View', () => {
|
2020-06-29 16:06:20 +09:00
|
|
|
let w: BaseWindow;
|
2019-08-27 00:37:30 -07:00
|
|
|
afterEach(async () => {
|
2020-03-20 13:28:31 -07:00
|
|
|
await closeWindow(w as any);
|
2020-06-29 16:06:20 +09:00
|
|
|
w = null as unknown as BaseWindow;
|
2020-03-20 13:28:31 -07:00
|
|
|
});
|
2019-08-27 00:37:30 -07:00
|
|
|
|
|
|
|
|
it('can be used as content view', () => {
|
2020-06-29 16:06:20 +09:00
|
|
|
w = new BaseWindow({ show: false });
|
2024-05-09 15:47:47 +02:00
|
|
|
const v = new View();
|
|
|
|
|
w.setContentView(v);
|
|
|
|
|
expect(w.contentView).to.equal(v);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('will throw when added as a child to itself', () => {
|
|
|
|
|
w = new BaseWindow({ show: false });
|
|
|
|
|
expect(() => {
|
|
|
|
|
w.contentView.addChildView(w.contentView);
|
|
|
|
|
}).to.throw('A view cannot be added as its own child');
|
2020-03-20 13:28:31 -07:00
|
|
|
});
|
2024-05-10 10:16:33 +02:00
|
|
|
|
|
|
|
|
it('does not crash when attempting to add a child multiple times', () => {
|
|
|
|
|
w = new BaseWindow({ show: false });
|
|
|
|
|
const cv = new View();
|
|
|
|
|
w.setContentView(cv);
|
|
|
|
|
|
|
|
|
|
const v = new View();
|
|
|
|
|
w.contentView.addChildView(v);
|
|
|
|
|
w.contentView.addChildView(v);
|
|
|
|
|
w.contentView.addChildView(v);
|
|
|
|
|
|
|
|
|
|
expect(w.contentView.children).to.have.lengthOf(1);
|
|
|
|
|
});
|
|
|
|
|
|
2024-09-12 10:01:05 +02:00
|
|
|
it('can be added as a child of another View', async () => {
|
|
|
|
|
const w = new BaseWindow();
|
|
|
|
|
const v1 = new View();
|
|
|
|
|
const v2 = new View();
|
|
|
|
|
|
|
|
|
|
v1.addChildView(v2);
|
|
|
|
|
w.contentView.addChildView(v1);
|
|
|
|
|
|
|
|
|
|
expect(w.contentView.children).to.deep.equal([v1]);
|
|
|
|
|
expect(v1.children).to.deep.equal([v2]);
|
|
|
|
|
});
|
|
|
|
|
|
2024-05-10 10:16:33 +02:00
|
|
|
it('correctly reorders children', () => {
|
|
|
|
|
w = new BaseWindow({ show: false });
|
|
|
|
|
const cv = new View();
|
|
|
|
|
w.setContentView(cv);
|
|
|
|
|
|
|
|
|
|
const v1 = new View();
|
|
|
|
|
const v2 = new View();
|
|
|
|
|
const v3 = new View();
|
|
|
|
|
w.contentView.addChildView(v1);
|
|
|
|
|
w.contentView.addChildView(v2);
|
|
|
|
|
w.contentView.addChildView(v3);
|
|
|
|
|
|
|
|
|
|
expect(w.contentView.children).to.deep.equal([v1, v2, v3]);
|
|
|
|
|
|
|
|
|
|
w.contentView.addChildView(v1);
|
|
|
|
|
w.contentView.addChildView(v2);
|
|
|
|
|
expect(w.contentView.children).to.deep.equal([v3, v1, v2]);
|
|
|
|
|
});
|
2024-07-16 20:16:25 -04:00
|
|
|
|
|
|
|
|
it('allows setting various border radius values', () => {
|
|
|
|
|
w = new BaseWindow({ show: false });
|
|
|
|
|
const v = new View();
|
|
|
|
|
w.setContentView(v);
|
|
|
|
|
v.setBorderRadius(10);
|
|
|
|
|
v.setBorderRadius(0);
|
|
|
|
|
v.setBorderRadius(-10);
|
|
|
|
|
v.setBorderRadius(9999999);
|
|
|
|
|
v.setBorderRadius(-9999999);
|
|
|
|
|
});
|
2025-01-31 13:27:16 -05:00
|
|
|
|
|
|
|
|
describe('view.getVisible|setVisible', () => {
|
|
|
|
|
it('is visible by default', () => {
|
|
|
|
|
const v = new View();
|
|
|
|
|
expect(v.getVisible()).to.be.true();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('can be set to not visible', () => {
|
|
|
|
|
const v = new View();
|
|
|
|
|
v.setVisible(false);
|
|
|
|
|
expect(v.getVisible()).to.be.false();
|
|
|
|
|
});
|
|
|
|
|
});
|
2025-11-14 12:12:22 -05:00
|
|
|
|
|
|
|
|
describe('view.getBounds|setBounds', () => {
|
|
|
|
|
it('defaults to 0,0,0,0', () => {
|
|
|
|
|
const v = new View();
|
|
|
|
|
expect(v.getBounds()).to.deep.equal({ x: 0, y: 0, width: 0, height: 0 });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('can be set and retrieved', () => {
|
|
|
|
|
const v = new View();
|
|
|
|
|
v.setBounds({ x: 10, y: 20, width: 300, height: 400 });
|
|
|
|
|
expect(v.getBounds()).to.deep.equal({ x: 10, y: 20, width: 300, height: 400 });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('emits bounds-changed when bounds mutate', () => {
|
|
|
|
|
const v = new View();
|
|
|
|
|
let called = 0;
|
|
|
|
|
v.once('bounds-changed', () => { called++; });
|
|
|
|
|
v.setBounds({ x: 5, y: 6, width: 7, height: 8 });
|
|
|
|
|
expect(called).to.equal(1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('allows zero-size bounds', () => {
|
|
|
|
|
const v = new View();
|
|
|
|
|
v.setBounds({ x: 1, y: 2, width: 0, height: 0 });
|
|
|
|
|
expect(v.getBounds()).to.deep.equal({ x: 1, y: 2, width: 0, height: 0 });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('allows negative coordinates', () => {
|
|
|
|
|
const v = new View();
|
|
|
|
|
v.setBounds({ x: -10, y: -20, width: 100, height: 50 });
|
|
|
|
|
expect(v.getBounds()).to.deep.equal({ x: -10, y: -20, width: 100, height: 50 });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('child bounds remain relative after parent moves', () => {
|
|
|
|
|
const parent = new View();
|
|
|
|
|
const child = new View();
|
|
|
|
|
parent.addChildView(child);
|
|
|
|
|
child.setBounds({ x: 10, y: 15, width: 25, height: 30 });
|
|
|
|
|
parent.setBounds({ x: 50, y: 60, width: 500, height: 600 });
|
|
|
|
|
expect(child.getBounds()).to.deep.equal({ x: 10, y: 15, width: 25, height: 30 });
|
|
|
|
|
});
|
|
|
|
|
});
|
2020-03-20 13:28:31 -07:00
|
|
|
});
|