| 
									
										
										
										
											2017-09-28 16:18:12 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google Inc. All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-27 17:06:06 -05:00
										 |  |  | import {Subject} from 'rxjs'; | 
					
						
							| 
									
										
										
										
											2017-09-28 16:18:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-28 18:27:51 +03:00
										 |  |  | export const patchDecodeBase64 = (proto: {decodeBase64: typeof atob}) => { | 
					
						
							|  |  |  |   let unpatch: () => void = () => undefined; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if ((typeof atob === 'undefined') && (typeof Buffer === 'function')) { | 
					
						
							|  |  |  |     const oldDecodeBase64 = proto.decodeBase64; | 
					
						
							|  |  |  |     const newDecodeBase64 = (input: string) => Buffer.from(input, 'base64').toString('binary'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     proto.decodeBase64 = newDecodeBase64; | 
					
						
							|  |  |  |     unpatch = () => { proto.decodeBase64 = oldDecodeBase64; }; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return unpatch; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-28 16:18:12 -07:00
										 |  |  | export class MockServiceWorkerContainer { | 
					
						
							|  |  |  |   private onControllerChange: Function[] = []; | 
					
						
							| 
									
										
										
										
											2017-10-26 10:29:36 -07:00
										 |  |  |   private onMessage: Function[] = []; | 
					
						
							| 
									
										
										
										
											2018-05-28 16:27:07 +03:00
										 |  |  |   mockRegistration: MockServiceWorkerRegistration|null = null; | 
					
						
							| 
									
										
										
										
											2017-09-28 16:18:12 -07:00
										 |  |  |   controller: MockServiceWorker|null = null; | 
					
						
							|  |  |  |   messages = new Subject(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 10:29:36 -07:00
										 |  |  |   addEventListener(event: 'controllerchange'|'message', handler: Function) { | 
					
						
							|  |  |  |     if (event === 'controllerchange') { | 
					
						
							|  |  |  |       this.onControllerChange.push(handler); | 
					
						
							|  |  |  |     } else if (event === 'message') { | 
					
						
							|  |  |  |       this.onMessage.push(handler); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-09-28 16:18:12 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   removeEventListener(event: 'controllerchange', handler: Function) { | 
					
						
							| 
									
										
										
										
											2017-10-26 10:29:36 -07:00
										 |  |  |     if (event === 'controllerchange') { | 
					
						
							|  |  |  |       this.onControllerChange = this.onControllerChange.filter(h => h !== handler); | 
					
						
							|  |  |  |     } else if (event === 'message') { | 
					
						
							|  |  |  |       this.onMessage = this.onMessage.filter(h => h !== handler); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-09-28 16:18:12 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async register(url: string): Promise<void> { return; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-28 16:27:07 +03:00
										 |  |  |   async getRegistration(): Promise<ServiceWorkerRegistration> { | 
					
						
							|  |  |  |     return this.mockRegistration as any; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-09-28 16:18:12 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   setupSw(url: string = '/ngsw-worker.js'): void { | 
					
						
							| 
									
										
										
										
											2018-05-28 16:27:07 +03:00
										 |  |  |     this.mockRegistration = new MockServiceWorkerRegistration(); | 
					
						
							| 
									
										
										
										
											2017-09-28 16:18:12 -07:00
										 |  |  |     this.controller = new MockServiceWorker(this, url); | 
					
						
							|  |  |  |     this.onControllerChange.forEach(onChange => onChange(this.controller)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 10:29:36 -07:00
										 |  |  |   sendMessage(value: Object): void { | 
					
						
							|  |  |  |     this.onMessage.forEach(onMessage => onMessage({ | 
					
						
							|  |  |  |                              data: value, | 
					
						
							|  |  |  |                            })); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-09-28 16:18:12 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class MockServiceWorker { | 
					
						
							|  |  |  |   constructor(private mock: MockServiceWorkerContainer, readonly scriptURL: string) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   postMessage(value: Object) { this.mock.messages.next(value); } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-28 16:28:32 +03:00
										 |  |  | export class MockServiceWorkerRegistration { | 
					
						
							|  |  |  |   pushManager: PushManager = new MockPushManager() as any; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class MockPushManager { | 
					
						
							|  |  |  |   private subscription: PushSubscription|null = null; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-28 18:27:51 +03:00
										 |  |  |   getSubscription(): Promise<PushSubscription|null> { return Promise.resolve(this.subscription); } | 
					
						
							| 
									
										
										
										
											2018-05-28 16:28:32 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |   subscribe(options?: PushSubscriptionOptionsInit): Promise<PushSubscription> { | 
					
						
							|  |  |  |     this.subscription = new MockPushSubscription() as any; | 
					
						
							|  |  |  |     return Promise.resolve(this.subscription !); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class MockPushSubscription { | 
					
						
							| 
									
										
										
										
											2018-05-28 18:27:51 +03:00
										 |  |  |   unsubscribe(): Promise<boolean> { return Promise.resolve(true); } | 
					
						
							| 
									
										
										
										
											2018-05-28 16:28:32 +03:00
										 |  |  | } |