Files
angular-docs-cn/modules/@angular/core/src/testability/testability.ts
T

154 lines
4.5 KiB
TypeScript
Raw Normal View History

import {Map, MapWrapper} from '../../src/facade/collection';
import {scheduleMicroTask} from '../../src/facade/lang';
import {BaseException} from '../../src/facade/exceptions';
import {NgZone} from '../zone/ng_zone';
import {ObservableWrapper} from '../../src/facade/async';
import {Injectable} from '../di/decorators';
/**
* The Testability service provides testing hooks that can be accessed from
* the browser and by services such as Protractor. Each bootstrapped Angular
* application on the page will have an instance of Testability.
*/
2015-03-31 12:36:43 -07:00
@Injectable()
export class Testability {
/** @internal */
_pendingCount: number = 0;
/** @internal */
2016-02-25 14:24:17 -08:00
_isZoneStable: boolean = true;
/**
* Whether any work was done since the last 'whenStable' callback. This is
* useful to detect if this could have potentially destabilized another
* component while it is stabilizing.
* @internal
*/
_didWork: boolean = false;
/** @internal */
2015-08-28 11:29:19 -07:00
_callbacks: Function[] = [];
2016-02-25 14:24:17 -08:00
constructor(private _ngZone: NgZone) { this._watchAngularEvents(); }
/** @internal */
2016-02-25 14:24:17 -08:00
_watchAngularEvents(): void {
ObservableWrapper.subscribe(this._ngZone.onUnstable, (_) => {
this._didWork = true;
2016-02-25 14:24:17 -08:00
this._isZoneStable = false;
});
2016-02-25 14:24:17 -08:00
this._ngZone.runOutsideAngular(() => {
ObservableWrapper.subscribe(this._ngZone.onStable, (_) => {
NgZone.assertNotInAngularZone();
scheduleMicroTask(() => {
this._isZoneStable = true;
this._runCallbacksIfReady();
2016-02-25 14:24:17 -08:00
});
});
});
}
increasePendingRequestCount(): number {
this._pendingCount += 1;
this._didWork = true;
return this._pendingCount;
}
decreasePendingRequestCount(): number {
this._pendingCount -= 1;
if (this._pendingCount < 0) {
throw new BaseException('pending async requests below zero');
}
this._runCallbacksIfReady();
return this._pendingCount;
}
2016-02-25 14:24:17 -08:00
isStable(): boolean {
return this._isZoneStable && this._pendingCount == 0 && !this._ngZone.hasPendingMacrotasks;
}
/** @internal */
_runCallbacksIfReady(): void {
2016-02-25 14:24:17 -08:00
if (this.isStable()) {
// Schedules the call backs in a new frame so that it is always async.
scheduleMicroTask(() => {
while (this._callbacks.length !== 0) {
(this._callbacks.pop())(this._didWork);
}
this._didWork = false;
});
} else {
// Not Ready
this._didWork = true;
}
}
whenStable(callback: Function): void {
this._callbacks.push(callback);
this._runCallbacksIfReady();
}
getPendingRequestCount(): number { return this._pendingCount; }
2015-10-10 22:11:13 -07:00
findBindings(using: any, provider: string, exactMatch: boolean): any[] {
// TODO(juliemr): implement.
return [];
}
findProviders(using: any, provider: string, exactMatch: boolean): any[] {
// TODO(juliemr): implement.
return [];
}
}
/**
* A global registry of {@link Testability} instances for specific elements.
*/
2015-03-31 12:36:43 -07:00
@Injectable()
export class TestabilityRegistry {
/** @internal */
2015-09-29 11:11:06 -07:00
_applications = new Map<any, Testability>();
2015-11-17 15:24:36 -08:00
constructor() { _testabilityGetter.addToWindow(this); }
2015-07-07 20:03:00 -07:00
registerApplication(token: any, testability: Testability) {
this._applications.set(token, testability);
}
2015-11-17 15:24:36 -08:00
getTestability(elem: any): Testability { return this._applications.get(elem); }
2015-08-28 11:29:19 -07:00
getAllTestabilities(): Testability[] { return MapWrapper.values(this._applications); }
getAllRootElements(): any[] { return MapWrapper.keys(this._applications); }
findTestabilityInTree(elem: Node, findInAncestors: boolean = true): Testability {
2015-11-17 15:24:36 -08:00
return _testabilityGetter.findTestabilityInTree(this, elem, findInAncestors);
}
}
/**
* Adapter interface for retrieving the `Testability` service associated for a
* particular context.
*/
2015-11-17 15:24:36 -08:00
export interface GetTestability {
addToWindow(registry: TestabilityRegistry): void;
findTestabilityInTree(registry: TestabilityRegistry, elem: any,
findInAncestors: boolean): Testability;
2015-11-17 15:24:36 -08:00
}
/* @ts2dart_const */
2015-11-17 15:24:36 -08:00
class _NoopGetTestability implements GetTestability {
addToWindow(registry: TestabilityRegistry): void {}
findTestabilityInTree(registry: TestabilityRegistry, elem: any,
findInAncestors: boolean): Testability {
2015-11-17 15:24:36 -08:00
return null;
}
}
/**
* Set the {@link GetTestability} implementation used by the Angular testing framework.
*/
export function setTestabilityGetter(getter: GetTestability): void {
2015-11-17 15:24:36 -08:00
_testabilityGetter = getter;
}
var _testabilityGetter: GetTestability = /*@ts2dart_const*/ new _NoopGetTestability();