Skip to content

Commit

Permalink
Added get/setParent to ContainerWindow (#308)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sly1024 authored May 7, 2020
1 parent d207f57 commit 2588729
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 2 deletions.
11 changes: 11 additions & 0 deletions packages/desktopjs-electron/src/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,17 @@ export class ElectronContainerWindow extends ContainerWindow {
return Promise.resolve();
}

public getParent(): Promise<ContainerWindow> {
return Promise.resolve(this.innerWindow.getParentWindow());
}

public setParent(parent: ContainerWindow): Promise<void> {
return new Promise<void>((resolve, reject) => {
this.innerWindow.setParentWindow(parent.innerWindow);
resolve();
});
}

public getBounds(): Promise<Rectangle> {
return new Promise<Rectangle>(resolve => {
const { x, y, width, height } = this.innerWindow.getBounds();
Expand Down
18 changes: 18 additions & 0 deletions packages/desktopjs-electron/tests/electron.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class MockWindow extends MockEventEmitter {
}

public getParentWindow(): any { return undefined; }
public setParentWindow(parent: any) { }

public getAllWindows(): any { return [new MockWindow(), new MockWindow("target")]; }

Expand Down Expand Up @@ -283,6 +284,23 @@ describe("ElectronContainerWindow", () => {
});
});

it("getParent calls underlying getParentWindow", (done) => {
spyOn(win.innerWindow, "getParentWindow");
win.getParent().then(() => {
expect(win.innerWindow.getParentWindow).toHaveBeenCalled();
done();
});
});

it("setParent calls underlying setParentWindow", (done) => {
const mockParent = { innerWindow: new MockWindow() };
spyOn(win.innerWindow, "setParentWindow");
win.setParent(<any>mockParent).then(() => {
expect(win.innerWindow.setParentWindow).toHaveBeenCalledWith(mockParent.innerWindow);
done();
});
});

it ("getOptions sends synchronous ipc message", (done) => {
spyOn(container.internalIpc, "sendSync").and.returnValue({ foo: "bar"});
spyOnProperty(win, "id", "get").and.returnValue(5);
Expand Down
10 changes: 10 additions & 0 deletions packages/desktopjs-openfin/src/openfin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ export class OpenFinContainerWindow extends ContainerWindow {
});
}

public getParent(): Promise<ContainerWindow> {
return Promise.resolve(null);
}

public setParent(parent: ContainerWindow): Promise<void> {
return new Promise<void>((resolve, reject) => {
resolve();
});
}

public setBounds(bounds: Rectangle): Promise<void> {
return new Promise<void>((resolve, reject) => {
this.innerWindow.setBounds(bounds.x, bounds.y, bounds.width, bounds.height, resolve, reject);
Expand Down
8 changes: 8 additions & 0 deletions packages/desktopjs-openfin/tests/openfin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,14 @@ describe("OpenFinContainerWindow", () => {
});
});

it("getParent does not throw", (done) => {
expect(() => win.getParent().then(done)).not.toThrow();
});

it("setParent does not throw", (done) => {
expect(() => win.setParent(null).then(done)).not.toThrow();
});

describe("getOptions", () => {
it("getOptions invokes underlying getOptions and returns undefined customData", (done) => {
spyOn(win.innerWindow, "getOptions").and.callFake(callback => callback({ }));
Expand Down
9 changes: 9 additions & 0 deletions packages/desktopjs/src/Default/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ export namespace Default {
return Promise.reject("Not supported");
}

public getParent(): Promise<ContainerWindow> {
// on the web, there's no parent
return Promise.resolve(null);
}

public setParent(parent: ContainerWindow): Promise<void> {
return Promise.resolve();
}

public getBounds(): Promise<Rectangle> {
return new Promise<Rectangle>(resolve => {
resolve(new Rectangle(this.innerWindow.screenX, this.innerWindow.screenY, this.innerWindow.outerWidth, this.innerWindow.outerHeight));
Expand Down
6 changes: 6 additions & 0 deletions packages/desktopjs/src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ export abstract class ContainerWindow extends EventEmitter {

public abstract flash(enable: boolean, options?: any): Promise<void>;

/** Returns the parent window of this window. */
public abstract getParent(): Promise<ContainerWindow>;

/** Sets the parent window of this window. */
public abstract setParent(parent: ContainerWindow): Promise<void>;

/** Set the current bounds of the window.
* @param {Rectangle} bounds
*/
Expand Down
12 changes: 10 additions & 2 deletions packages/desktopjs/tests/unit/Default/default.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe("DefaultContainerWindow", () => {
expect(state).toEqual(mockState);
}).then(done);
});
});
});

xdescribe("setState", () => {
it("setState undefined", (done) => {
Expand Down Expand Up @@ -184,13 +184,21 @@ describe("DefaultContainerWindow", () => {
});
});

it("getParent throws no errors", (done) => {
expect(() => win.getParent().then(done)).not.toThrow();
});

it("setParent throws no errors", (done) => {
expect(() => win.setParent(null).then(done)).not.toThrow();
});

it("getOptions", async (done) => {
const win = await new Default.DefaultContainer(<any>new MockWindow()).createWindow("url", { a: "foo" });
win.getOptions().then(options => {
expect(options).toBeDefined();
expect(options).toEqual({ a: "foo"});
}).then(done);
});
});

describe("addListener", () => {
it("addListener calls underlying window addEventListener with mapped event name", () => {
Expand Down

0 comments on commit 2588729

Please sign in to comment.