diff --git a/.gitignore b/.gitignore index a5749f1..a9d32d6 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ Thumbs.db Desktop.ini # Mac crap .DS_Store +dist diff --git a/projects/ngx-electron/src/lib/electron.service.ts b/projects/ngx-electron/src/lib/electron.service.ts index 6355472..d9c7ec9 100644 --- a/projects/ngx-electron/src/lib/electron.service.ts +++ b/projects/ngx-electron/src/lib/electron.service.ts @@ -24,6 +24,26 @@ export class ElectronService { return !!window.navigator.userAgent.match(/Electron/); } + public get isMacOS(): boolean { + return this.isElectronApp && process.platform === 'darwin'; + } + + public get isWindows(): boolean { + return this.isElectronApp && process.platform === 'win32'; + } + + public get isLinux(): boolean { + return this.isElectronApp && process.platform === 'linux'; + } + + public get isX86(): boolean { + return this.isElectronApp && process.arch === 'ia32'; + } + + public get isX64(): boolean { + return this.isElectronApp && process.arch === 'x64'; + } + public get desktopCapturer(): Electron.DesktopCapturer { return this.electron ? this.electron.desktopCapturer : null; } diff --git a/projects/ngx-electron/src/public_api.ts b/projects/ngx-electron/src/public_api.ts index 70eeb50..b4d863c 100644 --- a/projects/ngx-electron/src/public_api.ts +++ b/projects/ngx-electron/src/public_api.ts @@ -1,9 +1,9 @@ import { NgModule } from '@angular/core'; -import { ElectronService } from "./lib/electron.service"; -import { ElectronServiceRef } from "./lib/electron.service.ref"; +import { ElectronService } from './lib/electron.service'; +import { ElectronServiceRef } from './lib/electron.service.ref'; -export * from "./lib/electron.service"; -export * from "./lib/electron.service.ref"; +export * from './lib/electron.service'; +export * from './lib/electron.service.ref'; @NgModule({ declarations: [], diff --git a/projects/ngx-electron/tests/electron.service.spec.ts b/projects/ngx-electron/tests/electron.service.spec.ts index adc5a40..da0d961 100644 --- a/projects/ngx-electron/tests/electron.service.spec.ts +++ b/projects/ngx-electron/tests/electron.service.spec.ts @@ -1,6 +1,6 @@ -import {Injector} from '@angular/core'; -import {getTestBed, TestBed} from '@angular/core/testing'; -import {} from 'jasmine'; +import { Injector } from '@angular/core'; +import { getTestBed, TestBed } from '@angular/core/testing'; +import { } from 'jasmine'; import { ElectronService } from '../src/lib/electron.service'; import { NgxElectronModule } from '../src/public_api'; import { ElectronServiceRef } from '../src/lib/electron.service.ref'; @@ -65,6 +65,234 @@ describe('ElectronService', () => { }); }); + describe('isWindows', () => { + + it('should provide isElectronApp as an instance-property', () => { + expect(_electronService).hasOwnProperty('isWindows'); + }); + + it('should return a boolean', () => { + expect(typeof _electronService.isWindows).toEqual('boolean'); + }); + + + it('should return false if isElectronApp is false', () => { + expect(_electronService.isWindows).toBeFalsy(); + }); + + it('should return true if running in electron', () => { + let originalPlatform = process.platform; + let originalUserAgent = navigator.userAgent; + (navigator).__defineGetter__('userAgent', (): string => { + return 'foo Electron'; + }); + (process).__defineGetter__('platform', (): string => { + return 'win32'; + }); + + expect(_electronService.isWindows).toBeTruthy(); + + (process).__defineGetter__('platform', (): string => { + return originalPlatform; + }); + + (navigator).__defineGetter__('userAgent', (): string => { + return originalUserAgent; + }); + }); + }); + + describe('isLinux', () => { + + it('should provide isLinux as an instance-property', () => { + expect(_electronService).hasOwnProperty('isLinux'); + }); + + it('should return a boolean', () => { + expect(typeof _electronService.isLinux).toEqual('boolean'); + }); + + + it('should return false if isElectronApp is false', () => { + expect(_electronService.isLinux).toBeFalsy(); + }); + + it('should return true if running in electron', () => { + let originalPlatform = process.platform; + let originalUserAgent = navigator.userAgent; + (navigator).__defineGetter__('userAgent', (): string => { + return 'foo Electron'; + }); + (process).__defineGetter__('platform', (): string => { + return 'linux'; + }); + + expect(_electronService.isLinux).toBeTruthy(); + + (process).__defineGetter__('platform', (): string => { + return originalPlatform; + }); + + (navigator).__defineGetter__('userAgent', (): string => { + return originalUserAgent; + }); + }); + }); + + describe('isMacOS', () => { + + it('should provide isMacOS as an instance-property', () => { + expect(_electronService).hasOwnProperty('isMacOS'); + }); + + it('should return a boolean', () => { + expect(typeof _electronService.isMacOS).toEqual('boolean'); + }); + + + it('should return false if isElectronApp is false', () => { + expect(_electronService.isMacOS).toBeFalsy(); + }); + + it('should return true if running in electron', () => { + let originalPlatform = process.platform; + let originalUserAgent = navigator.userAgent; + (navigator).__defineGetter__('userAgent', (): string => { + return 'foo Electron'; + }); + (process).__defineGetter__('platform', (): string => { + return 'darwin'; + }); + + expect(_electronService.isMacOS).toBeTruthy(); + + (process).__defineGetter__('platform', (): string => { + return originalPlatform; + }); + + (navigator).__defineGetter__('userAgent', (): string => { + return originalUserAgent; + }); + }); + }); + + describe('isX86', () => { + + it('should provide isX86 as an instance-property', () => { + expect(_electronService).hasOwnProperty('isX86'); + }); + + it('should return a boolean', () => { + expect(typeof _electronService.isX86).toEqual('boolean'); + }); + + + it('should return false if isElectronApp is false', () => { + expect(_electronService.isX86).toBeFalsy(); + }); + + it('should return true if running in electron with 32bit arch', () => { + let originalArch = process.arch; + let originalUserAgent = navigator.userAgent; + (navigator).__defineGetter__('userAgent', (): string => { + return 'foo Electron'; + }); + (process).__defineGetter__('arch', (): string => { + return 'ia32'; + }); + + expect(_electronService.isX86).toBeTruthy(); + + (process).__defineGetter__('arch', (): string => { + return originalArch; + }); + + (navigator).__defineGetter__('userAgent', (): string => { + return originalUserAgent; + }); + }); + + it('should return false if running in electron with x64 arch', () => { + let originalArch = process.arch; + let originalUserAgent = navigator.userAgent; + (navigator).__defineGetter__('userAgent', (): string => { + return 'foo Electron'; + }); + (process).__defineGetter__('arch', (): string => { + return 'x64'; + }); + + expect(_electronService.isX86).toBeFalsy(); + + (process).__defineGetter__('arch', (): string => { + return originalArch; + }); + + (navigator).__defineGetter__('userAgent', (): string => { + return originalUserAgent; + }); + }); + }); + + describe('isX64', () => { + + it('should provide isX64 as an instance-property', () => { + expect(_electronService).hasOwnProperty('isX64'); + }); + + it('should return a boolean', () => { + expect(typeof _electronService.isX64).toEqual('boolean'); + }); + + + it('should return false if isElectronApp is false', () => { + expect(_electronService.isX64).toBeFalsy(); + }); + + it('should return true if running in electron with x64 arch', () => { + let originalArch = process.arch; + let originalUserAgent = navigator.userAgent; + (navigator).__defineGetter__('userAgent', (): string => { + return 'foo Electron'; + }); + (process).__defineGetter__('arch', (): string => { + return 'x64'; + }); + + expect(_electronService.isX64).toBeTruthy(); + + (process).__defineGetter__('arch', (): string => { + return originalArch; + }); + + (navigator).__defineGetter__('userAgent', (): string => { + return originalUserAgent; + }); + }); + + it('should return false if running in electron with 32bit arch', () => { + let originalArch = process.arch; + let originalUserAgent = navigator.userAgent; + (navigator).__defineGetter__('userAgent', (): string => { + return 'foo Electron'; + }); + (process).__defineGetter__('arch', (): string => { + return 'ia32'; + }); + + expect(_electronService.isX64).toBeFalsy(); + + (process).__defineGetter__('arch', (): string => { + return originalArch; + }); + + (navigator).__defineGetter__('userAgent', (): string => { + return originalUserAgent; + }); + }); + + }); + describe('api', () => { it('should expose desktopCapturer', () => { @@ -108,7 +336,7 @@ describe('ElectronService', () => { }); it('should expose nativeImage', () => { - expect(_electronService.hasOwnProperty('nativeImage')); + expect(_electronService.hasOwnProperty('nativeImage')); }); });