From c94ae93d2e90a9770eca45427f7a88ea3bbd80cc Mon Sep 17 00:00:00 2001 From: Ben Lambert Date: Mon, 10 Jul 2017 08:15:58 +0100 Subject: [PATCH 1/7] Add static getById method to window interface --- packages/api-specification/interface/window.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/api-specification/interface/window.ts b/packages/api-specification/interface/window.ts index 7775329..1fa74a1 100644 --- a/packages/api-specification/interface/window.ts +++ b/packages/api-specification/interface/window.ts @@ -283,6 +283,12 @@ declare namespace ssf { * @param window The native window to wrap. */ static wrap(window: Electron.BrowserWindow | fin.OpenFinWindow | BrowserWindow): Window; + + /** + * Get the window object with a particular id. Returns null if no window with that id exists. + * @param id The id of the window. + */ + static getById(id: string): Window; } /** From 93ceb96872f7ea718947a4cc68d59dbc540d3580 Mon Sep 17 00:00:00 2001 From: Ben Lambert Date: Mon, 10 Jul 2017 10:36:20 +0100 Subject: [PATCH 2/7] Implement static getById method in openfin --- packages/api-openfin/src/window.ts | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/packages/api-openfin/src/window.ts b/packages/api-openfin/src/window.ts index b4e43ed..eccb67a 100644 --- a/packages/api-openfin/src/window.ts +++ b/packages/api-openfin/src/window.ts @@ -242,9 +242,7 @@ class Window implements ssf.Window { fin.desktop.InterApplicationBus.unsubscribe('*' , 'ssf-child-windows', subscribeListener); const children = []; names.forEach((name) => { - const app = fin.desktop.Application.wrap(name); - const win = app.getWindow(); - const childWin = Window.wrap(win); + const childWin = Window.getById(name); children.push(childWin); }); resolve(children); @@ -282,9 +280,7 @@ class Window implements ssf.Window { resolve(null); return; } - const app = fin.desktop.Application.wrap(name); - const win = app.getWindow(); - const parentWin = Window.wrap(win); + const parentWin = Window.getById(name); resolve(parentWin); }; @@ -524,6 +520,25 @@ class Window implements ssf.Window { wrappedWin.id = win.uuid + ':' + win.name; return wrappedWin; } + + static getById(id: string) { + const idRegex = '(' + // Start group 1 + '[\w\d-]+' + // At least 1 word character, digit or dash + ')' + // End group 1 + ':' + // colon + '\\1'; // Same string that was matched in group 1 + + let app = null; + if (id.match(new RegExp(idRegex))) { + app = fin.desktop.Application.wrap(id.split(':')[0]); + } else { + // Assume just app id was passed in + app = fin.desktop.Application.wrap(id); + } + + const win = app.getWindow(); + return Window.wrap(win); + } } export default Window; From 35f7a80e3675a23f4fc5ce84b9dd2d3e68343ff0 Mon Sep 17 00:00:00 2001 From: Ben Lambert Date: Mon, 10 Jul 2017 11:10:16 +0100 Subject: [PATCH 3/7] Implement static getById method in electron --- packages/api-electron/src/preload/window.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/api-electron/src/preload/window.ts b/packages/api-electron/src/preload/window.ts index c8788f0..1a8a9bc 100644 --- a/packages/api-electron/src/preload/window.ts +++ b/packages/api-electron/src/preload/window.ts @@ -348,6 +348,13 @@ class Window implements ssf.Window { wrappedWindow.id = String(win.id); return wrappedWindow; } + + static getById(id: string) { + if (!isNaN(parseInt(id))) { + return Window.wrap(BrowserWindow.fromId(parseInt(id))); + } + return null; + } } export default Window; From b9b088b3c35cfcae15c11f24b871b0dc2a1a17c8 Mon Sep 17 00:00:00 2001 From: Ben Lambert Date: Mon, 10 Jul 2017 14:44:23 +0100 Subject: [PATCH 4/7] Change static method to be only on window objects --- .../api-specification/interface/window.ts | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/api-specification/interface/window.ts b/packages/api-specification/interface/window.ts index 1fa74a1..42cafca 100644 --- a/packages/api-specification/interface/window.ts +++ b/packages/api-specification/interface/window.ts @@ -277,18 +277,6 @@ declare namespace ssf { * @returns The window. */ static getCurrentWindow(callback?: () => void, errorCallback?: () => void): Window; - - /** - * Wraps a native container window with a ContainerJS window. - * @param window The native window to wrap. - */ - static wrap(window: Electron.BrowserWindow | fin.OpenFinWindow | BrowserWindow): Window; - - /** - * Get the window object with a particular id. Returns null if no window with that id exists. - * @param id The id of the window. - */ - static getById(id: string): Window; } /** @@ -467,5 +455,17 @@ declare namespace ssf { * @returns A promise that resolves to nothing when the window has unmaximized. */ unmaximize(): Promise; + + /** + * Wraps a native container window with a ContainerJS window. + * @param window The native window to wrap. + */ + static wrap(window: Electron.BrowserWindow | fin.OpenFinWindow | BrowserWindow): Window; + + /** + * Get the window object with a particular id. Returns null if no window with that id exists. + * @param id The id of the window. + */ + static getById(id: string): Window; } } From 2b965b4d87fb17f75d5af3cb42dc4730d37abe7c Mon Sep 17 00:00:00 2001 From: Ben Lambert Date: Tue, 11 Jul 2017 10:12:46 +0100 Subject: [PATCH 5/7] Add tests for getById method --- packages/api-electron/src/preload/window.ts | 12 +++-- packages/api-openfin/src/window.ts | 35 +++++++++++-- .../api-specification/interface/window.ts | 14 ++--- packages/api-tests/test/window.spec.js | 52 +++++++++++++++++++ 4 files changed, 98 insertions(+), 15 deletions(-) diff --git a/packages/api-electron/src/preload/window.ts b/packages/api-electron/src/preload/window.ts index 1a8a9bc..51a6f80 100644 --- a/packages/api-electron/src/preload/window.ts +++ b/packages/api-electron/src/preload/window.ts @@ -350,10 +350,14 @@ class Window implements ssf.Window { } static getById(id: string) { - if (!isNaN(parseInt(id))) { - return Window.wrap(BrowserWindow.fromId(parseInt(id))); - } - return null; + return new Promise(resolve => { + if (!isNaN(parseInt(id))) { + const bw = BrowserWindow.fromId(parseInt(id)) + resolve(bw === null ? null : Window.wrap(bw)); + return; + } + resolve(null); + }); } } diff --git a/packages/api-openfin/src/window.ts b/packages/api-openfin/src/window.ts index eccb67a..6be5727 100644 --- a/packages/api-openfin/src/window.ts +++ b/packages/api-openfin/src/window.ts @@ -522,6 +522,14 @@ class Window implements ssf.Window { } static getById(id: string) { + const appExists = (uuid): Promise => { + return new Promise(resolve => { + fin.desktop.System.getAllApplications((apps) => { + resolve(apps.filter((app) => app.uuid === uuid).length === 1); + }); + }); + }; + const idRegex = '(' + // Start group 1 '[\w\d-]+' + // At least 1 word character, digit or dash ')' + // End group 1 @@ -529,15 +537,34 @@ class Window implements ssf.Window { '\\1'; // Same string that was matched in group 1 let app = null; + let existsPromise: Promise; if (id.match(new RegExp(idRegex))) { - app = fin.desktop.Application.wrap(id.split(':')[0]); + existsPromise = appExists(id.split(':')[0]).then((exists) => { + if (!exists) { + app = null; + } else { + app = fin.desktop.Application.wrap(id.split(':')[0]); + } + }); } else { // Assume just app id was passed in - app = fin.desktop.Application.wrap(id); + existsPromise = appExists(id).then((exists) => { + if (!exists) { + app = null; + } else { + app = fin.desktop.Application.wrap(id); + } + }); } - const win = app.getWindow(); - return Window.wrap(win); + return existsPromise.then(() => { + if (app) { + const win = app.getWindow(); + return Window.wrap(win); + } + + return null; + }) } } diff --git a/packages/api-specification/interface/window.ts b/packages/api-specification/interface/window.ts index 42cafca..01355d8 100644 --- a/packages/api-specification/interface/window.ts +++ b/packages/api-specification/interface/window.ts @@ -277,6 +277,12 @@ declare namespace ssf { * @returns The window. */ static getCurrentWindow(callback?: () => void, errorCallback?: () => void): Window; + + /** + * Wraps a native container window with a ContainerJS window. + * @param window The native window to wrap. + */ + static wrap(window: Electron.BrowserWindow | fin.OpenFinWindow | BrowserWindow): Window; } /** @@ -456,16 +462,10 @@ declare namespace ssf { */ unmaximize(): Promise; - /** - * Wraps a native container window with a ContainerJS window. - * @param window The native window to wrap. - */ - static wrap(window: Electron.BrowserWindow | fin.OpenFinWindow | BrowserWindow): Window; - /** * Get the window object with a particular id. Returns null if no window with that id exists. * @param id The id of the window. */ - static getById(id: string): Window; + static getById(id: string): Promise; } } diff --git a/packages/api-tests/test/window.spec.js b/packages/api-tests/test/window.spec.js index 580e476..b979241 100644 --- a/packages/api-tests/test/window.spec.js +++ b/packages/api-tests/test/window.spec.js @@ -109,6 +109,58 @@ if (process.env.MOCHA_CONTAINER !== 'browser') { return chainPromises(steps); }); + it('Should get a window by its id #ssf.Window.getById', function() { + const windowTitle = 'windownamegetbyid'; + const windowOptions = getWindowOptions({ + name: windowTitle + }); + + /* eslint-disable no-undef */ + const idScript = (callback) => { + ssf.app.ready().then(() => { + const id = window.newWin.getId(); + const win = ssf.Window.getById(id); + callback(win !== null); + }); + }; + /* eslint-enable no-undef */ + + const steps = [ + ...setupWindowSteps(windowOptions), + () => selectWindow(app.client, 0), + () => executeAsyncJavascript(app.client, idScript), + (result) => assert.equal(result.value, true) + ]; + + return chainPromises(steps); + }); + + it('Should return null if no window with id exists #ssf.Window.getById', function() { + const windowTitle = 'windownamegetbyidwrong'; + const windowOptions = getWindowOptions({ + name: windowTitle + }); + + /* eslint-disable no-undef */ + const idScript = (callback) => { + ssf.app.ready().then(() => { + ssf.Window.getById('thisiswrong').then(win => { + callback(win === null); + }); + }); + }; + /* eslint-enable no-undef */ + + const steps = [ + ...setupWindowSteps(windowOptions), + () => selectWindow(app.client, 0), + () => executeAsyncJavascript(app.client, idScript), + (result) => assert.equal(result.value, true) + ]; + + return chainPromises(steps); + }); + it('Should return the maximum width #ssf.Window.getMaximumSize', function() { const windowTitle = 'windownamemaxwidth'; const maxWidth = 500; From cf1b3e09b401d98193c2eb04c5d84f79c2879808 Mon Sep 17 00:00:00 2001 From: Ben Lambert Date: Wed, 12 Jul 2017 13:34:23 +0100 Subject: [PATCH 6/7] Refactor exist check --- packages/api-openfin/src/window.ts | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/packages/api-openfin/src/window.ts b/packages/api-openfin/src/window.ts index 6be5727..3db4cdf 100644 --- a/packages/api-openfin/src/window.ts +++ b/packages/api-openfin/src/window.ts @@ -538,26 +538,10 @@ class Window implements ssf.Window { let app = null; let existsPromise: Promise; - if (id.match(new RegExp(idRegex))) { - existsPromise = appExists(id.split(':')[0]).then((exists) => { - if (!exists) { - app = null; - } else { - app = fin.desktop.Application.wrap(id.split(':')[0]); - } - }); - } else { - // Assume just app id was passed in - existsPromise = appExists(id).then((exists) => { - if (!exists) { - app = null; - } else { - app = fin.desktop.Application.wrap(id); - } - }); - } - - return existsPromise.then(() => { + const uuid = id.match(new RegExp(idRegex)) ? id.split(':')[0] : id; + return appExists(uuid).then((exists) => { + app = exists ? fin.desktop.Application.wrap(uuid) : null; + }).then(() => { if (app) { const win = app.getWindow(); return Window.wrap(win); From 983e6aa5b67066779c201fcde0624577950ace51 Mon Sep 17 00:00:00 2001 From: Ben Lambert Date: Wed, 12 Jul 2017 13:34:53 +0100 Subject: [PATCH 7/7] Remove only from tests --- packages/api-tests/test/window.core.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/api-tests/test/window.core.spec.js b/packages/api-tests/test/window.core.spec.js index c769972..3074695 100644 --- a/packages/api-tests/test/window.core.spec.js +++ b/packages/api-tests/test/window.core.spec.js @@ -616,7 +616,7 @@ describe('WindowCore API', function(done) { return chainPromises(steps); }); - it.only('Should wrap a native window in a containerjs window object #ssf.Window.wrap', function() { + it('Should wrap a native window in a containerjs window object #ssf.Window.wrap', function() { const windowTitle = 'windownamewrap'; const windowOptions = getWindowOptions({ name: windowTitle