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

Add facade for consistent access to underlying native JavaScript window #187

Merged
merged 1 commit into from
Sep 12, 2018
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
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