2016-04-12 16:40:37 +00:00
|
|
|
import {
|
|
|
|
|
Injectable,
|
|
|
|
|
TestabilityRegistry,
|
|
|
|
|
Testability,
|
|
|
|
|
GetTestability,
|
|
|
|
|
setTestabilityGetter
|
2016-04-29 00:50:03 +00:00
|
|
|
} from '@angular/core';
|
|
|
|
|
|
2016-05-31 22:22:59 +00:00
|
|
|
import {ListWrapper} from '../facade/collection';
|
|
|
|
|
import {global, isPresent} from '../facade/lang';
|
2016-04-29 00:50:03 +00:00
|
|
|
import {getDOM} from '../dom/dom_adapter';
|
|
|
|
|
|
|
|
|
|
|
2015-03-23 23:46:18 +00:00
|
|
|
|
|
|
|
|
class PublicTestability {
|
2015-10-28 22:04:55 +00:00
|
|
|
/** @internal */
|
2015-05-20 16:48:15 +00:00
|
|
|
_testability: Testability;
|
2015-03-23 23:46:18 +00:00
|
|
|
|
2015-05-20 16:48:15 +00:00
|
|
|
constructor(testability: Testability) { this._testability = testability; }
|
2015-03-23 23:46:18 +00:00
|
|
|
|
2015-08-27 15:44:59 +00:00
|
|
|
isStable(): boolean { return this._testability.isStable(); }
|
|
|
|
|
|
2015-05-20 16:48:15 +00:00
|
|
|
whenStable(callback: Function) { this._testability.whenStable(callback); }
|
2015-03-23 23:46:18 +00:00
|
|
|
|
2015-10-11 05:11:13 +00:00
|
|
|
findBindings(using: any, provider: string, exactMatch: boolean): any[] {
|
|
|
|
|
return this.findProviders(using, provider, exactMatch);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
findProviders(using: any, provider: string, exactMatch: boolean): any[] {
|
|
|
|
|
return this._testability.findBindings(using, provider, exactMatch);
|
2015-03-23 23:46:18 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-02 22:19:26 +00:00
|
|
|
export class BrowserGetTestability implements GetTestability {
|
|
|
|
|
static init() { setTestabilityGetter(new BrowserGetTestability()); }
|
|
|
|
|
|
|
|
|
|
addToWindow(registry: TestabilityRegistry): void {
|
2015-11-17 23:24:36 +00:00
|
|
|
global.getAngularTestability = (elem: any, findInAncestors: boolean = true) => {
|
2015-10-28 07:59:19 +00:00
|
|
|
var testability = registry.findTestabilityInTree(elem, findInAncestors);
|
|
|
|
|
if (testability == null) {
|
|
|
|
|
throw new Error('Could not find testability for element.');
|
|
|
|
|
}
|
|
|
|
|
return new PublicTestability(testability);
|
|
|
|
|
};
|
2015-11-17 23:24:36 +00:00
|
|
|
|
|
|
|
|
global.getAllAngularTestabilities = () => {
|
2015-07-30 22:51:06 +00:00
|
|
|
var testabilities = registry.getAllTestabilities();
|
|
|
|
|
return testabilities.map((testability) => { return new PublicTestability(testability); });
|
|
|
|
|
};
|
2016-01-05 20:56:24 +00:00
|
|
|
|
2016-02-18 21:51:20 +00:00
|
|
|
global.getAllAngularRootElements = () => registry.getAllRootElements();
|
|
|
|
|
|
2016-01-05 20:56:24 +00:00
|
|
|
var whenAllStable = (callback) => {
|
|
|
|
|
var testabilities = global.getAllAngularTestabilities();
|
|
|
|
|
var count = testabilities.length;
|
|
|
|
|
var didWork = false;
|
|
|
|
|
var decrement = function(didWork_) {
|
|
|
|
|
didWork = didWork || didWork_;
|
|
|
|
|
count--;
|
|
|
|
|
if (count == 0) {
|
|
|
|
|
callback(didWork);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
testabilities.forEach(function(testability) { testability.whenStable(decrement); });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!global.frameworkStabilizers) {
|
|
|
|
|
global.frameworkStabilizers = ListWrapper.createGrowableSize(0);
|
|
|
|
|
}
|
|
|
|
|
global.frameworkStabilizers.push(whenAllStable);
|
2015-03-23 23:46:18 +00:00
|
|
|
}
|
2015-11-17 23:24:36 +00:00
|
|
|
|
2016-04-12 16:40:37 +00:00
|
|
|
findTestabilityInTree(registry: TestabilityRegistry, elem: any,
|
|
|
|
|
findInAncestors: boolean): Testability {
|
2015-11-17 23:24:36 +00:00
|
|
|
if (elem == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
var t = registry.getTestability(elem);
|
|
|
|
|
if (isPresent(t)) {
|
|
|
|
|
return t;
|
|
|
|
|
} else if (!findInAncestors) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2016-04-29 00:50:03 +00:00
|
|
|
if (getDOM().isShadowRoot(elem)) {
|
|
|
|
|
return this.findTestabilityInTree(registry, getDOM().getHost(elem), true);
|
2015-11-17 23:24:36 +00:00
|
|
|
}
|
2016-04-29 00:50:03 +00:00
|
|
|
return this.findTestabilityInTree(registry, getDOM().parentElement(elem), true);
|
2015-11-17 23:24:36 +00:00
|
|
|
}
|
2015-03-23 23:46:18 +00:00
|
|
|
}
|