Skip to content

Commit

Permalink
Add facade for consistent access to underlying native JavaScript wind…
Browse files Browse the repository at this point in the history
…ow (#187)
  • Loading branch information
bingenito authored Sep 12, 2018
1 parent 4bcb513 commit 5117608
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/Default/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ export class DefaultContainerWindow extends ContainerWindow {
protected detachListener(eventName: string, listener: (...args: any[]) => void): void {
this.innerWindow.removeEventListener(windowEventMap[eventName] || eventName, listener);
}

public get nativeWindow(): Window {
return this.innerWindow;
}
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/Electron/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ export class ElectronContainerWindow extends ContainerWindow {
protected detachListener(eventName: string, listener: (...args: any[]) => void): void {
this.innerWindow.removeListener(windowEventMap[eventName] || eventName, listener);
}

public get nativeWindow(): Window {
// For Electron we can only return in and for current renderer
return this.window || window;
}
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/OpenFin/openfin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ export class OpenFinContainerWindow extends ContainerWindow {
protected detachListener(eventName: string, listener: (...args: any[]) => void): any {
this.innerWindow.removeEventListener(windowEventMap[eventName] || eventName, listener);
}

public get nativeWindow(): Window {
return this.innerWindow.getNativeWindow();
}
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ export abstract class ContainerWindow extends EventEmitter {

public abstract getOptions(): Promise<any>;

/** Gets the underlying native JavaScript window object. As some containers
* do not support native access, check for undefined.
*/
public get nativeWindow(): Window {
return undefined;
}

/**
* Override to provide custom container logic for adding an event handler.
*/
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/Default/default.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ describe("DefaultContainerWindow", () => {
});
});
});

it("nativeWindow returns wrapped window", () => {
const innerWindow = {};
const nativeWindow = new DefaultContainerWindow(innerWindow).nativeWindow;
expect(nativeWindow).toBeDefined();
expect(nativeWindow).toEqual(<any>innerWindow);
});
});

describe("DefaultContainer", () => {
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/Electron/electron.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@ describe("ElectronContainerWindow", () => {
win.removeListener("move", () => {});
expect(win.innerWindow.removeListener).toHaveBeenCalledWith("move", jasmine.any(Function));
});

it("nativeWindow returns window", () => {
var window = {};
const win = new ElectronContainerWindow(innerWin, container, <any>window);
expect(win.nativeWindow).toEqual(<any>window);
});
});

describe("window grouping", () => {
Expand Down
11 changes: 10 additions & 1 deletion tests/unit/OpenFin/openfin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class MockInterApplicationBus {

class MockWindow {
static singleton: MockWindow = new MockWindow("Singleton");
public nativeWindow: Window = jasmine.createSpyObj("window", ["location"]);

constructor(name?: string) {
this.name = name;
Expand All @@ -68,7 +69,7 @@ class MockWindow {

getParentWindow(): any { return MockWindow.singleton; }

getNativeWindow(): any { return jasmine.createSpyObj("window", ["location"]); }
getNativeWindow(): any { return this.nativeWindow; }

focus(callback: () => void, error: (reason) => void): any {
callback();
Expand Down Expand Up @@ -175,6 +176,14 @@ describe("OpenFinContainerWindow", () => {
expect(win.name).toEqual("NAME");
});

it ("nativeWindow invokes underlying getNativeWindow", () => {
spyOn(innerWin, "getNativeWindow").and.callThrough();
const nativeWin = win.nativeWindow;
expect(innerWin.getNativeWindow).toHaveBeenCalled();
expect(nativeWin).toBeDefined();
expect(nativeWin).toEqual(innerWin.nativeWindow);
});

it("focus", (done) => {
spyOn(innerWin, "focus").and.callThrough();
win.focus().then(() => {
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/window.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ class MockWindow extends ContainerWindow {
public restore(): Promise<void> { return Promise.resolve(); }
}

describe ("ContainerWindow", () => {
it("nativeWindow returns undefined", () => {
expect(new MockWindow(undefined).nativeWindow).toBeUndefined();
});
});

describe ("static events", () => {
let container: TestContainer;

Expand Down

0 comments on commit 5117608

Please sign in to comment.