diff --git a/packages/service-worker/worker/test/happy_spec.ts b/packages/service-worker/worker/test/happy_spec.ts index fafff3990b..c1bc37ac1f 100644 --- a/packages/service-worker/worker/test/happy_spec.ts +++ b/packages/service-worker/worker/test/happy_spec.ts @@ -12,9 +12,10 @@ import {Driver, DriverReadyState} from '../src/driver'; import {AssetGroupConfig, DataGroupConfig, Manifest} from '../src/manifest'; import {sha1} from '../src/sha1'; import {clearAllCaches, MockCache} from '../testing/cache'; +import {WindowClientImpl} from '../testing/clients'; import {MockRequest, MockResponse} from '../testing/fetch'; import {MockFileSystem, MockFileSystemBuilder, MockServerState, MockServerStateBuilder, tmpHashTableForFs} from '../testing/mock'; -import {MockClient, SwTestHarness, SwTestHarnessBuilder, WindowClientImpl} from '../testing/scope'; +import {SwTestHarness, SwTestHarnessBuilder} from '../testing/scope'; import {envIsSupported} from '../testing/utils'; (function() { diff --git a/packages/service-worker/worker/testing/clients.ts b/packages/service-worker/worker/testing/clients.ts new file mode 100644 index 0000000000..a525972ab7 --- /dev/null +++ b/packages/service-worker/worker/testing/clients.ts @@ -0,0 +1,77 @@ +/** + * @license + * Copyright Google LLC 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 + */ + +import {Subject} from 'rxjs'; + + +export class MockClient { + queue = new Subject(); + + constructor(readonly id: string) {} + + readonly messages: Object[] = []; + + postMessage(message: Object): void { + this.messages.push(message); + this.queue.next(message); + } +} + +export class WindowClientImpl extends MockClient implements WindowClient { + readonly ancestorOrigins: ReadonlyArray = []; + readonly focused: boolean = false; + readonly visibilityState: VisibilityState = 'hidden'; + frameType: ClientFrameType = 'top-level'; + url = 'http://localhost/unique'; + + constructor(readonly id: string) { + super(id); + } + + async focus(): Promise { + return this; + } + + async navigate(url: string): Promise { + return this; + } +} + +export class MockClients implements Clients { + private clients = new Map(); + + add(clientId: string): void { + if (this.clients.has(clientId)) { + return; + } + this.clients.set(clientId, new MockClient(clientId)); + } + + remove(clientId: string): void { + this.clients.delete(clientId); + } + + async get(id: string): Promise { + return this.clients.get(id)! as any as Client; + } + + getMock(id: string): MockClient|undefined { + return this.clients.get(id); + } + + async matchAll(options?: T): + Promise> { + return Array.from(this.clients.values()) as any[]; + } + + async openWindow(url: string): Promise { + return null; + } + + async claim(): Promise {} +} diff --git a/packages/service-worker/worker/testing/scope.ts b/packages/service-worker/worker/testing/scope.ts index 3dd0dcac5b..ddf6dd9a31 100644 --- a/packages/service-worker/worker/testing/scope.ts +++ b/packages/service-worker/worker/testing/scope.ts @@ -6,13 +6,12 @@ * found in the LICENSE file at https://angular.io/license */ -import {Subject} from 'rxjs'; - import {Adapter} from '../src/adapter'; import {AssetGroupConfig, Manifest} from '../src/manifest'; import {sha1} from '../src/sha1'; import {MockCacheStorage} from './cache'; +import {MockClient, MockClients} from './clients'; import {MockActivateEvent, MockExtendableMessageEvent, MockFetchEvent, MockInstallEvent, MockNotificationEvent, MockPushEvent} from './events'; import {MockHeaders, MockRequest, MockResponse} from './fetch'; import {MockServerState, MockServerStateBuilder} from './mock'; @@ -20,37 +19,6 @@ import {normalizeUrl, parseUrl} from './utils'; const EMPTY_SERVER_STATE = new MockServerStateBuilder().build(); -export class MockClient { - queue = new Subject(); - - constructor(readonly id: string) {} - - readonly messages: Object[] = []; - - postMessage(message: Object): void { - this.messages.push(message); - this.queue.next(message); - } -} -export class WindowClientImpl extends MockClient implements WindowClient { - readonly ancestorOrigins: ReadonlyArray = []; - readonly focused: boolean = false; - readonly visibilityState: VisibilityState = 'hidden'; - frameType: ClientFrameType = 'top-level'; - url = 'http://localhost/unique'; - - constructor(readonly id: string) { - super(id); - } - - async focus(): Promise { - return this; - } - async navigate(url: string): Promise { - return this; - } -} - export class SwTestHarnessBuilder { private origin = parseUrl(this.scopeUrl).origin; private server = EMPTY_SERVER_STATE; @@ -73,39 +41,6 @@ export class SwTestHarnessBuilder { } } -export class MockClients implements Clients { - private clients = new Map(); - - add(clientId: string): void { - if (this.clients.has(clientId)) { - return; - } - this.clients.set(clientId, new MockClient(clientId)); - } - - remove(clientId: string): void { - this.clients.delete(clientId); - } - - async get(id: string): Promise { - return this.clients.get(id)! as any as Client; - } - - getMock(id: string): MockClient|undefined { - return this.clients.get(id); - } - - async matchAll(options?: T): - Promise> { - return Array.from(this.clients.values()) as any[]; - } - async openWindow(url: string): Promise { - return null; - } - - async claim(): Promise {} -} - export class SwTestHarness extends Adapter implements ServiceWorkerGlobalScope { readonly clients = new MockClients(); private eventHandlers = new Map();